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 -- Load text module for trimming | |||
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 (and TRIM whitespace) | ||
local tabNames = {} | local tabNames = {} | ||
local i = 1 | local i = 1 | ||
while args[i] do | while args[i] do | ||
table.insert(tabNames, | -- mw.text.trim removes invisible spaces like " Overview " -> "Overview" | ||
local cleanName = text.trim(args[i]) | |||
if cleanName ~= "" then | |||
table.insert(tabNames, cleanName) | |||
end | |||
i = i + 1 | i = i + 1 | ||
end | end | ||
| Line 17: | Line 22: | ||
local basePage = args.base | local basePage = args.base | ||
if not basePage then | if not basePage then | ||
local currentSub = text.trim(title.subpageText) | |||
local currentSub = title.subpageText | |||
local isTabSubpage = false | local isTabSubpage = false | ||
| Line 29: | Line 33: | ||
if isTabSubpage then | if isTabSubpage then | ||
basePage = title.baseText -- Go up one level | basePage = title.baseText -- Go up one level | ||
else | else | ||
basePage = title.prefixedText -- We are | basePage = title.prefixedText -- We are on the root | ||
end | end | ||
end | end | ||
-- 3. Setup Styles | -- 3. Setup Styles | ||
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' | ||
| Line 46: | Line 50: | ||
local target = "" | local target = "" | ||
-- | -- Fix: Check matches "Overview" exactly, even if case differs slightly | ||
if name:lower() == 'overview' then | |||
if name == ' | |||
target = basePage | target = basePage | ||
else | else | ||
| Line 56: | Line 58: | ||
-- Determine Active State | -- Determine Active State | ||
-- We compare the targets to the current page title | |||
local isActive = (title.prefixedText == target) | local isActive = (title.prefixedText == target) | ||
Revision as of 02:46, 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 -- Load text module for trimming
function p.main(frame)
local args = frame:getParent().args
-- 1. Collect Tab Names (and TRIM whitespace)
local tabNames = {}
local i = 1
while args[i] do
-- mw.text.trim removes invisible spaces like " Overview " -> "Overview"
local cleanName = text.trim(args[i])
if cleanName ~= "" then
table.insert(tabNames, cleanName)
end
i = i + 1
end
-- 2. Auto-Detect the Base (Root) Page
local basePage = args.base
if not basePage then
local currentSub = text.trim(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
else
basePage = title.prefixedText -- We are on the root
end
end
-- 3. Setup Styles
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 = ""
-- Fix: Check matches "Overview" exactly, even if case differs slightly
if name:lower() == 'overview' then
target = basePage
else
target = basePage .. '/' .. name
end
-- Determine Active State
-- We compare the targets to the current page title
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