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.
 
 
 

560 line
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. client.request({
  116. cpn: 'social',
  117. method: 'chat',
  118. data: {
  119. message: '{' + item.name + '}',
  120. item: item,
  121. type: 'global'
  122. }
  123. });
  124. },
  125. onMouseDown: function (el, item, down, e) {
  126. if (e.button !== 0)
  127. return;
  128. if (down) {
  129. this.dragEl = el.clone()
  130. .appendTo(this.find('.grid'))
  131. .hide()
  132. .on('mouseup', this.onMouseDown.bind(this, null, null, false))
  133. .addClass('dragging');
  134. this.dragItem = el;
  135. events.emit('onHideItemTooltip', this.hoverItem);
  136. events.emit('onStartMoveItem', this.dragItem.data('item'));
  137. this.hoverItem = null;
  138. } else if (this.dragItem) {
  139. let method = 'moveItem';
  140. if ((this.hoverCell) && (this.hoverCell[0] !== this.dragItem[0])) {
  141. let placeholder = $('<div></div>')
  142. .insertAfter(this.dragItem);
  143. this.dragItem.insertBefore(this.hoverCell);
  144. this.hoverCell.insertBefore(placeholder);
  145. placeholder.remove();
  146. let msgs = [{
  147. id: this.dragItem.data('item').id,
  148. pos: this.dragItem.index()
  149. }];
  150. this.items.find(function (i) {
  151. return (i.id === this.dragItem.data('item').id);
  152. }, this).pos = this.dragItem.index();
  153. let hoverCellItem = this.hoverCell.data('item');
  154. if (hoverCellItem) {
  155. if ((hoverCellItem.name !== this.dragItem.data('item').name) || (!hoverCellItem.quantity)) {
  156. msgs.push({
  157. id: hoverCellItem.id,
  158. pos: this.hoverCell.index()
  159. });
  160. this.items.find(function (i) {
  161. return (i.id === hoverCellItem.id);
  162. }, this).pos = this.hoverCell.index();
  163. } else {
  164. method = 'combineStacks';
  165. msgs = {
  166. fromId: this.dragItem.data('item').id,
  167. toId: hoverCellItem.id
  168. };
  169. }
  170. }
  171. client.request({
  172. cpn: 'player',
  173. method: 'performAction',
  174. data: {
  175. cpn: 'inventory',
  176. method: method,
  177. data: msgs
  178. }
  179. });
  180. events.emit('onStopMoveItem', this.dragItem.data('item'));
  181. this.build();
  182. }
  183. this.dragItem = null;
  184. this.dragEl.remove();
  185. this.dragEl = null;
  186. this.hoverCell = null;
  187. this.find('.hover').removeClass('hover');
  188. }
  189. },
  190. onMouseMove: function (e) {
  191. if (!this.dragEl)
  192. return;
  193. let offset = this.find('.grid').offset();
  194. this.dragEl.css({
  195. left: e.clientX - offset.left - 40,
  196. top: e.clientY - offset.top - 40,
  197. display: 'block'
  198. });
  199. },
  200. onSortInventory: function () {
  201. client.request({
  202. cpn: 'player',
  203. method: 'performAction',
  204. data: {
  205. cpn: 'inventory',
  206. method: 'sortInventory',
  207. data: {}
  208. }
  209. });
  210. },
  211. showContext: function (item, e) {
  212. let menuItems = {
  213. drop: {
  214. text: 'drop',
  215. callback: this.performItemAction.bind(this, item, 'dropItem')
  216. },
  217. destroy: {
  218. text: 'destroy',
  219. callback: this.performItemAction.bind(this, item, 'destroyItem')
  220. },
  221. salvage: {
  222. text: 'salvage',
  223. callback: this.performItemAction.bind(this, item, 'salvageItem'),
  224. hotkey: 'f'
  225. },
  226. stash: {
  227. text: 'stash',
  228. callback: this.performItemAction.bind(this, item, 'stashItem')
  229. },
  230. learn: {
  231. text: 'learn',
  232. callback: this.performItemAction.bind(this, item, 'learnAbility')
  233. },
  234. quickSlot: {
  235. text: 'quickslot',
  236. callback: this.performItemAction.bind(this, item, 'setQuickSlot')
  237. },
  238. use: {
  239. text: 'use',
  240. callback: this.performItemAction.bind(this, item, 'useItem')
  241. },
  242. equip: {
  243. text: 'equip',
  244. callback: this.performItemAction.bind(this, item, 'equip')
  245. },
  246. split: {
  247. text: 'split stack',
  248. callback: this.splitStackStart.bind(this, item)
  249. },
  250. link: {
  251. text: 'link',
  252. callback: this.onClick.bind(this, item, true)
  253. },
  254. divider: '----------'
  255. };
  256. if (item.eq) {
  257. menuItems.learn.text = 'unlearn';
  258. menuItems.equip.text = 'unequip';
  259. }
  260. if (item.active)
  261. menuItems.activate.text = 'deactivate';
  262. let ctxConfig = [];
  263. if (item.ability)
  264. ctxConfig.push(menuItems.learn);
  265. else if (item.type === 'toy' || item.type === 'consumable' || item.useText || item.type === 'recipe') {
  266. if (item.useText)
  267. menuItems.use.text = item.useText;
  268. ctxConfig.push(menuItems.use);
  269. if (!item.has('quickSlot'))
  270. ctxConfig.push(menuItems.quickSlot);
  271. } else if (item.slot) {
  272. ctxConfig.push(menuItems.equip);
  273. if (!item.eq)
  274. ctxConfig.push(menuItems.divider);
  275. }
  276. if (!item.eq && !item.active && !item.quest) {
  277. const isAtStash = window.player.serverActions.hasAction('openStash');
  278. if (isAtStash && !item.noStash)
  279. ctxConfig.push(menuItems.stash);
  280. if (!item.noDrop)
  281. ctxConfig.push(menuItems.drop);
  282. if (!item.material && !item.noSalvage)
  283. ctxConfig.push(menuItems.salvage);
  284. }
  285. if (item.quantity > 1 && !item.quest)
  286. ctxConfig.push(menuItems.split);
  287. ctxConfig.push(menuItems.link);
  288. if (!item.eq && !item.active && !item.noDestroy) {
  289. ctxConfig.push(menuItems.divider);
  290. ctxConfig.push(menuItems.destroy);
  291. }
  292. if (isMobile)
  293. this.hideTooltip(null, this.hoverItem);
  294. if (ctxConfig.length > 0)
  295. events.emit('onContextMenu', ctxConfig, e);
  296. e.preventDefault();
  297. return false;
  298. },
  299. splitStackStart: function (item) {
  300. let box = this.find('.split-box').show();
  301. box.data('item', item);
  302. box.find('.amount')
  303. .val('1')
  304. .focus();
  305. },
  306. splitStackEnd: function (cancel, e) {
  307. let box = this.find('.split-box');
  308. if ((cancel) || (!e) || (e.target !== box.find('.btnSplit')[0])) {
  309. if ((cancel) && (!$(e.target).hasClass('btn')))
  310. box.hide();
  311. return;
  312. }
  313. box.hide();
  314. client.request({
  315. cpn: 'player',
  316. method: 'performAction',
  317. data: {
  318. cpn: 'inventory',
  319. method: 'splitStack',
  320. data: {
  321. itemId: box.data('item').id,
  322. stackSize: ~~this.find('.split-box .amount').val()
  323. }
  324. }
  325. });
  326. },
  327. onChangeStackAmount: function (e, amount) {
  328. let item = this.find('.split-box').data('item');
  329. let delta = amount;
  330. if (e)
  331. delta = (e.originalEvent.deltaY > 0) ? -1 : 1;
  332. if (input.isKeyDown('shift', true))
  333. delta *= 10;
  334. let elAmount = this.find('.split-box .amount');
  335. elAmount.val(Math.max(1, Math.min(item.quantity - 1, ~~elAmount.val() + delta)));
  336. },
  337. onEnterStackAmount: function (e) {
  338. let el = this.find('.split-box .amount');
  339. let val = el.val();
  340. if (+val !== ~~+val)
  341. el.val('');
  342. else if (val) {
  343. let item = this.find('.split-box').data('item');
  344. if (val < 0)
  345. val = '';
  346. else if (val > item.quantity - 1)
  347. val = item.quantity - 1;
  348. el.val(val);
  349. }
  350. },
  351. hideTooltip: function () {
  352. if (this.dragEl) {
  353. this.hoverCell = null;
  354. return;
  355. }
  356. events.emit('onHideItemTooltip', this.hoverItem);
  357. this.hoverItem = null;
  358. },
  359. onHover: function (el, item, e) {
  360. if (this.dragEl) {
  361. this.hoverCell = el;
  362. this.find('.hover').removeClass('hover');
  363. el.addClass('hover');
  364. return;
  365. }
  366. if (item)
  367. this.hoverItem = item;
  368. else
  369. item = this.hoverItem;
  370. if (!item)
  371. return;
  372. let ttPos = null;
  373. if (el) {
  374. if (el.hasClass('new')) {
  375. el.removeClass('new');
  376. el.find('.quantity').html((item.quantity > 1) ? item.quantity : '');
  377. delete item.isNew;
  378. }
  379. ttPos = {
  380. x: ~~(e.clientX + 32),
  381. y: ~~(e.clientY)
  382. };
  383. }
  384. events.emit('onShowItemTooltip', item, ttPos, true);
  385. },
  386. onGetItems: function (items, rerender) {
  387. this.items = items;
  388. if ((this.shown) && (rerender))
  389. this.build();
  390. },
  391. onDestroyItems: function (itemIds) {
  392. itemIds.forEach(function (id) {
  393. let item = this.items.find(i => i.id === id);
  394. if (item === this.hoverItem)
  395. this.hideTooltip();
  396. this.items.spliceWhere(i => i.id === id);
  397. }, this);
  398. if (this.shown)
  399. this.build();
  400. },
  401. onAfterShow: function () {
  402. this.find('.split-box').hide();
  403. this.build();
  404. this.hideTooltip();
  405. },
  406. beforeDestroy: function () {
  407. this.el.parent().css('background-color', 'transparent');
  408. this.el.parent().removeClass('blocking');
  409. },
  410. beforeHide: function () {
  411. if (this.oldSpellsZIndex) {
  412. $('.uiSpells').css('z-index', this.oldSpellsZIndex);
  413. this.oldSpellsZIndex = null;
  414. }
  415. events.emit('onHideInventory');
  416. events.emit('onHideContextMenu');
  417. this.hideTooltip();
  418. },
  419. performItemAction: function (item, action) {
  420. if (!item)
  421. return;
  422. else if ((action === 'equip') && ((item.material) || (item.quest) || (!window.player.inventory.canEquipItem(item))))
  423. return;
  424. else if ((action === 'learnAbility') && (!window.player.inventory.canEquipItem(item)))
  425. return;
  426. let data = item.id;
  427. let cpn = 'inventory';
  428. if (['equip', 'setQuickSlot'].includes(action)) {
  429. cpn = 'equipment';
  430. if (action === 'setQuickSlot') {
  431. data = {
  432. itemId: item.id,
  433. slot: 0
  434. };
  435. }
  436. }
  437. if (action === 'useItem')
  438. this.hide();
  439. client.request({
  440. cpn: 'player',
  441. method: 'performAction',
  442. data: {
  443. cpn: cpn,
  444. method: action,
  445. data: data
  446. }
  447. });
  448. },
  449. onKeyDown: function (key) {
  450. if (key === 'i')
  451. this.toggle();
  452. else if (key === 'shift' && this.hoverItem)
  453. this.onHover();
  454. },
  455. onKeyUp: function (key) {
  456. if (key === 'shift' && this.hoverItem)
  457. this.onHover();
  458. }
  459. };
  460. });