Jump to content

Module:Seats diagram

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Julio974fr (talk | contribs) at 08:21, 20 September 2023 (some progress). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('strict')
local p = {}
local getArgs = require('Module:Arguments').getArgs
local errorCategory = '[[Category:Compilation error of the Seats diagram template]]'
local getColor = require('Module:Political party').fetch

-- get the party color from Module:Party color
-- a = party; b = optional color
-- (copied from Module:Parliament_diagram)
local function color(a,b)
	local c = '#CCC'
	if (b) then
		c = b
	else
		c = getColor({a, "color"})
	end
	if string.sub(c,1,5) == '#' then c = "#" .. string.sub(c, 6, 11) end
	return c
end



local function getData(args)
	local totalSeats = 0
	local data = {}
	local num = 1
	
	while (args['n'..num]) do
		if (tonumber(args['n'..num])) then
			data[num] = {
				n = args['n'..num] and tonumber(args['n'..num]),
				c = color(args['p'..num] or '', args['c'..num]),
				b = args['b'..num] or '-',
				p = args['p'..num] or 'Serie '..num
			}
			totalSeats = totalSeats + data[num].n
			num = num+1
		else
			error(string.format('Invalid value for n%d', num),2)
		end
	end
	
	data['totalSeats'] = totalSeats
	return data
end

local function generateDiagram(root, args)
	local totalSeats = 0
	local data = getData(args)
	local width = args.width or 240
	local range = root
	
	local currentRotation = 0
	
	mw.logObject(data)
	for i, v in ipairs(data) do
		range = range:tag('div')
		range:css('transform', 'rotate('..tostring(180 * currentRotation)..'deg)')
		range:css('width', tostring(width)..'px')
		range:css('height', tostring(width/2)..'px')
		range:css('position', 'relative')
		range:css('transform-origin', 'bottom center')
		range:css('border-top-left-radius', tostring(width/2)..'px')
		range:css('border-top-right-radius', tostring(width/2)..'px')
		range:css('background', v.c)
		
		currentRotation = currentRotation + v.n / data.totalSeats
	end
	
	root:tag('div') --inner circle
	    :css('width', tostring(width*3/8)..'px')
	    :css('height', tostring(width*3/8)..'px')
	    :css('margin', tostring(-width*3/16)..'px auto')
	    :css('border-radius', tostring(width*3/16)..'px')
	    :css('transform', 'translate(0,0)')
	    :css('background', 'white')
	    :done()
	    :tag('div') --cover below
	    :css('width', tostring(width)..'px')
	    :css('height', tostring(width*3/8)..'px')
	    :css('transform', 'translate(0,0)')
	    :css('background', 'white')
	    :done()
	
	return root
end

local function parliamentFloat(root, args)
	local width = args.width or 240
	local mainDiv = root:tag('div')
	
	mainDiv:addClass('thumb')
	mainDiv:addClass('t'..args['float'])
	
	local divThumbinner = mainDiv:tag('div')
	divThumbinner:addClass('thumbinner')
	divThumbinner:css('width', tostring(width*1.125)..'px')
	divThumbinner:css('padding-right', '5px !important')
	
	if args['title'] then
		local divTitle = divThumbinner:tag('div')
		divTitle:addClass('thumbdescription')
		divTitle:css('font-weight', 'bold')
		divTitle:css('text-align', 'center')
		divTitle:css('font-size', '100%')
		divTitle:wikitext(args['title'])
	end
	
	local divThumbimage = divThumbinner:tag('div')
	divThumbimage:addClass('thumbimage')
	divThumbimage:addClass('diagram')
	divThumbimage:addClass('noprint')
	divThumbimage:css('width', tostring(width)..'px')
	divThumbimage:css('height', tostring(width/2)..'px')
	divThumbimage:css('padding', tostring(width/15)..'px')
	divThumbimage:css('overflow', 'hidden')
	divThumbimage:css('background', 'white')
	
	generateDiagram(divThumbimage, args)
	
	local divThumbdescription = divThumbinner:tag('div')
	if args.image_legend then
		divThumbdescription:wikitext(image_legend)
	else
		-- @todo Generate legend
	end
	
	return root
end

local function parliamentNonFloat(root, args)
	local width = args.width or 240
	local mainDiv = root:tag('div')
	
	local divCentered = mainDiv:tag('div')
	divCentered:addClass('centered')
	divCentered:addClass('diagram')
	divCentered:addClass('noprint')
	divCentered:css('width', tostring(width)..'px')
	divCentered:css('height', tostring(width/2)..'px')
	divCentered:css('padding', tostring(width/15)..'px')
	divCentered:css('overflow', 'hidden')
	divCentered:css('background', 'white')
	
	divCentered:css('text-align', 'center')
	
	generateDiagram(divCentered, args)
	
	return root
end

function p._parliament(args)
	local root = mw.html.create('div')
	
	if args['float'] then
		return parliamentFloat(root, args)
	else
		return parliamentNonFloat(root, args)
	end
end

function p.parliament(frame)
	return p._parliament(getArgs(frame))
end

return p