Module:Senate seats table

From MicrasWiki
Revision as of 16:05, 3 May 2026 by Ric (talk | contribs) (Created page with "local p = {} local factions = require('Module:Senate faction') local function getArgs(frame) local args = {} local parent = frame:getParent() if parent then for k, v in pairs(parent.args) do args[k] = v end end for k, v in pairs(frame.args) do args[k] = v end return args end function p.start(frame) local args = getArgs(frame) local text = '' text = text .. mw.text.tag('th', {style="width:4em;"}, 'Election') local i = 1 while args[i] and args[...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Documentation for this module may be created at Module:Senate seats table/doc

local p = {}

local factions = require('Module:Senate faction')

local function getArgs(frame)
	local args = {}
	local parent = frame:getParent()

	if parent then
		for k, v in pairs(parent.args) do
			args[k] = v
		end
	end

	for k, v in pairs(frame.args) do
		args[k] = v
	end

	return args
end

function p.start(frame)
	local args = getArgs(frame)
	local text = ''

	text = text .. mw.text.tag('th', {style="width:4em;"}, 'Election')

	local i = 1
	while args[i] and args[i] ~= '' do
		local code = args[i]
		local data = factions.getFaction{args={code}}

		local label = mw.text.tag('span', {style="color:" .. data.t .. ";"}, data.a)

		text = text .. mw.text.tag(
			'th',
			{
				style = "background:" .. data.f .. ";min-width:2.1em;max-width:2.3em;",
				title = data.n
			},
			"[[" .. data.l .. "|" .. label .. "]]"
		)

		i = i + 1
	end

	text = text .. mw.text.tag('th', {style="box-sizing:content-box;width:400px;"}, 'Graphic presentation')
	text = text .. mw.text.tag('th', {style="width:2.1em;"}, 'TOT')
	text = text .. mw.text.tag('th', {style="width:2.5em;"}, '%')

	return mw.text.tag('tr', {}, text)
end

function p.bar(frame)
	local args = getArgs(frame)
	local text = ''
	local width = tonumber(args.width or args.bredd) or 400
	local total = tonumber(args[2]) or 0

	if total == 0 then
		return ''
	end

	local i = 3
	while args[i] and args[i+1] do
		local code = args[i]
		local seats = tonumber(args[i+1]) or 0
		local data = factions.getFaction{args={code}}

		if seats > 0 then
			local px = math.floor(width * seats / total + 0.5)
			local content = ''

			if px > 16 then
				content = tostring(seats)
			end

			text = text .. mw.text.tag(
				'td',
				{
					style = "padding:0;border:0;background:" .. data.f .. ";color:" .. data.t .. ";width:" .. px .. "px;",
					title = tostring(seats)
				},
				content
			)
		end

		i = i + 2
	end

	text = mw.text.tag('tr', {}, text)

	return mw.text.tag(
		'table',
		{
			cellspacing="0",
			cellpadding="0",
			border="0",
			style="border:0;width:" .. width .. "px;text-align:center;line-height:1.3em;padding:0;"
		},
		text
	)
end

function p.row(frame)
	local args = getArgs(frame)
	local text = ''
	local total = tonumber(args[2]) or 0
	local sum = 0

	text = text .. mw.text.tag('td', {}, args[1] or '')

	local i = 3
	while args[i] and args[i+1] do
		local seats = tonumber(args[i+1]) or 0
		sum = sum + seats

		local display = ''
		if seats > 0 then
			display = tostring(seats)
		end

		text = text .. mw.text.tag('td', {}, display)
		i = i + 2
	end

	if sum == total then
		text = text .. mw.text.tag('td', {}, p.bar(frame))
	else
		text = text .. mw.text.tag(
			'td',
			{},
			mw.text.tag('span', {style="color:red;"}, 'ERROR: seat total mismatch')
		)
	end

	text = text .. mw.text.tag('td', {}, tostring(total))
	text = text .. mw.text.tag('td', {}, args.turnout or args.valdelt or '')

	return mw.text.tag('tr', {}, text)
end

function p.finish(frame)
	local args = getArgs(frame)
	local text = ''

	if args[1] and args[1] ~= '' then
		local note = args[1]

		local i = 2
		while args[i] do
			note = note .. mw.text.tag('br') .. args[i]
			i = i + 1
		end

		text = text .. mw.text.tag(
			'tr',
			{},
			mw.text.tag('td', {colspan="100", style="text-align:left;"}, note)
		)
	end

	return text
end

return p