Module:VehicleStates
From Tyne and Wear Metro wiki
Documentation for this module may be created at Module:VehicleStates/doc
local p = {}
local STATUS_CONFIG = {
['unbuilt'] = { name = 'Unbuilt', color = '#E0E0E0', category = 'Unbuilt vehicles' },
['built'] = { name = 'Built', color = '#AED6F1', category = 'Built vehicles' },
['delivered'] = { name = 'Delivered', color = '#5DADE2', category = 'Delivered vehicles' },
['local testing'] = { name = 'Local Testing', color = '#F4D03F', category = 'Vehicles in local testing' },
['active'] = { name = 'Active', color = '#27AE60', category = 'Active vehicles' },
['passenger service'] = { name = 'Passenger Service', color = '#27AE60', category = 'Vehicles in passenger service' },
['inactive'] = { name = 'Inactive', color = '#E67E22', category = 'Inactive vehicles' },
['permanently withdrawn'] = { name = 'Permanently Withdrawn', color = '#C0392B', category = 'Permanently withdrawn vehicles' },
['disposed'] = { name = 'Disposed', color = '#7F8C8D', category = 'Disposed vehicles' },
['scrapped'] = { name = 'Scrapped', color = '#2C3E50', category = 'Scrapped vehicles' },
['preserved'] = { name = 'Preserved', color = '#8E44AD', category = 'Preserved vehicles' }
}
local function normalizeKey(key)
if not key then return "" end
return string.lower(mw.text.trim(key))
end
function p._applySmwAndCategory(statusKey)
statusKey = normalizeKey(statusKey)
if statusKey == "" then return "" end
local output = ''
local smwResult = mw.smw.set( { ['Has vehicle status'] = statusKey } )
if smwResult ~= true then
output = output .. '<!-- SMW error: ' .. smwResult.error .. ' -->'
end
if STATUS_CONFIG[statusKey] then
output = output .. '[[Category:' .. STATUS_CONFIG[statusKey].category .. ']]'
end
return output
end
function p.applySmwAndCategory(frame)
return p._applySmwAndCategory(frame.args[1])
end
function p._renderStatusRow(statusKey, overrides)
statusKey = normalizeKey(statusKey)
if statusKey == "" then return "" end
local name = (overrides and overrides[statusKey] and overrides[statusKey].name) or (STATUS_CONFIG[statusKey] and STATUS_CONFIG[statusKey].name)
local color = (overrides and overrides[statusKey] and overrides[statusKey].color) or (STATUS_CONFIG[statusKey] and STATUS_CONFIG[statusKey].color)
local row = mw.html.create( 'tr' )
if color then
row:css( 'background-color', color )
-- If the color is dark, set text color to white for better readability
if string.match(color, '^#(%x%x%x%x%x%x)$') then
local r = tonumber(string.sub(color, 2, 3), 16)
local g = tonumber(string.sub(color, 4, 5), 16)
local b = tonumber(string.sub(color, 6, 7), 16)
if (r*0.299 + g*0.587 + b*0.114) < 186 then
row:css( 'color', '#FFF' )
end
end
end
row:tag( 'th' ):attr( 'scope', 'row' ):wikitext( 'Status' )
row:tag( 'td' ):wikitext( name or statusKey ):wikitext( p._applySmwAndCategory(statusKey) )
return row
end
function p.renderStatusRow(frame)
return p._renderStatusRow(frame.args[1])
end
return p