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.
 
 
 

369 lines
9.0 KiB

  1. define([
  2. 'js/system/events',
  3. 'css!ui/templates/tooltipItem/styles',
  4. 'html!ui/templates/tooltipItem/template',
  5. 'html!ui/templates/tooltipItem/templateTooltip',
  6. 'js/misc/statTranslations'
  7. ], function (
  8. events,
  9. styles,
  10. template,
  11. tplTooltip,
  12. statTranslations
  13. ) {
  14. let percentageStats = [
  15. 'addCritChance',
  16. 'addCritMultiplier',
  17. 'addAttackCritChance',
  18. 'addAttackCritMultiplier',
  19. 'addSpellCritChance',
  20. 'addSpellCritMultiplier',
  21. 'sprintChance',
  22. 'xpIncrease',
  23. 'blockAttackChance',
  24. 'blockSpellChance',
  25. 'dodgeAttackChance',
  26. 'dodgeSpellChance',
  27. 'attackSpeed',
  28. 'castSpeed',
  29. 'itemQuantity',
  30. 'magicFind',
  31. 'catchChance',
  32. 'catchSpeed',
  33. 'fishRarity',
  34. 'fishWeight',
  35. 'fishItems'
  36. ];
  37. return {
  38. tpl: template,
  39. type: 'tooltipItem',
  40. tooltip: null,
  41. item: null,
  42. postRender: function () {
  43. this.tooltip = this.el.find('.tooltip');
  44. this.onEvent('onShowItemTooltip', this.onShowItemTooltip.bind(this));
  45. this.onEvent('onHideItemTooltip', this.onHideItemTooltip.bind(this));
  46. },
  47. onHideItemTooltip: function (item) {
  48. if (
  49. (!this.item) ||
  50. (
  51. (this.item !== item) &&
  52. (this.item.refItem) &&
  53. (this.item.refItem !== item)
  54. )
  55. )
  56. return;
  57. this.item = null;
  58. this.tooltip.hide();
  59. },
  60. onShowItemTooltip: function (item, pos, compare, bottomAlign, shiftDown) {
  61. this.item = item;
  62. let tempStats = $.extend(true, {}, item.stats);
  63. let enchantedStats = item.enchantedStats || {};
  64. if ((compare) && (shiftDown)) {
  65. if (!item.eq) {
  66. let compareStats = compare.stats;
  67. for (let s in tempStats) {
  68. if (compareStats[s]) {
  69. let delta = tempStats[s] - compareStats[s];
  70. if (delta > 0)
  71. tempStats[s] = '+' + delta;
  72. else if (delta < 0)
  73. tempStats[s] = delta;
  74. } else
  75. tempStats[s] = '+' + tempStats[s];
  76. }
  77. for (let s in compareStats) {
  78. if (!tempStats[s])
  79. tempStats[s] = -compareStats[s];
  80. }
  81. }
  82. } else {
  83. Object.keys(tempStats).forEach(function (s) {
  84. if (enchantedStats[s]) {
  85. tempStats[s] -= enchantedStats[s];
  86. if (tempStats[s] <= 0)
  87. delete tempStats[s];
  88. tempStats['_' + s] = enchantedStats[s];
  89. }
  90. });
  91. }
  92. let stats = Object.keys(tempStats)
  93. .map(function (s) {
  94. let isEnchanted = (s[0] === '_');
  95. let statName = s;
  96. if (isEnchanted)
  97. statName = statName.substr(1);
  98. statName = statTranslations.translate(statName);
  99. let value = tempStats[s];
  100. if (percentageStats.indexOf(s) > -1)
  101. value += '%';
  102. else if ((s.indexOf('element') === 0) && (s.indexOf('Resist') === -1))
  103. value += '%';
  104. let row = value + ' ' + statName;
  105. let rowClass = '';
  106. if (compare) {
  107. if (row.indexOf('-') > -1)
  108. rowClass = 'loseStat';
  109. else if (row.indexOf('+') > -1)
  110. rowClass = 'gainStat';
  111. }
  112. if (isEnchanted)
  113. rowClass += ' enchanted';
  114. row = '<div class="' + rowClass + '">' + row + '</div>';
  115. return row;
  116. }, this)
  117. .sort(function (a, b) {
  118. return (a.replace(' enchanted', '').length - b.replace(' enchanted', '').length);
  119. })
  120. .sort(function (a, b) {
  121. if ((a.indexOf('enchanted') > -1) && (b.indexOf('enchanted') === -1))
  122. return 1;
  123. else if ((a.indexOf('enchanted') === -1) && (b.indexOf('enchanted') > -1))
  124. return -1;
  125. return 0;
  126. })
  127. .join('');
  128. let implicitStats = (item.implicitStats || []).map(function (s) {
  129. let stat = s.stat;
  130. let statName = statTranslations.translate(stat);
  131. let value = s.value;
  132. if (percentageStats.indexOf(stat) > -1)
  133. value += '%';
  134. else if ((stat.indexOf('element') === 0) && (stat.indexOf('Resist') === -1))
  135. value += '%';
  136. let row = value + ' ' + statName;
  137. let rowClass = '';
  138. row = '<div class="' + rowClass + '">' + row + '</div>';
  139. return row;
  140. }).join('');
  141. let itemName = item.name;
  142. if (item.quantity > 1)
  143. itemName += ' x' + item.quantity;
  144. let level = null;
  145. if (item.level)
  146. level = item.level.push ? item.level[0] + ' - ' + item.level[1] : item.level;
  147. let html = tplTooltip
  148. .replace('$NAME$', itemName)
  149. .replace('$QUALITY$', item.quality)
  150. .replace('$TYPE$', item.type)
  151. .replace('$SLOT$', item.slot)
  152. .replace('$IMPLICITSTATS$', implicitStats)
  153. .replace('$STATS$', stats)
  154. .replace('$LEVEL$', level);
  155. if (item.requires) {
  156. html = html
  157. .replace('$ATTRIBUTE$', item.requires[0].stat)
  158. .replace('$ATTRIBUTEVALUE$', item.requires[0].value);
  159. }
  160. if (item.power)
  161. html = html.replace('$POWER$', ' ' + (new Array(item.power + 1)).join('+'));
  162. if ((item.spell) && (item.spell.values)) {
  163. let abilityValues = '';
  164. for (let p in item.spell.values) {
  165. if ((compare) && (shiftDown)) {
  166. let delta = item.spell.values[p] - compare.spell.values[p];
  167. // adjust by EPSILON to handle float point imprecision, otherwise 3.15 - 2 = 1.14 or 2 - 3.15 = -1.14
  168. // have to move away from zero by EPSILON, not a simple add
  169. if (delta >= 0)
  170. delta += Number.EPSILON;
  171. else
  172. delta -= Number.EPSILON;
  173. delta = ~~((delta) * 100) / 100;
  174. let rowClass = '';
  175. if (delta > 0) {
  176. rowClass = 'gainDamage';
  177. delta = '+' + delta;
  178. } else if (delta < 0)
  179. rowClass = 'loseDamage';
  180. abilityValues += '<div class="' + rowClass + '">' + p + ': ' + delta + '</div>';
  181. } else
  182. abilityValues += p + ': ' + item.spell.values[p] + '<br/>';
  183. }
  184. if (!item.ability)
  185. abilityValues = abilityValues;
  186. html = html.replace('$DAMAGE$', abilityValues);
  187. }
  188. this.tooltip.html(html);
  189. if (!item.level)
  190. this.tooltip.find('.level').hide();
  191. else
  192. this.tooltip.find('.level').show();
  193. if (!item.implicitStats)
  194. this.tooltip.find('.implicitStats').hide();
  195. else
  196. this.tooltip.find('.implicitStats').show();
  197. if (!item.requires) {
  198. if (!item.level)
  199. this.tooltip.find('.requires').hide();
  200. else
  201. this.tooltip.find('.requires .stats').hide();
  202. } else
  203. this.tooltip.find('.requires .stats').show();
  204. if ((!item.stats) || (!Object.keys(item.stats).length))
  205. this.tooltip.children('.stats').hide();
  206. if ((!item.type) || (item.type === item.name))
  207. this.tooltip.find('.type').hide();
  208. else {
  209. this.tooltip.find('.type')
  210. .html(item.type)
  211. .show();
  212. }
  213. if (item.power)
  214. this.tooltip.find('.power').show();
  215. let equipErrors = window.player.inventory.equipItemErrors(item);
  216. equipErrors.forEach(function (e) {
  217. this.tooltip.find('.requires').addClass('high-level');
  218. this.tooltip.find('.requires .' + e).addClass('high-level');
  219. }, this);
  220. if ((item.material) || (item.quest)) {
  221. this.tooltip.find('.level').hide();
  222. this.tooltip.find('.info').hide();
  223. if (item.material)
  224. this.tooltip.find('.material').show();
  225. else if (item.quest)
  226. this.tooltip.find('.quest').show();
  227. } else if (item.eq)
  228. this.tooltip.find('.info').hide();
  229. if (!item.ability)
  230. this.tooltip.find('.damage').hide();
  231. else
  232. this.tooltip.find('.info').hide();
  233. if (item.spell) {
  234. this.tooltip.find('.spellName')
  235. .html(item.spell.name)
  236. .addClass('q' + item.spell.quality)
  237. .show();
  238. this.tooltip.find('.damage')
  239. .show();
  240. if (item.ability)
  241. this.tooltip.find('.spellName').hide();
  242. } else
  243. this.tooltip.find('.spellName').hide();
  244. this.tooltip.find('.worth').html(item.worthText ? ('<br />value: ' + item.worthText) : '');
  245. if (item.effects && item.effects[0].text && item.type !== 'mtx') {
  246. let htmlEffects = '';
  247. item.effects.forEach(function (e, i) {
  248. htmlEffects += e.text;
  249. if (i < item.effects.length - 1)
  250. htmlEffects += '<br />';
  251. });
  252. this.find('.effects')
  253. .html(htmlEffects)
  254. .show();
  255. } else if (item.description) {
  256. this.find('.effects')
  257. .html(item.description)
  258. .show();
  259. } else
  260. this.find('.effects').hide();
  261. if (item.type === 'Reward Card') {
  262. this.find('.spellName')
  263. .html('Set Size: ' + item.setSize)
  264. .show();
  265. }
  266. if (item.factions) {
  267. let htmlFactions = '';
  268. item.factions.forEach(function (f, i) {
  269. let htmlF = f.name + ': ' + f.tierName;
  270. if (f.noEquip)
  271. htmlF = '<font class="color-red">' + htmlF + '</font>';
  272. htmlFactions += htmlF;
  273. if (i < item.factions.length - 1)
  274. htmlFactions += '<br />';
  275. });
  276. this.find('.faction')
  277. .html(htmlFactions)
  278. .show();
  279. } else
  280. this.find('.faction').hide();
  281. if ((shiftDown) || (!compare))
  282. this.tooltip.find('.info').hide();
  283. if (item.cd) {
  284. this.tooltip.find('.info')
  285. .html('cooldown: ' + item.cd)
  286. .show();
  287. } else if (item.uses) {
  288. this.tooltip.find('.info')
  289. .html('uses: ' + item.uses)
  290. .show();
  291. }
  292. this.tooltip
  293. .show();
  294. if (pos) {
  295. if (bottomAlign)
  296. pos.y -= this.tooltip.height();
  297. this.tooltip.css({
  298. left: pos.x,
  299. top: pos.y
  300. });
  301. }
  302. events.emit('onBuiltItemTooltip', this.tooltip);
  303. },
  304. showWorth: function (canAfford) {
  305. this.tooltip.find('.worth').show();
  306. if (!canAfford)
  307. this.tooltip.find('.worth').addClass('no-afford');
  308. }
  309. };
  310. });