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 6: Line 6:
     local args = frame:getParent().args
     local args = frame:getParent().args
      
      
     -- 1. Gather all tab names into a list
     -- 1. Collect Tab Names
     local tabNames = {}
     local tabNames = {}
     local i = 1
     local i = 1
Line 14: Line 14:
     end
     end
      
      
     -- 2. Determine the Base Page (Root)
     -- 2. Auto-Detect the Base (Root) Page
     local basePage = args.base
     local basePage = args.base
     if not basePage then
     if not basePage then
         -- Auto-Detection Logic:
         -- Check if we are currently sitting on one of the tabs (e.g., ".../The Grind")
         local currentSubpage = title.subpageText
         local currentSub = title.subpageText
         local isSubpage = false
         local isTabSubpage = false
          
          
        -- Check if the current subpage matches one of our tabs (e.g., "The Grind")
         for _, name in ipairs(tabNames) do
         for _, name in ipairs(tabNames) do
             if name == currentSubpage then
             if name == currentSub then
                 isSubpage = true
                 isTabSubpage = true
                 break
                 break
             end
             end
         end
         end
          
          
         if isSubpage then
         if isTabSubpage then
            -- We are on a tab subpage, so the Base is the parent
             basePage = title.baseText -- Go up one level (parent is the root)
             basePage = title.baseText
         else
         else
             -- We are not on a subpage (or the subpage isn't in the tab list)
             basePage = title.prefixedText -- We are already on the root
            -- So we assume we are already on the Base Page.
            basePage = title.prefixedText
         end
         end
     end
     end


     -- 3. Determine Style
     -- 3. Setup Styles (Main vs Sub)
     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 HTML
     -- 4. Build the Tabs
     local root = html.create('div'):addClass(containerClass)
     local root = html.create('div'):addClass(containerClass)
      
      
     for _, tabName in ipairs(tabNames) do
     for _, name in ipairs(tabNames) do
         local target = ""
         local target = ""
          
          
         -- Special Rule: "Overview" always links to the Base Page
         -- === THE FIX ===
         if tabName == "Overview" then
        -- If the tab is "Overview", link to the Base Page directly.
        -- Otherwise, link to Base Page + "/" + Tab Name.
         if name == 'Overview' then
             target = basePage
             target = basePage
         else
         else
             target = basePage .. '/' .. tabName
             target = basePage .. '/' .. name
         end
         end
          
          
         -- Check if this tab is active
         -- Determine Active State
         local isActive = (title.prefixedText == target)
         local isActive = (title.prefixedText == target)
       
        -- If we are on the Base Page and this is Overview, it's active
        if tabName == "Overview" and title.prefixedText == basePage then
            isActive = true
        end
          
          
         local tabDiv = root:tag('div'):addClass(tabClass)
         local tabDiv = root:tag('div'):addClass(tabClass)
         if isActive then tabDiv:addClass('active') end
         if isActive then tabDiv:addClass('active') end
          
          
         tabDiv:wikitext('[[' .. target .. '|' .. tabName .. ']]')
         tabDiv:wikitext('[[' .. target .. '|' .. name .. ']]')
     end
     end
      
      

Revision as of 02:42, 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()

function p.main(frame)
    local args = frame:getParent().args
    
    -- 1. Collect Tab Names
    local tabNames = {}
    local i = 1
    while args[i] do
        table.insert(tabNames, args[i])
        i = i + 1
    end
    
    -- 2. Auto-Detect the Base (Root) Page
    local basePage = args.base
    if not basePage then
        -- Check if we are currently sitting on one of the tabs (e.g., ".../The Grind")
        local currentSub = title.subpageText
        local isTabSubpage = false
        
        for _, name in ipairs(tabNames) do
            if name == currentSub then
                isTabSubpage = true
                break
            end
        end
        
        if isTabSubpage then
            basePage = title.baseText -- Go up one level (parent is the root)
        else
            basePage = title.prefixedText -- We are already on the root
        end
    end

    -- 3. Setup Styles (Main vs 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 the Tabs
    local root = html.create('div'):addClass(containerClass)
    
    for _, name in ipairs(tabNames) do
        local target = ""
        
        -- === THE FIX ===
        -- If the tab is "Overview", link to the Base Page directly.
        -- Otherwise, link to Base Page + "/" + Tab Name.
        if name == 'Overview' then
            target = basePage
        else
            target = basePage .. '/' .. name
        end
        
        -- Determine Active State
        local isActive = (title.prefixedText == target)
        
        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