Module:Tabs
From eSportsAmaze
More actions
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