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.
 
 
 

254 lines
5.4 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/stash/template',
  5. 'css!ui/templates/stash/styles',
  6. 'html!ui/templates/inventory/templateItem'
  7. ], function (
  8. events,
  9. client,
  10. template,
  11. styles,
  12. tplItem
  13. ) {
  14. return {
  15. tpl: template,
  16. centered: true,
  17. hoverItem: null,
  18. shiftDown: false,
  19. items: [],
  20. modal: true,
  21. postRender: function () {
  22. this.onEvent('onGetStashItems', this.onGetStashItems.bind(this));
  23. this.onEvent('onDestroyStashItems', this.onDestroyStashItems.bind(this));
  24. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  25. this.onEvent('onKeyUp', this.onKeyUp.bind(this));
  26. },
  27. build: function () {
  28. let container = this.el.find('.grid')
  29. .empty();
  30. let items = this.items;
  31. let iLen = items.length;
  32. let remainder = iLen % 8;
  33. let startNoPad = ~~(iLen / 8);
  34. if (remainder == 0)
  35. startNoPad--;
  36. startNoPad *= 8;
  37. for (let i = 0; i < iLen; i++) {
  38. let item = items[i];
  39. let imgX = -item.sprite[0] * 64;
  40. let imgY = -item.sprite[1] * 64;
  41. let itemEl = $(tplItem)
  42. .appendTo(container);
  43. let spritesheet = item.spritesheet || '../../../images/items.png';
  44. if (!item.spritesheet) {
  45. if (item.material)
  46. spritesheet = '../../../images/materials.png';
  47. else if (item.quest)
  48. spritesheet = '../../../images/questItems.png';
  49. else if (item.type == 'consumable')
  50. spritesheet = '../../../images/consumables.png';
  51. }
  52. itemEl
  53. .data('item', item)
  54. .on('mousemove', this.onHover.bind(this, itemEl, item))
  55. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  56. .find('.icon')
  57. .css('background', 'url(' + spritesheet + ') ' + imgX + 'px ' + imgY + 'px')
  58. .on('contextmenu', this.showContext.bind(this, item));
  59. if (item.quantity)
  60. itemEl.find('.quantity').html(item.quantity);
  61. if (item.eq)
  62. itemEl.addClass('eq');
  63. if (item.isNew)
  64. itemEl.addClass('new');
  65. if (i >= startNoPad) {
  66. if (i == iLen - 1)
  67. itemEl.css('margin', '0px 0px 0px 0px');
  68. else
  69. itemEl.css('margin', '0px 8px 0px 0px');
  70. }
  71. }
  72. },
  73. showContext: function (item, e) {
  74. events.emit('onContextMenu', [{
  75. text: 'withdraw',
  76. callback: this.withdraw.bind(this, item)
  77. }], e);
  78. e.preventDefault;
  79. return false;
  80. },
  81. hideTooltip: function () {
  82. events.emit('onHideItemTooltip', this.hoverItem);
  83. this.hoverItem = null;
  84. },
  85. onHover: function (el, item, e) {
  86. if (item)
  87. this.hoverItem = item;
  88. else
  89. item = this.hoverItem;
  90. let ttPos = null;
  91. if (el) {
  92. el.removeClass('new');
  93. delete item.isNew;
  94. let elOffset = el.offset();
  95. ttPos = {
  96. x: ~~(elOffset.left + 74),
  97. y: ~~(elOffset.top + 4)
  98. };
  99. }
  100. let compare = null;
  101. if (this.shiftDown) {
  102. compare = window.player.inventory.items.find(function (i) {
  103. return ((i.eq) && (i.slot == item.slot));
  104. });
  105. }
  106. events.emit('onShowItemTooltip', item, ttPos, compare);
  107. },
  108. onClick: function (el, item) {
  109. client.request({
  110. cpn: 'player',
  111. method: 'performAction',
  112. data: {
  113. cpn: 'equipment',
  114. method: 'equip',
  115. data: item.id
  116. }
  117. });
  118. },
  119. onGetStashItems: function (items) {
  120. this.items = items;
  121. //Sort by slot
  122. this.items.sort(function (a, b) {
  123. if (((a.material) && (b.material)) || ((a.quest) && (b.quest)) || ((a.slot != null) && (a.slot == b.slot))) {
  124. if (a.type == b.type) {
  125. if (a.name < b.name)
  126. return -1;
  127. else if (a.name == b.name)
  128. return 0;
  129. else if (a.name > b.name)
  130. return 1;
  131. } else if ((a.type || '') < (b.type || ''))
  132. return -1;
  133. else if ((a.type || '') == (b.type || ''))
  134. return 0;
  135. else if ((a.type || '') > (b.type || ''))
  136. return 1;
  137. } else if ((a.quest) && (!b.quest))
  138. return -1;
  139. else if ((b.quest) && (!a.quest))
  140. return 1;
  141. else if ((a.material) && (!b.material))
  142. return -1;
  143. else if (b.material && (!a.material))
  144. return 1;
  145. else if (a.slot > b.slot)
  146. return -1;
  147. else if (a.slot < b.slot)
  148. return 1;
  149. else
  150. return b.id - a.id;
  151. });
  152. if (this.shown)
  153. this.build();
  154. },
  155. onDestroyStashItems: function (itemIds) {
  156. itemIds.forEach(function (id) {
  157. let item = this.items.find(i => i.id == id);
  158. if (item == this.hoverItem)
  159. this.hideTooltip();
  160. this.items.spliceWhere(i => i.id == id);
  161. }, this);
  162. if (this.shown)
  163. this.build();
  164. },
  165. toggle: function () {
  166. if ((!this.shown) && (!window.player.stash.active))
  167. return;
  168. this.shown = !this.el.is(':visible');
  169. if (this.shown) {
  170. this.show();
  171. events.emit('onShowOverlay', this.el);
  172. this.build();
  173. } else {
  174. this.hide();
  175. events.emit('onHideOverlay', this.el);
  176. events.emit('onHideContextMenu');
  177. }
  178. },
  179. onOpenStash: function () {
  180. this.build();
  181. },
  182. beforeDestroy: function () {
  183. events.emit('onHideOverlay', this.el);
  184. },
  185. withdraw: function (item) {
  186. if (!item)
  187. return;
  188. client.request({
  189. cpn: 'player',
  190. method: 'performAction',
  191. data: {
  192. cpn: 'stash',
  193. method: 'withdraw',
  194. data: item.id
  195. }
  196. });
  197. },
  198. onKeyDown: function (key) {
  199. if (key == 'u')
  200. this.toggle();
  201. else if (key == 'shift') {
  202. this.shiftDown = true;
  203. if (this.hoverItem)
  204. this.onHover();
  205. } else if ((key == 'esc') && (this.shown))
  206. this.toggle();
  207. },
  208. onKeyUp: function (key) {
  209. if (key == 'shift') {
  210. this.shiftDown = false;
  211. if (this.hoverItem)
  212. this.onHover();
  213. }
  214. }
  215. };
  216. });