"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 "" + " " + "" + _item.name + "
"; }; /** * Returns the attributes of the item for the tooltip. * * @method getAttributes * @param {boolean} newline true if an extra
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] + "
"; return (part !== "" && newline ? part + "
" : 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 += "" + buffs.replace(/(?:\r\n|\r|\n)/g, "
") + "

"; 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
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 += "" + " " + "Unused " + infusionSlotType + " Infusion Slot
"; } return (part !== "" && newline ? part + "
" : 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(//, function(match) { match = match.substring(4, match.length - 1); match = match.charAt(0).toUpperCase() + match.slice(1); return ""; }); desc = desc.replace(/<\/c>/, ""); return (desc !== "" ? desc + "
" : 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.
" + "Excessive alcohol consumption will result in" + "intoxication.
"; case "Food": case "Utility": return "Double-click to consume.
" + "
" + "Nourishment(" + _item.details.duration_ms / 60000 + "m): " + desc.replace(/(?:\r\n|\r|\n)/g, "
") + "
"; case "Generic": return "Double-click to consume.
" + desc.replace(/(?:\r\n|\r|\n)/g, "
") + "
"; default: return "Double-click to consume.
" + 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 + "
" : "" ); }; /** * 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
"; // Account bound if(Polyfill.indexOf(flags, "AccountBound") >= 0) if(Polyfill.indexOf(flags, "AccountBindOnUse") >= 0) part += "Account Bound on Acquire or Use
"; else part += "Account Bound on Acquire
"; else if(Polyfill.indexOf(flags, "AccountBindOnUse") >= 0) part += "Account Bound on Use
"; // Soul bound if(Polyfill.indexOf(flags, "SoulbindOnAcquire") >= 0) if(Polyfill.indexOf(flags, "SoulbindOnUse") >= 0) part += "Soulbound on Acquire or Use
"; else part += "Soulbound on Acquire
"; else if(Polyfill.indexOf(flags, "SoulbindOnUse") >= 0) part += "Soulbound on Use
"; /* 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
"; else part += "Can\'t be salvaged
"; else part += "Can\'t be sold
";*/ 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 + "
" ); }; /** * 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 += "" + "" + multiplier + " " + " "; } if(value >= 100 || part.length > 0) { multiplier = Math.floor(value / 100); value -= multiplier * 100; part += "" + "" + multiplier + " " + " "; } if(value >= 1 || part.length > 0) { part += "" + "" + value + " " + ""; } 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 + "
" + getAttributes(true) + getInfusionSlots(true) + getRarity() + _item.details.weight_class + "
" + getArmorType() + " Armor
" + 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() + "
" + getRarity() + "Back Item
" + getDescription() + getLevel() + getFlags() + getValue(); }, /** * Returns the tooltip string for a bag. * * @method * @return {string} the tooltip string for a bag. */ Bag : function() { return "" + getHeader() + "
" + getDescription() + getLevel() + getValue(); }, /** * Returns the tooltip string for a consumable. * * @method * @return {string} the tooltip string for a consumable. */ Consumable : function() { return "" + getHeader() + getConsumableDescription() + "
" + "Consumable
" + getLevel() + getFlags() + getValue(); }, /** * Returns the tooltip string for a container. * * @method * @return {string} the tooltip string for a container. */ Container : function() { return "" + getHeader() + getDescription() + "
" + "Consumable
" + 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 ? "
" : "") + 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() + "
" + "Miniature
" + getFlags(); }, /** * Returns the tooltip string for a salvage kit. * * @method * @return {string} the tooltip string for a salvage kit. */ Tool : function() { return "" + getHeader() + "
" + getRarity() + "Consumable
" + 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 + "
" + 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() + "
" + "Trophy
" + getFlags() + getValue(); }, /** * Returns the tooltip string for an upgrade component. * * @method * @return {string} the tooltip string for an upgrade component. */ UpgradeComponent : function() { return "" + getHeader() + "
" + getBuffs() + getDescription() + getLevel() + (_item.details.type === "Gem" ? "
" : "") + 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) + "
" + getAttributes(true) + getInfusionSlots(true) + getRarity() + _item.details.type + "
" + "(" + getWeaponType() + ")
" + 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() + "
" + 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 = "
" + "
"; if(tooltipString.hasOwnProperty(_item.type)) tooltip += tooltipString[_item.type](); else tooltip += tooltipString.Generic(); tooltip += "
"; return tooltip; }; } ItemTooltip.type = "items"; ItemTooltip.attr = "itemid";