Module:InfoboxProto: Difference between revisions

From Elwiki
No edit summary
No edit summary
Line 63: Line 63:
     end
     end


    local clearfix = mw.html.create('div'):addClass('clearfix');
    infobox = infobox .. clearfix;
     return infobox;
     return infobox;



Revision as of 19:41, 10 May 2022

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.color == nil then
        if args['Color'] ~= nil then
            args.color = args['Color'];
        else
            args.color = '{{ColorSel|Misc}}';
        end
    end

    if title == nil then
        title = args.name
    end

    local infobox = mw.html.create('div'):addClass('infobox-new');
    infobox:tag('div'):addClass('infobox-new-header'):css('background-color', args.color:gsub("%#", "#")):wikitext(title);
    local img = args.image;
    if args.image == nil then
        img = args['Image'];
    end
    infobox:tag('div'):addClass('infobox-new-image'):wikitext(img);

    -- Adds a normal row
    function addField(param, field_name, double, field_value)
        if args[param] ~= nil or field_value ~= nil then
            local row = infobox:tag('div'):addClass('infobox-row');
            if field_name == nil then
                field_name = titleCase(param);
            end
            if field_value == nil then
                field_value = args[param];
            end
            row:tag('div'):addClass('infobox-row-title'):tag('span'):wikitext(field_name);
            if (param == 'video' or param == 'tree' or param == 'stat') then
                local special_row = row:tag('div'):addClass('infobox-row-content'):wikitext(field_value);
                if param == 'video' then
                    special_row:addClass('infobox-video-row')
                end
            else
                row:tag('div'):addClass('infobox-row-content'):tag('span'):wikitext(field_value);
            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

    local clearfix = mw.html.create('div'):addClass('clearfix');
    infobox = infobox .. clearfix;
    return infobox;

end

return p