Module:Userbox: Difference between revisions

From IdleOn MMO Wiki
(Created page with "local string = require('Module:String') local table = require('Module:Table') local getArgs = require('Module:Arguments').getArgs local p = {} local skinDefault = mw.loadData('Module:Userbox/Skins') function p.create(frame) local args = getArgs(frame) local main, id, info = {}, {}, {} local flavor = args[1] or args['skin'] main.class = args['class'] main.margin = args['margin'] main.style = args['style'] main.float = args['float'] main.border_width = args['b...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
local string = require('Module:String')
local string = require('Module:String/Nano')
local table = require('Module:Table')
local table = require('Module:Table')
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs

Latest revision as of 11:59, 6 February 2023

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

local string = require('Module:String/Nano')
local table = require('Module:Table')
local getArgs = require('Module:Arguments').getArgs

local p = {}

local skinDefault = mw.loadData('Module:Userbox/Skins')

function p.create(frame)
	local args = getArgs(frame)
	local main, id, info = {}, {}, {}
	local flavor = args[1] or args['skin']
	
	main.class = args['class']
	main.margin = args['margin']
	main.style = args['style']
	main.float = args['float']
	main.border_width = args['border-width']
	main.border_color = args['border-color']
	main.height = args['height']
	
	id.id = args['id']
	id.width = args['id-width']
	id.color = args['id-color']
	id.background = args['id-background']
	id.align = args['id-align']
	id.size = args['id-size']
	id.padding = args['id-padding']
	id.css = args['id-css']
	
	info.info = args['info']
	info.color = args['info-color']
	info.background = args['info-background']
	info.align = args['info-align']
	info.size = args['info-size']
	info.padding = args['info-padding']
	info.css = args['info-css']
	
	return p._create(flavor, main, id, info)
end

function p._create(flavor, main, id, info)
	local mainStyle, idStyle, infoStyle = {}, {}, {}
	local mainHtml, idHtml, infoHtml
	main, id, info = main or {}, id or {}, info or {}
	-- Helper functions
	local function squash(style)
		local ret = {}
		for k,v in pairs(style) do
			ret[#ret+1] = ('%s: %s;'):format(k,v)
		end
		return table.concat(ret, '')
	end
	local function stringify(val)
		val = string.trim(val and tostring(val) or '')
		return (val ~= '') and val or nil
	end
	-- Get methods for choosing
	local function getGenericValue(val, def)
		val = stringify(val)
		return val or def
	end
	local function getColorValue(val, def)
		val = stringify(val)
		return val and (val:lower() == 'none' and 'transparent' or val) or def
	end
	local function getNumericValue(val, def)
		val = stringify(val)
		return val and val:match('^%s*(%d+)') or def
	end
	-- Default Tables
	local sDefault = skinDefault[flavor and flavor:lower()] or skinDefault.modern
	local default = skinDefault.modern
	local function getDefault(tableKey, attr)
		return sDefault[tableKey][attr] or default[tableKey][attr]
	end
	-- Getter entry point
	local function getWithMethod(method, tabl, tableKey, attr)
		return method(tabl[attr], getDefault(tableKey, attr))
	end
	
	mainStyle["width"] = '330px'
	mainStyle["min-height"] = "90px"
	mainStyle["margin"] = getWithMethod(getGenericValue, main, 'main', 'margin')
	mainStyle["float"] = getWithMethod(getGenericValue, main, 'main', 'float')
	mainStyle["border"] = getWithMethod(getNumericValue, main, 'main', 'border_width') .. 'px solid ' .. getWithMethod(getColorValue, main, 'main','border_color')
	mainStyle["height"] = getWithMethod(getNumericValue, main, 'main', 'height') .. 'px'
	
	idStyle["width"] = getWithMethod(getNumericValue, id, 'id', 'width') .. 'px'
	idStyle["color"] = getWithMethod(getColorValue, id, 'id', 'color')
	idStyle["background"] = getWithMethod(getColorValue, id, 'id', 'background')
	idStyle["text-align"] = getWithMethod(getGenericValue, id, 'id', 'align')
	idStyle["font-size"] = getWithMethod(getNumericValue, id, 'id', 'size') .. 'px'
	idStyle["padding"] = getWithMethod(getGenericValue, id, 'id', 'padding')
	idStyle["vertical-align"] = 'middle'
	
	infoStyle["color"] = getWithMethod(getColorValue, info, 'info', 'color')
	infoStyle["background"] = getWithMethod(getColorValue, info, 'info', 'background')
	infoStyle["text-align"] = getWithMethod(getGenericValue, info, 'info', 'align')
	infoStyle["font-size"] = getWithMethod(getNumericValue, info, 'info', 'size') .. 'px'
	infoStyle["padding"] = getWithMethod(getGenericValue, info, 'info', 'padding')
	infoStyle["vertical-align"] = 'middle'
	
	idHtml = string.wrapHtml(id.id or 'id', 'th', { style = squash(idStyle) .. (id.css or ''), class = 'userbox-id' })
	
	infoHtml = string.wrapHtml(info.info or "''info''", 'td', { style = squash(infoStyle) .. (info.css or ''), class = 'userbox-info' })
	
	
	main.style = squash(mainStyle) .. (main.style or '')
	main.class = getGenericValue(main.class, getDefault('main', 'class'))
	main.class = main.class and ('article-userbox ' .. main.class) or 'article-userbox'
	
	mainHtml = string.wrapHtml(idHtml..infoHtml, 'table', { cellspacing = "0", class = main.class, style = main.style })
	
	return mainHtml
end

return p