tools
/
gw2-tooltips
Archived
1
0
Fork 0
This repository has been archived on 2022-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
gw2-tooltips/js/SkillTooltip.js

251 lines
7.5 KiB
JavaScript

"use strict";
/**
* SkillTooltip.
*
* @class
* @param {object} skill the skill's data. Should have the structure as
* specified in the Guild Wars 2 API version 2
* documentation.
*/
function SkillTooltip(skill)
{
// Shortcut references to main Tooltip objects.
var Data = GW2Tooltip.TooltipData;
var Polyfill = GW2Tooltip.Polyfill;
var _skill = skill;
/**
* Returns the header for the tooltip, with skill name, skill cast time and
* skill recharge time.
*
* @method getHeader
* @return {string} the header for the tooltip, with skill name, skill
* cast time and skill recharge time.
*/
var getHeader = function()
{
return "" +
"<span class=\"SkillTooltipBeige\">" +
_skill.name +
"</span>" +
"<div style=\"float:right\">" +
// Casting time is not exposed in API
//getSmallFact("CastingTime", "value") +
getSmallFact("Recharge", "value") +
"</div><br />";
};
/**
* Returns the description of the skill for the tooltip, or "" if it has
* none.
*
* @method getDescription
* @return {string} the description of the skill for the tooltip, or ""
* if it has none.
*/
var getDescription = function()
{
var desc;
if(_skill.desc !== undefined && _skill.desc !== "")
desc = _skill.desc;
else if(_skill.description !== undefined && _skill.description !== "")
desc = _skill.description;
else
return "";
return desc.replace(/^[^\s]+\./, function(val)
{
return "<span class=\"SkillTooltipBeige\">" + val + "</span>";
}) + "<br />";
};
/**
* Returns the first fact with the given type.
*
* @method getRawFacts
* @param {string} type the type of the fact to return.
* @return {fact[]} the first fact with the given type.
*/
var getRawFacts = function(type)
{
var i, length = _skill.facts.length,
facts = [];
for(i = 0; i < length; i++)
if(_skill.facts[i].type === type)
facts.push(_skill.facts[i]);
return facts;
};
/**
* Returns the given sub-value followed by an icon of the first fact with
* the given type, or "" if the fact does not exist.
*
* @method getSmallFact
* @param {string} type the type of the fact to return.
* @param {string} val the key of the sub-value of the fact.
* @return {string} the given sub-value followed by an icon of the
* first fact with the given type, or "" if the
* fact does not exist.
*/
var getSmallFact = function(type, val)
{
var fact = getRawFacts(type)[0];
if(fact === undefined)
return "";
return "" +
fact[val] +
"<img " +
"class=\"SkillTooltipIconSmall\" " +
"src=\"" + (fact.icon || "") + "\" " +
"/>";
};
/**
* Returns a large fact with the given icon and text.
*
* @method getLargeFact
* @param {string} icon the URL to the icon. Undefined values
* allowed.
* @param {string} text the text to be displayed next to the icon.
* @return {string} a large fact with the given icon and text.
*/
var getLargeFact = function(icon, text)
{
return "" +
"<div class=\"SkillTooltipInfoSection\">" +
"<img " +
"class=\"SkillTooltipIconLarge\" " +
"src=\"" + (icon || "") + "\" " +
"/>" +
"<div>" +
"<div>" +
text +
"</div>" +
"</div>" +
"</div>";
};
/**
* Returns all large facts for the given type in undetermined order.
*
* @method getLargeFacts
* @param {string} type the type of the large facts.
* @param {string} val the key of the sub-value of the fact.
* @param {string} suffix a bit of text to append to the value.
* @return {string} all large facts for the given type in undetermined
* order.
*/
var getLargeFacts = function(type, val, suffix)
{
var facts = getRawFacts(type);
if(facts.length === 0)
return "";
var ret = [],
i, length = facts.length;
for(i = 0; i < length; i++)
ret.push(getLargeFact(
facts[i].icon,
facts[i].text + ": " + facts[i][val] + (suffix || "")
));
return ret.join("");
};
/**
* Returns the buffs of the skill for the tooltip.
*
* @method getBuffs
* @return {string} the buffs of the skill for the tooltip.
*/
var getBuffs = function()
{
var buff, buffs = getRawFacts("Buff"),
ret = [];
var i, length = buffs.length;
for(i = 0; i < length; i++)
{
buff = buffs[i];
ret.push(getLargeFact(
buff.icon,
buff.status +
(
buff.duration !== undefined && buff.duration != "0" ?
"(" + buff.duration + "s)" :
""
) +
": " + (buff.description || "")
));
}
return ret.join("");
};
/**
* Returns a large fact if the skill is unblockable, or "" othwerise.
*
* @method getUnblockable
* @return {string} a large fact if the skill is unblockable, or ""
* othwerise.
*/
var getUnblockable = function()
{
var fact = getRawFacts("Unblockable")[0];
if(fact === undefined)
return "";
return getLargeFact(fact.icon, "Unblockable");
};
/**
* List of tooltip generators for subtypes.
*
* @type {Object}
*/
var tooltipString =
{
Generic : function()
{
return "" +
getHeader() +
getDescription() + "<br />" +
//getLargeFacts("Damage", "hit_count") +
getBuffs() +
getLargeFacts("Number", "value") +
getLargeFacts("Time", "duration", " seconds") +
getLargeFacts("Distance", "distance") +
getLargeFacts("ComboFinisher", "finisher_type") +
getLargeFacts("Range", "value") +
getUnblockable();
}
};
/**
* Returns the tooltip string appropriate for the item.
*
* @method getTooltip
* @return {string} the tooltip string appropriate for the item.
*/
this.getTooltip = function()
{
var tooltip = "<div class=\"GW2Tooltip\">" +
"<div class=\"GW2TooltipContents\">";
if(tooltipString.hasOwnProperty(_skill.type))
tooltip += tooltipString[_skill.type]();
else
tooltip += tooltipString.Generic();
tooltip += "</div></div>";
return tooltip;
};
}
SkillTooltip.type = "skills";
SkillTooltip.attr = "skillid";