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/dist/js/ItemTooltip.js

714 lines
20 KiB
JavaScript

"use strict";
/**
* ItemTooltip.
*
* @class
* @param {object} item the item's data. Should have the structure as
* specified in the Guild Wars 2 API version 2
* documentation.
*/
function ItemTooltip(item)
{
// Shortcut references to main Tooltip objects.
var Data = GW2Tooltip.TooltipData;
var Polyfill = GW2Tooltip.Polyfill;
var _item = item;
/**
* Returns the header for the tooltip, with image and item name.
*
* @method getHeader
* @return {string} the header for the tooltip.
*/
var getHeader = function()
{
return "" +
"<img class=\"GW2TooltipImage\" src=\"" + _item.icon + "\" /> " +
"<span class=\"GW2TooltipTitle GW2TooltipRarity" +
_item.rarity + "\">" +
_item.name +
"</span><br />";
};
/**
* Returns the attributes of the item for the tooltip.
*
* @method getAttributes
* @param {boolean} newline true if an extra <br /> should
* be added to the end of the
* string.
* @return {string} the attributes of the item for the
* tooltip.
*/
var getAttributes = function(newline)
{
if(!_item.details.infix_upgrade)
return "";
var part = "",
attributes = _item.details.infix_upgrade.attributes,
i, length = attributes.length;
for(i = 0; i < length; i++)
part += "" +
"+" + attributes[i].modifier +
" " + Data.attributes[attributes[i].attribute] +
"<br />";
return (part !== "" && newline ? part + "<br />" : part);
};
/**
* Returns the buffs of the item for the tooltip.
*
* @method getBuffs
* @return {string} the buffs of the item for the tooltip.
*/
var getBuffs = function()
{
if(_item.details.infix_upgrade.buff === undefined)
return "";
var part = "",
buffs = _item.details.infix_upgrade.buff.description;
if(buffs.length === 0)
return "";
part += "<span style=\"color:#5599ff\">" +
buffs.replace(/(?:\r\n|\r|\n)/g, "<br />") +
"</span><br />";
return part;
};
/**
* Returns the URL to the icon of the specified infusion slot type.
*
* @method getInfusionIcon
* @param {string} type the infusion slot type. Should be one of
* following values: "Agony", "Defense",
* "Utility" or "Offense". The URL to the
* Agony icon is returned otherwise.
* @return {string} the URL to the icon of the specified infusion slot
* type.
*/
var getInfusionIcon = function(type)
{
switch(type)
{
case "Defense":
return Data.icons.base +
Data.icons.ui_infusion_slot_defensive;
case "Offense":
return Data.icons.base +
Data.icons.ui_infusion_slot_offensive;
case "Utility":
return Data.icons.base +
Data.icons.ui_infusion_slot_utility;
default:
return Data.icons.base +
Data.icons.ui_infusion_slot_agony;
}
};
/**
* Returns the infusion slots of the item for the tooltip.
*
* @method getInfusionSlots
* @param {boolean} newline true if an extra <br /> should be added
* to the end of the string.
* @return {string} the infusion slots part of the tooltip.
*/
var getInfusionSlots = function(newline)
{
var part = "",
infusionSlotType,
infusionSlots = _item.details.infusion_slots,
i, length = infusionSlots.length;
for(i = 0; i < length; i++)
{
infusionSlotType = infusionSlots[i].flags[0];
part += "" +
"<img class=\"GW2TooltipInfusionSlotImage\" src=\"" +
getInfusionIcon(infusionSlotType) +
"\" /> " +
"Unused " + infusionSlotType + " Infusion Slot<br />";
}
return (part !== "" && newline ? part + "<br />" : part);
};
/**
* Returns the type of the item if it is an armor piece, or "" otherwise.
*
* @method getArmorType
* @return {string} the type of the item if it is an armor piece, or ""
* otherwise.
*/
var getArmorType = function()
{
if(_item.type !== "Armor")
return "";
switch(_item.details.type)
{
case "Boots":
return "Foot";
case "Coat":
return "Chest";
case "Gloves":
return "Hand";
case "Helm":
case "HelmAquatic":
return "Helm";
case "Leggings":
return "Leg";
case "Shoulders":
return "Shoulder";
default:
return "";
}
};
/**
* Returns the type of the item if it is a weapon, or "" otherwise.
*
* @method getWeaponType
* @return {string} the type of the item if it is a weapon, or ""
* otherwise.
*/
var getWeaponType = function()
{
if(_item.type !== "Weapon")
return "";
switch(_item.details.type)
{
case "Axe":
case "Dagger":
case "Mace":
case "Pistol":
case "Scepter":
case "Sword":
return "Main Hand";
case "Focus":
case "Shield":
case "Torch":
case "Warhorn":
return "Off Hand";
case "Greatsword":
case "Hammer":
case "LongBow":
case "Rifle":
case "ShortBow":
case "Staff":
return "Two-Handed";
case "Harpoon":
case "Speargun":
case "Trident":
return "Aquatic";
case "LargeBundle":
case "SmallBundle":
case "Toy":
case "TwoHandedToy":
return "Other";
default:
return "";
}
};
/**
* Returns the description of the item for the tooltip.
*
* @method getDescription
* @return {string} the description of the item for the tooltip.
*/
var getDescription = function()
{
if(!_item.hasOwnProperty("description"))
return "";
var desc = _item.description.replace(/<c=@.+?>/, function(match)
{
match = match.substring(4, match.length - 1);
match = match.charAt(0).toUpperCase() + match.slice(1);
return "<span class=\"GW2TooltipDesc" + match + "\">";
});
desc = desc.replace(/<\/c>/, "</span>");
return (desc !== "" ? desc + "<br />" : desc);
};
/**
* Returns the description for the tooltip if the item is a consumable, or
* "" otherwise.
*
* @method getConsumableDescription
* @return {string} the description for the tooltip if the item is a
* consumable, or "" otherwise.
*/
var getConsumableDescription = function()
{
if(_item.type !== "Consumable")
return "";
var desc = _item.description;
if(desc === undefined || desc === "")
if(_item.details.description !== undefined &&
_item.details.description !== "")
desc = _item.details.description;
else
desc = "";
switch(_item.details.type)
{
case "Booze":
return "Double-click to consume.<br />" +
"Excessive alcohol consumption will result in" +
"intoxication.<br />";
case "Food":
case "Utility":
return "Double-click to consume.<br />" +
"<div class=\"ItemTooltipConsumableDescriptionIndent\">" +
"Nourishment(" + _item.details.duration_ms / 60000 +
"m): " +
desc.replace(/(?:\r\n|\r|\n)/g,
"<br />") + "</div>";
case "Generic":
return "Double-click to consume.<br />" +
desc.replace(/(?:\r\n|\r|\n)/g,
"<br />") + "<br />";
default:
return "Double-click to consume.<br />" +
getDescription();
}
};
/**
* Returns the level of the item for the tooltip.
*
* @method getLevel
* @return {string} the level of the item for the tooltip.
*/
var getLevel = function()
{
return (
_item.level > 0 ?
"Required Level: " + _item.level + "<br />" :
""
);
};
/**
* Returns the flags of the item for the tooltip.
*
* @method getFlags
* @return {string} the flags of the item for the tooltip.
*/
var getFlags = function()
{
var part = "",
flags = _item.flags;
// Unique
if(Polyfill.indexOf(flags, "Unique") >= 0)
part += "Unique<br />";
// Account bound
if(Polyfill.indexOf(flags, "AccountBound") >= 0)
if(Polyfill.indexOf(flags, "AccountBindOnUse") >= 0)
part += "Account Bound on Acquire or Use<br />";
else
part += "Account Bound on Acquire<br />";
else if(Polyfill.indexOf(flags, "AccountBindOnUse") >= 0)
part += "Account Bound on Use<br />";
// Soul bound
if(Polyfill.indexOf(flags, "SoulbindOnAcquire") >= 0)
if(Polyfill.indexOf(flags, "SoulbindOnUse") >= 0)
part += "Soulbound on Acquire or Use<br />";
else
part += "Soulbound on Acquire<br />";
else if(Polyfill.indexOf(flags, "SoulbindOnUse") >= 0)
part += "Soulbound on Use<br />";
/* Not actually used in-game
// Sold or salvaged
if(Polyfill.indexOf(flags, "NoSalvage") >= 0)
if(Polyfill.indexOf(flags, "NoSell") >= 0)
part += "Can\'t be sold or salvaged<br />";
else
part += "Can\'t be salvaged<br />";
else
part += "Can\'t be sold<br />";*/
return part;
};
/**
* Returns the rarity of the item for the tooltip.
*
* @method getRarity
* @return {string} the rarity of the item for the tooltip.
*/
var getRarity = function()
{
return (
_item.rarity == "Basic" ?
"" :
_item.rarity + "<br />"
);
};
/**
* Returns the value of the item for the tooltip.
*
* @method getValue
* @return {string} the value of the item for the tooltip.
*/
var getValue = function()
{
if(Polyfill.indexOf(_item.flags, "NoSell") > -1)
return "";
var part = "",
value = item.vendor_value,
multiplier;
if(value >= 10000)
{
multiplier = Math.floor(value / 10000);
value -= multiplier * 10000;
part += "" +
"<span class=\"GW2TooltipValueGold\">" +
multiplier +
"</span> " +
"<img class=\"GW2TooltipValueIcon\" src=\"" +
Data.icons.base + Data.icons.ui_coin_gold +
"\" /> ";
}
if(value >= 100 || part.length > 0)
{
multiplier = Math.floor(value / 100);
value -= multiplier * 100;
part += "" +
"<span class=\"GW2TooltipValueSilver\">" +
multiplier +
"</span> " +
"<img class=\"GW2TooltipValueIcon\" src=\"" +
Data.icons.base + Data.icons.ui_coin_silver +
"\" /> ";
}
if(value >= 1 || part.length > 0)
{
part += "" +
"<span class=\"GW2TooltipValueCopper\">" +
value +
"</span> " +
"<img class=\"GW2TooltipValueIcon\" src=\"" +
Data.icons.base + Data.icons.ui_coin_copper +
"\" />";
}
return part;
};
/**
* List of tooltip generators for subtypes.
*
* @type {Object}
*/
var tooltipString =
{
/**
* Returns the tooltip string for an armor piece.
*
* @method
* @return {string} the tooltip string for an armor piece.
*/
Armor : function()
{
return "" +
getHeader() +
"Defense: " + _item.details.defense + "<br />" +
getAttributes(true) +
getInfusionSlots(true) +
getRarity() +
_item.details.weight_class + "<br />" +
getArmorType() + " Armor<br />" +
getLevel() +
getDescription() +
getFlags() +
getValue();
},
/**
* Returns the tooltip string for a back item.
*
* @method
* @return {string} the tooltip string for a back item.
*/
Back : function()
{
return "" +
getHeader() +
"<br />" +
getRarity() +
"Back Item<br />" +
getDescription() +
getLevel() +
getFlags() +
getValue();
},
/**
* Returns the tooltip string for a bag.
*
* @method
* @return {string} the tooltip string for a bag.
*/
Bag : function()
{
return "" +
getHeader() +
"<br />" +
getDescription() +
getLevel() +
getValue();
},
/**
* Returns the tooltip string for a consumable.
*
* @method
* @return {string} the tooltip string for a consumable.
*/
Consumable : function()
{
return "" +
getHeader() +
getConsumableDescription() +
"<br />" +
"Consumable<br />" +
getLevel() +
getFlags() +
getValue();
},
/**
* Returns the tooltip string for a container.
*
* @method
* @return {string} the tooltip string for a container.
*/
Container : function()
{
return "" +
getHeader() +
getDescription() +
"<br />" +
"Consumable<br />" +
getFlags() +
getValue();
},
/**
* Returns the tooltip string for a gathering tool.
*
* @method
* @return {string} the tooltip string for a gathering tool.
*/
Gathering : function()
{
return "" +
getHeader() +
getDescription() +
(_item.level > 0 ? "<br />" : "") +
getLevel() +
getFlags();
},
/**
* Returns the tooltip string for a gizmo.
*
* @method
* @return {string} the tooltip string for a gizmo.
*/
Gizmo : function()
{
return "" +
getHeader() +
getDescription() +
getFlags() +
getValue();
},
/**
* Returns the tooltip string for a miniature.
*
* @method
* @return {string} the tooltip string for a miniature.
*/
MiniPet : function()
{
return "" +
getHeader() +
getDescription() +
"<br />" +
"Miniature<br />" +
getFlags();
},
/**
* Returns the tooltip string for a salvage kit.
*
* @method
* @return {string} the tooltip string for a salvage kit.
*/
Tool : function()
{
return "" +
getHeader() + "<br />" +
getRarity() +
"Consumable<br />" +
getDescription() +
getFlags();
},
/**
* Returns the tooltip string for a trinket.
*
* @method
* @return {string} the tooltip string for a trinket.
*/
Trinket : function()
{
return "" +
getHeader() +
getAttributes(true) +
getInfusionSlots(true) +
getRarity() +
_item.details.type + "<br />" +
getLevel() +
getDescription() +
getFlags() +
getValue();
},
/**
* Returns the tooltip string for a trophy.
*
* @method
* @return {string} the tooltip string for a trophy.
*/
Trophy : function()
{
return "" +
getHeader() +
getDescription() +
"<br />" +
"Trophy<br />" +
getFlags() +
getValue();
},
/**
* Returns the tooltip string for an upgrade component.
*
* @method
* @return {string} the tooltip string for an upgrade component.
*/
UpgradeComponent : function()
{
return "" +
getHeader() +
"<br />" +
getBuffs() +
getDescription() +
getLevel() +
(_item.details.type === "Gem" ? "<br />" : "") +
getValue();
},
/**
* Returns the tooltip string for a weapon.
*
* @method
* @return {string} the tooltip string for a weapon.
*/
Weapon : function()
{
return "" +
getHeader() +
"Weapon Strength: " +
Polyfill.commaSeparate(_item.details.min_power) + " - " +
Polyfill.commaSeparate(_item.details.max_power) + "<br />" +
getAttributes(true) +
getInfusionSlots(true) +
getRarity() +
_item.details.type + "<br />" +
"(" + getWeaponType() + ")<br />" +
getLevel() +
getDescription() +
getFlags() +
getValue();
},
/**
* Returns the tooltip string for a generic item. Used for crafting
* materials( and traits).
*
* @method
* @return {string} the tooltip string for a generic item.
*/
Generic : function()
{
return "" +
getHeader() +
getDescription() +
getFlags() +
"<br />" +
getValue();
}
};
/**
* 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(_item.type))
tooltip += tooltipString[_item.type]();
else
tooltip += tooltipString.Generic();
tooltip += "</div></div>";
return tooltip;
};
}
ItemTooltip.type = "items";
ItemTooltip.attr = "itemid";