Modul:ValgInfoboks
Udseende
require("strict")
local p = {}
local partyMod = require("Modul:Politisk parti")
local yesno = require("Modul:Yesno")
--------------------------------------------------------------------
-- Helpers
--------------------------------------------------------------------
-- Fjern ' og " og trim – så ''Ny'' bliver til "ny"
local function normFlag(s)
if not s then return "" end
s = s:gsub("'", ""):gsub('"', "")
s = s:lower()
return s
end
local function trim(s)
if s then
return mw.text.trim (s)
else
return ""
end
end
local function extractYear(text)
if not text then return "" end
local year = text:match("(%d%d%d%d)")
return year or ""
end
local function percentValue(aktiv, prevPct, nowPct)
aktiv = (aktiv or ""):lower()
prevPct = prevPct or ""
nowPct = nowPct or ""
if aktiv == "ja" then
-- Aktiv visning: brug aktuelle stemmer
return nowPct ~= "" and nowPct or prevPct
else
-- Passiv visning: brug også aktuelle stemmer
return nowPct ~= "" and nowPct or prevPct
end
end
local function getPartyField(party, field)
if partyMod and partyMod._fetch then
local ok = partyMod._fetch({party, field})
if ok and ok ~= "" then return ok end
end
if partyMod and partyMod.fetch then
local fake = { args = {party, field} }
local ok = partyMod.fetch(fake)
if ok and ok ~= "" then return ok end
end
return nil
end
local function deltaMandater(prev, now)
prev = trim(prev)
now = trim(now)
-- Hvis det er Ny parti
if normFlag(prev) == "ny" then
return "''Ny''"
end
-- Kræver tal
local p = tonumber(prev)
local n = tonumber(now)
if not p or not n then
return ""
end
local diff = n - p
if diff > 0 then
return string.format('<span style="color:green;">+%d</span>', diff)
elseif diff < 0 then
return string.format('<span style="color:red;">%d</span>', diff)
else
return "0"
end
end
--------------------------------------------------------------------
-- BYG EN RÆKKE
--------------------------------------------------------------------
local function buildRow(args, i, aktiv, prevYear)
local parti = trim(args["parti"..i])
if parti == "" then
return nil
end
local leder = trim(args["partileder"..i] or args["leder"..i])
local prevPct = trim(args["stemmer_forrige_valg"..i]):gsub("([%d])%%","%1 %%")
local nowPct = trim(args["stemmer"..i]):gsub("([%d])%%","%1 %%")
local seatsPrev = trim(args["mandater_forrige_valg"..i])
local seatsNow = trim(args["mandater_aktuelt"..i])
local seatsFinal = trim(args["mandater"..i])
-- Party colors/links
local kortnavn = getPartyField(parti, "shortname") or parti
local farve = getPartyField(parti, "color") or "#F8F9FA"
local link = string.format("[[%s|%s]]", parti, kortnavn)
---------------------------------------------------------------
-- SPECIALCASE: NYT PARTI
---------------------------------------------------------------
local isNew = (normFlag(prevPct) == "ny")
and (trim(seatsPrev) == "" or normFlag(seatsPrev) == "ny")
-- Nyt parti (aktiv = JA): behold den gamle udskrivning
if isNew and aktiv == "ja" then
local visPct = "''Ny''"
local kolAktiv = seatsNow ~= "" and seatsNow or ""
return string.format([[|-
| style="width:20px; background:%s !important; padding:0;" |
| style="padding-left:6px; text-align:left;" | %s
| style="width:180px; white-space:nowrap; text-align:left;" | %s
| style="text-align:center;" colspan="2" | %s
| style="width:60px; text-align:center;" | %s
]], farve, link, leder, visPct, kolAktiv)
end
-- Nyt parti (aktiv = NEJ): NY i +/- og normale værdier i de to 'Valgt'-kolonner
if isNew and aktiv ~= "ja" then
local visPct = nowPct -- procent er nu
local mandater = seatsFinal or "" -- mandater nu
local delta = "''Ny''" -- Ny kun i delta
return string.format([[|-
| style="width:20px; background:%s !important; padding:0;" |
| style="padding-left:6px; text-align:left;" | %s
| style="width:180px; white-space:nowrap; text-align:left;" | %s
| style="width:60px; text-align:right;" | %s
| style="width:60px; text-align:right;" | %s
| style="width:60px; text-align:right;" | %s
]], farve, link, leder, visPct, mandater, delta)
end
---------------------------------------------------------------
-- FÆLLES BEREGNINGER (ALT deklareres her → ingen fejl)
---------------------------------------------------------------
local visPct = percentValue(aktiv, prevPct, nowPct)
local kolPrev = seatsPrev ~= "" and seatsPrev or seatsNow or ""
local kolAktiv
if aktiv == "ja" then
kolAktiv = seatsNow ~= "" and seatsNow or kolPrev
else
kolAktiv = seatsFinal ~= "" and seatsFinal or seatsNow or kolPrev
end
local delta = ""
if aktiv ~= "ja" then
delta = deltaMandater(seatsPrev, seatsFinal)
end
---------------------------------------------------------------
-- UDSKRIVNING
---------------------------------------------------------------
if aktiv == "ja" then
-- Normal visning
return string.format([[|-
| style="width:20px; background:%s !important; padding:0;" |
| style="padding-left:6px; text-align:left;" | %s
| style="width:180px; white-space:nowrap; text-align:left;" | %s
| style="width:40px; text-align:center;" | %s
| style="width:60px; text-align:center;" | %s
| style="width:60px; text-align:center;" | %s
]], farve, link, leder, visPct, kolPrev, kolAktiv)
else
-- Passiv visning (med +/-)
return string.format([[|-
| style="width:20px; background:%s !important; padding:0;" |
| style="padding-left:6px; text-align:left;" | %s
| style="width:180px; white-space:nowrap; text-align:left;" | %s
| style="width:60px; text-align:right;" | %s
| style="width:60px; text-align:right;" | %s
| style="width:60px; text-align:right;" | %s
]], farve, link, leder, visPct, kolAktiv, delta)
end
end
--------------------------------------------------------------------
-- RENDER TABLE
--------------------------------------------------------------------
function p.renderTable(frame)
local args = frame.args
if not args or next(args) == nil then
args = frame:getParent().args or {}
end
local aktiv = (args["aktiv"] or ""):lower()
local prevYear = extractYear(args["forrige_valg"] or "")
if prevYear == "" then prevYear = "Forrige" end
local colAktiv = (aktiv == "ja") and "Nuv." or "Valgt"
local out = {}
local header
if aktiv == "ja" then
-- Normal header
header = string.format([[
{| class="wikitable valgtabel"
!
! style="text-align:center;" | Parti
! style="width:180px;" | Leder
! style="width:60px; text-align:center;" colspan="2" | %s
! style="width:60px; text-align:center;" | %s
]], prevYear, colAktiv)
else
-- Alternativ header når aktiv = nej
header = [[
{| class="wikitable valgtabel"
!
! style="text-align:center;" | Parti
! style="width:180px;" | Leder
! style="width:60px; text-align:center;" colspan="2" | Valgt
! style="width:60px; text-align:center;" | +/-
]]
end
out[#out+1] = header
for i = 1, 50 do
local overskrift = args["overskrift_" .. i]
if overskrift and trim(overskrift) ~= "" then
out[#out+1] = string.format([[
|-
! colspan="6" style="text-align:center; background:#e6e6e6;" | %s
]], overskrift)
end
local row = buildRow(args, i, aktiv, prevYear)
if row then
out[#out+1] = row
end
end
-- LUK PARTITABELLEN
out[#out+1] = "|}"
out[#out+1] = "<hr style=\"margin:20px 0;\" />"
-- STATSMINISTER-SEKTION (INGEN TABEL)
local titel = trim(args["titel"])
local tidl_valg = trim(args["tidl_valg"])
local tidl_parti = trim(args["tidl_parti"])
local tidl_billede = trim(args["tidl_billede"])
local efter_titel = trim(args["efter_titel"])
local efter_valg = trim(args["efter_valg"])
local efter_parti = trim(args["efter_parti"])
local efter_billede = trim(args["efter_billede"])
local has_new_pm = (efter_valg ~= "")
-- Lav billedbokse
local pmImage1 = ""
if tidl_billede ~= "" then
pmImage1 = string.format('<div style="text-align:left;">[[File:%s|70px]]</div>', tidl_billede)
end
local pmImage2 = ""
if efter_billede ~= "" then
pmImage2 = string.format('<div style="text-align:right;">[[File:%s|70px]]</div>', efter_billede)
end
if titel ~= "" then
-- KUN SIDDENDE STATSMINISTER (ingen ny)
if not has_new_pm then
out[#out+1] = string.format([===[
<div style="width:100%%;">
<div style="width:50%%;">
<div style="text-align:left;"><b>Siddende %s</b></div>
%s
<div style="text-align:left;">%s</div>
<div style="text-align:left;"><small>%s</small></div>
</div>
</div>
]===],
titel,
pmImage1, tidl_valg, tidl_parti
)
-- BEGGE KOLONNER
else
out[#out+1] = string.format([===[
<div style="display:flex; justify-content:space-between; width:100%%;">
<div style="width:45%%;">
<div style="text-align:left;"><b>Siddende %s</b></div>
%s
<div style="text-align:left;">%s</div>
<div style="text-align:left;"><small>%s</small></div>
</div>
<div style="width:45%%;">
<div style="text-align:right;"><b>Ny %s</b></div>
%s
<div style="text-align:right;">%s</div>
<div style="text-align:right;"><small>%s</small></div>
</div>
</div>
]===],
titel,
pmImage1, tidl_valg, tidl_parti,
efter_titel,
pmImage2, efter_valg, efter_parti
)
end
end
return table.concat(out, "\n")
end -- slut på renderTable
return p