Module:Tabs: Difference between revisions
From eSportsAmaze
More actions
Esportsamaze (talk | contribs) No edit summary |
Esportsamaze (talk | contribs) No edit summary |
||
| Line 6: | Line 6: | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
-- 1. | -- 1. Collect Tab Names | ||
local tabNames = {} | local tabNames = {} | ||
local i = 1 | local i = 1 | ||
| Line 14: | Line 14: | ||
end | end | ||
-- 2. | -- 2. Auto-Detect the Base (Root) Page | ||
local basePage = args.base | local basePage = args.base | ||
if not basePage then | if not basePage then | ||
-- | -- Check if we are currently sitting on one of the tabs (e.g., ".../The Grind") | ||
local | local currentSub = title.subpageText | ||
local | local isTabSubpage = false | ||
for _, name in ipairs(tabNames) do | for _, name in ipairs(tabNames) do | ||
if name == | if name == currentSub then | ||
isTabSubpage = true | |||
break | break | ||
end | end | ||
end | end | ||
if | if isTabSubpage then | ||
basePage = title.baseText -- Go up one level (parent is the root) | |||
basePage = title.baseText | |||
else | else | ||
-- We | basePage = title.prefixedText -- We are already on the root | ||
end | end | ||
end | end | ||
-- 3. | -- 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 | -- 4. Build the Tabs | ||
local root = html.create('div'):addClass(containerClass) | local root = html.create('div'):addClass(containerClass) | ||
for _, | for _, name in ipairs(tabNames) do | ||
local target = "" | local target = "" | ||
-- | -- === THE FIX === | ||
if | -- 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 .. '/' .. | target = basePage .. '/' .. name | ||
end | end | ||
-- | -- Determine Active State | ||
local isActive = (title.prefixedText == target) | local isActive = (title.prefixedText == target) | ||
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 .. '|' .. | 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