Aller au contenu

Module:Leader

Une page de Wikipédia, l'encyclopédie libre.

 Documentation[créer] [purger]
--
-- Ce module implémentera {{Leader}}
--
require('strict')
local getArgs = require('Module:Arguments').getArgs

local p = {}

local function read_team(args, round)
	local read_next = true
	local team =  {}
	local i = 1
	while read_next do
		local basename = 'c' .. tostring(i)
		local name = args[basename .. '-name']
		if name then 
			team[i] = {}
			team[i].name = name
			team[i].from = tonumber(args[basename .. '-from'] or '')
			team[i].to = tonumber(args[basename .. '-to'] or '')
			team[i].color = args[basename .. '-color'] or '#FFFFFF'
			if team[i].from and team[i].from > round then 
				team[i].from = round
				read_next = false
			end
			if team[i].to and team[i].to > round then 
				team[i].to = round
				read_next = false
			end
		else
			read_next = false
		end
		i = i + 1
	end
	if #team == 0 then return team end
	-- annuler les valeurs négatives
	for _,team in ipairs(team) do
		if team.from and (team.from < 0 or team.from > round) then team.from = nil end
		if team.to and (team.to < 0 or team.to > round) then team.to = nil end
	end
	-- normaliser et attribuer des valeurs par défaut si la valeur from ou to n'est pas attribuée
	team[1].from = team[1].from or 1
	for i = 2, #team do
		team[i].from = team[i].from or ((team[i-1].to or team[i-1].from) +1)
	end
	team[#team].to = team[#team].to or round
	for i = 1, #team-1 do
		team[i].to = team[i].to or (team[i+1].from -1)
	end 
	return team
end

function p.leader(frame)
	local args = getArgs(frame) 
	local round = tonumber(args.round) or 38 
	local team = read_team(args, round)
	--if #team == 0 then return '<span class="error">Pas de leader inséré</span>' end
	--if true then return mw.text.jsonEncode(team) end
	local div = mw.html.create('div'):css('overflow', 'auto')
	local int_table = div:tag('table'):cssText('font-size:80%; text-align:center; border-collapse:collapse;')
	
	-- première ligne de dimensionnement
	local first_row = int_table:tag('tr'):css('visibility', 'hidden')
	first_row:tag('td'):attr('rowspan', '5'):attr('width', '10px')
	first_row:tag('td'):wikitext('——'):css('height', '25px')
	for i = 2, round do first_row:tag('td'):wikitext('——') end
	first_row:tag('td'):attr('rowspan', '5'):attr('width', '1px')
	
	-- ligne pour les noms
	local name_row = int_table:tag('tr'):cssText('font-size:105%; vertical-align:bottom;')
	local last_pos = 0
	for _,team in ipairs(team) do
		if team.from > last_pos + 1 then
			name_row:tag('th'):attr('colspan', tostring(team.from - last_pos - 1))
		end
		name_row:tag('th'):attr('colspan', tostring(team.to - team.from + 1)):wikitext(team.name)
		last_pos = team.to
	end
	if last_pos < round then name_row:tag('th'):attr('colspan', tostring(round-last_pos)) end
	
	-- ligne pour les bandes colorées
	local color_row = int_table:tag('tr'):css('height', '15px')
	last_pos = 0
	for _,team in ipairs(team) do
		if team.from > last_pos + 1 then
			color_row:tag('td'):attr('colspan', tostring(team.from - last_pos - 1))
		end		
		color_row:tag('td')
			:attr('colspan', tostring(team.to - team.from + 1))
			:cssText('border-left:inset 1px white;')
			:css('background', team.color)
		last_pos = team.to
	end
	if last_pos < round then color_row:tag('td'):attr('colspan', tostring(round-last_pos)) end

	-- ligne par règle
	local ruler_row = int_table:tag('tr'):cssText('border-top:solid 1px black;')
	local ruler_style = 'border-left:solid 1px black;'
	ruler_row:tag('td'):cssText(ruler_style):attr('height', '1px'):wikitext(' ')
	for i = 2, round-1 do ruler_row:tag('td'):cssText(ruler_style):wikitext(' ') end
	ruler_row:tag('td'):cssText('border-left:solid 1px black; border-right:solid 1px black;'):wikitext(' ')

	-- ligne par leader
	local header_row = int_table:tag('tr')
	for i=1, round do header_row:tag('th'):wikitext(tostring(i) .. '') end

	-- retourner le résultat
	return tostring(div)
end

return p