heroRank.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * implement this for IE6
  3. * https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
  4. * */
  5. if (!Array.prototype.filter) {
  6. Array.prototype.filter = function(fun/*, thisArg*/) {
  7. 'use strict';
  8. // if (this === void 0 || this === null) {
  9. if (this === null) {
  10. throw new TypeError();
  11. }
  12. var t = Object(this);
  13. var len = t.length >>> 0;
  14. if (typeof fun !== 'function') {
  15. throw new TypeError();
  16. }
  17. var res = [];
  18. var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
  19. for (var i = 0; i < len; i++) {
  20. if (i in t) {
  21. var val = t[i];
  22. // NOTE: Technically this should Object.defineProperty at
  23. // the next index, as push can be affected by
  24. // properties on Object.prototype and Array.prototype.
  25. // But that method's new, and collisions should be
  26. // rare, so use the more-compatible alternative.
  27. if (fun.call(thisArg, val, i, t)) {
  28. res.push(val);
  29. }
  30. }
  31. }
  32. return res;
  33. };
  34. }
  35. var gResults = null; // keep origin for filtering
  36. var gResultsUsing = null;
  37. var gPageIndex = 0;
  38. var gTotalItems = 0;
  39. var gTotalPages = 0;
  40. var ITEMS_PER_PAGE = 10;
  41. var gSorting = {
  42. defaultSortByField: 'present_rate',
  43. // low to high
  44. present_rate: false,
  45. win_rate: true
  46. };
  47. /*
  48. Reference
  49. - http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects
  50. */
  51. var sort_by = function (field, reverse, primer) {
  52. var key = primer ?
  53. function (x) {
  54. return primer(x[field])
  55. } :
  56. function (x) {
  57. return x[field]
  58. };
  59. reverse = [-1, 1][+!!reverse];
  60. return function (a, b) {
  61. return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
  62. }
  63. };
  64. function updateTable() {
  65. gTotalItems = gResultsUsing.length;
  66. gTotalPages = Math.ceil(gTotalItems / ITEMS_PER_PAGE);
  67. if (gResultsUsing.length) {
  68. if (gPageIndex > (gTotalPages - 1)) {
  69. return;
  70. }
  71. }
  72. var output = '';
  73. for (var i = 0; i < ITEMS_PER_PAGE; i++) {
  74. var offset = i + gPageIndex * ITEMS_PER_PAGE;
  75. var item = gResultsUsing[offset];
  76. if (item) {
  77. var firstColumn;
  78. var championPresentRate = item.champion_present_rate;
  79. var champion = championPresentRate.champion;
  80. if (offset >= 0 && offset <= 2) {
  81. firstColumn = '<td align="center" class="type1"><img src="http://img.lolbox.duowan.com/icon-herorank-top' + (offset + 1) + '.jpg" /></td>';
  82. } else {
  83. firstColumn = '<td align="center" class="type1">' + (offset + 1) + '</td>';
  84. }
  85. output += '<tr>' +
  86. firstColumn +
  87. '<td valign="middle" class="type2"><span class="hero-name"><img src="' + champion.url_img['28x28'] + '" alt="' + champion['name'] + ' image" />' + champion['title'] + '</span></td>' +
  88. '<td align="center" class="type3">' + championPresentRate['present_rate'].toFixed(2) + '%</td>' +
  89. '<td align="center" class="type4">' + championPresentRate['win_rate'].toFixed(2) + '%</td>' +
  90. '<td align="center" class="type5">' + championPresentRate['total_present'] + '</td>' +
  91. '<td align="center" class="type6"><a href="heroTop10Players.html?hero=' + champion['name'] + '">查看榜单</a></td>' +
  92. '</tr>';
  93. }
  94. }
  95. if (!output) {
  96. output += '<tr><td colspan="6" align="center" style="padding-top: 80px; font-weight: bolder; font-size: 30px; color: #c00;">没有找到匹配英雄</td></tr>';
  97. }
  98. $('#rankListBody').html(output);
  99. updatePaginator();
  100. }
  101. function paginatorJump() {
  102. var index = Number(document.getElementById('go').value) - 1;
  103. if (index > -1 && index < gTotalPages) {
  104. gPageIndex = index;
  105. updateTable();
  106. } else {
  107. $('#rankListBody').html('<tr><td colspan="6">输入页数不正确</td></tr>');
  108. }
  109. }
  110. function filterChampion(name) {
  111. if (!name) {
  112. gResultsUsing = gResults;
  113. updateTable();
  114. return;
  115. }
  116. function filterFn(item) {
  117. var nameLower = name.toLowerCase();
  118. return item['nameUS'].toLowerCase().indexOf(nameLower) >= 0 ||
  119. item['nameCN'].toLowerCase().indexOf(nameLower) >= 0 ||
  120. item['titleCN'].indexOf(nameLower) >= 0;
  121. }
  122. gResultsUsing = gResults.filter(filterFn);
  123. updateTable();
  124. }
  125. function updatePaginator() {
  126. var output = '';
  127. output += '<a>第' + (gPageIndex + 1) + '/' + gTotalPages + '页</a>';
  128. if (gPageIndex > 0) {
  129. output += '<a title="上一页" class="pre-next" rel="prev" href="#" onclick="javascript:gPageIndex--;updateTable();">上一页</a>';
  130. }
  131. if (gPageIndex < (gTotalPages - 1)) {
  132. output += '<a title="下一页" class="pre-next" rel="next" href="#" onclick="javascript:gPageIndex++;updateTable();">下一页</a>';
  133. }
  134. output += '<a class="current">第 <input type="text" id="go" size=1 value="' + (Number(gPageIndex) + 1) + '"/>页 </a> <a href="#" class="page-go" onclick="javascript:paginatorJump();"> Go </a> ';
  135. $('#pages').html(output);
  136. }
  137. function parseQueryString(qs)
  138. {
  139. if (!qs) {
  140. var parser = document.createElement('a');
  141. parser.href = window.location.toString();
  142. qs = parser.search;
  143. qs = qs.replace(/^\?/, '');
  144. }
  145. var d = {};
  146. var a = qs.split('&');
  147. for (var i in a)
  148. {
  149. var b = a[i].split('=');
  150. d[decodeURIComponent(b[0])] = decodeURIComponent(b[1]);
  151. }
  152. return d;
  153. }
  154. function isIE() {
  155. var ua = window.navigator.userAgent;
  156. return ua.indexOf("MSIE ") > 0;
  157. }
  158. function loadRankData(tabRank) {
  159. $('#rankListBody').html('<tr><td colspan="6" align="center"><img src="img/loading.gif" width="400" height="400"></td></tr>');
  160. var data;
  161. var url = "http://api.lolbox.duowan.com/api/v2/rank/champion_present_rate/" + tabRank + '/';
  162. if (!isIE) {
  163. data = parseQueryString();
  164. } else {
  165. data = {};
  166. }
  167. data['rank'] = tabRank;
  168. $.ajax({
  169. url: url,
  170. dataType:'jsonp',
  171. success:function (resp) {
  172. var results = resp.results;
  173. if (!results || !results.length) {
  174. $('#rankListBody').html('<tr><td colspan="6">暂无数据,请稍后重试</td></tr>');
  175. return;
  176. }
  177. for(var i=0;i<results.length;i++) {
  178. var item = results[i],
  179. championPresentRate = item.champion_present_rate;
  180. item.present_rate = championPresentRate.present_rate;
  181. item.total_present = championPresentRate.total_present;
  182. item.win_rate = championPresentRate.win_rate;
  183. }
  184. gResults = results;
  185. gResultsUsing = results;
  186. resortResult();
  187. },
  188. error:function () {
  189. $('#rankListBody').html('<tr><td colspan="6">获取数据失败,请稍后重试!</td></tr>');
  190. }
  191. });
  192. }
  193. function resortResult(fieldName) {
  194. if (!fieldName) {
  195. fieldName = gSorting.defaultSortByField;
  196. }
  197. var reverse = gSorting[fieldName];
  198. var sortBy = sort_by(fieldName, reverse, parseFloat)
  199. gResultsUsing.sort(sortBy);
  200. updateTable();
  201. updateSortByFieldNameTextStyle(fieldName);
  202. }
  203. function updateSortByFieldNameTextStyle(fieldName) {
  204. $('#presentRate').css('font-weight', 'normal');
  205. $('#winRate').css('font-weight', 'normal');
  206. $("#" + fieldName).css('font-weight', 'bolder');
  207. }
  208. $(document).ready(function () {
  209. $('.box-bd').keyup(function(event) {
  210. if (event.keyCode == 13) {
  211. var elementID = document.activeElement.id;
  212. if (elementID == "searchKey") {
  213. var keyword = document.getElementById('searchKey').value;
  214. filterChampion(keyword);
  215. } else if (elementID == "go") {
  216. paginatorJump();
  217. }
  218. }
  219. });
  220. $('#searchKey').focus(function () {
  221. if (this.value == '搜索英雄') {
  222. this.value = '';
  223. }
  224. });
  225. $('#btnSearch').bind("click", function (event) {
  226. var keyword = document.getElementById('searchKey').value;
  227. filterChampion(keyword);
  228. });
  229. // binding for tab onClicked
  230. $('.mod-tabs-trigger li').bind('click', function (event) {
  231. $(this).siblings().removeClass('selected').end().addClass('selected');
  232. gPageIndex = 0;
  233. gSorting.present_rate = false;
  234. gSorting.win_rate = true;
  235. var tabRank = $(this).attr('data-type');
  236. loadRankData(tabRank);
  237. });
  238. // binding for sortBy onClicked
  239. $(".m-rank-herotable").on('click', '.sort', function(event) {
  240. var fieldName = $(this).attr('id');
  241. if ($(this).hasClass('sort--up')) {
  242. $(this).removeClass("sort--up").addClass('sort--down');
  243. gSorting[fieldName] = false;
  244. } else if ($(this).hasClass('sort--down')) {
  245. $(this).removeClass("sort--down").addClass('sort--up');
  246. gSorting[fieldName] = true;
  247. }
  248. resortResult(fieldName);
  249. event.preventDefault();
  250. });
  251. $("#icon-note").hover(function () {
  252. $(this).addClass('hover');
  253. }, function () {
  254. $(this).removeClass('hover');
  255. });
  256. gPageIndex = 0;
  257. loadRankData('all');
  258. $('.fnMenu')
  259. .on('click', '.refresh', function(evt){
  260. evt.preventDefault();
  261. window.location.reload();
  262. })
  263. .on('click', '.back', function(evt){
  264. evt.preventDefault();
  265. window.history.back();
  266. });
  267. });