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
 
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:
local html = mw.html
local html = mw.html
local title = mw.title.getCurrentTitle()
local title = mw.title.getCurrentTitle()
local text = mw.text


function p.main(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
   
 
     -- 1. Gather all tab names into a list
     -- Collect tab names
     local tabNames = {}
     local tabNames = {}
     local i = 1
     local i = 1
     while args[i] do
     while args[i] do
         table.insert(tabNames, args[i])
         local t = text.trim(args[i])
        if t ~= "" then table.insert(tabNames, t) end
         i = i + 1
         i = i + 1
     end
     end
   
 
     -- 2. Determine the Base Page (Root)
     -- Resolve base page
     local basePage = args.base
     local basePage = args.base
     if not basePage then
     if not basePage then
         -- Auto-Detection Logic:
         basePage = title.prefixedText
         local currentSubpage = title.subpageText
         local parts = text.split(title.prefixedText, '/')
         local isSubpage = false
         local foundRoot = false
          
         for idx, part in ipairs(parts) do
        -- Check if the current subpage matches one of our tabs (e.g., "The Grind")
            for _, tab in ipairs(tabNames) do
        for _, name in ipairs(tabNames) do
                if part:lower() == tab:lower() and tab:lower() ~= 'overview' then
            if name == currentSubpage then
                    basePage = table.concat(parts, '/', 1, idx - 1)
                isSubpage = true
                    foundRoot = true; break
                 break
                 end
             end
             end
        end
            if foundRoot then break end
       
        if isSubpage then
            -- We are on a tab subpage, so the Base is the parent
            basePage = title.baseText
        else
            -- We are not on a subpage (or the subpage isn't in the tab list)
            -- So we assume we are already on the Base Page.
            basePage = title.prefixedText
         end
         end
     end
     end


    -- 3. Determine Style
     local isSub = (args.type == 'sub')
     local isSub = (args.type == 'sub')
    local containerClass = isSub and 'pastel-bar' or 'chrome-tabs-container'
    local tabClass = isSub and 'pastel-pill' or 'chrome-tab'


     -- 4. Build HTML
     -- Build tab data (target + isActive)
     local root = html.create('div'):addClass(containerClass)
     local tabs = {}
      
     local activeLabel = tabNames[1] or ""
     for _, tabName in ipairs(tabNames) do
     for _, name in ipairs(tabNames) do
         local target = ""
         local target = (name:lower() == 'overview') and basePage or (basePage .. '/' .. name)
       
        -- Special Rule: "Overview" always links to the Base Page
        if tabName == "Overview" then
            target = basePage
        else
            target = basePage .. '/' .. tabName
        end
       
        -- Check if this tab is active
         local isActive = (title.prefixedText == target)
         local isActive = (title.prefixedText == target)
          
            or (title.prefixedText:sub(1, #target + 1) == target .. '/' and name:lower() ~= 'overview')
         -- If we are on the Base Page and this is Overview, it's active
        if isActive then activeLabel = name end
        if tabName == "Overview" and title.prefixedText == basePage then
         table.insert(tabs, { name = name, target = target, active = isActive })
             isActive = true
    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
         end
          
         return tostring(bar)
        local tabDiv = root:tag('div'):addClass(tabClass)
        if isActive then tabDiv:addClass('active') end
       
        tabDiv:wikitext('[[' .. target .. '|' .. tabName .. ']]')
     end
     end
      
 
     return tostring(root)
    -- ── 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
end


return p
return p

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