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.
 
 
 

207 lines
3.9 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. 'js/input',
  8. 'ui/shared/renderItem'
  9. ], function (
  10. events,
  11. client,
  12. template,
  13. styles,
  14. tplItem,
  15. input,
  16. renderItem
  17. ) {
  18. return {
  19. tpl: template,
  20. centered: true,
  21. hoverItem: null,
  22. items: [],
  23. maxItems: null,
  24. modal: true,
  25. hasClose: true,
  26. postRender: function () {
  27. [
  28. 'onKeyUp',
  29. 'onKeyDown',
  30. 'onOpenStash',
  31. 'onAddStashItems',
  32. 'onRemoveStashItems'
  33. ]
  34. .forEach(e => {
  35. this.onEvent(e, this[e].bind(this));
  36. });
  37. },
  38. build: function () {
  39. const { el, maxItems, items } = this;
  40. el.removeClass('scrolls');
  41. if (maxItems > 50)
  42. el.addClass('scrolls');
  43. const container = this.el.find('.grid').empty();
  44. const renderItemCount = Math.max(items.length, maxItems);
  45. for (let i = 0; i < renderItemCount; i++) {
  46. const item = items[i];
  47. const itemEl = renderItem(container, item);
  48. if (!item)
  49. continue;
  50. let moveHandler = this.onHover.bind(this, itemEl, item);
  51. let downHandler = () => {};
  52. if (isMobile) {
  53. moveHandler = () => {};
  54. downHandler = this.onHover.bind(this, itemEl, item);
  55. }
  56. itemEl
  57. .data('item', item)
  58. .on('mousedown', downHandler)
  59. .on('mousemove', moveHandler)
  60. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  61. .find('.icon')
  62. .on('contextmenu', this.showContext.bind(this, item));
  63. }
  64. },
  65. showContext: function (item, e) {
  66. events.emit('onContextMenu', [{
  67. text: 'withdraw',
  68. callback: this.withdraw.bind(this, item)
  69. }], e);
  70. e.preventDefault();
  71. return false;
  72. },
  73. hideTooltip: function () {
  74. events.emit('onHideItemTooltip', this.hoverItem);
  75. this.hoverItem = null;
  76. },
  77. onHover: function (el, item, e) {
  78. if (item)
  79. this.hoverItem = item;
  80. else
  81. item = this.hoverItem;
  82. let ttPos = null;
  83. if (el) {
  84. el.removeClass('new');
  85. delete item.isNew;
  86. let elOffset = el.offset();
  87. ttPos = {
  88. x: ~~(elOffset.left + 74),
  89. y: ~~(elOffset.top + 4)
  90. };
  91. }
  92. events.emit('onShowItemTooltip', item, ttPos, true);
  93. },
  94. onGetStashItems: function (items) {
  95. this.items = items;
  96. if (this.shown)
  97. this.build();
  98. },
  99. onAddStashItems: function (addItems) {
  100. const { items } = this;
  101. addItems.forEach(newItem => {
  102. const existIndex = items.findIndex(i => i.id === newItem.id);
  103. if (existIndex !== -1)
  104. items.splice(existIndex, 1, newItem);
  105. else
  106. items.push(newItem);
  107. });
  108. },
  109. onRemoveStashItems: function (removeItemIds) {
  110. const { items } = this;
  111. removeItemIds.forEach(id => {
  112. const item = items.find(i => i.id === id);
  113. if (item === this.hoverItem)
  114. this.hideTooltip();
  115. items.spliceWhere(i => i.id === id);
  116. });
  117. if (this.shown)
  118. this.build();
  119. },
  120. onAfterShow: function () {
  121. if ((!this.shown) && (!window.player.stash.active))
  122. return;
  123. events.emit('onShowOverlay', this.el);
  124. this.build();
  125. },
  126. beforeHide: function () {
  127. if ((!this.shown) && (!window.player.stash.active))
  128. return;
  129. events.emit('onHideOverlay', this.el);
  130. events.emit('onHideContextMenu');
  131. },
  132. onOpenStash: function ({ items, maxItems }) {
  133. this.maxItems = maxItems;
  134. this.show();
  135. this.onGetStashItems(items);
  136. },
  137. beforeDestroy: function () {
  138. events.emit('onHideOverlay', this.el);
  139. },
  140. withdraw: function (item) {
  141. if (!item)
  142. return;
  143. client.request({
  144. cpn: 'player',
  145. method: 'performAction',
  146. data: {
  147. cpn: 'stash',
  148. method: 'withdraw',
  149. data: {
  150. itemId: item.id
  151. }
  152. }
  153. });
  154. },
  155. onKeyDown: function (key) {
  156. if (key === 'shift' && this.hoverItem)
  157. this.onHover();
  158. else if (key === 'esc' && this.shown)
  159. this.toggle();
  160. },
  161. onKeyUp: function (key) {
  162. if (key === 'shift' && this.hoverItem)
  163. this.onHover();
  164. }
  165. };
  166. });