battleSearch.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. $(function() {
  2. //战绩查询
  3. var GradeSearch = function() {
  4. this.locationHref = "playerList.html";
  5. this.fromCookie = false;
  6. this.server = "all";
  7. };
  8. GradeSearch.prototype = {
  9. constructor: GradeSearch,
  10. init: function() {
  11. var that = this;
  12. this._initCookie();
  13. $("#serversUl").niceScroll({
  14. cursorcolor: "#D9D9D9",
  15. cursorwidth: "8",
  16. cursoropacitymin: 1,
  17. cursorborder: 'none'
  18. });
  19. $("#searchBtn").on("click", function(e) {
  20. that._submit(e);
  21. });
  22. $('#keyword').keydown(function(e) {
  23. if (e.which == 13) {
  24. that._submit(e);
  25. }
  26. });
  27. $("#clearBtn").on("click", function(e) {
  28. e.preventDefault();
  29. that._clearCookie();
  30. });
  31. $("body").delegate('#records>a', 'click', function(e) {
  32. e.preventDefault();
  33. var value = $(this).find("label").html();
  34. $("#keyword").val(value);
  35. that.fromCookie = true;
  36. that._submit(e);
  37. });
  38. $("#serversUl>li").on("click", function(e) {
  39. e.preventDefault();
  40. that.server = $(this).attr("data-server");
  41. $("#servers").html(that.server);
  42. });
  43. //热门视频、正在直播
  44. // this._getData().done(function(data) {
  45. // var hotVideoHtml = '';
  46. // var liveHtml = '';
  47. // _.each(data.hot_video.slice(0, 4), function(item, key){
  48. // if(key >= 2) return;
  49. // hotVideoHtml += '<li><a href="'+item.url_source+'" target="_blank"><img src="'+item.url_media+'"></a><p><span class="word">'+item.title+'</span></p></li>';
  50. // });
  51. // _.each(data.live, function(item, key){
  52. // if(key >= 1) return;
  53. // // console.log(item)
  54. // var countUrls = item.url_media.split(",")
  55. // liveHtml += '<li><a href="'+item.url_source+'?from=lol" target="_blank"><img class="big-img" src="'+countUrls[0]+'"></a><img class="avatar-img" src='+countUrls[1]+'><div class="v-shadow"></div><p><span class="word">'+item.title+'</span></p></li>';
  56. // });
  57. // $('.video-left ul').html(hotVideoHtml);
  58. // $('.video-right ul').html(liveHtml);
  59. // });
  60. },
  61. _initCookie: function() {
  62. var serverCookies = getCookie("pnCache");
  63. if (serverCookies) {
  64. $("#records").html('');
  65. var sCookies = serverCookies.split("-").unique().reverse();
  66. $.each(sCookies, function(i, cookiePn) {
  67. var item = decodeURI(cookiePn);
  68. var $key = i == 0 ? ("<a><label>" + item + "</label></a>") : ("<a><span>|</span><label>" + item + "</label></a>");
  69. $("#records").append($key);
  70. });
  71. $("#clearBtn").show();
  72. } else {
  73. $("#clearBtn").hide();
  74. }
  75. },
  76. _submit: function(e) {
  77. var that = this;
  78. e.preventDefault();
  79. var value = encodeURI($("#keyword").val());
  80. if (value) {
  81. $("#keyword").val("");
  82. var shCookie = getCookie("pnCache"),
  83. shCookieString = '',
  84. sh;
  85. if (shCookie) {
  86. // var shCookieArr = shCookie.split("-");
  87. // for (var i = 0, len = shCookieArr.length; i < len; i++) {
  88. // var item = shCookieArr[i];
  89. // if (i == 0) {
  90. // shCookieString += item;
  91. // } else {
  92. // shCookieString += '-' + item;
  93. // }
  94. // }
  95. sh = shCookie + "-" + value;
  96. } else {
  97. sh = value;
  98. }
  99. if (!this.fromCookie) {
  100. addCookie("pnCache", sh);
  101. setTimeout(function() {
  102. that._initCookie();
  103. });
  104. }
  105. this.fromCookie = false;
  106. //数据上报
  107. try {window.external.data_report("click/search", "点击/搜索");} catch(err) {}
  108. //跳转
  109. window.location.href = this.locationHref + "?sn=" + encodeURI(this.server) + "&pn=" + value;
  110. }
  111. },
  112. _clearCookie: function() {
  113. delCookie("pnCache");
  114. $('#records').html('');
  115. $('#clearBtn').hide();
  116. },
  117. _getData: function() {
  118. return $.ajax({
  119. url: 'http://lolbox.duowan.com/api/article/index.php?format=jsonp',
  120. dataType: 'jsonp',
  121. jsonpCallback: 'jQueryJsonp',
  122. jsonp: 'callback'
  123. });
  124. }
  125. };
  126. Array.prototype.unique = function() {
  127. var res = [];
  128. var json = {};
  129. for (var i = 0; i < this.length; i++) {
  130. if (!json[this[i]]) {
  131. res.push(this[i]);
  132. json[this[i]] = 1;
  133. }
  134. }
  135. return res;
  136. };
  137. function getCookie(cookieName) {
  138. var cookieString = document.cookie;
  139. var cookies = cookieString.split(';');
  140. for (var i = 0; i < cookies.length; i++) {
  141. var cookie = cookies[i];
  142. var start = cookie.indexOf(cookieName + '=');
  143. if (start == -1 || start > 1) continue;
  144. start += cookieName.length + 1;
  145. return cookie.substring(start);
  146. }
  147. return null;
  148. }
  149. //写cookies
  150. function addCookie(name,value){
  151. var Days = 30;
  152. var exp = new Date();
  153. exp.setTime(exp.getTime() + Days*24*60*60*1000);
  154. document.cookie = name + "="+ value + ";expires=" + exp.toGMTString();
  155. }
  156. function delCookie(name) { //为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
  157. var date = new Date();
  158. date.setTime(date.getTime() - 10000);
  159. document.cookie = name + "=x; expires=" + date.toGMTString();
  160. // document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  161. }
  162. new GradeSearch().init();
  163. });