25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

217 satır
4.0 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. onClick: function (el, item) {
  95. client.request({
  96. cpn: 'player',
  97. method: 'performAction',
  98. data: {
  99. cpn: 'equipment',
  100. method: 'equip',
  101. data: item.id
  102. }
  103. });
  104. },
  105. onGetStashItems: function (items) {
  106. this.items = items;
  107. if (this.shown)
  108. this.build();
  109. },
  110. onAddStashItems: function (addItems) {
  111. const { items } = this;
  112. addItems.forEach(newItem => {
  113. const existIndex = items.findIndex(i => i.id === newItem.id);
  114. if (existIndex !== -1)
  115. items.splice(existIndex, 1, newItem);
  116. else
  117. items.push(newItem);
  118. });
  119. },
  120. onRemoveStashItems: function (removeItemIds) {
  121. const { items } = this;
  122. removeItemIds.forEach(id => {
  123. const item = items.find(i => i.id === id);
  124. if (item === this.hoverItem)
  125. this.hideTooltip();
  126. items.spliceWhere(i => i.id === id);
  127. });
  128. if (this.shown)
  129. this.build();
  130. },
  131. onAfterShow: function () {
  132. if ((!this.shown) && (!window.player.stash.active))
  133. return;
  134. events.emit('onShowOverlay', this.el);
  135. this.build();
  136. },
  137. beforeHide: function () {
  138. if ((!this.shown) && (!window.player.stash.active))
  139. return;
  140. events.emit('onHideOverlay', this.el);
  141. events.emit('onHideContextMenu');
  142. },
  143. onOpenStash: function ({ items, maxItems }) {
  144. this.maxItems = maxItems;
  145. this.show();
  146. this.onGetStashItems(items);
  147. },
  148. beforeDestroy: function () {
  149. events.emit('onHideOverlay', this.el);
  150. },
  151. withdraw: function (item) {
  152. if (!item)
  153. return;
  154. client.request({
  155. cpn: 'player',
  156. method: 'performAction',
  157. data: {
  158. cpn: 'stash',
  159. method: 'withdraw',
  160. data: item.id
  161. }
  162. });
  163. },
  164. onKeyDown: function (key) {
  165. if (key === 'shift' && this.hoverItem)
  166. this.onHover();
  167. else if (key === 'esc' && this.shown)
  168. this.toggle();
  169. },
  170. onKeyUp: function (key) {
  171. if (key === 'shift' && this.hoverItem)
  172. this.onHover();
  173. }
  174. };
  175. });