Module:Infobox election/sandbox
Appearance
![]() | This is the module sandbox page for Module:Infobox election (diff). |
![]() | This module depends on the following other modules: |
Implements {{Infobox election/row}} and {{Infobox election/shortname}} with a reduced post-expand include size
Usage
{{#invoke:Infobox election|row}}
{{#invoke:Infobox election|shortname}}
local p = {}
local args = {}
local origArgs = {}
local InfoboxImage = require('Module:InfoboxImage').InfoboxImage
local delink = require('Module:Delink')._delink
local fetch = require('Module:Political party')._fetch
local notblank = function (v) return (mw.text.trim(v or "") ~= "") end
local ucfirst = function (s) return mw.ustring.upper(mw.ustring.sub(s,1,1)) .. mw.ustring.gsub(mw.ustring.sub(s,2), '_', ' ') end
local function _row(frame, rargs)
local rtype = mw.ustring.lower(rargs.type or '')
local output = {'|-'}
setmetatable(output, {__call = function (t, v) t[#t+1] = v end }) -- Append to array by calling it
local function threeCells(cellType, title, always, shortname)
local centeredCell = function (key)
local name = shortname and p._shortname(rargs[key .. '_name'], rargs[key]) or (rargs[key] or '')
return ('| style="' .. (notblank(rargs[key]) and 'width:75px;' or '') .. 'text-align:center" | ' .. name )
end
if always or ( notblank(rargs[cellType .. '1']) or notblank(rargs[cellType .. '2']) or notblank(rargs[cellType .. '3']) ) then
output('|-')
output('! style="width:75px;text-align:left" |' .. (title or ucfirst(cellType or '')))
output(centeredCell(cellType .. '1'))
output(centeredCell(cellType .. '2'))
if always or notblank(rargs.col3) then
output(centeredCell(cellType .. '3'))
end
end
end
if notblank(rargs.divider) and (
notblank(rargs.party1) or notblank(rargs.color1) or notblank(rargs.leader1) or notblank(rargs.party2) or notblank(rargs.color2) or notblank(rargs.leader2) or notblank(rargs.party3) or notblank(rargs.color3) or notblank(rargs.leader3)
) then
output('| colspan=4 | ' .. rargs.divider)
end
output('|- class="sr-only"')
if rargs.ongoing ~= "yes" and (
rtype == "parliamentary" or rtype == "legislative"
) and (
notblank(rargs.party1) or notblank(rargs.party2) or notblank(rargs.party2)
) then
output('! style="text-align:left;" | ')
output('! style="text-align:center" | ' .. (notblank(rargs.party1) and rargs.header1 or ''))
output('! style="text-align:center" | ' .. (notblank(rargs.party2) and rargs.header2 or ''))
if notblank(rargs.col3) then
output('! style="text-align:center" | ' .. (notblank(rargs.party3) and rargs.header3 or ''))
end
end
-- Images --
output('|-')
if notblank(rargs.party1) or notblank(rargs.color1) or notblank(rargs.image1) or notblank(rargs.party2) or notblank(rargs.color2) or notblank(rargs.image2) or notblank(rargs.party3) or notblank(rargs.color3) or notblank(rargs.image3) then
if notblank(rargs.filler) then
output('! <br />' .. rargs.filler)
else
output('| ')
end
local image = {}
for i = 1, rargs['col3'] and 3 or 2, 1 do
image[i] = '| class="notheme" style="text-align:center;'
if not notblank(rargs['party' .. i]) and not notblank(rargs['color' .. i]) then
image[i] = image[i] .. 'width:0;'
end
if notblank(rargs['color' .. i]) then
image[i] = image[i] .. 'border-bottom: 6px solid #' .. mw.ustring.gsub(mw.ustring.gsub(rargs['color' .. i],'#',''),'#','')
elseif rargs.party_color ~= 'no' and notblank(rargs['party'] .. i) then
local color = mw.ustring.replace(rargs['party' .. i], '^(%[%[%s*[^%]%[]*%]%]).*$', '%1')
color = delink({[1] = color, wikilinks = 'target'})
color = fetch({[1] = color, [2] = 'color', error = 'NOTHERE'})
if color ~= 'NOTHERE' then
image[i] = image[i] .. 'border-bottom: 6px solid' .. color
end
end
image[i] = image[i] .. ';" | ' .. InfoboxImage( frame:newChild{
title = frame:getTitle(), args = {
image = rargs['image' .. i] or '',
size = notblank(rargs['image' .. i .. '_size']) and rargs['image' .. i .. '_size'] or (rargs.image_size or ''),
alt = rags['alt' .. i] or '',
upright = notblank(rargs['image' .. i .. '_upright']) and rargs['image' .. i .. '_upright'] or (
(notblank(rargs['col3']) and notblank(rargs['image3'])) and '0.4' or '0.6'
),
suppressplaceholder = 'yes',
class = notblank(rargs['pageimage' .. i] and '' or 'notpageimage noresize'),
}
} )
output(image[i])
end
end
-- Presidential & by-election nominee --
if notblank(rargs['nominee1']) or notblank(rargs['nominee2']) or notblank(rargs['nominee3']) then
if ({primary = 1, presidential = 1, ['by-election'] = 1})[rtype] == 1 then
threeCells('nominee', '', true) -- nominee, no alternative title, always show
end
else
threeCells('candidate') -- nominee
end
-- Party leader --
if rtype == 'parliamentary' or rtype == 'legislative' then
threeCells('leader')
end
-- Party name --
threeCells('party', (notblank(rargs.party_label) and rargs.party_label or 'Party'), false, true) -- party, alternative name, don't always show, use shortname
-- Alliance name --
threeCells('alliance', '', false, true) -- alliance, not alternative title, don't always show, use shortname
-- Primary or presidential
if rtype == 'primary' or rtype == 'presidential' then
-- Home state --
threeCells('home_state')
-- Running mate --
if rtype == 'presidential' then
threeCells('running_mate')
-- Electoral vote --
threeCells('electoral_vote', (rargs.college_votes == 'no' and 'Projected e' or 'E') .. 'lectoral vote')
end
-- Delegate count --
threeCells('delegate_count', (rargs.ongoings == 'yes' and 'Estimated d' or 'D') .. 'elegate count')
-- States carried --
threeCells('states_carried', rtype == 'primary' and 'Contests won' or 'States carried')
end
-- Parliamentary or legislative --
if rtype == 'parliamentary' or rtype == 'legislative' then
-- Leader since --
threeCells('leader_since')
-- Leader's seat --
threeCells('leaders_seat', "Leader's seat")
-- Last election --
threeCells('last_election')
-- Seats before --
threeCells('seats_before', rargs.ongoing == 'yes' and 'Current seats' or 'Seats before')
-- Ongoing --
if rargs.ongoing == 'yes' then
-- Seats needed --
threeCells('seats_needed')
-- Not ongoing --
else
-- Seats won --
threeCells('seats', 'Seats won')
-- Seats after --
threeCells('seats_after')
-- Seat change --
threeCells('seat_change')
end
end
-- Primary or not ongoing --
if rargs.ongoing ~= 'yes' or rtype == 'primary' then
-- Popular vote --
threeCells('popular_vote', (rargs.vote_type or 'Popular') .. ' vote')
-- Percentage --
threeCells('percentage')
-- Primary or presidential
if rtype == 'primary' or rtype == 'presidential' then
-- Counties won --
threeCells('counties_won')
-- Counties 25% threshold --
threeCells('counties_threshhold', 'Counties with 25% vote')
end
end
-- Swing --
if (rtype == 'parliamentary' or rtype == 'legislative') and rargs.ongoing ~= 'yes' then
threeCells('swing')
end
-- Blank fields --
for i = 1,6,1 do
if notblank(rargs[i .. 'blank']) then
threeCells(i ..'data', rargs[i .. 'blank'])
end
end
return table.concat(output, '\n')
end
function p._shortname(link, name)
if link ~= 'no' and notblank(name) and delink({[1] = name, wikilinks = 'target'}) == name then
-- the below array items are intentionally missing the training 's'
local names = {
independent = '[[Independent politician|' .. name .. ']]',
miscellaneous = '[[Independent politician|DIV]]',
other = 'Others',
['other parties and independent'] = 'Others & [[Independent politician|IND]]',
regionalist = '[[Regionalism (politics)|Regionalists]]',
tbc = '[[To be announced|TBC]]',
tbd = '[[To be announced|TBD]]',
vacant = '[[Casual vacancy|Vacant]]'
}
return names[mw.ustring.gsub(mw.ustring.lower(name), 's$', '')] or
('[[' .. name .. '|' .. fetch({[1] = name, [2] = 'shortname'}) .. ']]')
end
return name or ''
end
function p.row(frame)
local args = {}
for k,v in pairs(frame:getParent().args) do
if notblank(v) then args[k] = mw.text.trim(v) end
end
for k,v in pairs(frame.args) do
if notblank(v) then args[k] = mw.text.trim(v) end
end
return _row(frame, args)
end
function p.infobox(frame)
local origArgs = {}
for k,v in pairs(frame:getParent().args) do
if notblank(v) then origArgs[k] = mw.text.trim(v) end
end
for k,v in pairs(frame.args) do
if notblank(v) then origArgs[k] = mw.text.trim(v) end
end
local pagetitle = frame:getParent() and frame:getParent():getTitle() or frame:getTitle()
local etype = mw.ustring.lower(origArgs.type or '')
args = {
templatestyles = 'Infobox election/styles.css',
['child templatestyles'] = 'Screen reader-only/styles.css',
child = origArgs.child or origArgs.embed or '',
bodyclass = 'ib-election infobox-table' .. (notblank(origArgs.election_date) and 'vevent' or ''),
bodystyle = '',
titleclass = 'summary',
title = ((origArgs.child or origArgs.embed or '') == 'yes')
and (notblank(origArgs.election_name) and '<div style="background-color:#ddd; font-size:120%; font-weight:bold">'..origArgs.election_name..'</div>')
or (notblank(origArgs.election_name) and origArgs.election_name or pagetitle),
subheader1 = notblank(origArgs.flag_image)
and (InfoboxImage(
frame:newChild{ title = frame:getTitle(), args = {
image = origArgs.flag_image,
size = "50px",
alt = "",
link = ""
} }
) .. "<hr />")
or (require('Module:CountryData').gettable(mw.getCurrentFrame(),origArgs.country).alias and (require('Module:flag').deco(
frame:newChild{ title = frame:getTitle(), args = {
[1] = origArgs.country,
variant = origArgs.flag_year,
size = "50px"
} }
) .. "<hr />") ),
subheader2 = 'TBD',
subheader3 = 'TBD',
subheader4 = 'TBD',
header1 = (etype == 'primary' or etype == 'presidential') and (
notblank(origArgs.votes_for_election) and (
'<hr />' .. origArgs.votes_for_election .. (
notblank(origArgs.needed_votes) and '<br />' .. origArgs.needed_votes .. ' votes needed to win'
)
) or ''
) or (
(etype == 'parliamentary' or etype == 'legislative' or etype == 'by-election') and (
notblank(origArgs.seats_for_election) and (
'<hr />' .. origArgs.seats_for_election .. (
notblank(origArgs.majority_seats) and '<br />' .. origArgs.majority_seats .. ' seats needed for a majority'
)
) or ''
) or ''
),
data2 = notblank(origArgs.opinion_polls) and ('[[' .. origArgs.opinion_polls .. '|Opinion polls]]') or '',
label3 = 'Registered',
data3 = origArgs.registered,
label4 = 'Turnout',
data4 = origArgs.turnout,
label5 = 'Votes counted',
data5 = notblank(origArgs.votes_counted) and (
'<div class="center" style="width:auto;margin-left:auto;margin-right:auto;">' ..
_percentageBar(origArgs.votes_counted, origArgs.votes_counted .. '%', '1BCE0E') ..
"</div>as of '''" .. (origArgs.last_update or '') .. ' ' .. (origArgs.time_zone or '') .. "'''"
) or '',
label6 = 'Reporting',
data6 = notblank(origArgs.reporting) and (
'<div class="center" style="width:auto;margin-left:auto;margin-right:auto;">' ..
_percentageBar(origArgs.reporting, origArgs.reporting .. '%', '1BCE0E') ..
"</div>as of '''" .. (origArgs.last_update or '') .. ' ' .. (origArgs.time_zone or '') .. "'''"
) or '',
label7 = 'Declared',
data7 = notblank(origArgs.declared) and (
'<div class="center" style="width:auto;margin-left:auto;margin-right:auto;">' ..
_percentageBar(origArgs.declared, origArgs.declared .. '%', '1BCE0E') ..
"</div>as of '''" .. (origArgs.last_update or '') .. ' ' .. (origArgs.time_zone or '') .. "'''"
) or '',
}
local width = 0
local map = false
for _, v in ipairs({'', '2', '3', '4'}) do
if notblank(origArgs['map' .. v]) or notblank(origArgs['map' .. v .. '_image']) then
map = true
local nWidth = mw.ustring.gsub(origArgs['map' .. v .. '_size'] or '300', '%s*px%s*$', '')
width = math.max(width, tonumber(nWidth) or 300)
end
end
if map then
args.bodystyle = tostring(math.min(300, width)) .. "px"
end
return require('Module:Infobox').infoboxTemplate(
frame:newChild{ title = frame:getTitle(), args = args }
)
end
return p