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
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local html = mw.html
local html = mw.html
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. Determine Base Page
     -- Collect tab names
     -- 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
        local t = text.trim(args[i])
    -- Fallback for complicated URLs if .baseText isn't enough:
        if t ~= "" then table.insert(tabNames, t) end
     if not args.base and currentTitle.text:find('/') then
        i = i + 1
         -- Grab the first 3 parts (e.g., Game/Tournaments/Event)
    end
         local parts = mw.text.split(currentTitle.text, '/')
 
         if #parts >= 3 then
    -- Resolve base page
            basePage = table.concat(parts, '/', 1, 3)
     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
     end
     end


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


     -- 3. Build the Container
     -- Build tab data (target + isActive)
     local root = html.create('div'):addClass(containerClass)
    local tabs = {}
   
    local activeLabel = tabNames[1] or ""
    -- 4. Iterate through numbered args (1, 2, 3...)
     for _, name in ipairs(tabNames) do
    local i = 1
        local target = (name:lower() == 'overview') and basePage or (basePage .. '/' .. name)
    while args[i] do
        local isActive = (title.prefixedText == target)
         local tabName = args[i]
            or (title.prefixedText:sub(1, #target + 1) == target .. '/' and name:lower() ~= 'overview')
         local target = ""
         if isActive then activeLabel = name end
       
         table.insert(tabs, { name = name, target = target, active = isActive })
         -- Logic: "Overview" usually links to the Base Page itself.
    end
         -- All other tabs link to BasePage/TabName
 
         if tabName == "Overview" then
    if isSub then
             target = basePage
         -- ── SUB TABS: Scrollable pills ──
        else
         local bar = html.create('div'):addClass('espa-subtabs')
             target = basePage .. '/' .. tabName
         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)
        -- Active State Check
    end
        local isActive = (currentTitle.text == target)
 
          
    -- ── MAIN TABS: Segmented (desktop) + Dropdown (mobile) ──
         -- Build the Tab
    local wrapper = html.create('div'):addClass('espa-tabs-wrapper')
        local tabDiv = root:tag('div')
 
            :addClass(tabClass)
    -- Desktop segmented control
          
    local seg = wrapper:tag('div'):addClass('espa-tabs')
         if isActive then
    for _, tab in ipairs(tabs) do
            tabDiv:addClass('active')
        local t = seg:tag('div'):addClass('espa-tab')
         end
         if tab.active then t:addClass('active') end
          
         t:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]')
         tabDiv:wikitext('[[' .. target .. '|' .. tabName .. ']]')
    end
       
 
        i = i + 1
    -- 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
     end
   
 
     return tostring(root)
     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