Jump to content
Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Documentation for this module may be created at Module:Item/doc

-- The in-game style item tooltip of Template:Item.
local p = {}

local QUALITIES = {
	unique = 'Unique', set = 'Set', rare = 'Rare', magic = 'Magic', crafted = 'Crafted',
	runeword = 'Runeword', normal = 'Normal', superior = 'Superior', socketed = 'Socketed',
}

local function lines(text)
	local out = {}
	for line in mw.text.gsplit(text or '', '\n', true) do
		line = mw.text.trim(line)
		if line ~= '' then
			table.insert(out, line)
		end
	end
	return out
end

-- "+[10-20] to Strength": the rolled range gets its own colour
local function stat_line(line)
	return (line:gsub('%[(%-?%d+%-%-?%d+)%]', '<span class="d2-range">[%1]</span>'))
end

function p.tooltip(frame)
	local a = frame:getParent().args
	local quality = mw.ustring.lower(a.quality or 'normal')
	if not QUALITIES[quality] then
		quality = 'normal'
	end
	local box = mw.html.create('div'):addClass('d2-item'):addClass('d2-q-' .. quality)
	if a.inline and a.inline ~= '' then
		box:addClass('d2-item-inline')
	elseif a.store == 'no' then
		-- an item quoted in a page's text sits in the text flow, not floated beside it
		box:addClass('d2-item-quote')
	end
	if a.anchor and a.anchor ~= '' then
		-- the id a [[Page#anchor]] link points to (html5 fragments: spaces become underscores)
		box:attr('id', (mw.text.trim(a.anchor):gsub('%s+', '_')))
	end
	if a.image and a.image ~= '' then
		box:tag('div'):addClass('d2-item-image'):wikitext('[[File:' .. a.image .. '|link=|alt=]]')
	end
	box:tag('div'):addClass('d2-item-name'):wikitext(a.name or mw.title.getCurrentTitle().text)
	if a.base and a.base ~= '' then
		box:tag('div'):addClass('d2-item-base'):wikitext(a.base)
	end
	local meta = box:tag('div'):addClass('d2-item-meta')
	for _, line in ipairs(lines(a.meta)) do
		meta:tag('div'):wikitext(line)
	end
	if a.lvlreq and a.lvlreq ~= '' then
		meta:tag('div'):wikitext('Required Level: ' .. a.lvlreq)
	end
	local stats = box:tag('div'):addClass('d2-item-stats')
	for _, line in ipairs(lines(a.stats)) do
		stats:tag('div'):wikitext(stat_line(line))
	end
	-- set bonuses: the partial ones green, the full set's gold
	for _, part in ipairs({ { a.setbonus, 'd2-item-setbonus' }, { a.fullset, 'd2-item-fullset' } }) do
		local text = lines(part[1])
		if #text > 0 then
			local div = box:tag('div'):addClass(part[2])
			for _, line in ipairs(text) do
				div:tag('div'):wikitext(stat_line(line))
			end
		end
	end
	if a.flavour and a.flavour ~= '' then
		box:tag('div'):addClass('d2-item-flavour'):wikitext(a.flavour)
	end
	if a.footer and a.footer ~= '' then
		box:tag('div'):addClass('d2-item-footer'):wikitext(a.footer)
	end
	return frame:extensionTag('templatestyles', '', { src = 'Template:Item/styles.css' }) .. tostring(box)
end

return p