Module:Tabber: Difference between revisions

From Coral Island Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(13 intermediate revisions by the same user not shown)
Line 14: Line 14:
frame = frame or mw.getCurrentFrame()
frame = frame or mw.getCurrentFrame()
local tabber = mw.html.create('div'):addClass('tabber')
local tabber = mw.html.create('div'):addClass('custom-tabber pi-media-collection')
local labels = mw.html.create('ul'):addClass('tabber__header ' .. (args.no_border == nil and 'with-bottom-border' or ''))
local labels = mw.html.create('ul'):addClass('pi-media-collection-tabs' .. (args.no_border == nil and ' with-bottom-border' or ''))
local contents = mw.html.create()
local contents = mw.html.create()
local i = 1
local i = 1
Line 51: Line 51:
labels
labels
:tag('li')
:tag('li')
:addClass('tabber__tab')
:addClass('pi-tab-link ' .. (i==1 and 'current' or ''))
:attr('aria-selected', i==1 and 'true' or 'false')
:attr('data-pi-tab', 'pi-tab-'..y)
:attr('id', 'tab-' .. label:gsub(' ', '_') .. '-0')
:attr('href', '#' .. label:gsub(' ', '_') .. '-0')
:wikitext(label)
:wikitext(label)
local contentContainer =  
local contentContainer =  
contents
contents
:tag('div')
:tag('div')
:attr('id', label:gsub(' ', '_') .. '-0')
:attr('id', 'pi-tab-'..y)
:attr('aria-hidden', i==1 and 'false' or 'true')
:addClass('pi-media-collection-tab-content  ' .. (i==1 and 'current' or ''))
:addClass('tabber__panel')
contentContainer:newline()
addContent(contentContainer, content)
addContent(contentContainer, content)
contentContainer:newline()
end
end
i = i + 2 --unnamed go in pairs
i = i + 2 --unnamed go in pairs

Latest revision as of 18:24, 13 January 2024

This module implements {{Tabber}}.


local p = {}
local lib = require('Module:Feature')

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)

	return p._main(args, frame)
end

function p._main(args, frame)
	if args == nil then return ''
	elseif type(args) ~= 'table' then return args
	end
	frame = frame or mw.getCurrentFrame()
	
	local tabber = mw.html.create('div'):addClass('custom-tabber pi-media-collection')
	local labels = mw.html.create('ul'):addClass('pi-media-collection-tabs' .. (args.no_border == nil and ' with-bottom-border' or ''))
	local contents = mw.html.create()
	local i = 1
	local y = '1'
	local validTabs = 0
	
	local function addContent(container, content)
		if type(content) == 'string' then
			if args.nowiki then
				local content = 
				frame:preprocess(
					mw.text.decode(
						mw.text.unstripNoWiki(
							content
						)
					)
				)
				container:wikitext(content) --plain string content encased in nowiki as to avoid bad mediawiki parsing
			else
				container:wikitext(content) --plain string content
			end
		elseif type(content) == 'table' and tostring(content) == 'table' then --table content 
			for _, subContent in ipairs(content) do
				addContent(container, subContent)
			end
		elseif type(content) == 'table' then container:node(content) --html content
		end
	end
	
	while (args[i] and args[i+1]) or (args['label' .. y] and args['content' .. y]) do
		local label = args['label' .. y] or args[i]
		local content = args['content' .. y] or args[i+1]
		if lib.isNotEmpty(label) and lib.isNotEmpty(content) then
			validTabs = validTabs + 1
			labels
				:tag('li')
					:addClass('pi-tab-link ' .. (i==1 and 'current' or ''))
					:attr('data-pi-tab', 'pi-tab-'..y)
					:wikitext(label)
						
			local contentContainer = 
			contents
				:tag('div')
					:attr('id', 'pi-tab-'..y)
					:addClass('pi-media-collection-tab-content  ' .. (i==1 and 'current' or ''))
			addContent(contentContainer, content)
		end
		i = i + 2 --unnamed go in pairs
		y = tostring(tonumber(y)+1)
	end
	if validTabs <2 then return (args['content1'] or args[2] or '') end
	tabber:node(labels)
	tabber:node(contents)
	
	return tabber
end

return p