battleSearch.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. $("#servers").on("click", function(e) {
  44. e.preventDefault();
  45. e.stopPropagation();
  46. $("#serversUl").toggle();
  47. });
  48. $("body").on("click", function() {
  49. var $rankUl = $("#serversUl");
  50. if ($rankUl.css("display") != "none") {
  51. $rankUl.css("display", "none");
  52. }
  53. });
  54. //热门视频、正在直播
  55. // this._getData().done(function(data) {
  56. // var hotVideoHtml = '';
  57. // var liveHtml = '';
  58. // _.each(data.hot_video.slice(0, 4), function(item, key){
  59. // if(key >= 2) return;
  60. // hotVideoHtml += '<li><a href="'+item.url_source+'" target="_blank"><img src="'+item.url_media+'"></a><p><span class="word">'+item.title+'</span></p></li>';
  61. // });
  62. // _.each(data.live, function(item, key){
  63. // if(key >= 1) return;
  64. // // console.log(item)
  65. // var countUrls = item.url_media.split(",")
  66. // 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>';
  67. // });
  68. // $('.video-left ul').html(hotVideoHtml);
  69. // $('.video-right ul').html(liveHtml);
  70. // });
  71. },
  72. _initCookie: function() {
  73. var serverCookies = getCookie("pnCache");
  74. if (serverCookies) {
  75. $("#records").html('');
  76. var sCookies = serverCookies.split("-").unique().reverse();
  77. $.each(sCookies, function(i, cookiePn) {
  78. var item = decodeURI(cookiePn);
  79. var $key = i == 0 ? ("<a><label>" + item + "</label></a>") : ("<a><span>|</span><label>" + item + "</label></a>");
  80. $("#records").append($key);
  81. });
  82. $("#clearBtn").show();
  83. } else {
  84. $("#clearBtn").hide();
  85. }
  86. },
  87. _submit: function(e) {
  88. var that = this;
  89. e.preventDefault();
  90. var value = encodeURI($("#keyword").val());
  91. if (value) {
  92. $("#keyword").val("");
  93. var shCookie = getCookie("pnCache"),
  94. shCookieString = '',
  95. sh;
  96. if (shCookie) {
  97. // var shCookieArr = shCookie.split("-");
  98. // for (var i = 0, len = shCookieArr.length; i < len; i++) {
  99. // var item = shCookieArr[i];
  100. // if (i == 0) {
  101. // shCookieString += item;
  102. // } else {
  103. // shCookieString += '-' + item;
  104. // }
  105. // }
  106. sh = shCookie + "-" + value;
  107. } else {
  108. sh = value;
  109. }
  110. if (!this.fromCookie) {
  111. addCookie("pnCache", sh);
  112. setTimeout(function() {
  113. that._initCookie();
  114. });
  115. }
  116. this.fromCookie = false;
  117. //数据上报
  118. try {
  119. window.external.data_report("click/search", "点击/搜索");
  120. } catch (err) {}
  121. //跳转
  122. window.location.href = this.locationHref + "?sn=" + encodeURI(this.server) + "&pn=" + value;
  123. }
  124. },
  125. _clearCookie: function() {
  126. delCookie("pnCache");
  127. $('#records').html('');
  128. $('#clearBtn').hide();
  129. },
  130. _getData: function() {
  131. return $.ajax({
  132. url: 'http://lolbox.duowan.com/api/article/index.php?format=jsonp',
  133. dataType: 'jsonp',
  134. jsonpCallback: 'jQueryJsonp',
  135. jsonp: 'callback'
  136. });
  137. }
  138. };
  139. Array.prototype.unique = function() {
  140. var res = [];
  141. var json = {};
  142. for (var i = 0; i < this.length; i++) {
  143. if (!json[this[i]]) {
  144. res.push(this[i]);
  145. json[this[i]] = 1;
  146. }
  147. }
  148. return res;
  149. };
  150. function getCookie(cookieName) {
  151. var cookieString = document.cookie;
  152. var cookies = cookieString.split(';');
  153. for (var i = 0; i < cookies.length; i++) {
  154. var cookie = cookies[i];
  155. var start = cookie.indexOf(cookieName + '=');
  156. if (start == -1 || start > 1) continue;
  157. start += cookieName.length + 1;
  158. return cookie.substring(start);
  159. }
  160. return null;
  161. }
  162. //写cookies
  163. function addCookie(name, value) {
  164. var Days = 30;
  165. var exp = new Date();
  166. exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  167. document.cookie = name + "=" + value + ";expires=" + exp.toGMTString();
  168. }
  169. function delCookie(name) { //为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
  170. var date = new Date();
  171. date.setTime(date.getTime() - 10000);
  172. document.cookie = name + "=x; expires=" + date.toGMTString();
  173. // document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  174. }
  175. new GradeSearch().init();
  176. });