Module:Tabs: Difference between revisions
From eSportsAmaze
More actions
Esportsamaze (talk | contribs) No edit summary |
Esportsamaze (talk | contribs) 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 | 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 | -- 1. Collect Tab Names | ||
local tabNames = {} | local tabNames = {} | ||
local i = 1 | local i = 1 | ||
while args[i] do | while args[i] do | ||
local t = text.trim(args[i]) | |||
local | if t ~= "" then table.insert(tabNames, t) end | ||
if | |||
i = i + 1 | i = i + 1 | ||
end | end | ||
-- 2. | -- 2. INTELLIGENT ROOT DETECTION | ||
local basePage = args.base | local basePage = args.base | ||
if not basePage then | if not basePage then | ||
basePage = title.prefixedText | |||
for _, | local parts = text.split(title.prefixedText, '/') | ||
local foundRoot = false -- Flag to track if we found the root | |||
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 | end | ||
end | end | ||
-- 3. | -- 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' | ||
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(' ', '_') | |||
if name:lower() == 'overview' then | if name:lower() == 'overview' then | ||
target = basePage | target = basePage | ||
else | else | ||
target = basePage .. '/' .. | target = basePage .. '/' .. cleanName | ||
end | 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) | 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