Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

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

local p = {}

function p.render(frame)
    local args = frame:getParent().args
    
    -- 1. SETUP CONTAINER
    local root = mw.html.create('div')
        :addClass('citizen-infobox')
        :css({
            ['float'] = 'right',
            ['width'] = '300px',
            ['max-width'] = '100%',
            ['background'] = 'var(--color-surface-2)',
            ['border'] = '1px solid var(--border-color-base)',
            ['border-radius'] = 'var(--border-radius-base, 8px)',
            ['box-shadow'] = 'var(--box-shadow-level-1)',
            ['margin'] = '0 0 1em 1em',
            ['overflow'] = 'hidden',
            ['clear'] = 'right' -- Prevents floating weirdness
        })

    -- 2. HEADER / TITLE
    if args['title'] or args['name'] then
        root:tag('div')
            :css({
                ['background'] = 'var(--color-primary)', 
                ['color'] = '#fff', 
                ['padding'] = '12px',
                ['text-align'] = 'center',
                ['font-weight'] = 'bold',
                ['font-size'] = '1.2em'
            })
            :wikitext(args['title'] or args['name'])
    end

    -- 3. IMAGES & TABS LOGIC
    local images = {}
    local i = 1
    
    -- We loop to find image1, image2...
    while args['image' .. i] do
        table.insert(images, {
            file    = args['image' .. i],
            -- FIX: Look for 'image1tabtext' OR 'tabtext1'
            tab     = args['image' .. i .. 'tabtext'] or args['tabtext' .. i] or ('View ' .. i),
            -- FIX: Look for 'image1undertext' OR 'undertext1'
            caption = args['image' .. i .. 'undertext'] or args['undertext' .. i] or ''
        })
        i = i + 1
    end

    if #images > 0 then
        local imageContainer = root:tag('div'):addClass('infobox-image-area')
            :css({['background'] = 'var(--color-surface-1)', ['text-align'] = 'center', ['border-bottom'] = '1px solid var(--border-color-base)'})

        -- A. Render Buttons (only if > 1 image)
        if #images > 1 then
            local btnRow = imageContainer:tag('div')
                :css({['display'] = 'flex', ['justify-content'] = 'center', ['gap'] = '8px', ['padding'] = '10px 10px 0 10px'})
            
            for idx, imgData in ipairs(images) do
                local btn = btnRow:tag('span')
                    :addClass('infobox-tab-btn')
                    :attr('data-target', 'infobox-img-' .. idx)
                    :wikitext(imgData.tab)
                
                -- Force first button active by default
                if idx == 1 then btn:addClass('active') end
            end
        end

        -- B. Render Images
        for idx, imgData in ipairs(images) do
            local imgWrapper = imageContainer:tag('div')
                :addClass('infobox-img-content')
                :attr('id', 'infobox-img-' .. idx)
                :css({['padding'] = '15px'})
            
            -- Force first image visible by default
            if idx == 1 then imgWrapper:addClass('active') end

            -- The Image
            imgWrapper:tag('div'):wikitext(string.format('[[File:%s|260px]]', imgData.file))
            
            -- The Caption
            if imgData.caption ~= '' then
                imgWrapper:tag('div')
                    :css({['font-size'] = '0.9em', ['color'] = 'var(--color-subtle)', ['margin-top'] = '8px', ['font-style'] = 'italic'})
                    :wikitext(imgData.caption)
            end
        end
    end

    -- 4. DATA ROWS
    local contentTable = root:tag('table')
        :css({['width'] = '100%', ['border-collapse'] = 'collapse', ['font-size'] = '0.95em'})
    
    local j = 1
    -- Loop to find data1name/data1body OR label1/value1
    while args['label' .. j] or args['data' .. j .. 'name'] or args['header' .. j] do
        
        -- Header Row
        if args['header' .. j] then
            contentTable:tag('tr'):tag('th')
                :attr('colspan', '2')
                :css({
                    ['background'] = 'var(--color-surface-2)',
                    ['border-top'] = '1px solid var(--border-color-base)',
                    ['padding'] = '8px',
                    ['text-align'] = 'center',
                    ['color'] = 'var(--color-base)'
                })
                :wikitext(args['header' .. j])
        
        -- Data Row
        else
            local label = args['label' .. j] or args['data' .. j .. 'name']
            local value = args['value' .. j] or args['data' .. j .. 'body']

            if label and value then
                local row = contentTable:tag('tr')
                    :css({['border-bottom'] = '1px solid var(--border-color-subtle)'})
                
                -- Label Cell
                row:tag('th')
                    :css({['text-align'] = 'left', ['padding'] = '10px', ['width'] = '40%', ['color'] = 'var(--color-subtle)'})
                    :wikitext(label)
                
                -- Value Cell
                row:tag('td')
                    :css({['text-align'] = 'right', ['padding'] = '10px', ['font-weight'] = '500'})
                    :wikitext(value)
            end
        end
        j = j + 1
    end

    return tostring(root)
end

return p