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

Module:Tabs: Difference between revisions

From eSportsAmaze
No edit summary
No edit summary
 
Line 1: Line 1:
-- Module:Tabs (redesigned)
local p = {}
local p = {}
local html = mw.html
local html = mw.html

Latest revision as of 01:20, 5 March 2026

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

local p = {}
local html = mw.html
local title = mw.title.getCurrentTitle()
local text = mw.text

function p.main(frame)
    local args = frame:getParent().args

    -- Collect tab names
    local tabNames = {}
    local i = 1
    while args[i] do
        local t = text.trim(args[i])
        if t ~= "" then table.insert(tabNames, t) end
        i = i + 1
    end

    -- Resolve base page
    local basePage = args.base
    if not basePage then
        basePage = title.prefixedText
        local parts = text.split(title.prefixedText, '/')
        local foundRoot = false
        for idx, part in ipairs(parts) do
            for _, tab in ipairs(tabNames) do
                if part:lower() == tab:lower() and tab:lower() ~= 'overview' then
                    basePage = table.concat(parts, '/', 1, idx - 1)
                    foundRoot = true; break
                end
            end
            if foundRoot then break end
        end
    end

    local isSub = (args.type == 'sub')

    -- Build tab data (target + isActive)
    local tabs = {}
    local activeLabel = tabNames[1] or ""
    for _, name in ipairs(tabNames) do
        local target = (name:lower() == 'overview') and basePage or (basePage .. '/' .. name)
        local isActive = (title.prefixedText == target)
            or (title.prefixedText:sub(1, #target + 1) == target .. '/' and name:lower() ~= 'overview')
        if isActive then activeLabel = name end
        table.insert(tabs, { name = name, target = target, active = isActive })
    end

    if isSub then
        -- ── SUB TABS: Scrollable pills ──
        local bar = html.create('div'):addClass('espa-subtabs')
        for _, tab in ipairs(tabs) do
            local pill = bar:tag('div'):addClass('espa-subpill')
            if tab.active then pill:addClass('active') end
            pill:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]')
        end
        return tostring(bar)
    end

    -- ── MAIN TABS: Segmented (desktop) + Dropdown (mobile) ──
    local wrapper = html.create('div'):addClass('espa-tabs-wrapper')

    -- Desktop segmented control
    local seg = wrapper:tag('div'):addClass('espa-tabs')
    for _, tab in ipairs(tabs) do
        local t = seg:tag('div'):addClass('espa-tab')
        if tab.active then t:addClass('active') end
        t:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]')
    end

    -- Mobile dropdown
    local mob = wrapper:tag('div'):addClass('espa-tabs-mobile')
    local trigger = mob:tag('div'):addClass('espa-dropdown-trigger')
    local left = trigger:tag('div'):addClass('espa-trigger-left')
    left:tag('div'):addClass('espa-active-dot')
    left:tag('span'):wikitext(activeLabel)
    trigger:tag('span'):addClass('espa-chevron'):wikitext('▼')

    local menu = mob:tag('div'):addClass('espa-dropdown-menu')
    for _, tab in ipairs(tabs) do
        local item = menu:tag('div'):addClass('espa-dropdown-item')
        if tab.active then item:addClass('active') end
        local check = item:tag('div'):addClass('espa-item-check')
        if tab.active then check:wikitext('✓') end
        item:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]')
    end

    return tostring(wrapper)
end

return p