FANDOM


-- <nowiki>
------------------------------------------------------------------------------
-- Infobox module
--
-- Documented at [[Module talk:Sandbox/Cqm/I2]]
------------------------------------------------------------------------------
 
local p = {}
p.__index = p
 
local math = math
 
------------------------------------------------------------------------------
-- Dependencies
------------------------------------------------------------------------------
 
------------------------------------------------------------------------------
-- Variables used by multiple methods
------------------------------------------------------------------------------
 
------------------------------------------------------------------------------
-- Mappings
------------------------------------------------------------------------------
local tagmap = {
    th = 'th',
    td = 'td',
    argh = 'th',
    argd = 'td'
}
 
------------------------------------------------------------------------------
-- Private methods
------------------------------------------------------------------------------
 
 
------------------------------------------------------------------------------
-- Public methods
------------------------------------------------------------------------------
 
--
-- Create a new Infobox instance
--
function p.new(args)
    local wrapper = mw.html.create('div')
        :addClass('infobox-wrapper')
    local buttons = wrapper:tag('div')
        :addClass('infobox-buttons')
    local infobox = wrapper:tag('table')
        :addClass('wikitable infobox plainlinks')
    local columns = tonumber(args.columns) or 2
 
    local self = setmetatable({
        wrapper = wrapper,
        buttons = buttons,
        infobox = infobox,
        columns = columns
    }, p)
 
    return self
end
 
--
-- Add a row to the infobox
--
function p.addRow(self, args)
    local row = self.infobox:tag('tr')
    local colspan = nil
    local cell
 
    if args.rowClass then
        row:addClass(args.rowClass)
    end
 
    for i, v in ipairs(args) do
        cell = row:tag(tagmap[v.tag] or 'td')
 
        if v.attr then
            cell:attr(v.attr)
        end
 
        if v.css then
            cell:css(v.css)
        end
 
        -- specific attributes
        if v.colspan then
            cell:attr('colspan', v.colspan)
        else
            if not v.noCalcColspan then
                -- calculate it dynamically
                if colspan == nil then
                    colspan = math.floor(self.columns / columns)
                end
 
                cell:attr('colspan', colspan)
            end
        end
 
        if v.rowspan then
            cell:attr('rowspan', v.rowspan)
        end
 
        if v.title then
            cell:attr('title', v.title)
        end
 
        if v.class then
            if type(v.class) == 'table' then
                v.class = table.concat(v.class, ' ')
            end
 
            cell:addClass(v.class)
        end
    end
 
    return self
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.