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
-- 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
-- Name of the template, not nil or empty
-- @return self
-- @return self
-- the current object
-- 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"
-- 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
-- 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
-- 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
-- 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
-- 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
-- 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
-- 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
-- @params ... {{ name, func, default }, ...}
-- @param ... {{ name, func, default }, ...}
-- name string
-- name string
-- The name of the parameter, not nil cannot be duplicate
-- The name of the parameter, not nil, cannot be duplicate
-- func function, table or string
-- func function, table or string
-- A function that accepts the parameter as an argument and
-- A function that accepts the parameter as an argument and
-- returns a string, OR
-- returns a string, OR
-- A table that has the parameter as a key, OR
-- A table that has the parameter as a key, OR
-- An empty string
-- An empty string
-- default string or nil
-- default string or nil
-- The default value if no argument is given
-- The default value if no argument is given
-- @return self
-- @return self
-- the current object
-- 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 ~= "" then
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")
error("name must not be nil or empty")
end
end
if self.paramnames[v.name] then
error("name cannot be duplicaate")
end
if type(v.func) ~= 'function' and
type(v.func) ~= 'table' 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
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
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(param)
if param == nil or param == "" then
error("Param must not be nil or empty")
end
local content = '?'
local content = '?'
local argparams = self.params[v.content]
local argparams = self.params[param]
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[param] or self.params[param].default or ''
if argparams['type'] == 'function' then
if argparams['type'] == 'function' then
content = self.params[v.content].func(arg)
content = self.params[param].func(arg)
elseif argparams['type'] == 'table' then
elseif argparams['type'] == 'table' then
content = self.params[v.content].func[arg]
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
--[[
{ content, title }
-- @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
content = self:getContent(argt[1]) -- tables start at 1
local v = argt[1] -- tables start at 1
content = v.content
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
table.insert(t, i, v.title .. "=" .. self:getContent(v))
if v.tag == 'argtd' then
c = self:getContent(c)
end
table.insert(t, i, v.title .. "=" .. c)
end
end
content = mw.getCurrentFrame()
:callParserFunction({
content = mw.getCurrentFrame():callParserFunction({
name = '#tag',
name = '#tag',
args = {
args = { 'tabber', table.concat(t, "|-|") }
'tabber',
})
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')