Module:Tooltip format

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
Documentation for Module:Tooltip format does not exist yet [create] (How does this work?)
local Icon = require('Module:Icon')
local getArgs = require('Module:Arguments').getArgs
local p = {}

local function div(class)
	return mw.html.create("div"):addClass(class)
end

local function dt(content)
	return mw.html.create("dt"):wikitext(content)
end
local function dd(content)
	return mw.html.create("dd"):wikitext(content)
end

local function notEmpty(value)
	return value and value ~= ""
end

-- Main attribute formatting functions
local function formatDuration(value)
	if value == 0 then
		return "Instant"
	end
	return value .. "s" -- seconds
end
local function formatDistance(value)
	return value .. "y" -- yalms
end

function p.renderTooltip(frame)
	local data = getArgs(frame)

	local tooltip = div("tooltip")
	local header = div("tooltip__header")
	tooltip:node(header)

	-- Icon
	local icon = div("tooltip_icon")
	header:node(icon)
	icon:node(Icon.gameIcon({
		icon = data['Has game icon'],
		type = data['Has context'] or "", -- aaaAAAAAAAAAAAAAAAAAAAA MINIONS WHY
		frameType = data['Has game icon frame type'],
		size = 'big'
	}))

	local headline = div("tooltip__headline")
	header:node(headline)

	-- Title
	local title = div("tooltip__title")
	headline:node(title)
	local title_text = data['Has canonical name'] or data['Pagename'] or "FIXMEEEEEE"
	title:wikitext(title_text)

	-- Subtitle
	local subtitle_text --- @type string
	if data['Has context'] == 'Action' then -- Actions
		subtitle_text = data['Has action type']
			.. " · Range " .. (data['Has action range'] or 0) .. "y"
			.. " · Radius " .. (data['Has action radius'] or 0) .. "y"
	elseif notEmpty(data['Has item type']) then -- Items incl. weapons and shields
		subtitle_text = data['Has item type']
		if data['Has material type'] then
			subtitle_text = subtitle_text .. " · " .. data['Has material type']
		end
	elseif notEmpty(data['Has armor type']) then -- Armor and accessories
		subtitle_text = data['Has armor type']
	elseif notEmpty(data['Has journal category']) then -- Quests
		subtitle_text = data['Has journal category']
		if notEmpty(data['Has journal genre']) then
			subtitle_text = subtitle_text .. " › " .. data['Has journal genre']
		end
	else
		subtitle_text = data['Has context'] -- fallback
	end
	if subtitle_text then
		local subtitle = div("tooltip__subtitle")
		headline:node(subtitle)
		subtitle:wikitext(subtitle_text)
	end

	-- Action/equipment main attributes
	local attribute_labels
	if data['Has context'] == 'Action' then
		attribute_labels = {
			{'Has casting time', 'Cast', formatDuration},
			{'Has recast time', 'Recast', formatDuration},
			{'Has mp cost', 'MP Cost'},
		}
	elseif data['Has equipment supertype'] == "Weapon" then
		attribute_labels = {
			{'Has weapon physical damage', 'Physical Damage'},
			{'Has weapon magic damage', 'Magic Damage'},
			{'Has weapon auto-attack damage', 'Auto-attack'},
			{'Has weapon delay', 'Delay'}, -- weirdly auto-attack delay is *not* shown in seconds
		}
	elseif data['Has armor type'] == "Shield" then
		attribute_labels = {
			{'Has block strength', 'Block Strength'},
			{'Has block rate', 'Block Rate'},
		}
	elseif data['Has equipment supertype'] == "Armor" or data['Has equipment supertype'] == 'Accessory' then
		attribute_labels = {
			{'Has armor defense', 'Defense'},
			{'Has armor magic defense', 'Magic defense'},
		}
	end
	if attribute_labels then
		local main_attributes = mw.html.create("dl")
		for _, attr in ipairs(attribute_labels) do
			if notEmpty(data[attr[1]]) then
				local value = data[attr[1]]
				if attr[3] then -- Custom formatter
					value = attr[3](value)
				end
				main_attributes:node(dt(attr[2])):node(dd(value))
			end
		end
		local main_attributes_wrapper = div('tooltip__ability-info'):node(main_attributes)
		tooltip:node(div('tooltip__ability-info'):node(main_attributes))
	end

	-- Item level
	if data['Has item level'] then
		local item_level = div('tooltip__item-level'):wikitext('Item level ' .. data['Has item level'])
		tooltip:node(item_level)
	end

	-- Description
	if data['Has game description'] then
		local description_text = data['Has game description']
		if data['Has facts'] then -- TODO: is this relevant to anything other than actions/traits?
			description_text = description_text .. data['Has facts']
		end
		local description = div('tooltip__description'):wikitext('\n' .. description_text .. '\n')
		tooltip:node(description)
	end

	-- Item vendor cost
	if data['Has vendor cost'] then
		-- TODO: do the fancy gsub hack to turn the gil icon into a special character
		-- formerly: {{#replace:{{{Has vendor cost}}}|<span style="position:relative; bottom:2px;">[[File:Gil.png|20px|link=Gil|Gil]]</span>|{{special chat character|gil}}}}
		local cost_text = "Shop selling price: " .. data['Has vendor cost']
		local vendor_cost = div('tooltip__line'):wikitext(cost_text)
		tooltip:node(vendor_cost)
	end

	-- Item sell value
	if data['Has vendor value'] then
		local vendor_value = div('tooltip__line'):wikitext('Sells for ' .. data['Has vendor value'] .. ' gil')
		tooltip:node(vendor_value)
	end

	return tostring(tooltip)
end

return p

--[==[
	{{#if:{{{Has dexterity bonus}}}|
		<div class="tooltip__section-title">Bonuses</div>
		<div class="tooltip__effect-list"><ul>
			<li>Dexterity +{{{Has dexterity bonus}}}</li>
			<li>Critical Hit +{{{Has critical hit bonus}}}</li>
			<li>Vitality +{{{Has vitality bonus}}}</li>
			<li>Determination +{{{Has determination bonus}}}</li>
		</ul></div>
	}}
	<!--
	<div class="tooltip__section-title">Collectable</div>
	<div class="tooltip__attribute-table">
; Collectability
:200
	</div>
	<div class="tooltip__section-title">Effects</div>
	<div class="tooltip__effect-list">
*Gathering +7% (Max 37)
*GP +4% (Max 19)
	</div>
	-->
	<!--
	<div class="tooltip__property-list">
		<ul>
			<li>Unsellable</li>
			<li>Market Prohibited</li>
		</ul>
	</div>
	-->
	<!--
	<div class="tooltip__line tooltip--detail">Lana Rhill</div>
	-->
</div>
]==]--