Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

188 řádky
4.3 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/leaderboard/template',
  5. 'css!ui/templates/leaderboard/styles',
  6. 'js/system/globals'
  7. ], function (
  8. events,
  9. client,
  10. template,
  11. styles,
  12. globals
  13. ) {
  14. return {
  15. tpl: template,
  16. centered: true,
  17. modal: true,
  18. prophecyFilter: null,
  19. records: [],
  20. offset: 0,
  21. pageSize: 10,
  22. maxOffset: 0,
  23. postRender: function () {
  24. this.onEvent('onShowLeaderboard', this.toggle.bind(this, true));
  25. this.find('.prophecy[prophecy]').on('click', this.onProphecyClick.bind(this));
  26. this.find('.prophecy-mine').on('click', this.onMine.bind(this));
  27. this.find('.prophecy-none').on('click', this.onNone.bind(this));
  28. this.find('.button').on('click', this.onRefresh.bind(this));
  29. this.find('.buttons .btn').on('click', this.onPage.bind(this));
  30. },
  31. onPage: function (e) {
  32. var el = $(e.currentTarget);
  33. var offset = ~~el.attr('offset');
  34. this.offset += offset;
  35. if (this.offset < 0)
  36. this.offset = 0;
  37. else if (this.offset > this.maxOffset)
  38. this.offset = this.maxOffset;
  39. this.getList(true);
  40. },
  41. onMine: function () {
  42. var prophecies = window.player.prophecies;
  43. prophecies = prophecies ? prophecies.list : [];
  44. prophecies.forEach(function (p) {
  45. this.onProphecyClick({
  46. currentTarget: this.find('.prophecy[prophecy="' + p + '"]')
  47. });
  48. }, this);
  49. },
  50. onNone: function () {
  51. this.find('.prophecy[prophecy]').removeClass('selected');
  52. this.prophecyFilter = [];
  53. },
  54. onRefresh: function () {
  55. this.getList();
  56. },
  57. onProphecyClick: function (e) {
  58. var el = $(e.currentTarget);
  59. el.toggleClass('selected');
  60. var prophecyName = el.attr('prophecy');
  61. var exists = this.prophecyFilter.some(function (p) {
  62. return (p == prophecyName);
  63. }, this);
  64. if (exists) {
  65. this.prophecyFilter.spliceWhere(function (p) {
  66. return (p == prophecyName);
  67. }, this);
  68. } else
  69. this.prophecyFilter.push(prophecyName);
  70. },
  71. getList: function (keepOffset) {
  72. this.el.addClass('disabled');
  73. if (!this.prophecyFilter) {
  74. var prophecies = window.player.prophecies;
  75. this.prophecyFilter = prophecies ? prophecies.list : [];
  76. this.prophecyFilter = $.extend(true, [], this.prophecyFilter);
  77. }
  78. client.request({
  79. module: 'leaderboard',
  80. method: 'requestList',
  81. data: {
  82. prophecies: this.prophecyFilter,
  83. offset: this.offset * this.pageSize
  84. },
  85. callback: this.onGetList.bind(this, keepOffset)
  86. });
  87. },
  88. onGetList: function (keepOffset, result) {
  89. this.records = result;
  90. if (!keepOffset) {
  91. this.offset = 0;
  92. var foundIndex = this.records.list.firstIndex(function (r) {
  93. return (r.name == window.player.name);
  94. }, this);
  95. if (foundIndex != -1)
  96. this.offset = ~~(foundIndex / this.pageSize);
  97. }
  98. var container = this.find('.list').empty();
  99. var low = this.offset * this.pageSize;
  100. var high = Math.min(result.length, low + this.pageSize);
  101. this.maxOffset = Math.ceil(result.length / this.pageSize) - 1;
  102. for (var i = 0; i < this.records.list.length; i++) {
  103. var r = this.records.list[i];
  104. var html = '<div class="row"><div class="col">' + r.level + '</div><div class="col">' + r.name + '</div></div>';
  105. var el = $(html)
  106. .appendTo(container);
  107. if (r.name == window.player.name)
  108. el.addClass('self');
  109. else {
  110. var online = globals.onlineList.some(function (o) {
  111. return (o.name == r.name);
  112. });
  113. if (online)
  114. el.addClass('online');
  115. }
  116. if (r.dead)
  117. el.addClass('disabled');
  118. }
  119. this.updatePaging();
  120. this.el.removeClass('disabled');
  121. },
  122. updatePaging: function () {
  123. this.find('.buttons .btn').removeClass('disabled');
  124. if (this.offset == 0)
  125. this.find('.btn-first, .btn-prev').addClass('disabled');
  126. if (this.offset >= this.maxOffset)
  127. this.find('.btn-next, .btn-last').addClass('disabled');
  128. },
  129. toggle: function () {
  130. var shown = !this.el.is(':visible');
  131. if (shown) {
  132. this.find('.prophecy[prophecy]').removeClass('selected');
  133. var prophecies = window.player.prophecies;
  134. prophecies = prophecies ? prophecies.list : [];
  135. prophecies.forEach(function (p) {
  136. this.find('.prophecy[prophecy="' + p + '"]').addClass('selected');
  137. }, this);
  138. this.prophecyFilter = null;
  139. this.getList();
  140. this.show();
  141. } else
  142. this.hide();
  143. }
  144. };
  145. });