Module:MetrocarInfobox

From Tyne and Wear Metro wiki

Documentation for this module may be created at Module:MetrocarInfobox/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' )
	captionElement:tag( 'span' ):addClass( 'unit-identifier' )
		:wikitext( '[[Has unit identifier::' .. ( ('Metrocar ' .. args['Unit number']) or mw.title.getCurrentTitle().text ) .. ']]' )
	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 = {
			active = "#dfd",
			['permanently withdrawn'] = "#fdd",
			scrapped = "#d99",
			preserved = "#ddf"
		}
		local statusKey = args['Status']:lower()
		local row = tableElement:tag( 'tr' )
		if STATUS_COLORS[statusKey] then
			row:css( 'background-color', STATUS_COLORS[statusKey] )
		end
		if statusKey == "active" then
			statusKey = "passenger service"
		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 accepted for service'] then
        table.insert(dates, 'Accepted for service [[Has date accepted for service::' .. args['Date accepted for service'] .. ']]')
    end
	if args['Date starting Wabtec refurb'] then
        table.insert(dates, 'Pre-Wabtec<sup title="Left Gosforth for Wabtect refurbishment">[?]</sup> [[Has date starting Wabtec refurb::' .. args['Date starting Wabtec refurb'] .. ']]')
    end
	if args['Date ending Wabtec refurb'] then
        table.insert(dates, 'Post-Wabtec<sup title="Arrived back at Gosforth after Wabtect refurbishment">[?]</sup> [[Has date ending Wabtec refurb::' .. args['Date ending Wabtec refurb'] .. ']]')
    end
	if args['Date of last known passenger service'] then
        table.insert(dates, 'Last known <abbr title="passenger service">pax</abbr> [[Has date of last known passenger service::' .. args['Date of last known passenger service'] .. ']]')
    end
	if args['Date officially withdrawn'] then
        table.insert(dates, 'Officially withdrawn [[Has date officially withdrawn::' .. args['Date officially withdrawn'] .. ']]')
    end
	if args['Date disposed'] then
        table.insert(dates, 'Disposed [[Has date disposed::' .. args['Date disposed'] .. ']]')
    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['Grim reaper units'] then
		local row = tableElement:tag( 'tr' )
		row:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'Grim reapers' )
		local td = row:tag( 'td' )
	    for i, unit in ipairs(mw.text.split(args['Grim reaper units'], '%+')) do
	    	if i > 1 then
	    		td:wikitext( '+' )
    		end
    		unit = mw.text.trim(unit)
    		if mw.title.new( unit ).exists then
	        	td:wikitext( '[[Has grim reaper unit::' .. unit .. '| ]][[' .. unit .. ']]' )
        	else
	        	td:wikitext( '[[Has grim reaper unit::' .. unit .. ']]' )
    		end
	    end
	end
	
	if args['Held liveries'] then
		local row = tableElement:tag( 'tr' )
		row:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'Held liveries' )
		local list = row:tag( 'td' )
			:tag( 'ol' ):addClass( 'held-liveries ')
	    for livery in mw.text.gsplit(args['Held liveries'], ',') do
	    	livery = mw.text.trim(livery)
	    	list:tag( 'li' )
	    		:wikitext( '[[Has held livery::' .. livery .. ']]' )
	    end
	end
	
	if args['Held saloon moquettes'] then
		local row = tableElement:tag( 'tr' )
		row:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'Held saloon moquettes' )
		local list = row:tag( 'td' )
			:tag( 'ol' ):addClass( 'held-liveries ')
	    for moquette in mw.text.gsplit(args['Held saloon moquettes'], ',') do
	    	moquette = mw.text.trim(livery)
	    	list:tag( 'li' )
	    		:wikitext( '[[Has held saloon moquette::' .. moquette .. ']]' )
	    end
	end
	
	if args['Cab A moquette'] or args['Cab B moquette'] then
		local row = tableElement:tag( 'tr' )
		local th = row:tag( 'th' ):attr( 'scope', 'row' )
		local td = row:tag( 'td' )
		if args['Cab A moquette'] and args['Cab B moquette'] then
			th:wikitext( 'Cab moquettes' )
			local cabMoquettesTable = td:tag( 'table' ):addClass( 'cab-moquettes' )
			local cabARow cabMoquettesTable:tag( 'tr' )
			cabARow:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'A' )
			cabARow:tag( 'td' ):wikitext( '[[Has cab A moquette::' .. args['Cab A moquette'] .. ']]' )
			local cabBRow cabMoquettesTable:tag( 'tr' )
			cabBRow:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'B' )
			cabBRow:tag( 'td' ):wikitext( '[[Has cab B moquette::' .. args['Cab B moquette'] .. ']]' )
		elseif args['Cab A moquette'] then
			th:wikitext( 'Cab A moquette' )
			td:wikitext( '[[Has cab A moquette::' .. args['Cab A moquette'] .. ']]' )
		else
			th:wikitext( 'Cab B moquette' )
			td:wikitext( '[[Has cab B moquette::' .. args['Cab B moquette'] .. ']]' )
		end
	end
	
    return tableElement
end

return p