Module:InfoboxProto

From Elwiki
Revision as of 22:48, 28 April 2022 by Ritsu (talk | contribs)

Documentation for this module may be created at Module:InfoboxProto/doc

require('Module:CommonFunctions')
local getArgs = require('Module:Arguments').getArgs
local p = {}

-- Main process
function p.main(frame, title)
    local args = getArgs(frame);

    if args.textcolor == nil then
        args.textcolor = 'white';
    else
        args.textcolor = 'black';
    end

    local infobox = mw.html.create('div'):addClass('infobox-new');
    infobox:tag('div'):addClass('infobox-new-header'):css('background-color', args.color:gsub("%#", "#")):css(
        'color', args.textcolor):wikitext(title);
    infobox:tag('div'):addClass('infobox-new-image'):wikitext(args.image);

    -- Adds a normal row
    function addField(param, field_name, double)
        if args[param] ~= nil then
            local row = infobox:tag('div'):addClass('infobox-row');
            if field_name == nil then
                field_name = titleCase(param);
            end
            row:tag('div'):addClass('infobox-row-title'):wikitext(field_name);
            if (param == 'video' or param == 'tree' or param == 'stats') then
                row:tag('div'):addClass('infobox-row-content'):wikitext(args[param]);
            else
                row:tag('div'):addClass('infobox-row-content'):tag('span'):wikitext(args[param]);
            end
        end
        if double == true then addField2(param, field_name) end;
    end

    -- Adds a row with 2 columns
    function addField2(param, field_name)
        if args[param .. '1'] ~= nil and args[param .. '2'] ~= nil then
            local row = infobox:tag('div'):addClass('infobox-row'):addClass('infobox-double-row');
            if field_name == nil then
                field_name = titleCase(param):gsub("%d+", '');
            end
            row:tag('div'):addClass('infobox-row-title'):wikitext(field_name);
            row:tag('div'):addClass('infobox-row-content'):tag('span'):wikitext(args[param .. '1']);
            row:tag('div'):addClass('infobox-row-content'):tag('span'):wikitext(args[param .. '2']);
        end
    end

    return infobox;

end

return p