You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

191 lines
4.4 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. let el = $(e.currentTarget);
  33. let 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. let prophecies = window.player.prophecies;
  43. prophecies = prophecies ? prophecies.list : [];
  44. this.prophecyFilter = [];
  45. this.find('.prophecy').removeClass('selected');
  46. prophecies.forEach(function (p) {
  47. this.onProphecyClick({
  48. currentTarget: this.find('.prophecy[prophecy="' + p + '"]')
  49. });
  50. }, this);
  51. },
  52. onNone: function () {
  53. this.find('.prophecy[prophecy]').removeClass('selected');
  54. this.prophecyFilter = [];
  55. },
  56. onRefresh: function () {
  57. this.getList();
  58. },
  59. onProphecyClick: function (e) {
  60. let el = $(e.currentTarget);
  61. el.toggleClass('selected');
  62. let prophecyName = el.attr('prophecy');
  63. let exists = this.prophecyFilter.some(function (p) {
  64. return (p == prophecyName);
  65. }, this);
  66. if (exists) {
  67. this.prophecyFilter.spliceWhere(function (p) {
  68. return (p == prophecyName);
  69. }, this);
  70. } else
  71. this.prophecyFilter.push(prophecyName);
  72. },
  73. getList: function (keepOffset) {
  74. this.el.addClass('disabled');
  75. if (!this.prophecyFilter) {
  76. let prophecies = window.player.prophecies;
  77. this.prophecyFilter = prophecies ? prophecies.list : [];
  78. this.prophecyFilter = $.extend(true, [], this.prophecyFilter);
  79. }
  80. client.request({
  81. module: 'leaderboard',
  82. method: 'requestList',
  83. data: {
  84. prophecies: this.prophecyFilter,
  85. offset: this.offset * this.pageSize
  86. },
  87. callback: this.onGetList.bind(this, keepOffset)
  88. });
  89. },
  90. onGetList: function (keepOffset, result) {
  91. this.records = result;
  92. if (!keepOffset) {
  93. this.offset = 0;
  94. let foundIndex = this.records.list.firstIndex(function (r) {
  95. return (r.name == window.player.name);
  96. }, this);
  97. if (foundIndex != -1)
  98. this.offset = ~~(foundIndex / this.pageSize);
  99. }
  100. let container = this.find('.list').empty();
  101. let low = this.offset * this.pageSize;
  102. let high = Math.min(result.length, low + this.pageSize);
  103. this.maxOffset = Math.ceil(result.length / this.pageSize) - 1;
  104. for (let i = 0; i < this.records.list.length; i++) {
  105. var r = this.records.list[i];
  106. let html = '<div class="row"><div class="col">' + r.level + '</div><div class="col">' + r.name + '</div></div>';
  107. let el = $(html)
  108. .appendTo(container);
  109. if (r.name == window.player.name)
  110. el.addClass('self');
  111. else {
  112. let online = globals.onlineList.some(function (o) {
  113. return (o.name == r.name);
  114. });
  115. if (online)
  116. el.addClass('online');
  117. }
  118. if (r.dead)
  119. el.addClass('disabled');
  120. }
  121. this.updatePaging();
  122. this.el.removeClass('disabled');
  123. },
  124. updatePaging: function () {
  125. this.find('.buttons .btn').removeClass('disabled');
  126. if (this.offset == 0)
  127. this.find('.btn-first, .btn-prev').addClass('disabled');
  128. if (this.offset >= this.maxOffset)
  129. this.find('.btn-next, .btn-last').addClass('disabled');
  130. },
  131. toggle: function () {
  132. let shown = !this.el.is(':visible');
  133. if (shown) {
  134. this.find('.prophecy[prophecy]').removeClass('selected');
  135. let prophecies = window.player.prophecies;
  136. prophecies = prophecies ? prophecies.list : [];
  137. prophecies.forEach(function (p) {
  138. this.find('.prophecy[prophecy="' + p + '"]').addClass('selected');
  139. }, this);
  140. this.prophecyFilter = null;
  141. this.getList();
  142. this.show();
  143. } else
  144. this.hide();
  145. }
  146. };
  147. });