Module:MBNavbox

From MB Wiki
Jump to navigation Jump to search

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

local p = {}

-- Helper function to check if a file exists (requires expensive function overhead, 
-- so we usually just assume the naming convention: Item Name.png)
local function getIcon(item)
    return string.format('[[File:%s.png|22px|link=%s|alt=%s]]', item, item, item)
end

function p.main(frame)
    local args = frame:getParent().args
    local title = args.title or 'Character Info'
    
    -- MB Theme Colors (Map these to your Rarity/Tiers)
    local theme = {
        bg = '#1a1a1a',           -- Deep dark background
        header = '#2c7a7b',       -- Default Teal
        border = '#a1e9dc',       -- Glowing border
        text = '#f5fdfb'
    }
    
    -- Handle Rarity/Tier coloring
    if args.tier == '7' or args.tier == 'Noble' then
        theme.header = 'linear-gradient(135deg, #38b2ac 0%, #2c7a7b 100%)'
        theme.border = '#81e6d9'
    end

    -- Container
    local container = mw.html.create('table')
        :addClass('mb-navbox')
        :css({
            ['width'] = '100%',
            ['background'] = theme.bg,
            ['border'] = '2px solid ' .. theme.border,
            ['border-radius'] = '8px',
            ['color'] = theme.text,
            ['border-collapse'] = 'separate',
            ['border-spacing'] = '2px',
            ['margin'] = '10px 0'
        })

    -- Title Bar
    container:tag('tr'):tag('th')
        :attr('colspan', '2')
        :css({
            ['background'] = theme.header,
            ['padding'] = '10px',
            ['font-size'] = '1.1em',
            ['text-shadow'] = '1px 1px 2px #000'
        })
        :wikitext(title)

    -- Row Logic (Smarter Loop)
    local i = 1
    while args['group' .. i] or args['list' .. i] do
        local groupName = args['group' .. i] or ''
        local listData = args['list' .. i] or ''
        
        local row = container:tag('tr')
        
        -- Group Header (Left side)
        row:tag('td')
            :addClass('mb-group')
            :css({
                ['background'] = 'rgba(255,255,255,0.05)',
                ['width'] = '150px',
                ['font-weight'] = 'bold',
                ['padding'] = '8px',
                ['border-right'] = '1px solid ' .. theme.border .. '33'
            })
            :wikitext(groupName)
            
        -- List Content (Right side)
        local listCell = row:tag('td')
            :addClass('mb-content')
            :css('padding', '8px')

        -- SMART PARSER: Check if we should auto-icon
        if args.auto_icons == 'yes' then
            local items = mw.text.split(listData, ',%s*') -- Split by comma
            local formattedItems = {}
            for _, item in ipairs(items) do
                -- Remove bullets if present to clean string
                local cleanItem = item:gsub('^%s*%*%s*', '')
                table.insert(formattedItems, getIcon(cleanItem) .. ' [[' .. cleanItem .. ']]')
            end
            listCell:wikitext(table.concat(formattedItems, ' • '))
        else
            listCell:wikitext(listData)
        end

        i = i + 1
    end

    return tostring(container)
end

return p