Module:Icon

From Coral Island Wiki
Revision as of 21:51, 21 December 2023 by Granipouss (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local p = {}
local lib = require('Module:Feature')
local LL = require('Module:Link label')._main

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		removeBlanks = false,
		wrapper = { 'Template:Icon' }
	})
	return p._main(args)
end

function p._main(args)
	local item = args.name or args[1]
	if not item then return mw.html.create() end -- if no input then return blank
	local nolink = lib.isNotEmpty(args.nolink) or false
	local notext = lib.isNotEmpty(args.notext) or false
	local link = nolink and '' or (args.link or item)
	local size = tonumber(args.size or args.s or '30')
	local M_size = size
	local upscale
	if M_size < 51 then
		M_size = 51
		
		--figure out what to size to default back down to
		--  multiples of 8 as they wont become decimal in any screen density
		if     size < 24 then upscale = '16'
		elseif size < 32 then upscale = '24'
		elseif size < 40 then upscale = '32'
		elseif size < 48 then upscale = '40'
		else                  upscale = '48'
		end
	end -- 50px and below gets autoformatted into infoicon
	local ext = args.ext or 'png'
	local amount = args.amount or args.x or args[2]
	local text = args.text or args[3]
	local note = args.note
	local quality = args.quality or args.q
	quality = p.verifyQuality(args.q)
	-- mw.logObject(args)
	local prefix = args.prefix or ''
	local suffix = args.suffix or ''
	prefix = prefix:gsub('{space}', ' ')
	suffix = suffix:gsub('{space}', ' ')
	
	local icon = mw.html.create():tag('span'):addClass('custom-icon')
	local icon_image = icon:tag('span'):addClass('custom-icon-image desktop-only')
	local M_icon_image = icon:tag('span'):addClass('custom-icon-image mobile-only hidden noborder')
	local icon_text = icon:tag('span'):addClass('custom-icon-text')
	
	if upscale then
		M_icon_image:addClass('upscaled upscaled-' .. upscale)
	end
	
	-- main icon image
	local filename = table.concat({prefix, item, suffix})
	icon_image:wikitext('[[File:', filename, '.', ext, '|', size, 'px|link=', link, ']]')
	M_icon_image:wikitext('[[File:', filename, '.', ext, '|', M_size, 'px|link=', link, ']]')
	
	-- quality icon if valid
	if lib.isNotEmpty(quality) then
		icon_image:addClass(' quality-' .. quality)
		M_icon_image:addClass(' quality-' .. quality)
	end
	
	-- auto link unless disable
	if not notext then
		if link == '' then icon_text:wikitext(' ', item)
		elseif link == item then icon_text:wikitext(' ', LL(item))
		else icon_text:wikitext(' [[', link, '|', item, ']]')
		end
	end
	
	-- append text if any
	if lib.isNotEmpty(text) then
		icon_text:wikitext(' ', text)
	end
	
	-- amount if any
	if lib.isNotEmpty(amount) then
		amount = tonumber(amount) ~= nil and lib.thousandsSeparator(amount) or amount
		icon_text:wikitext(' × ', amount)
	end
	
	-- ending note if any
	if lib.isNotEmpty(note) then
		icon_text:wikitext(' (', note, ')')
	end
	-- mw.logObject(icon) -- debug
	return icon
end

function p.verifyQuality(quality)
	if (tonumber(quality) == nil and tostring(quality) == nil) or lib.isEmpty(quality) then return nil end
	if tonumber(quality) == 1 then quality = 'bronze'
	elseif tonumber(quality) == 2 then quality = 'silver'
	elseif tonumber(quality) == 3 then quality = 'gold'
	elseif tonumber(quality) == 4 then quality = 'osmium'
	elseif tostring(quality) and not lib.inArray({'bronze', 'silver', 'gold', 'osmium'}, quality) then
		quality = nil
	else
		quality = nil
	end
	return quality
end

return p