Module:Sandbox/ChaoticShadow/InfoboxBuilder: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 12: | Line 12: | ||
--- Create the infobox |
--- Create the infobox |
||
-- @return obj metatable |
-- @return obj metatable |
||
-- |
-- A metatable describing the infobox |
||
function InfoboxBuilder.new() |
function InfoboxBuilder.new() |
||
local obj = setmetatable({ |
local obj = setmetatable({ |
||
Line 31: | Line 31: | ||
--- Set the infobox name, for use with bottom links |
--- Set the infobox name, for use with bottom links |
||
-- @param arg string |
-- @param arg string |
||
-- |
-- Name of the template, not nil or empty |
||
-- @return self |
-- @return self |
||
-- |
-- The current object |
||
function InfoboxBuilder:setName(arg) |
function InfoboxBuilder:setName(arg) |
||
if arg == nil or arg == '' then |
if arg == nil or arg == '' then |
||
Line 46: | Line 46: | ||
--- Set the width of the infobox |
--- Set the width of the infobox |
||
-- @param arg string |
-- @param arg string |
||
-- |
-- Width of the infobox, should be a valid value for the CSS "width" |
||
-- property, not nil or empty |
-- property, not nil or empty |
||
-- @return self |
-- @return self |
||
-- |
-- The current object |
||
function InfoboxBuilder:setWidth(arg) |
function InfoboxBuilder:setWidth(arg) |
||
if arg == nil or arg == '' then |
if arg == nil or arg == '' then |
||
Line 62: | Line 62: | ||
--- Set the text color of the header |
--- Set the text color of the header |
||
-- @param arg string |
-- @param arg string |
||
-- |
-- Text color of the header, should be a valid value for the CSS |
||
-- "color" property, not nil or empty |
-- "color" property, not nil or empty |
||
-- @return self |
-- @return self |
||
-- |
-- The current object |
||
function InfoboxBuilder:setHeaderTextColor(arg) |
function InfoboxBuilder:setHeaderTextColor(arg) |
||
if arg == nil or arg == '' then |
if arg == nil or arg == '' then |
||
Line 78: | Line 78: | ||
--- Set the background color of the header |
--- Set the background color of the header |
||
-- @param arg string |
-- @param arg string |
||
-- |
-- Background color of the header, should be a valid value for the |
||
-- CSS "background-color" property, not nil or empty |
-- CSS "background-color" property, not nil or empty |
||
-- @return self |
-- @return self |
||
-- |
-- The current object |
||
function InfoboxBuilder:setHeaderBackgroundColor(arg) |
function InfoboxBuilder:setHeaderBackgroundColor(arg) |
||
if arg == nil or arg == '' then |
if arg == nil or arg == '' then |
||
Line 99: | Line 99: | ||
-- Same as setHeaderBackgroundColor |
-- Same as setHeaderBackgroundColor |
||
-- @return self |
-- @return self |
||
-- |
-- The current object |
||
function InfoboxBuilder:setHeaderColors(arg) |
function InfoboxBuilder:setHeaderColors(arg) |
||
if arg == nil then |
if arg == nil then |
||
Line 112: | Line 112: | ||
--- Sets the infobox params |
--- Sets the infobox params |
||
-- @ |
-- @param ... {{ name, func, default }, ...} |
||
-- |
-- name string |
||
-- |
-- The name of the parameter, not nil, cannot be duplicate |
||
-- |
-- func function, table or string |
||
-- |
-- A function that accepts the parameter as an argument and |
||
-- |
-- returns a string, OR |
||
-- |
-- A table that has the parameter as a key, OR |
||
-- |
-- An empty string |
||
-- |
-- default string or nil |
||
-- |
-- The default value if no argument is given |
||
-- @return self |
-- @return self |
||
-- |
-- The current object |
||
function InfoboxBuilder:setParams(...) |
function InfoboxBuilder:setParams(...) |
||
for i, v in ipairs(...) do |
for i, v in ipairs(...) do |
||
if v.name and v.name |
if v.name == nil and v.name == "" then |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
⚫ | |||
"\"string\"") |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
error("name must not be nil or empty") |
error("name must not be nil or empty") |
||
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
Line 153: | Line 150: | ||
end |
end |
||
--- Sets the infobox arguments |
|||
-- @param args Frame |
|||
-- A frame object, passed in when invoked |
|||
-- @return self |
|||
-- The current object |
|||
function InfoboxBuilder:setArgs(args) |
function InfoboxBuilder:setArgs(args) |
||
for k,v in pairs(args) do |
for k,v in pairs(args) do |
||
Line 161: | Line 163: | ||
end |
end |
||
--- Gets the content associated with a parameter |
|||
⚫ | |||
-- @param param string |
|||
-- The param name, not nil or empty |
|||
-- @return content string |
|||
-- A string containing the content |
|||
⚫ | |||
if param == nil or param == "" then |
|||
error("Param must not be nil or empty") |
|||
end |
|||
local content = '?' |
local content = '?' |
||
local argparams = self.params[ |
local argparams = self.params[param] |
||
⚫ | |||
if argparams == nil then |
|||
error(string.format("No such param: %s", param)) |
|||
end |
|||
⚫ | |||
if argparams['type'] == 'function' then |
if argparams['type'] == 'function' then |
||
content = self.params[ |
content = self.params[param].func(arg) |
||
elseif argparams['type'] == 'table' then |
elseif argparams['type'] == 'table' then |
||
content = self.params[ |
content = self.params[param].func[arg] |
||
elseif argparams['type'] == 'string' then |
elseif argparams['type'] == 'string' then |
||
content = arg |
content = arg |
||
Line 177: | Line 193: | ||
end |
end |
||
--- Adds a header |
|||
-- @param arg { content, attr, colspan, rowspan, css } |
|||
-- content string or nil The wikitext to be rendered |
|||
-- attr {...} or nil The attributes of the cell in table form |
|||
-- colspan number or nil The colspan of the cell |
|||
-- roswpan number or nil The rowspan of the cell |
|||
-- css {...} or nil The css of the cell in table form |
|||
-- @return self |
|||
-- The current object |
|||
function InfoboxBuilder:addHeader(arg) |
function InfoboxBuilder:addHeader(arg) |
||
local _cell = self.infobox:tag('tr'):tag('th') |
local _cell = self.infobox:tag('tr'):tag('th') |
||
Line 203: | Line 228: | ||
end |
end |
||
--- Adds an image, or switchable images |
|||
--[[ |
|||
-- @param ... { { tag, content, title }, ... } |
|||
-- tag "artd" or "td" The |
|||
]]-- |
|||
-- content string The |
|||
-- title string or nil The |
|||
-- @return self |
|||
-- The current object |
|||
function InfoboxBuilder:addImage(...) |
function InfoboxBuilder:addImage(...) |
||
local argt = ... |
local argt = ... |
||
Line 211: | Line 240: | ||
local _cell = self.infobox:tag('tr'):tag('td') |
local _cell = self.infobox:tag('tr'):tag('td') |
||
local content = '?' |
local content = '?' |
||
if #argt < 2 then |
if #argt < 2 then |
||
local v = argt[1] -- tables start at 1 |
|||
⚫ | |||
if v.tag == 'argtd' then |
|||
content = self:getContent(content) |
|||
end |
|||
else |
else |
||
local t = {} |
local t = {} |
||
for i, v in ipairs(argt) do |
for i, v in ipairs(argt) do |
||
local c = v.content |
|||
⚫ | |||
if v.tag == 'argtd' then |
|||
c = self:getContent(c) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
⚫ | |||
content = mw.getCurrentFrame():callParserFunction({ |
|||
name = '#tag', |
|||
args = { 'tabber', table.concat(t, "|-|") } |
|||
}) |
|||
table.concat(t, "|-|") |
|||
⚫ | |||
}) |
|||
end |
end |
||
Line 234: | Line 270: | ||
end |
end |
||
--- Adds a row, with columns up to 30 columns spanned |
|||
-- @param ... { { tag, content, attr, colspan, rowspan, css }, ... } |
|||
-- @return self |
|||
-- The current object |
|||
function InfoboxBuilder:addRow(...) |
function InfoboxBuilder:addRow(...) |
||
local _row = self.infobox:tag('tr') |
local _row = self.infobox:tag('tr') |