Module:Sandbox/ChaoticShadow/InfoboxBuilder: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 12:
--- Create the infobox
-- @return obj metatable
-- aA metatable describing the infobox
function InfoboxBuilder.new()
local obj = setmetatable({
Line 31:
--- Set the infobox name, for use with bottom links
-- @param arg string
-- nameName of the template, not nil or empty
-- @return self
-- theThe current object
function InfoboxBuilder:setName(arg)
if arg == nil or arg == '' then
Line 46:
--- Set the width of the infobox
-- @param arg string
-- widthWidth of the infobox, should be a valid value for the CSS "width"
-- property, not nil or empty
-- @return self
-- theThe current object
function InfoboxBuilder:setWidth(arg)
if arg == nil or arg == '' then
Line 62:
--- Set the text color of the header
-- @param arg string
-- textText color of the header, should be a valid value for the CSS
-- "color" property, not nil or empty
-- @return self
-- theThe current object
function InfoboxBuilder:setHeaderTextColor(arg)
if arg == nil or arg == '' then
Line 78:
--- Set the background color of the header
-- @param arg string
-- backgroundBackground color of the header, should be a valid value for the
-- CSS "background-color" property, not nil or empty
-- @return self
-- theThe current object
function InfoboxBuilder:setHeaderBackgroundColor(arg)
if arg == nil or arg == '' then
Line 99:
-- Same as setHeaderBackgroundColor
-- @return self
-- theThe current object
function InfoboxBuilder:setHeaderColors(arg)
if arg == nil then
Line 112:
 
--- Sets the infobox params
-- @paramsparam ... {{ 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
-- theThe current object
function InfoboxBuilder:setParams(...)
for i, v in ipairs(...) do
if v.name == nil and v.name ~== "" then
if self.paramnames[v.name] == nil then
if type(v.func) == 'function' or
type(v.func) == 'table' or
type(v.func) == 'string'
then
self.params[v.name] = {
['type'] = type(v.func),
func = v.func,
default = v.default
}
table.insert(self.paramnames, v.name)
else
error("func must be of type \"function\", \"table\", or " ..
"\"string\"")
end
else
error("name cannot be duplicaate")
end
else
error("name must not be nil or empty")
end
if self.paramnames[v.name] == nil then
error("name cannot be duplicaate")
end
if type(v.func) =~= 'function' orand
type(v.func) =~= 'table' or and
type(v.func) =~= 'string'
then
error("func must be of type \"function\", \"table\", or \" ..string\"")
end
self.params[v.name] = {
['type'] = type(v.func),
func = v.func,
default = v.default
}
table.insert(self.paramnames, v.name)
end
Line 153 ⟶ 150:
end
 
--- Sets the infobox arguments
-- @param args Frame
-- A frame object, passed in when invoked
-- @return self
-- The current object
function InfoboxBuilder:setArgs(args)
for k,v in pairs(args) do
Line 161 ⟶ 163:
end
 
--- Gets the content associated with a parameter
function InfoboxBuilder:getContent(v)
-- @param param string
-- The param name, not nil or empty
-- @return content string
-- A string containing the content
function InfoboxBuilder:getContent(vparam)
if param == nil or param == "" then
error("Param must not be nil or empty")
end
local content = '?'
local argparams = self.params[v.contentparam]
local arg = self.args[v.content] or self.params[v.content].default or ''
if argparams == nil then
error(string.format("No such param: %s", param))
end
local arg = self.args[v.contentparam] or self.params[v.contentparam].default or ''
if argparams['type'] == 'function' then
content = self.params[v.contentparam].func(arg)
elseif argparams['type'] == 'table' then
content = self.params[v.contentparam].func[arg]
elseif argparams['type'] == 'string' then
content = arg
Line 177 ⟶ 193:
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)
local _cell = self.infobox:tag('tr'):tag('th')
Line 203 ⟶ 228:
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(...)
local argt = ...
Line 211 ⟶ 240:
local _cell = self.infobox:tag('tr'):tag('td')
local content = '?'
if #argt < 2 then
contentlocal v = self:getContent(argt[1]) -- tables start at 1
content = mwv.getCurrentFrame()content
if v.tag == 'argtd' then
content = self:getContent(content)
end
else
local t = {}
for i, v in ipairs(argt) do
local c = v.content
table.insert(t, i, v.title .. "=" .. self:getContent(v))
if v.tag == 'argtd' then
c = self:getContent(c)
elseend
}
table.insert(t, i, v.title .. "=" .. self:getContent(v)c)
end
content = mw.getCurrentFrame()
content = mw.getCurrentFrame():callParserFunction({
name = '#tag',
args = { 'tabber', table.concat(t, "|-|") }
'tabber',})
table.concat(t, "|-|")
}
})
end
Line 234 ⟶ 270:
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(...)
local _row = self.infobox:tag('tr')
439

edits