Module:Sandbox/ChaoticShadow/InfoboxBuilder: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 13: | Line 13: | ||
local obj = setmetatable({ |
local obj = setmetatable({ |
||
name = '', |
name = '', |
||
palette = { |
|||
text = '', |
|||
bg = '' |
|||
}, |
|||
params = {}, |
params = {}, |
||
paramnames = {}, |
paramnames = {}, |
||
Line 32: | Line 36: | ||
assert(arg ~= nil) |
assert(arg ~= nil) |
||
self.infobox:css('width', arg) |
self.infobox:css('width', arg) |
||
return self |
|||
end |
|||
function InfoboxBuilder:setTextColor(arg) |
|||
assert(arg ~= nil) |
|||
self.palette.text = arg |
|||
return self |
|||
end |
|||
function InfoboxBuilder:setBackgroundColor(arg) |
|||
assert(arg ~= nil) |
|||
self.palette.bg = arg |
|||
return self |
return self |
||
Line 63: | Line 81: | ||
function InfoboxBuilder:addHeader(arg) |
function InfoboxBuilder:addHeader(arg) |
||
local _cell = self.infobox:tag('tr'):tag('th') |
local _cell = self.infobox:tag('tr'):tag('th') |
||
_cell:css({ |
|||
['text-align'] = 'center', |
|||
['background-color'] = self.palette.bg, |
|||
['color'] = self.palette.text |
|||
}) |
|||
if arg.attr then |
if arg.attr then |
||
Line 123: | Line 146: | ||
return self |
return self |
||
end |
|||
function InfoboxBuilder:setupCols() |
|||
local spacer = self.infobox:tag('tr') |
|||
for i=1,30,1 do |
|||
spacer:tag('td') |
|||
:attr('colspan', 1) |
|||
:css('width', 'calc(100% / 30)') |
|||
end |
|||
end |
end |
||
Revision as of 21:33, 20 June 2021
Documentation for this module may be created at Module:Sandbox/ChaoticShadow/InfoboxBuilder/doc
local InfoboxBuilder = {}
InfoboxBuilder.__index = InfoboxBuilder
InfoboxBuilder.__tostring = InfoboxBuilder.tostring
local tagmap = {
th = 'th',
td = 'td',
argth = 'th',
argtd = 'td'
}
function InfoboxBuilder.new()
local obj = setmetatable({
name = '',
palette = {
text = '',
bg = ''
},
params = {},
paramnames = {},
args = {},
infobox = mw.html.create('table'):addClass('infobox')
}, InfoboxBuilder)
return obj
end
function InfoboxBuilder:setName(arg)
assert(arg ~= nil)
self.name = arg
return self
end
function InfoboxBuilder:setWidth(arg)
assert(arg ~= nil)
self.infobox:css('width', arg)
return self
end
function InfoboxBuilder:setTextColor(arg)
assert(arg ~= nil)
self.palette.text = arg
return self
end
function InfoboxBuilder:setBackgroundColor(arg)
assert(arg ~= nil)
self.palette.bg = arg
return self
end
function InfoboxBuilder:setParams(...)
for i, v in ipairs(...) do
if v.name 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)
end
end
end
return self
end
function InfoboxBuilder:setArgs(args)
for k,v in pairs(args) do
self.args[k] = v
end
return self
end
function InfoboxBuilder:addHeader(arg)
local _cell = self.infobox:tag('tr'):tag('th')
_cell:css({
['text-align'] = 'center',
['background-color'] = self.palette.bg,
['color'] = self.palette.text
})
if arg.attr then
_cell:attr(arg.attr)
end
if arg.colspan then
_cell:attr('colspan', arg.colspan)
end
if arg.rowspan then
_cell:attr('rowspan', arg.rowspan)
end
if arg.css then
_cell:css(arg.css)
end
_cell:wikitext(arg.content)
return self
end
function InfoboxBuilder:addRow(...)
local _row = self.infobox:tag('tr')
for i, v in ipairs(...) do
mw.log(v.tag)
mw.log(tagmap[v.tag])
local _cell = _row:tag(tagmap[v.tag] or 'td')
if v.attr then
_cell:attr(v.attr)
end
if v.colspan then
_cell:attr('colspan', v.colspan)
end
if v.rowspan then
_cell:attr('rowspan', v.rowspan)
end
if v.css then
_cell:css(v.css)
end
if v.tag == 'th' or v.tag == 'td' then
_cell:wikitext(v.content)
elseif v.tag == 'argth' or v.tag == 'argtd' then
local content = '?'
local argparams = self.params[v.content]
local arg = self.args[v.content] or self.params[v.content].default
if argparams['type'] == 'function' then
content = self.params[v.content].func(arg)
elseif argparams['type'] == 'table' then
content = self.params[v.content][arg]
elseif argparams['type'] == 'string' then
content = arg
end
_cell:wikitext(content)
end
end
return self
end
function InfoboxBuilder:setupCols()
local spacer = self.infobox:tag('tr')
for i=1,30,1 do
spacer:tag('td')
:attr('colspan', 1)
:css('width', 'calc(100% / 30)')
end
end
function InfoboxBuilder:tostring()
return tostring(self.infobox)
end
return InfoboxBuilder