No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
local Tabs = {} | local Tabs = {} | ||
function Tabs. | function Tabs.main(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local tabCount = Tabs.number_of_tabs(args) | local tabCount = Tabs.number_of_tabs(args) | ||
local | local currentTitle = mw.title.getCurrentTitle().prefixedText | ||
local | local activeTab = nil | ||
local outerDiv = mw.html.create('div') | local outerDiv = mw.html.create('div') | ||
:addClass('tabs- | :addClass('tabs-container') | ||
local ul = outerDiv:tag('ul') | local ul = outerDiv:tag('ul') | ||
:attr('class', ' | :attr('class', 'tabs') | ||
for i = 1, tabCount do | for i = 1, tabCount do | ||
local link = args['link' .. i] | local link = args['link' .. i] | ||
Line 21: | Line 18: | ||
local text | local text | ||
-- If there is no name, then use last part of the link | |||
if tabName == nil and link ~= nil then | |||
tabName = | tabName = mw.text.split(link, '/')[#mw.text.split(link, '/')] | ||
text = '[[' .. link .. '|' .. tabName .. ']]' | text = '[[' .. link .. '|' .. tabName .. ']]' | ||
else | else | ||
text = tabName | text = '[[' .. link .. '|' .. tabName .. ']]' | ||
end | end | ||
local li = ul:tag('li') | local li = ul:tag('li') | ||
:wikitext(text) | :wikitext(text) | ||
if (link == currentTitle) then | |||
if ( | activeTab = i | ||
li:addClass('active') | li:addClass('active') | ||
end | end | ||
Line 39: | Line 36: | ||
-- optionally add more tab bars beneath | -- optionally add more tab bars beneath | ||
local extra = '' | local extra = '' | ||
if | if activeTab and args['tabs' .. activeTab] ~= nil then | ||
extra = args['tabs' .. | extra = args['tabs' .. activeTab] | ||
end | end | ||
return tostring(outerDiv) .. extra | return tostring(outerDiv) .. extra | ||
end | end | ||
-- Computes the number of tabs requested | -- Computes the number of tabs requested | ||
function Tabs.number_of_tabs(args) | function Tabs.number_of_tabs(args) | ||
args = args or {} | args = args or {} | ||
local i = 0 | local i = 0 | ||
Line 93: | Line 52: | ||
end | end | ||
if i == 0 then | if i == 0 then | ||
error(' | error('The Tabs template needs name and link arguments.') | ||
end | end | ||
return i | return i | ||
end | end | ||
return Tabs | return Tabs |
Revision as of 13:21, 12 September 2022
Documentation for this module may be created at Module:Tabs/doc
local getArgs = require('Module:Arguments').getArgs
local Tabs = {}
function Tabs.main(frame)
local args = getArgs(frame)
local tabCount = Tabs.number_of_tabs(args)
local currentTitle = mw.title.getCurrentTitle().prefixedText
local activeTab = nil
local outerDiv = mw.html.create('div')
:addClass('tabs-container')
local ul = outerDiv:tag('ul')
:attr('class', 'tabs')
for i = 1, tabCount do
local link = args['link' .. i]
local tabName = args['name' .. i]
local text
-- If there is no name, then use last part of the link
if tabName == nil and link ~= nil then
tabName = mw.text.split(link, '/')[#mw.text.split(link, '/')]
text = '[[' .. link .. '|' .. tabName .. ']]'
else
text = '[[' .. link .. '|' .. tabName .. ']]'
end
local li = ul:tag('li')
:wikitext(text)
if (link == currentTitle) then
activeTab = i
li:addClass('active')
end
end
-- optionally add more tab bars beneath
local extra = ''
if activeTab and args['tabs' .. activeTab] ~= nil then
extra = args['tabs' .. activeTab]
end
return tostring(outerDiv) .. extra
end
-- Computes the number of tabs requested
function Tabs.number_of_tabs(args)
args = args or {}
local i = 0
while args['name' .. (i + 1)] ~= nil or args['link' .. (i + 1)] ~= nil do
i = i + 1
end
if i == 0 then
error('The Tabs template needs name and link arguments.')
end
return i
end
return Tabs