Jump to content

Module:Sandbox/SD0001/Tabbed window

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by SD0001 (talk | contribs) at 14:07, 29 January 2025 (fix variable name). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local Arguments = require('Module:Arguments')
local Calculator = require('Module:Sandbox/SD0001/Calculator')

local p = {}

p.main = function(frame)
	local args = Arguments.getArgs(frame)
	local tabs = getTabs(args)

	local calc = Calculator:new({ class='tpl-tabbed-window', refreshOnLoad = true })
	calc:hidden({ id = 'header', default = 1})

	for idx, tab in ipairs(tabs) do
		calc:hidden({ id = 'h'..idx, formula = 'ifequal(header,'..idx..')', default = (idx == 1 and 1 or 0) })
	end
	
	local panelHeader = calc:subContainer({ 
		style = 'background-color: var(--background-color-interactive, #eaecf0)' 
	})

	for idx, tab in ipairs(tabs) do
		panelHeader:button({ contents = tab.header, type = 'default', weight = 'quiet', formula = idx,  ['for'] = 'header',
			role = 'tab', style = 'background-color:rgba(255,255,255,var(--calculator-h'..idx..'))' })
	end
	
	for idx, tab in ipairs(tabs) do
		calc:hideIfZero({ class = 'panel', role = 'tabpanel', formula = 'h'..idx, text = tab.content })
	end

	return frame:extensionTag('templatestyles', '', {src = 'User:SD0001/Tabbed window/styles.css'})
		.. tostring(calc)
end

function getTabs(args)
	local tabs = {}
	for param, value in pairs(args) do
		if string.match(param, "^tab%d+") ~= nil then
			local num = tonumber(string.match(param, '%d+'))
			if num ~= nil and args['content'..num] ~= nil then
				tabs[num] = {header = value, content = args['content'..num]}
			end
		end
	end
	return tabs
end


return p