Module:Tabs: Difference between revisions
From eSportsAmaze
More actions
Esportsamaze (talk | contribs) No edit summary |
Esportsamaze (talk | contribs) No edit summary |
||
| (5 intermediate revisions by the same user not shown) | |||
| 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 | |||
function p.main(frame) | function p.main(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
-- | -- Collect tab names | ||
local tabNames = {} | local tabNames = {} | ||
local i = 1 | local i = 1 | ||
while args[i] do | while args[i] do | ||
table.insert(tabNames, | local t = text.trim(args[i]) | ||
if t ~= "" then table.insert(tabNames, t) end | |||
i = i + 1 | i = i + 1 | ||
end | end | ||
-- | -- Resolve base page | ||
local basePage = args.base | local basePage = args.base | ||
if not basePage then | if not basePage then | ||
basePage = title.prefixedText | |||
local | local parts = text.split(title.prefixedText, '/') | ||
local | 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 | end | ||
if foundRoot then break end | |||
end | end | ||
end | end | ||
local isSub = (args.type == 'sub') | local isSub = (args.type == 'sub') | ||
local | -- Build tab data (target + isActive) | ||
local tabs = {} | |||
local activeLabel = tabNames[1] or "" | |||
local | |||
for _, name in ipairs(tabNames) do | for _, name in ipairs(tabNames) do | ||
local target = | 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 | |||
target | -- ── 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 | end | ||
return tostring(bar) | |||
end | |||
local | |||
-- ── MAIN TABS: Segmented (desktop) + Dropdown (mobile) ── | |||
local | local wrapper = html.create('div'):addClass('espa-tabs-wrapper') | ||
if | |||
-- 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 | 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