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.
 
 
 

558 lines
13 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/inventory/template',
  5. 'css!ui/templates/inventory/styles',
  6. 'html!ui/templates/inventory/templateItem',
  7. 'js/input',
  8. 'js/config',
  9. 'ui/shared/renderItem'
  10. ], function (
  11. events,
  12. client,
  13. template,
  14. styles,
  15. tplItem,
  16. input,
  17. config,
  18. renderItem
  19. ) {
  20. return {
  21. tpl: template,
  22. centered: true,
  23. items: [],
  24. dragItem: null,
  25. dragEl: null,
  26. hoverCell: null,
  27. modal: true,
  28. hasClose: true,
  29. oldSpellsZIndex: 0,
  30. postRender: function () {
  31. this.onEvent('onGetItems', this.onGetItems.bind(this));
  32. this.onEvent('onDestroyItems', this.onDestroyItems.bind(this));
  33. this.onEvent('onShowInventory', this.toggle.bind(this));
  34. this.onEvent('onToggleQualityIndicators', this.onToggleQualityIndicators.bind(this));
  35. this.onToggleQualityIndicators(config.qualityIndicators);
  36. this.onEvent('onToggleUnusableIndicators', this.onToggleUnusableIndicators.bind(this));
  37. this.onToggleUnusableIndicators(config.unusableIndicators);
  38. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  39. this.onEvent('onKeyUp', this.onKeyUp.bind(this));
  40. this.find('.grid')
  41. .on('mousemove', this.onMouseMove.bind(this))
  42. .on('mouseleave', this.onMouseDown.bind(this, null, null, false));
  43. this.find('.split-box .amount')
  44. .on('mousewheel', this.onChangeStackAmount.bind(this))
  45. .on('input', this.onEnterStackAmount.bind(this));
  46. this.find('.split-box').on('click', this.splitStackEnd.bind(this, true));
  47. this.find('.split-box .btnSplit').on('click', this.splitStackEnd.bind(this, null));
  48. this.find('.split-box .btnLess').on('click', this.onChangeStackAmount.bind(this, null, -1));
  49. this.find('.split-box .btnMore').on('click', this.onChangeStackAmount.bind(this, null, 1));
  50. this.find('.btnSortInv').on('click', this.onSortInventory.bind(this));
  51. },
  52. build: function () {
  53. let container = this.el.find('.grid')
  54. .empty();
  55. let items = this.items
  56. .filter(function (item) {
  57. return !item.eq;
  58. });
  59. let iLen = Math.max(items.length, 50);
  60. let rendered = [];
  61. for (let i = 0; i < iLen; i++) {
  62. let itemEl = null;
  63. let item = items.find(f => (f.pos !== null && f.pos === i));
  64. if (!item) {
  65. itemEl = renderItem(container, null);
  66. itemEl
  67. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  68. .on('mousemove', this.onHover.bind(this, itemEl, item))
  69. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  70. .children()
  71. .remove();
  72. continue;
  73. } else
  74. rendered.push(item);
  75. itemEl = renderItem(container, item);
  76. let clickHandler = this.onMouseDown.bind(this, itemEl, item, true);
  77. let moveHandler = this.onHover.bind(this, itemEl, item);
  78. if (isMobile) {
  79. clickHandler = this.onHover.bind(this, itemEl, item);
  80. moveHandler = () => {};
  81. }
  82. itemEl
  83. .data('item', item)
  84. .on('click', this.onClick.bind(this, item, false))
  85. .on('mousedown', clickHandler)
  86. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  87. .on('mousemove', moveHandler)
  88. .on('mouseleave', this.hideTooltip.bind(this, itemEl, item))
  89. .find('.icon')
  90. .on('contextmenu', this.showContext.bind(this, item));
  91. }
  92. },
  93. onToggleQualityIndicators: function (state) {
  94. const className = `quality-${state.toLowerCase()}`;
  95. $('.ui-container')
  96. .removeClass('quality-off quality-bottom quality-border quality-background')
  97. .addClass(className);
  98. },
  99. onToggleUnusableIndicators: function (state) {
  100. const className = `unusable-${state.toLowerCase()}`;
  101. $('.ui-container')
  102. .removeClass('unusable-off unusable-border unusable-top unusable-background')
  103. .addClass(className);
  104. },
  105. onClick: function (item, forceCtrl) {
  106. let msg = {
  107. item: item,
  108. success: true
  109. };
  110. events.emit('beforeInventoryClickItem', msg);
  111. if (!msg.success)
  112. return;
  113. if (!forceCtrl && !input.isKeyDown('ctrl', true))
  114. return;
  115. events.emit('beforeInventoryLinkItem', msg);
  116. },
  117. onMouseDown: function (el, item, down, e) {
  118. if (e.button !== 0)
  119. return;
  120. if (down) {
  121. this.dragEl = el.clone()
  122. .appendTo(this.find('.grid'))
  123. .hide()
  124. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  125. .addClass('dragging');
  126. this.dragItem = el;
  127. events.emit('onHideItemTooltip', this.hoverItem);
  128. events.emit('onStartMoveItem', this.dragItem.data('item'));
  129. this.hoverItem = null;
  130. } else if (this.dragItem) {
  131. let method = 'moveItem';
  132. if ((this.hoverCell) && (this.hoverCell[0] !== this.dragItem[0])) {
  133. const data = {};
  134. let placeholder = $('<div></div>')
  135. .insertAfter(this.dragItem);
  136. this.dragItem.insertBefore(this.hoverCell);
  137. this.hoverCell.insertBefore(placeholder);
  138. placeholder.remove();
  139. let msgs = [{
  140. itemId: this.dragItem.data('item').id,
  141. targetPos: this.dragItem.index()
  142. }];
  143. data.moveMsgs = msgs;
  144. this.items.find(function (i) {
  145. return (i.id === this.dragItem.data('item').id);
  146. }, this).pos = this.dragItem.index();
  147. let hoverCellItem = this.hoverCell.data('item');
  148. if (hoverCellItem) {
  149. if ((hoverCellItem.name !== this.dragItem.data('item').name) || (!hoverCellItem.quantity)) {
  150. msgs.push({
  151. itemId: hoverCellItem.id,
  152. targetPos: this.hoverCell.index()
  153. });
  154. this.items.find(function (i) {
  155. return (i.id === hoverCellItem.id);
  156. }, this).pos = this.hoverCell.index();
  157. } else {
  158. delete data.moveMsgs;
  159. data.fromId = this.dragItem.data('item').id;
  160. data.toId = hoverCellItem.id;
  161. method = 'combineStacks';
  162. }
  163. }
  164. client.request({
  165. cpn: 'player',
  166. method: 'performAction',
  167. data: {
  168. cpn: 'inventory',
  169. method: method,
  170. data
  171. }
  172. });
  173. events.emit('onStopMoveItem', this.dragItem.data('item'));
  174. this.build();
  175. }
  176. this.dragItem = null;
  177. this.dragEl.remove();
  178. this.dragEl = null;
  179. this.hoverCell = null;
  180. this.find('.hover').removeClass('hover');
  181. }
  182. },
  183. onMouseMove: function (e) {
  184. if (!this.dragEl)
  185. return;
  186. let offset = this.find('.grid').offset();
  187. this.dragEl.css({
  188. left: e.clientX - offset.left - 40,
  189. top: e.clientY - offset.top - 40,
  190. display: 'block'
  191. });
  192. },
  193. onSortInventory: function () {
  194. client.request({
  195. cpn: 'player',
  196. method: 'performAction',
  197. data: {
  198. cpn: 'inventory',
  199. method: 'sortInventory',
  200. data: {}
  201. }
  202. });
  203. },
  204. showContext: function (item, e) {
  205. let menuItems = {
  206. drop: {
  207. text: 'drop',
  208. callback: this.performItemAction.bind(this, item, 'dropItem')
  209. },
  210. destroy: {
  211. text: 'destroy',
  212. callback: this.performItemAction.bind(this, item, 'destroyItem')
  213. },
  214. salvage: {
  215. text: 'salvage',
  216. callback: this.performItemAction.bind(this, item, 'salvageItem'),
  217. hotkey: 'f'
  218. },
  219. stash: {
  220. text: 'stash',
  221. callback: this.performItemAction.bind(this, item, 'stashItem')
  222. },
  223. learn: {
  224. text: 'learn',
  225. callback: this.performItemAction.bind(this, item, 'learnAbility')
  226. },
  227. quickSlot: {
  228. text: 'quickslot',
  229. callback: this.performItemAction.bind(this, item, 'setQuickSlot')
  230. },
  231. use: {
  232. text: 'use',
  233. callback: this.performItemAction.bind(this, item, 'useItem')
  234. },
  235. equip: {
  236. text: 'equip',
  237. callback: this.performItemAction.bind(this, item, 'equip')
  238. },
  239. split: {
  240. text: 'split stack',
  241. callback: this.splitStackStart.bind(this, item)
  242. },
  243. link: {
  244. text: 'link',
  245. callback: this.onClick.bind(this, item, true)
  246. },
  247. divider: '----------'
  248. };
  249. if (item.eq) {
  250. menuItems.learn.text = 'unlearn';
  251. menuItems.equip.text = 'unequip';
  252. }
  253. if (item.active)
  254. menuItems.activate.text = 'deactivate';
  255. let ctxConfig = [];
  256. if (item.ability)
  257. ctxConfig.push(menuItems.learn);
  258. else if (item.type === 'toy' || item.type === 'consumable' || item.useText || item.type === 'recipe') {
  259. if (item.useText)
  260. menuItems.use.text = item.useText;
  261. ctxConfig.push(menuItems.use);
  262. if (!item.has('quickSlot'))
  263. ctxConfig.push(menuItems.quickSlot);
  264. } else if (item.slot) {
  265. ctxConfig.push(menuItems.equip);
  266. if (!item.eq)
  267. ctxConfig.push(menuItems.divider);
  268. }
  269. if (!item.eq && !item.active && !item.quest) {
  270. const isAtStash = window.player.serverActions.hasAction('openStash');
  271. if (isAtStash && !item.noStash)
  272. ctxConfig.push(menuItems.stash);
  273. if (!item.noDrop)
  274. ctxConfig.push(menuItems.drop);
  275. if (!item.material && !item.noSalvage)
  276. ctxConfig.push(menuItems.salvage);
  277. }
  278. if (item.quantity > 1 && !item.quest)
  279. ctxConfig.push(menuItems.split);
  280. ctxConfig.push(menuItems.link);
  281. if (!item.eq && !item.active && !item.noDestroy) {
  282. ctxConfig.push(menuItems.divider);
  283. ctxConfig.push(menuItems.destroy);
  284. }
  285. if (isMobile)
  286. this.hideTooltip(null, this.hoverItem);
  287. if (ctxConfig.length > 0)
  288. events.emit('onContextMenu', ctxConfig, e);
  289. e.preventDefault();
  290. return false;
  291. },
  292. splitStackStart: function (item) {
  293. let box = this.find('.split-box').show();
  294. box.data('item', item);
  295. box.find('.amount')
  296. .val('1')
  297. .focus();
  298. },
  299. splitStackEnd: function (cancel, e) {
  300. let box = this.find('.split-box');
  301. if ((cancel) || (!e) || (e.target !== box.find('.btnSplit')[0])) {
  302. if ((cancel) && (!$(e.target).hasClass('btn')))
  303. box.hide();
  304. return;
  305. }
  306. box.hide();
  307. client.request({
  308. cpn: 'player',
  309. method: 'performAction',
  310. data: {
  311. cpn: 'inventory',
  312. method: 'splitStack',
  313. data: {
  314. itemId: box.data('item').id,
  315. stackSize: ~~this.find('.split-box .amount').val()
  316. }
  317. }
  318. });
  319. },
  320. onChangeStackAmount: function (e, amount) {
  321. let item = this.find('.split-box').data('item');
  322. let delta = amount;
  323. if (e)
  324. delta = (e.originalEvent.deltaY > 0) ? -1 : 1;
  325. if (input.isKeyDown('shift', true))
  326. delta *= 10;
  327. let elAmount = this.find('.split-box .amount');
  328. elAmount.val(Math.max(1, Math.min(item.quantity - 1, ~~elAmount.val() + delta)));
  329. },
  330. onEnterStackAmount: function (e) {
  331. let el = this.find('.split-box .amount');
  332. let val = el.val();
  333. if (+val !== ~~+val)
  334. el.val('');
  335. else if (val) {
  336. let item = this.find('.split-box').data('item');
  337. if (val < 0)
  338. val = '';
  339. else if (val > item.quantity - 1)
  340. val = item.quantity - 1;
  341. el.val(val);
  342. }
  343. },
  344. hideTooltip: function () {
  345. if (this.dragEl) {
  346. this.hoverCell = null;
  347. return;
  348. }
  349. events.emit('onHideItemTooltip', this.hoverItem);
  350. this.hoverItem = null;
  351. },
  352. onHover: function (el, item, e) {
  353. if (this.dragEl) {
  354. this.hoverCell = el;
  355. this.find('.hover').removeClass('hover');
  356. el.addClass('hover');
  357. return;
  358. }
  359. if (item)
  360. this.hoverItem = item;
  361. else
  362. item = this.hoverItem;
  363. if (!item)
  364. return;
  365. let ttPos = null;
  366. if (el) {
  367. if (el.hasClass('new')) {
  368. el.removeClass('new');
  369. el.find('.quantity').html((item.quantity > 1) ? item.quantity : '');
  370. delete item.isNew;
  371. }
  372. ttPos = {
  373. x: ~~(e.clientX + 32),
  374. y: ~~(e.clientY)
  375. };
  376. }
  377. events.emit('onShowItemTooltip', item, ttPos, true);
  378. },
  379. onGetItems: function (items, rerender) {
  380. this.items = items;
  381. if ((this.shown) && (rerender))
  382. this.build();
  383. },
  384. onDestroyItems: function (itemIds) {
  385. itemIds.forEach(function (id) {
  386. let item = this.items.find(i => i.id === id);
  387. if (item === this.hoverItem)
  388. this.hideTooltip();
  389. this.items.spliceWhere(i => i.id === id);
  390. }, this);
  391. if (this.shown)
  392. this.build();
  393. },
  394. onAfterShow: function () {
  395. this.find('.split-box').hide();
  396. this.build();
  397. this.hideTooltip();
  398. },
  399. beforeDestroy: function () {
  400. this.el.parent().css('background-color', 'transparent');
  401. this.el.parent().removeClass('blocking');
  402. },
  403. beforeHide: function () {
  404. if (this.oldSpellsZIndex) {
  405. $('.uiSpells').css('z-index', this.oldSpellsZIndex);
  406. this.oldSpellsZIndex = null;
  407. }
  408. events.emit('onHideInventory');
  409. events.emit('onHideContextMenu');
  410. this.hideTooltip();
  411. },
  412. performItemAction: function (item, action) {
  413. if (!item)
  414. return;
  415. else if ((action === 'equip') && ((item.material) || (item.quest) || (!window.player.inventory.canEquipItem(item))))
  416. return;
  417. else if ((action === 'learnAbility') && (!window.player.inventory.canEquipItem(item)))
  418. return;
  419. let data = {
  420. itemId: item.id
  421. };
  422. let cpn = 'inventory';
  423. if (['equip', 'setQuickSlot'].includes(action)) {
  424. cpn = 'equipment';
  425. if (action === 'setQuickSlot') {
  426. data = {
  427. itemId: item.id,
  428. slot: 0
  429. };
  430. }
  431. }
  432. if (action === 'useItem')
  433. this.hide();
  434. client.request({
  435. cpn: 'player',
  436. method: 'performAction',
  437. data: {
  438. cpn,
  439. method: action,
  440. data
  441. }
  442. });
  443. },
  444. onKeyDown: function (key) {
  445. if (key === 'i')
  446. this.toggle();
  447. else if (key === 'shift' && this.hoverItem)
  448. this.onHover();
  449. },
  450. onKeyUp: function (key) {
  451. if (key === 'shift' && this.hoverItem)
  452. this.onHover();
  453. }
  454. };
  455. });