Jump to content

Module:Sidebar games events

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 16:01, 8 September 2017 (start a new module based on code Module:Sidebar, lots of bugs, will require more work). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

--
-- This module implements {{Sidebar games events}}
-- This module was created using code taken directly from [[Module:Sidebar]] 
--
require('Module:No globals')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local navbar = require('Module:Navbar')._navbar

local function trimAndAddAutomaticNewline(s)
	-- For compatibility with the original {{sidebar with collapsible lists}}
	-- implementation, which passed some parameters through {{#if}} to trim
	-- their whitespace. This also triggered the automatic newline behavior.
	-- ([[meta:Help:Newlines and spaces#Automatic newline]])
	s = mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1")
	if mw.ustring.find(s, '^[#*:;]') or mw.ustring.find(s, '^{|') then
		return '\n' .. s
	else
		return s
	end
end

function p.sidebar(frame, args)
	if not args then
		args = getArgs(frame)
	end
	
	local root = mw.html.create()

	root = root
		:tag('table')
		:addClass('vertical-navbox')
		:addClass('nowraplinks')
		:css('float', args.float or 'right')
		:css('clear', (args.float == 'none' and 'both') or args.float or 'right')
		:css('width', args.width or '22.0em')
		:css('margin', args.float == 'left' and '0 1.0em 1.0em 0' or '0 0 1.0em 1.0em')
		:css('background', '#f5faff')
		:css('border', '1px solid #B8C7D9')
		:css('padding', '0.2em')
		:css('border-spacing', '0.4em 0')
		:css('text-align', 'center')
		:css('line-height', '1.4em')
		:css('font-size', '88%')

	-- enumerate the rows and count the columns
	local cols = 1
	local colindex = {a = '2', b = '3', c = '4'}
	local lets = {'a', 'b', 'c'}
	local rowNums = {}
	local subEvents = 0

	for k, v in pairs(args) do
		k = '' .. k
		local num = k:match('^event(%d+)$') 
			or k:match('^image(%d+)$')
			or k:match('^type(%d+)[a-c]$')
			or k:match('^event(%d+)%.%d+$')
			or k:match('^results(%d+)%.%d+[a-c]$')
		if num then table.insert(rowNums, tonumber(num)) end
		
		local let = k:match('^results%d+%.%d+([a-c])$')
			or k:match('^type%d+([a-c])$')
		if let and colindex[let] then
			local n = tonumber(colindex[let])
			cols = (n > cols) and n or cols
		end
		
		local subnum = k:match('^results%d+%.(%d+)[a-c]$')
			or k:match('^event%d+%.(%d+)$')
		if subnum then
			subnum = tonumber(subnum)
			subevents = (subnum > subevents) and subnum or subevents
		end
	end
	-- remove duplicates from the list (e.g. 3 will be duplicated if both event3 and image3 are specified)
	table.sort(rowNums)
	for i = #rowNums, 1, -1 do
		if rowNums[i] == rowNums[i - 1] then
			table.remove(rowNums, i)
		end
	end

	-- add the top level header	
	if args.event then
		local t = args.event
		if args.games then
			t = '[[' .. args.event .. ' at the ' .. args.games .. '|' .. args.event .. ']]'
			t = t .. ' at the ' .. '[[' .. args.games .. ']]'
		end
		root
			:tag('tr')
				:tag('th')
					:css('padding', '0.2em 0.4em 0.2em')
					:css('font-size', '145%')
					:css('line-height', '1.2em')
					:css('background', '#cedff2')
					:attr('colspan', cols)
					:wikitext(t)
	end

	if args.image then
		local imageCell = root:tag('tr'):tag('td')

		imageCell
			:css('padding', '0.2em 0 0.4em')
			:attr('colspan', cols)
			:wikitext(args.image)

		if args.caption then
			imageCell
				:tag('div')
					:css('padding-top', '0.2em')
					:css('line-height', '1.2em')
					:wikitext(args.caption)
		end
	end

	-- start adding rows
	for i, num in ipairs(rowNums) do
		local heading = nil
		local event = args['event' .. num]
		local image = args['image' .. num]
		if event and image then
			heading = event .. '<br>' .. image
		elseif event then
			heading = event
		elseif image then
			heading = image
		end
		
		if heading then
			root
				:tag('tr')
					:tag('th')
						:css('padding', '0.1em')
						:css('background', '#deeaf6')
						:attr('colspan', cols)
						:wikitext(heading)
		end

		local showtypes = false
		for j, let in ipairs(lets) do
			if j <= cols then
				if args['type' .. num .. let] then
					showtypes = true
				end
			end
		end
		if showtypes == true then
			local row = root:tag('tr')
			row:tag('th')
			for j, let in ipairs(lets) do
				if j <= cols then
					local t = args['type' .. num .. let]
					local cell = row:tag('th')
					if t then
						cell
							:css('background', '#deeaf6')
							:wikitext(t)
					end
				end
			end
		end

		for k=1,subevents do
			local hasresults = false
			for j, let in ipairs(lets) do
				if j <= cols then
					if args['results' .. num .. let] then
						hasresults = true
					end
				end
			end
	
			if hasresults then
				local row = root:tag('tr')
				local cell = row:tag('th')
				local t = args['event' .. num .. '.' .. k]
				cell:wikitext(t)
				for j, let in ipairs(lets) do
					if j <= cols then
						t = args['results' .. num .. '.' .. k .. let]
						cell = row:tag('td'):wikitext(t)
					end
				end
			end
		end
	end

	local navbarArg = args.navbar or args.tnavbar
	if navbarArg ~= 'none' and navbarArg ~= 'off' and (args.name or frame:getParent():getTitle():gsub('/sandbox$', '') ~= 'Template:Sidebar') then
		root
			:tag('tr')
				:tag('td')
					:css('text-align', 'right')
					:css('font-size', '115%')
					:attr('colspan', cols)
					:wikitext(navbar{
						args.name,
						mini = 1,
						fontstyle = args.navbarfontstyle or args.tnavbarfontstyle
					})
	end

	return tostring(root)
end

return p