Module:Iconbar

From WoopMC
Revision as of 14:40, 30 December 2021 by StardustLabs>Tera458 (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This module implements {{iconbar}}. It should generally be invoked directly on template pages, rather than using the iconbar template.

Parent arguments are automatically merged with directly passed arguments (the latter overwriting the former).

Dependencies


local p = {}
function p.bar( f )
	local args = require( 'Module:ProcessArgs' ).merge()
	local alt = args.alt or ''
	local link = args.link or ''
	local full = args.full
	local half = args.half or 'Half ' .. full
	local empty = args.empty or 'Empty ' .. full
	local value = math.abs( tonumber( args.value ) or 0 ) / 2
	local min = math.ceil( math.abs( tonumber( args.min ) or 0 ) / 2 )
	local size = args.size or ''
	local title = args.title or ''
	local reverse = args.reverse or ''
	
	if title:lower() == 'none' then
		title = ''
	elseif title ~= '' then
		title = ' title="' .. title .. '"'
	else
		title = ' title="' .. value .. '"'
	end
	
	local fullIcon = ''
	local halfIcon = ''
	local emptyIcon = ''
	
	if tonumber( size ) then
		size = '|' .. size .. 'px'
	elseif size ~= '' then
		size = '|' .. size
	end
	
	if value == 0 then
		emptyIcon = '[[File:' .. empty .. size .. '|link=' .. link .. '|alt=' .. alt .. ']]'
	else
		fullIcon = string.rep( '[[File:' .. full .. size .. '|link=' .. link .. '|alt=' .. alt .. ']]', math.floor( value ) )
		
		if math.floor( value ) ~= value then
			halfIcon = '[[File:' .. half .. size .. '|link=' .. link .. '|alt=' .. alt .. ']]'
		end
	end
	
	if min - value >= 1 then
		emptyIcon = string.rep( '[[File:' .. empty .. size .. '|link=' .. link .. '|alt=' .. alt .. ']]', min - math.ceil( value ) )
	end
	
	if reverse ~= '' then
		return '<span class="pixel-image nowrap"' .. title .. '>' .. emptyIcon .. halfIcon .. fullIcon .. '</span>'
	else
		return '<span class="pixel-image nowrap"' .. title .. '>' .. fullIcon .. halfIcon .. emptyIcon .. '</span>'
	end
end
return p