Jump to content

Module:Medals table: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 134: Line 134:
:wikitext(nation)
:wikitext(nation)
:tag('td')
:tag('td')
:css('background-color', color)
:wikitext(gold)
:wikitext(gold)
:tag('td')
:tag('td')
:css('background-color', color)
:wikitext(silver)
:wikitext(silver)
:tag('td')
:tag('td')
:css('background-color', color)
:wikitext(bronze)
:wikitext(bronze)
:tag('td')
:tag('td')
:css('background-color', color)
:wikitext(total)
:wikitext(total)
end
end
Line 151: Line 147:
if limitReached then
if limitReached then
root:tag('tr')
root:tag('tr')
:css('font-style', 'italic')
:tag('td')
:tag('td')
:wikitext(remainingStart..'–'..remainingEnd)
:wikitext(remainingStart..'–'..remainingEnd)
:tag('td')
:tag('td')
:css('font-style', 'italic')
:css('text-align','left')
:css('text-align','left')
:wikitext(args['remaining_link'] or args['remaining_text'] or 'Remaining')
:wikitext(args['remaining_link'] or args['remaining_text'] or 'Remaining')

Revision as of 23:50, 19 September 2018

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs

local p = {}
function p.createTable(frame, args)
	if not args then
		args = getArgs(frame)
	end

    local team = args['team'] or 'NOC'
	local root = mw.html.create()
	local host = args['host']
	local hostColor = '#ccccff'
	local defaultRowColor = '#f8f9fa'
	local flagTemplate = args['flag_template'] or 'flagteam'
	local event = args['event']
	local totalGold = 0
	local totalSilver = 0
	local totalBronze = 0
	
	local remainingGold = 0
	local remainingSilver = 0
	local remainingBronze = 0
	local remainingStart = 0
	local remainingEnd = 0
	local limitReached = false
	local showLimit = tonumber(args['show_limit'])

	root = root
		:tag('table')
		:addClass('wikitable')
		:addClass('sortable')
		:addClass('plainrowheaders')
		:addClass('jquery-tablesorter')
		:css('text-align', 'center')
	
	root:tag('caption')
		:wikitext(args['caption'])
	-- add the header row
	root:tag('tr')
			:tag('th')
				:wikitext('Rank')
			:tag('th')
				:wikitext(team)
			:tag('th')
				:addClass('headerSort')
				:css('width', '6em')
				:css('background-color', 'gold')
				:wikitext('Gold')
			:tag('th')
				:addClass('headerSort')
				:css('width', '6em')
				:css('background-color', 'silver')
				:wikitext('Silver')
			:tag('th')
				:addClass('headerSort')
				:css('width', '6em')
				:css('background-color', '#c96')
				:wikitext('Bronze')
			:tag('th')
				:css('width', '6em')
				:wikitext('Total')
	
	-- enumerate the rows
	local rowNums = {}
	
	for k,v in pairs(args) do
		k = ''..k
		local nation = k:match('^gold_([A-Z][A-Z][A-Z])$') 
		if nation then
			local gold   = (tonumber(args['gold_' .. nation]) or 0)
			local silver = (tonumber(args['silver_' .. nation]) or 0)
			local bronze = (tonumber(args['bronze_' .. nation]) or 0)
			table.insert(rowNums, {gold, silver, bronze, nation}) 
		end
	end
	
	table.sort(rowNums, function (a, b) 
		return a[1] > b[1] or (a[1] == b[1] and a[2] > b[2]) or (a[1] == b[1] and a[2] == b[2] and a[3] > b[3]) end
	)
	
	local lastGold, lastSilver, lastBronze = -1
	
	local lastspan, lastrankcell = 1, nil
	for i, anum in ipairs(rowNums) do
		local IOC = anum[4]
		local name   = args['name_' .. IOC]
		local gold   = args['gold_' .. IOC] or 0
		local silver = args['silver_' .. IOC] or 0
		local bronze = args['bronze_' .. IOC] or 0
		local isHost = args['host_' .. IOC]
		-- this is mainly for the parameter names example so you can override it.
		local total  = args['total_' .. IOC] or gold + silver + bronze
		local color
		if isHost then color = hostColor else color = defaultRowColor end

		if args['grand_total'] then else
				totalGold = totalGold + gold
				totalSilver = totalSilver + silver
				totalBronze = totalBronze + bronze
		end
		local nation
		if name then
			nation = name
		else
			nation = mw.getCurrentFrame():expandTemplate({title = flagTemplate, args = { IOC , event } })
		end

		if showLimit and (i>showLimit) then 
			if remainingStart == 0 then remainingStart = i end
			limitReached = true
			remainingGold = remainingGold + gold
			remainingSilver = remainingSilver + silver
			remainingBronze = remainingBronze + bronze
		else
			local row = root:tag('tr')
			--Don't put the color on the row because of ranks spanning multiple rows. 
			--:css('background-color', color)

			-- if total == lasttotal then
			if (gold == lastGold) and (silver == lastSilver) and (bronze == lastBronze) then
				lastspan = lastspan + 1
				lastrankcell:attr('rowspan',lastspan)
			else
				lastspan = 1
				lastrankcell = row:tag('td'):wikitext(i)
				lastGold   = gold
				lastSilver = silver
				lastBronze = bronze
			end
			row:tag('td')
					:css('background-color', color)
					:css('text-align','left')
					:wikitext(nation)
				:tag('td')
					:wikitext(gold)
				:tag('td')
					:wikitext(silver)
				:tag('td')
					:wikitext(bronze)
				:tag('td')
					:wikitext(total)
		end
		remainingEnd = i
	end
	
	if limitReached then
		root:tag('tr')
				:tag('td')
					:wikitext(remainingStart..'–'..remainingEnd)
				:tag('td')
					:css('font-style', 'italic')
					:css('text-align','left')
					:wikitext(args['remaining_link'] or args['remaining_text'] or 'Remaining')
				:tag('td')
					:wikitext(remainingGold)
				:tag('td')
					:wikitext(remainingSilver)
				:tag('td')
					:wikitext(remainingBronze)
				:tag('td')
					:wikitext(remainingGold+remainingSilver+remainingBronze)
	end

	root:tag('tr')
		:addClass('sortbottom')
		:tag('th')
			:wikitext('Totals ('..remainingEnd..' '..team..'s)')
			:attr('colspan',2)
		:tag('th')
			:wikitext(args['total_gold'] or totalGold)
		:tag('th')
			:wikitext(args['total_silver'] or totalSilver)
		:tag('th')
			:wikitext(args['total_bronze'] or totalBronze)
		:tag('th')
			:wikitext(args['grand_total'] or totalGold+totalSilver+totalBronze)

	if host then
		root
			:tag('p')
				:wikitext(host)
				:tag('span')
					:css('background-color', hostColor)
					:css('text-align', 'center')
					:css('padding', '2px')
					:css('margin', '2px')
					:css('border', '1px solid darkgray')
					:css('height', '1em')
					:wikitext('   ')
	end

	return tostring(root)
end
return p