Jump to content

Module:Start tab

From Wikisource

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local TableTools = require('Module:TableTools')

local function _dummytab(args)
	local td = mw.html.create('td')
		:addClass('wst-dummytab')
		:css({['border-bottom'] = args.border})
		:allDone()
	
	if not args['tab spacing percent'] then
		td:wikitext(' ')
	end
	
	return td
end

local function _tab(args)
	local tds = {}
	
	local tab_spacing_percent
	if args['tab spacing percent'] then
		tab_spacing_percent = args['tab spacing percent'] .. '%'
	end
	
	if not yesno(args.preums) then
		local pre_td = mw.html.create('td')
			:addClass('wst-pretab')
			:css({
				['width'] = tab_spacing_percent,
				['border-bottom'] = args.border
			})
			:allDone()
		
		if not tab_spacing_percent then
			pre_td:wikitext(' ')
		end
		table.insert(tds, pre_td)
	end
	
	local nowrap_class = (yesno(args['nowrap']) and 'wst-tab-nowrap') or 'wst-tab-wrap'
	
	local is_current_tab = mw.title.new(args['link']).fullText == mw.title.getCurrentTitle().fullText
	local links_class = (is_current_tab and 'wst-tab-links') or 'wst-tab-no-links'
	local bg_color = (is_current_tab and args['on tab color']) or args['off tab color']
	
	local width = 100 - 2 - (args.param_count - 1) * (args['tab spacing percent'] or 2)
	if width % args.param_count == 0 then
		width = width/args.param_count .. '%'
	else
		width = 'calc(' .. width .. '%/' .. args.param_count .. ')'
	end
	
	local main_td = mw.html.create('td')
		:addClass(table.concat({'wst-tab', nowrap_class, links_class}, ' '))
		:css({
			['text-align'] = args['tab alignment'],
			['border-top-left-radius'] = args.rounding,
			['border-top-right-radius'] = args.rounding,
			['border'] = args.border,
			['font-size'] = args['font-size'],
			['background-color'] = bg_color,
			['color'] = bg_color and 'inherit',
			['width'] = width
		})
		:allDone()
	
	if args['image'] then
		main_td:wikitext('[[File:' .. args.image .. '|20px|' .. args.tab .. '|link=' .. (args.link or '') .. ']] ')
	end
	
	if yesno(args['freeform']) or not args['link'] then
		main_td:wikitext(args.tab)
	else
		main_td:wikitext('[[' .. args.link .. '|' .. args.tab .. ']]')
	end
	
	table.insert(tds, main_td)
	
	return tds
end

local function _start_tab(args)
	-- get arguments
	local tab_data = {}
	
	-- numbered arguments
	for k, v in pairs(args) do
		local n = string.match(k, '%d+$')
		n = tonumber(n)
		
		if n then
			local key = string.gsub(k, '%-%d+$', '')
			tab_data[n] = tab_data[n] or {}
			tab_data[n][key] = v
		end
	end
	
	tab_data = TableTools.compressSparseArray(tab_data)
	
	local param_count = #tab_data
	local nowrap = yesno(args.nowrap)
	
	-- shared arguments
	args['tab spacing percent'] = tonumber(args['tab spacing percent'])
	
	for i = 1, param_count do
		tab_data[i]['param_count'] = param_count
		tab_data[i]['preums'] = i == 1
		tab_data[i]['rounding'] = args.rounding
		tab_data[i]['tab spacing percent'] = args['tab spacing percent']
		tab_data[i]['font-size'] = args['tab font-size']
		tab_data[i]['tab alignment'] = args['tab alignment']
		tab_data[i]['nowrap'] = nowrap
		tab_data[i]['off tab color'] = tab_data[i]['off tab color'] or args['off tab color']
		tab_data[i]['on tab color'] = tab_data[i]['on tab color'] or args['on tab color']
		
		mw.logObject(tab_data[i])
	end
	
	-- assemble table
	local tab_table = mw.html.create('table'):addClass('wst-start-tab'):allDone()
	local tab_row = tab_table:tag('tr')
	
	tab_row:node(_dummytab({['border'] = args.border, ['tab spacing percent'] = args['tab spacing percent']}))
	for i, data in ipairs(tab_data) do
		if data['tab'] then
			local tds = _tab(data, tab_row)
			tab_row:node(tds[1])
			tab_row:node(tds[2])
		end
	end
	tab_row:node(_dummytab({['border'] = args.border, ['tab spacing percent'] = args['tab spacing percent']}))
	
	-- frame
	local frame_open = ''
	if yesno(args.frame) then
		frame_open = mw.html.create('div')
			:addClass('wst-tab-content-frame ')
			:css({
				['border'] = args.border,
				['background-color'] = args['frame color'] or args['on tab color'],
				['color'] = (args['frame color'] or args['on tab color']) and 'inherit',
				['zoom'] = '1'
			})
			:tag('div')
			:allDone()
		
		frame_open = string.gsub(tostring(frame_open), '</div></div>$', '')
	end
	
	return tostring(tab_table) .. frame_open
end

function p.start_tab(frame)
	return _start_tab(getArgs(frame))
end

return p