Modul:PoliticalComposition

Documentația acestui modul poate fi creată la Modul:PoliticalComposition/doc

local p = {}
local partyData = mw.loadData('Modul:Polparty/countrydata')
local polParty = require('Modul:Polparty')
local TableTools = require('Modul:TableTools')
local getArgs = require('Modul:Arguments').getArgs
local illWd = require('Modul:Ill-wd').fromArgs

local function displayTable(labels, country, composition)
	table.sort(composition, function(c1, c2)
		return c1.mandates > c2.mandates
	end)
	
	if #composition < 1 then
		return ''
	end
	
	local mainTable = mw.html.create('table')
		:attr('cellpadding', '2')
		:attr('cellspacing', '0')
		:addClass('wikitable')
		:tag('tr')
			:tag('th'):css('background', 'var(--background-color-neutral,#eaecf0)'):css('color', 'var(--color-base, #000)'):wikitext(mw.text.decode('&nbsp;&nbsp;&nbsp;')):done()
			:tag('th'):css('background', 'var(--background-color-neutral,#eaecf0)'):css('color', 'var(--color-base, #000)'):wikitext(labels.party):done()
			:tag('th'):css('background', 'var(--background-color-neutral,#eaecf0)'):css('color', 'var(--color-base, #000)'):wikitext(labels.mandates):done()
			:tag('th'):css('background', 'var(--background-color-neutral,#eaecf0)'):css('color', 'var(--color-base, #000)'):attr('colspan', tostring(composition[1].mandates)):wikitext(labels.composition):allDone()
	
	for _,eachComp in ipairs(composition) do
		local crtPartyData = partyData[country or 'România'][eachComp.abbr]
		local crtPartyColor = crtPartyData and crtPartyData.color or '888'
		local crtPartyLabel
		if crtPartyData and crtPartyData.link then
			if eachComp.fullName then
				crtPartyLabel = '[[' .. crtPartyData.link .. '|' .. eachComp.fullName .. ']]'
			else
				crtPartyLabel = polParty.fromArgs(country, eachComp.abbr, 'fns')
			end
		elseif crtPartyData and crtPartyData.q then
			crtPartyLabel = illWd(crtPartyData.q, eachComp.fullName or (crtPartyData and crtPartyData.nom))
		else
			crtPartyLabel = eachComp.fullName or (crtPartyData and crtPartyData.nom)
		end
		
		local crtRow = mainTable:tag('tr')
			:tag('td'):attr('bgcolor', '#' .. crtPartyColor):done()
			:tag('td'):wikitext(crtPartyLabel):done()
			:tag('td'):css('text-align', 'right'):wikitext("'''", tostring(eachComp.mandates), "'''"):done()
			
		for colCellIdx = 1,eachComp.mandates do
			crtRow:tag('td'):attr('bgcolor', '#' .. crtPartyColor):wikitext(mw.text.decode('&nbsp;'))
		end
		for colCellIdx = (eachComp.mandates + 1),composition[1].mandates do
			crtRow:tag('td'):wikitext(mw.text.decode('&nbsp;'))
		end
	end
	
	return tostring(mainTable)
end

local function displayFromFrame(frame)
	local args = getArgs(frame)
	local labels = {}
	labels.party = args['eticheta_partid'] or 'Partid'
	labels.mandates = args['eticheta_mandate'] or 'Mandate'
	labels.composition = args['eticheta_compoziție'] or 'Componență'
	
	local country = args['țară'] or args['country'] or 'România'
	
	local composition = {}
	for _,eachPartyData in ipairs(TableTools.numData(args)) do
		local party = {}
		party.fullName = eachPartyData['nume_complet']
		party.abbr = eachPartyData['nume_scurt']
		if mw.ustring.match(eachPartyData['mandate'], '%d+') then
			party.mandates = tonumber(eachPartyData['mandate'])
		else
			party.mandates = 'EROARE: mandatele se exprimă în numere'
		end
		table.insert(composition, party)
	end
	
	return displayTable(labels, country, composition)
end

p.displayFromFrame = displayFromFrame
return p