Module:Class555Infobox
From Tyne and Wear Metro wiki
Documentation for this module may be created at Module:Class555Infobox/doc
local p = {}
function p.main(frame)
local args = frame:getParent().args
local tableElement = mw.html.create( 'table' ):addClass( 'infobox' )
local captionElement = tableElement:tag( 'caption' )
local unitIdentifier = mw.text.trim(args['Unit number'] or mw.title.getCurrentTitle().text)
captionElement:tag( 'span' ):addClass( 'unit-identifier' )
:wikitext( '[[Has unit identifier::' .. unitIdentifier .. ']]' )
if args['Official nickname'] then
captionElement:tag( 'span' ):addClass( 'nickname' )
:wikitext( '[[Has official nickname::' .. args['Official nickname'] .. ']]' )
end
if args['Photo'] then
tableElement:tag( 'tr' )
:tag( 'td' ):attr( 'colspan', '2' )
:wikitext( '[[Has primary image::File:' .. args['Photo'] .. '|300px]]' )
end
if args['Status'] then
local STATUS_COLORS = {
unbuilt = "#d99",
built = "#ffd",
delivered = "#9dd",
["kilometre accumulation"] = "#fdd",
["passenger service"] = "#dfd",
}
local statusKey = args['Status']:lower()
local row = tableElement:tag( 'tr' )
if STATUS_COLORS[statusKey] then
row:css( 'background-color', STATUS_COLORS[statusKey] )
end
row:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'Status' )
row:tag( 'td' ):wikitext( '[[Has unit status::' .. statusKey .. '|' .. args['Status'] .. ']]' )
end
local dates = {}
if args['Date ex-works'] then
table.insert(dates, 'Ex-works<sup title="Left the production line">[?]</sup> [[Has date ex-works::' .. args['Date ex-works'] .. ']]')
end
if args['Date delivered'] then
table.insert(dates, 'Delivered to Metro [[Has date delivered to Metro::' .. args['Date delivered'] .. ']]')
end
if args['Date of first daylight test'] then
table.insert(dates, 'First daylight test [[Has date of first daylight test::' .. args['Date of first daylight test'] .. ']]')
end
if args['Date of first passenger service'] then
table.insert(dates, 'First passenger service [[Has date of first known passenger service::' .. args['Date of first passenger service'] .. ']]')
end
if #dates == 1 then
tableElement:tag( 'tr' )
:tag( 'td' ):addClass( 'key-dates' )
:attr( 'colspan', '2' )
:wikitext( dates[1] )
elseif #dates > 1 then
local row = tableElement:tag( 'tr' )
row:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'Key dates' )
local list = row:tag( 'td' )
:tag( 'ul' ):addClass( 'key-dates' )
for _, date in ipairs(dates) do
list:tag('li'):wikitext(date)
end
end
if args['Delivery convoys'] then
local row = tableElement:tag( 'tr' )
row:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'Delivery convoys' )
local td = row:tag( 'td' )
local ol = td:tag( 'ol' )
local allDeliveryUnits = {}
for convoy in mw.text.gsplit(args['Delivery convoys'], ',') do
local unitsInConvoy = {}
for unit in mw.text.gsplit(convoy, '%s*%+%s*') do
if unit ~= '' then
table.insert(unitsInConvoy, unit)
allDeliveryUnits[unit] = true
end
end
if #unitsInConvoy > 0 then
local li = ol:tag( 'li' )
li:wikitext( "[[Has delivery convoy::" .. table.concat(unitsInConvoy, "+") .. "| ]]" )
for i, unit in ipairs(unitsInConvoy) do
if i > 1 then
li:wikitext( '+' )
end
if mw.title.new( unit ).exists then
li:wikitext( '[[' .. unit .. ']]' )
else
li:wikitext( unit )
end
end
end
end
allDeliveryUnits[unitIdentifier] = nil
for unit, _ in pairs(allDeliveryUnits) do
td:wikitext("[[Has delivery convoy unit::" .. unit .. "| ]]")
end
end
if args['Artwork A'] or args['Artwork B'] then
local row = tableElement:tag( 'tr' )
row:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'Artworks' )
local ol = row:tag( 'td' )
:tag( 'ol' )
if args['Artwork A'] then
ol:tag( 'li' ):wikitext( '[[Has 555 artwork A::' .. args['Artwork A'] .. ']]' )
end
if args['Artwork B'] then
ol:tag( 'li' ):wikitext( '[[Has 555 artwork B::' .. args['Artwork B'] .. ']]' )
end
end
return tableElement
end
return p