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
Created page with "local p = {} local html = mw.html function p.main(frame) local args = frame:getParent().args -- 1. Determine Base Page -- If 'base' is provided, use it. Otherwise, assume the first 3 parts of the current title are the "Root". -- (Matches your old logic: {{#titleparts:{{PAGENAME}}|3}}) local currentTitle = mw.title.getCurrentTitle() local basePage = args.base or currentTitle.baseText -- Fallback for complicated URLs if .baseText isn't eno..."
 
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local html = mw.html
local html = mw.html
local title = mw.title.getCurrentTitle()


function p.main(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
      
      
     -- 1. Determine Base Page
     -- 1. Gather all tab names into a list
     -- If 'base' is provided, use it. Otherwise, assume the first 3 parts of the current title are the "Root".
     local tabNames = {}
    -- (Matches your old logic: {{#titleparts:{{PAGENAME}}|3}})
     local i = 1
     local currentTitle = mw.title.getCurrentTitle()
    while args[i] do
     local basePage = args.base or currentTitle.baseText
        table.insert(tabNames, args[i])
     -- Fallback for complicated URLs if .baseText isn't enough:
        i = i + 1
    if not args.base and currentTitle.text:find('/') then
    end
         -- Grab the first 3 parts (e.g., Game/Tournaments/Event)
   
         local parts = mw.text.split(currentTitle.text, '/')
    -- 2. Determine the Base Page (Root)
         if #parts >= 3 then
     local basePage = args.base
             basePage = table.concat(parts, '/', 1, 3)
     if not basePage then
        -- Auto-Detection Logic:
        local currentSubpage = title.subpageText
        local isSubpage = false
       
         -- Check if the current subpage matches one of our tabs (e.g., "The Grind")
         for _, name in ipairs(tabNames) do
            if name == currentSubpage then
                isSubpage = true
                break
            end
        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


     -- 2. Determine Style (Main vs Sub)
     -- 3. Determine Style
     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'


     -- 3. Build the Container
     -- 4. Build HTML
     local root = html.create('div'):addClass(containerClass)
     local root = html.create('div'):addClass(containerClass)
      
      
     -- 4. Iterate through numbered args (1, 2, 3...)
     for _, tabName in ipairs(tabNames) do
    local i = 1
    while args[i] do
        local tabName = args[i]
         local target = ""
         local target = ""
          
          
         -- Logic: "Overview" usually links to the Base Page itself.
         -- Special Rule: "Overview" always links to the Base Page
        -- All other tabs link to BasePage/TabName
         if tabName == "Overview" then
         if tabName == "Overview" then
             target = basePage
             target = basePage
Line 41: Line 57:
         end
         end
          
          
         -- Active State Check
         -- Check if this tab is active
         local isActive = (currentTitle.text == target)
         local isActive = (title.prefixedText == target)
          
          
         -- Build the Tab
         -- If we are on the Base Page and this is Overview, it's active
         local tabDiv = root:tag('div')
         if tabName == "Overview" and title.prefixedText == basePage then
             :addClass(tabClass)
             isActive = true
        end
          
          
         if isActive then
        local tabDiv = root:tag('div'):addClass(tabClass)
            tabDiv:addClass('active')
         if isActive then tabDiv:addClass('active') end
        end
          
          
         tabDiv:wikitext('[[' .. target .. '|' .. tabName .. ']]')
         tabDiv:wikitext('[[' .. target .. '|' .. tabName .. ']]')
       
        i = i + 1
     end
     end
      
      

Revision as of 02:40, 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. Gather all tab names into a list
    local tabNames = {}
    local i = 1
    while args[i] do
        table.insert(tabNames, args[i])
        i = i + 1
    end
    
    -- 2. Determine the Base Page (Root)
    local basePage = args.base
    if not basePage then
        -- Auto-Detection Logic:
        local currentSubpage = title.subpageText
        local isSubpage = false
        
        -- Check if the current subpage matches one of our tabs (e.g., "The Grind")
        for _, name in ipairs(tabNames) do
            if name == currentSubpage then
                isSubpage = true
                break
            end
        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

    -- 3. Determine Style
    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
    local root = html.create('div'):addClass(containerClass)
    
    for _, tabName in ipairs(tabNames) do
        local target = ""
        
        -- 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)
        
        -- 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)
        if isActive then tabDiv:addClass('active') end
        
        tabDiv:wikitext('[[' .. target .. '|' .. tabName .. ']]')
    end
    
    return tostring(root)
end

return p