Module:Tabs: Difference between revisions

From TwogPedia
(Created page with "local Tabs = {} function Tabs.static(args) local tabCount = Tabs.number_of_tabs(args) local this = tonumber(args['This']) or Tabs.computeThis(args, tabCount) local this2 = tonumber(args['This2']) local outerDiv = mw.html.create('div') :addClass('tabs-static') :attr('data-nosnippet', '') local ul = outerDiv:tag('ul') :attr('class', 'nav nav-tabs navigation-not-searchable tabs tabs' .. tabCount) for i = 1, tabCount do local link = args['link' .. i] loca...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
local Tabs = {}
local Tabs = {}


function Tabs.static(args)
function Tabs.main(frame)
local args = getArgs(frame)
local tabCount = Tabs.number_of_tabs(args)
local tabCount = Tabs.number_of_tabs(args)
local this = tonumber(args['This']) or Tabs.computeThis(args, tabCount)
local currentTitle = mw.title.getCurrentTitle().prefixedText
local this2 = tonumber(args['This2'])
local activeTab = nil
 
local outerDiv = mw.html.create('div')
local outerDiv = mw.html.create('div')
:addClass('tabs-static')
:addClass('tabs-container')
:attr('data-nosnippet', '')


local ul = outerDiv:tag('ul')
local ul = outerDiv:tag('ul')
:attr('class', 'nav nav-tabs navigation-not-searchable tabs tabs' .. tabCount)
:attr('class', 'tabs')


for i = 1, tabCount do
for i = 1, tabCount do
Line 18: Line 19:


local text
local text
if link ~= nil then
-- If there is no name, then use last part of the link
-- If name is unspecified, then use the last link segment as the name
if tabName == nil and link ~= nil then
tabName = tabName or Tabs.computeLastSegment(link)
tabName = mw.text.split(link, '/')[#mw.text.split(link, '/')]
text = '[[' .. link .. '|' .. tabName .. ']]'
text = '[[' .. link .. '|' .. tabName .. ']]'
else
elseif link ~= nil then
text = '[[' .. link .. '|' .. tabName .. ']]'
else  
text = tabName
text = tabName
end
end
Line 28: Line 31:
local li = ul:tag('li')
local li = ul:tag('li')
:wikitext(text)
:wikitext(text)
 
if (i == this) or (i == this2) then
-- if (link == currentTitle) then
li:addClass('active')
if (args.org) then
if (currentTitle:sub(1, #link) == link) then
activeTab = i
li:addClass('active')
end
else
if (link == currentTitle) then
activeTab = i
li:addClass('active')
end
end
end
end
end
Line 36: Line 48:
-- optionally add more tab bars beneath
-- optionally add more tab bars beneath
local extra = ''
local extra = ''
if this and args['tabs' .. this] ~= nil then
if activeTab and args['tabs' .. activeTab] ~= nil then
extra = args['tabs' .. this]
extra = args['tabs' .. activeTab]
end
end


return tostring(outerDiv) .. extra
return tostring(outerDiv) .. extra
end
function Tabs.computeThis(args, tabCount)
local fullPageName = mw.title.getCurrentTitle().prefixedText
local this = nil
-- Finds the link that is a prefix of the current page. If there are more than one, choose the longest, then first.
-- For example, if the current page is ab/cd/e3, then among
--  ab/cd/e1
--  ab/cd/e2
--  ab/cd/e
--  ab/cd
--  ab/cg
--  ab
-- it will pick ab/cd.
local maxLinkLength = -1
for i = 1, tabCount do
local link = args['link' .. i]
if link ~= nil then
link = mw.ustring.gsub(link, '_', ' ')
local linkLength = mw.ustring.len(link)
local charAfter = mw.ustring.sub(fullPageName, linkLength + 1, linkLength + 1)
if
mw.ustring.sub(fullPageName, 1, linkLength) == link
-- Prefix must be aligned at '/' boundaries
and (charAfter == '/' or charAfter == '')
and linkLength > maxLinkLength then
maxLinkLength = linkLength
this = i
end
end
end
return this
end
end


Line 83: Line 59:
args = args or {}
args = args or {}
local i = 0
local i = 0
while args['name' .. (i + 1)] ~= nil or args['link' .. (i + 1)] ~= nil do
while args['name' .. (i + 1)] ~= nil or args['link' .. (i + 1)] ~= nil do
i = i + 1
i = i + 1
end
end
if i == 0 then
if i == 0 then
error('You are trying to add a "Tabs" template without arguments for names nor links')
error('The Tabs template needs name and link arguments.')
end
end
return i
return i
end
-- Computes the last segment of a page name
-- e.g. Tabs.computeLastSegment('abc/def/ghi') -> 'ghi'
function Tabs.computeLastSegment(pageName)
local start = 1
while true do
local nextStart, nextEnd = mw.ustring.find(pageName, '/', start)
if nextStart == nil then
return mw.ustring.sub(pageName, start)
end
start = nextEnd + 1
end
end
--[[
Creates dynamic tabs.
Entry point of Template:Tabs dynamic
]]
function Tabs.dynamic(args)
local this = tonumber(args['This']) or 1
local hideShowAll = args['hide-showall']
local tabs = Tabs.number_of_tabs(args)
local list = '\n<ul class="nav nav-tabs tabs tabs' .. tabs .. '">'
local innerDiv = '\n<div class="tabs-content wiki-bordercolor-light">'
local i = 1
while(i <= tabs) do
local text = args['name' .. i]
local li = '\n<li class="tab' .. i .. ' ' ..
((this == i) and 'active' or '') ..
'>' .. text .. '</li>'
list = list .. li
local content = args['content' .. i]
if content ~= nil then
local contentDiv = '\n<div class="content' .. i ..
((this == i) and ' active' or '') .. '">' ..
'\n' .. content .. '</div>'
innerDiv = innerDiv .. contentDiv
end
i = i + 1
end
if (hideShowAll == nil) then
local li = '<li class="show-all">Show All</li>'
list = list .. li
end
list = list .. '\n</ul>'
local outerDiv = '<div class="tabs-dynamic">'
outerDiv = outerDiv .. list .. innerDiv
if args['content1'] ~= nil then
outerDiv = outerDiv .. '\n</div>\n</div>' -- close off both inner and outer
end
return outerDiv
end
end


return Tabs
return Tabs

Latest revision as of 10:54, 21 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 .. ']]'
		elseif link ~= nil then
			text = '[[' .. link .. '|' .. tabName .. ']]'
		else 
			text = tabName
		end

		local li = ul:tag('li')
			:wikitext(text)
		
		-- if (link == currentTitle) then
		if (args.org) then
			if (currentTitle:sub(1, #link) == link) then
				activeTab = i
				li:addClass('active')
			end
		else 
			if (link == currentTitle) then
				activeTab = i
				li:addClass('active')
			end
		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