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 |
||
| (7 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local html = mw.html | local html = mw.html | ||
local title = mw.title.getCurrentTitle() | |||
local text = mw.text | |||
function p.main(frame) | function p.main(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
-- | -- Collect tab names | ||
local tabNames = {} | |||
local i = 1 | |||
local | while args[i] do | ||
local basePage = args.base | local t = text.trim(args[i]) | ||
if t ~= "" then table.insert(tabNames, t) end | |||
if not | i = i + 1 | ||
end | |||
local parts = | |||
if | -- Resolve base page | ||
local basePage = args.base | |||
if not basePage then | |||
basePage = title.prefixedText | |||
local parts = text.split(title.prefixedText, '/') | |||
local foundRoot = false | |||
for idx, part in ipairs(parts) do | |||
for _, tab in ipairs(tabNames) do | |||
if part:lower() == tab:lower() and tab:lower() ~= 'overview' then | |||
basePage = table.concat(parts, '/', 1, idx - 1) | |||
foundRoot = true; break | |||
end | |||
end | |||
if foundRoot then break end | |||
end | end | ||
end | end | ||
local isSub = (args.type == 'sub') | local isSub = (args.type == 'sub') | ||
-- | -- Build tab data (target + isActive) | ||
local | local tabs = {} | ||
local activeLabel = tabNames[1] or "" | |||
for _, name in ipairs(tabNames) do | |||
local target = (name:lower() == 'overview') and basePage or (basePage .. '/' .. name) | |||
local isActive = (title.prefixedText == target) | |||
or (title.prefixedText:sub(1, #target + 1) == target .. '/' and name:lower() ~= 'overview') | |||
if isActive then activeLabel = name end | |||
table.insert(tabs, { name = name, target = target, active = isActive }) | |||
-- | end | ||
- | |||
if isSub then | |||
-- ── SUB TABS: Scrollable pills ── | |||
local bar = html.create('div'):addClass('espa-subtabs') | |||
target | for _, tab in ipairs(tabs) do | ||
local pill = bar:tag('div'):addClass('espa-subpill') | |||
if tab.active then pill:addClass('active') end | |||
pill:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]') | |||
end | end | ||
return tostring(bar) | |||
end | |||
-- ── MAIN TABS: Segmented (desktop) + Dropdown (mobile) ── | |||
-- | local wrapper = html.create('div'):addClass('espa-tabs-wrapper') | ||
-- Desktop segmented control | |||
local seg = wrapper:tag('div'):addClass('espa-tabs') | |||
if | for _, tab in ipairs(tabs) do | ||
local t = seg:tag('div'):addClass('espa-tab') | |||
if tab.active then t:addClass('active') end | |||
t:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]') | |||
end | |||
-- Mobile dropdown | |||
local mob = wrapper:tag('div'):addClass('espa-tabs-mobile') | |||
local trigger = mob:tag('div'):addClass('espa-dropdown-trigger') | |||
local left = trigger:tag('div'):addClass('espa-trigger-left') | |||
left:tag('div'):addClass('espa-active-dot') | |||
left:tag('span'):wikitext(activeLabel) | |||
trigger:tag('span'):addClass('espa-chevron'):wikitext('▼') | |||
local menu = mob:tag('div'):addClass('espa-dropdown-menu') | |||
for _, tab in ipairs(tabs) do | |||
local item = menu:tag('div'):addClass('espa-dropdown-item') | |||
if tab.active then item:addClass('active') end | |||
local check = item:tag('div'):addClass('espa-item-check') | |||
if tab.active then check:wikitext('✓') end | |||
item:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]') | |||
end | end | ||
return tostring( | return tostring(wrapper) | ||
end | end | ||
return p | return p | ||
Latest revision as of 01:20, 5 March 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
-- 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
-- Resolve base page
local basePage = args.base
if not basePage then
basePage = title.prefixedText
local parts = text.split(title.prefixedText, '/')
local foundRoot = false
for idx, part in ipairs(parts) do
for _, tab in ipairs(tabNames) do
if part:lower() == tab:lower() and tab:lower() ~= 'overview' then
basePage = table.concat(parts, '/', 1, idx - 1)
foundRoot = true; break
end
end
if foundRoot then break end
end
end
local isSub = (args.type == 'sub')
-- Build tab data (target + isActive)
local tabs = {}
local activeLabel = tabNames[1] or ""
for _, name in ipairs(tabNames) do
local target = (name:lower() == 'overview') and basePage or (basePage .. '/' .. name)
local isActive = (title.prefixedText == target)
or (title.prefixedText:sub(1, #target + 1) == target .. '/' and name:lower() ~= 'overview')
if isActive then activeLabel = name end
table.insert(tabs, { name = name, target = target, active = isActive })
end
if isSub then
-- ── SUB TABS: Scrollable pills ──
local bar = html.create('div'):addClass('espa-subtabs')
for _, tab in ipairs(tabs) do
local pill = bar:tag('div'):addClass('espa-subpill')
if tab.active then pill:addClass('active') end
pill:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]')
end
return tostring(bar)
end
-- ── MAIN TABS: Segmented (desktop) + Dropdown (mobile) ──
local wrapper = html.create('div'):addClass('espa-tabs-wrapper')
-- Desktop segmented control
local seg = wrapper:tag('div'):addClass('espa-tabs')
for _, tab in ipairs(tabs) do
local t = seg:tag('div'):addClass('espa-tab')
if tab.active then t:addClass('active') end
t:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]')
end
-- Mobile dropdown
local mob = wrapper:tag('div'):addClass('espa-tabs-mobile')
local trigger = mob:tag('div'):addClass('espa-dropdown-trigger')
local left = trigger:tag('div'):addClass('espa-trigger-left')
left:tag('div'):addClass('espa-active-dot')
left:tag('span'):wikitext(activeLabel)
trigger:tag('span'):addClass('espa-chevron'):wikitext('▼')
local menu = mob:tag('div'):addClass('espa-dropdown-menu')
for _, tab in ipairs(tabs) do
local item = menu:tag('div'):addClass('espa-dropdown-item')
if tab.active then item:addClass('active') end
local check = item:tag('div'):addClass('espa-item-check')
if tab.active then check:wikitext('✓') end
item:wikitext('[[' .. tab.target .. '|' .. tab.name .. ']]')
end
return tostring(wrapper)
end
return p