Module:Tabs: Difference between revisions
From eSportsAmaze
More actions
Esportsamaze (talk | contribs) 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..." |
Esportsamaze (talk | contribs) 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. | -- 1. Gather all tab names into a list | ||
local tabNames = {} | |||
local i = 1 | |||
local | while args[i] do | ||
local basePage = args.base | table.insert(tabNames, args[i]) | ||
-- | i = i + 1 | ||
end | |||
-- | |||
-- 2. Determine the Base Page (Root) | |||
if | local basePage = args.base | ||
basePage = | 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 | ||
-- | -- 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' | ||
-- | -- 4. Build HTML | ||
local root = html.create('div'):addClass(containerClass) | local root = html.create('div'):addClass(containerClass) | ||
for _, tabName in ipairs(tabNames) do | |||
local target = "" | local target = "" | ||
-- | -- Special Rule: "Overview" always links to the Base Page | ||
if tabName == "Overview" then | if tabName == "Overview" then | ||
target = basePage | target = basePage | ||
| Line 41: | Line 57: | ||
end | end | ||
-- | -- Check if this tab is active | ||
local isActive = ( | 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 | |||
if isActive then | local tabDiv = root:tag('div'):addClass(tabClass) | ||
if isActive then tabDiv:addClass('active') end | |||
tabDiv:wikitext('[[' .. target .. '|' .. tabName .. ']]') | tabDiv:wikitext('[[' .. target .. '|' .. tabName .. ']]') | ||
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