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.
 
 
 

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