Jump to content

Module:Overlay

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 14:33, 16 September 2016 (add partial support for numbers greater than 30). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- this module implements [[Template:Overlay]]
local p = {}

local mArguments = require('Module:Arguments')

local function buildimage(n, lk, f, c, t)
	local res = ''
	if (f == 'color' or f == 'colour') then
		res = '<span title="' .. t .. 'style="background-color:' .. c .. '">&nbsp;&nbsp;</span>'
		if( lk ~= '' ) then
			res = '[[' .. lk .. '|' .. res .. ']]'
		end
	elseif (f == 'text') then
		res = '<span title="' .. t .. 'style="font-weight:bold; font-size:85%; color:' .. c .. '">' .. n .. '</span>'
		if( link ~= '' ) then
			res = '[[' .. lk .. '|' .. res .. ']]'
		end
	else
		t = (lk ~= '') and ('link=' .. lk .. '|alt=' .. lk .. '|' .. t) or t
		if n < 31 then
			res = '[[File:' .. n .. ' white, ' .. c .. ' rounded rectangle.svg|50x' .. ((n < 10) and 13 or 12) .. 'px|' .. t .. ']]'
		else
			local div = mw.html.create('div')
				:css('display', 'inline-block')
				:css('width', 'auto')
				:css('height', 'auto')
				:css('text-align', 'center')
				:css('vertical-align', 'middle')
				:css('font-size', '85%')
				:css('margin', '2px')
				:css('-moz-border-radius', '3px')
				:css('-webkit-border-radius', '3px')
				:css('border-radius', '3px')
				:css('background-color', c)
				:tag('span')
				:css('color', 'white')
				:css('font-weight', 'bold')
				:attr('title', t)
			res = tostring(div)
		end
	end
	return res
end

local function buildlegend(data, cols, border)
	local root = mw.html.create('table')
		:css('width', '100%')
		:css('border', (border ~= 'no') and '1px #ccc solid' or '')
	local outerrow = root:tag('tr')
	local percol = math.ceil(#data / cols)
	local k = 0
	for j=1,cols do
		local colcell = outerrow:tag('td')
			:css('width', (math.floor(10/cols)/10) .. '%')
			:css('vertical-align', 'top')
		local coltable = colcell:tag('table')
			:css('width', '100%')
			:css('font-size', '85%')
			:css('line-height', '95%')
		for l = 1,percol do
			k = k + 1
			if k <= #data then
				local rdata = data[k]
				local tr = coltable:tag('tr'):css('vertical-align','top')
				tr:tag('td')
					:css('width', '12px')
					:css('text-align', 'right')
					:css('padding-bottom', '2px')
					:wikitext(rdata[1] or '')
				tr:tag('td')
					:css('padding-bottom', '2px')
					:wikitext(rdata[2] or '')
			end
		end
	end
	return tostring(root)
end

local function buildlegenditem(im, lk, t)
	local res = {im, ''}
	if t ~= '' then
		if lk ~= '' then
			res[2] = '[[' .. lk .. '|' .. t .. ']]'
		else
			res[2] = t
		end
	else
		res[2] = '[[' .. lk .. ']]'
	end
	return res 
end

function p.main(frame)
	local args = mArguments.getArgs(frame)
	
	local image = args['image'] or ''
	local width = tonumber(args['width'] or '500') or 500
	local height = tonumber(args['height'] or '500') or 500
	local columns = tonumber(args['columns'] or '3') or 3
	local grid = ((args['grid'] or ''):lower() == 'yes') and 'yes' or 'no'
	local legendbox = ((args['legendbox'] or ''):lower() == 'no') and 'no' or 'yes'
	local overlay = (image == '') and 'no' or ( ((args['overlay'] or ''):lower() == 'no') and 'no' or 'yes' )
	local float = args['float'] or 'center'
	local border = ((args['border'] or ''):lower() == 'no') and 'no' or 'yes'

	-- create the root table
	local root = mw.html.create('table')
	if float == 'center' or float == 'centre' then
		root:css('margin-left', 'auto')
			:css('margin-right', 'auto')
	elseif float == 'right' then
		root:css('float', 'right')
			:css('clear', 'right')
			:css('margin-left', '1em')
	elseif float == 'left' then
		root:css('float', 'left')
			:css('clear', 'left')
			:css('margin-right', '1em')
	else
		root:css('float', float)
	end
	if border == 'yes' then
		root:css('border', '1px #ccc solid')
	end
	local legenditems = {}
	for k=1,50 do
		table.insert(legenditems, '')
	end
	if image ~= '' then
		local cell = root:tag('tr'):tag('td')
		cell:attr('align', 'center')
		if( columns > 1 and legendbox == 'yes' ) then
			cell:attr('colspan', columns)
		end
		local imagediv = cell:tag('div')
		imagediv:css('position','relative')
			:css('left', '0px')
			:css('top', '0px')
			:css('width', ((grid == 'yes') and 940 or width) .. 'px')
			:css('height', ((grid == 'yes') and 940 or height) .. 'px')
		if grid == 'yes' then
			imagediv:tag('span')
				:css('position', 'absolute')
				:css('left', '0px')
				:css('top', '0px')
				:css('z-index', '2')
				:wikitext('[[File:Grid 99, 100 int red 50 int yellow (940).svg|940px]]')
		end
		imagediv:tag('span')
			:css('position', 'absolute')
			:css('left', '0px')
			:css('top', '0px')
			:css('z-index', '0')
			:css('width', width .. 'px')
			:css('height', height .. 'px')
			:wikitext('[[File:' .. image .. '|' .. width .. 'x' .. height .. 'px]]')
		local colori = args['color'] or args['colour'] or 'red'
		local formi = ''
		for i=1,50 do
			formi = args['overlay' .. i .. 'form'] or formi
			colori = args['overlay' .. i .. 'color'] or args['overlay' .. i .. 'colour'] or colori
			local linki = args['overlay' .. i .. 'link'] or ''
			local tipi = args['overlay' .. i .. 'tip'] or args['overlay' .. i] or ''
			local overlayi = args['overlay' .. i] or args['overlay' .. i .. 'tip'] or ''
			local imagei = ''
			if formi == 'text' then
				if overlayi ~= '' then
					imagei = '<span style="font-weight:bold; color:' .. colori .. '">' .. i .. '</span> '
					legenditems[i] = buildlegenditem(imagei, '', overlayi)
				end
			elseif formi == 'color' or formi == 'colour' then
				if overlayi ~= '' then
					imagei = '<span style="background-color:' .. colori .. '">&#160;&#160;</span> '
					legenditems[i] = buildlegenditem(imagei, '', overlayi)
				end
			else
				imagei = (overlayi ~= '' or tipi ~= '') 
					and buildimage(i, linki, formi, colori, tipi) or ''
				if imagei ~= '' then
					legenditems[i] = buildlegenditem(imagei, args['overlay' .. i .. 'link'] or '', overlayi)
				end
			end
			if overlay == 'yes' then
				if args['overlay' .. i .. 'left'] then
					imagediv:tag('span')
							:css('position', 'absolute')
							:css('left', (args['overlay' .. i .. 'left'] or '') .. 'px')
							:css('top', (args['overlay' .. i .. 'top'] or '') .. 'px')
							:css('z-index', '1')
							:wikitext(imagei)
				end
				for j =1,3 do
					if args['overlay' .. i .. 'left' .. j] then
						imagediv:tag('span')
							:css('position', 'absolute')
							:css('left', (args['overlay' .. i .. 'left' .. j] or '') .. 'px')
							:css('top', (args['overlay' .. i .. 'top' .. j] or '') .. 'px')
							:css('z-index', '1')
							:wikitext(imagei)
					end
				end
			end
		end
	end
	while legenditems[#legenditems] == '' do
		table.remove(legenditems,#legenditems)
	end
	-- Legend
	legend = {{}, {}, {}, {}, {}}
	legendnums = {}
	for i=1,#legenditems do
		table.insert(legendnums, 0)
	end
	for i=1,5 do
		if args['legend' .. i .. 'start'] then
			k1 = tonumber(args['legend' .. i .. 'start'] or 1) or #legenditems
			k2 = #legenditems
			if args['legend' .. i .. 'end'] then
				k2 = tonumber(args['legend' .. i .. 'end']) or k2
			elseif args['legend' .. (i+1) .. 'start'] then
				k2 = (tonumber(args['legend' .. (i+1) .. 'start']) or k2) - 1
			end
			k1 = (k1 < 1) and 1 or k1
			k2 = (k2 > #legenditems) and #legenditems or k2
			for k=k1,k2 do
				table.insert(legend[i], legenditems[k])
				legendnums[k] = 1
			end
		end
	end
	for k = 1,#legendnums do
		if legendnums[k] == 0 then
			table.insert(legend[1], legenditems[k])
		end
	end
	if columns > 0 then
		for i = 1,5 do
			local locallegend = legend[i]
			if (locallegend and #locallegend > 0) then
				local cell = root:tag('tr'):tag('td')
				if args['legend' .. i .. 'title'] then
					cell:wikitext(args['legend' .. i .. 'title'])
				end
				cell:wikitext(buildlegend(locallegend, columns, border))
			end
		end
	end -- end if columns > 0
	return tostring(root)
end

return p