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 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 -- Load text module for trimming
local text = mw.text


function p.main(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
      
      
     -- 1. Collect Tab Names (and TRIM whitespace)
     -- 1. Collect Tab Names
     local tabNames = {}
     local tabNames = {}
     local i = 1
     local i = 1
     while args[i] do
     while args[i] do
        -- mw.text.trim removes invisible spaces like " Overview " -> "Overview"
         local t = text.trim(args[i])
         local cleanName = text.trim(args[i])
         if t ~= "" then table.insert(tabNames, t) end
         if cleanName ~= "" then
            table.insert(tabNames, cleanName)
        end
         i = i + 1
         i = i + 1
     end
     end
      
      
     -- 2. Auto-Detect the Base (Root) Page
     -- 2. INTELLIGENT ROOT DETECTION
     local basePage = args.base
     local basePage = args.base
     if not basePage then
     if not basePage then
         local currentSub = text.trim(title.subpageText)
         basePage = title.prefixedText
        local isTabSubpage = false
          
          
         for _, name in ipairs(tabNames) do
         local parts = text.split(title.prefixedText, '/')
            if name == currentSub then
        local foundRoot = false -- Flag to track if we found the root
                isTabSubpage = true
       
                 break
        for idx, part in ipairs(parts) do
            local cleanPart = part:gsub('_', ' ')
           
            for _, tab in ipairs(tabNames) do
                -- If we find a matching tab name in the URL (e.g. "The Grind")
                -- and it's not "Overview"
                if cleanPart == tab and tab:lower() ~= 'overview' then
                    -- The Root is the path up to this point
                    basePage = table.concat(parts, '/', 1, idx - 1)
                    foundRoot = true
                    break -- Break inner loop
                end
            end
           
            if foundRoot then
                 break -- Break outer loop
             end
             end
        end
       
        if isTabSubpage then
            basePage = title.baseText -- Go up one level
        else
            basePage = title.prefixedText -- We are on the root
         end
         end
     end
     end


     -- 3. Setup Styles
     -- 3. Build HTML
     local isSub = (args.type == 'sub')
     local isSub = (args.type == 'sub')
     local containerClass = isSub and 'pastel-bar' or 'chrome-tabs-container'
     local containerClass = isSub and 'pastel-bar' or 'chrome-tabs-container'
     local tabClass = isSub and 'pastel-pill' or 'chrome-tab'
     local tabClass = isSub and 'pastel-pill' or 'chrome-tab'
      
      
    -- 4. Build the Tabs
     local root = html.create('div'):addClass(containerClass)
     local root = html.create('div'):addClass(containerClass)
      
      
     for _, name in ipairs(tabNames) do
     for _, name in ipairs(tabNames) do
         local target = ""
         local target = ""
        local cleanName = name:gsub(' ', '_')
          
          
        -- Fix: Check matches "Overview" exactly, even if case differs slightly
         if name:lower() == 'overview' then
         if name:lower() == 'overview' then
             target = basePage
             target = basePage
         else
         else
             target = basePage .. '/' .. name
             target = basePage .. '/' .. cleanName
         end
         end
          
          
         -- Determine Active State
         -- 4. ACTIVE STATE LOGIC
         -- We compare the targets to the current page title
        local isActive = false
        local isActive = (title.prefixedText == target)
       
         if title.prefixedText == target then
            -- Exact match? Always active.
            isActive = true
        elseif title.prefixedText:find(target .. '/', 1, true) == 1 then
            -- Are we on a sub-sub-page? (e.g. .../The_Grind/Group_A)
            -- YES, highlight this tab... UNLESS it is Overview.
            if name:lower() ~= 'overview' then
                isActive = true
            end
        end
          
          
         local tabDiv = root:tag('div'):addClass(tabClass)
         local tabDiv = root:tag('div'):addClass(tabClass)

Revision as of 02:51, 25 January 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
    
    -- 1. 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
    
    -- 2. INTELLIGENT ROOT DETECTION
    local basePage = args.base
    if not basePage then
        basePage = title.prefixedText 
        
        local parts = text.split(title.prefixedText, '/')
        local foundRoot = false -- Flag to track if we found the root
        
        for idx, part in ipairs(parts) do
            local cleanPart = part:gsub('_', ' ')
            
            for _, tab in ipairs(tabNames) do
                -- If we find a matching tab name in the URL (e.g. "The Grind")
                -- and it's not "Overview"
                if cleanPart == tab and tab:lower() ~= 'overview' then
                    -- The Root is the path up to this point
                    basePage = table.concat(parts, '/', 1, idx - 1)
                    foundRoot = true
                    break -- Break inner loop
                end
            end
            
            if foundRoot then
                break -- Break outer loop
            end
        end
    end

    -- 3. Build HTML
    local isSub = (args.type == 'sub')
    local containerClass = isSub and 'pastel-bar' or 'chrome-tabs-container'
    local tabClass = isSub and 'pastel-pill' or 'chrome-tab'
    
    local root = html.create('div'):addClass(containerClass)
    
    for _, name in ipairs(tabNames) do
        local target = ""
        local cleanName = name:gsub(' ', '_')
        
        if name:lower() == 'overview' then
            target = basePage
        else
            target = basePage .. '/' .. cleanName
        end
        
        -- 4. ACTIVE STATE LOGIC
        local isActive = false
        
        if title.prefixedText == target then
            -- Exact match? Always active.
            isActive = true
        elseif title.prefixedText:find(target .. '/', 1, true) == 1 then
            -- Are we on a sub-sub-page? (e.g. .../The_Grind/Group_A)
            -- YES, highlight this tab... UNLESS it is Overview.
            if name:lower() ~= 'overview' then
                isActive = true
            end
        end
        
        local tabDiv = root:tag('div'):addClass(tabClass)
        if isActive then tabDiv:addClass('active') end
        
        tabDiv:wikitext('[[' .. target .. '|' .. name .. ']]')
    end
    
    return tostring(root)
end

return p