Module:Sandbox/ChaoticShadow/InfoboxBuilder: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 20: Line 20:
bg = 'transparent'
bg = 'transparent'
},
},
params = {},
params = {
paramnames = {},
{ name = 'bg color' },
{ name = 'text color' }
},
paramnames = { 'bg color', 'text color' },
args = {},
args = {},
final_args = {},
final_args = {},
Line 131: Line 134:
end
end
local param_content = self:getContent(param)
local raw_param_value = self.args[param]
local colors = color_table[param_content]
local colors = color_table[raw_param_value]
if colors == nil then
if colors == nil then
Line 145: Line 148:


--- Sets the infobox params
--- Sets the infobox params
-- @param ... {{ name, func, default }, ...}
-- @param ... {{ name, func, default, should_hide }, ...}
-- name string
-- name string
-- The name of the parameter, not nil, cannot be duplicate
-- The name of the parameter, not nil, cannot be duplicate
Line 163: Line 166:
end
end
if self.paramnames[v.name] then
if self.paramnames[v.name] then
error("name cannot be duplicaate")
error("name cannot be duplicate")
end
end
Line 184: Line 187:
function InfoboxBuilder:setArgs(args)
function InfoboxBuilder:setArgs(args)
for k,v in pairs(args) do
for k,v in pairs(args) do
if v ~= '' then
self.args[k] = v
self.args[k] = v
end
end
if self.args['bg color'] then
self:setHeaderBackgroundColor(self.args['bg color'])
end
if self.args['text color'] then
self:setHeaderTextColor(self.args['text color'])
end
end
Line 200: Line 212:
end
end
if self.final_args[param] then
local content = '?'
local argparams = self.params[param]
return self.final_args[param]
end
local content = nil
local current_param = self.params[param]
if argparams == nil then
if current_param == nil then
error(string.format("No such param: %s", param))
error(string.format("No such param: %s", param))
end
end
local arg = self.args[param] or self.params[param].default
local raw_param_value = self.args[param] or current_param.default
if arg == nil then
if raw_param_value == nil then
return arg
return raw_param_value
end
end
if argparams['type'] == 'function' then
if current_param['type'] == 'function' then
content = self.params[param].func(arg)
content = current_param.func(raw_param_value)
elseif argparams['type'] == 'table' then
elseif current_param['type'] == 'table' then
content = self.params[param].func[arg]
content = current_param.func[raw_param_value]
else
else
content = arg
content = raw_param_value
end
end
Line 301: Line 317:
local t = {}
local t = {}
for _,v in ipairs(actual_args) do
for _,v in ipairs(actual_args) do
table.insert(t, v.title .. "=" .. v.content)
table.insert(t, v.title .. '=' .. v.content)
end
end
content = mw.getCurrentFrame():callParserFunction({
content = mw.getCurrentFrame():callParserFunction({
name = '#tag',
name = '#tag',
args = { 'tabber', table.concat(t, "|-|") }
args = { 'tabber', table.concat(t, '|-|') }
})
})
end
end
Line 331: Line 347:
-- @return self
-- @return self
-- The current object
-- The current object
function InfoboxBuilder:addRow(...)
function InfoboxBuilder:addRow(should_hide, ...)
local _row = self.infobox:tag('tr')
local argt = ...
local argt = ...
local actual_values = {}
for i, v in ipairs(argt) do
for i,v in ipairs(argt) do
if v.tag == 'argth' or v.tag == 'argtd' then
table.insert(actual_values, self:getContent(v.content))
end
end
-- if should_hide and #actual_values > 0 then
-- return self
-- end
local _row = self.infobox:tag('tr')
for i,v in ipairs(argt) do
local _cell = _row:tag(tagmap[v.tag] or 'td')
local _cell = _row:tag(tagmap[v.tag] or 'td')
:attr('colspan', 30 / #argt)
:attr('colspan', 30 / #argt)