Advertisement

Module:Layer

Edit documentation

Description

This module is the basis for the template Layered images.

See also


local p = {}
local lib = require("Module:Shared")

function p.main(frame)
	local args = lib.getArgs(frame)
	local bg = args.background_image
	local bg_size = args.background_image_size or '250px'
	local files = lib.split(args.icon_files, ';')
	local coords = lib.split(args.icon_coordinates, ';')
	local sizes = lib.split(args.icon_size, ';') or {'32px'}
	local tt = lib.split(args.icon_texttip, ';')
	local texts = lib.split(args.text, ';')
	local txt_coords = lib.split(args.text_coordinates, ';')
	local txt_sizes = lib.split(args.text_size, ';') or {'32px'}
	local ttt = lib.split(args.text_texttip, ';')
	
	if not bg then return "Background image not specified!" end
	if files and #files ~= #coords or (#sizes ~= 1 and #sizes ~= #files) then return "Icon coordinates or sizes missing!" end
	if texts and #texts ~= #txt_coords or (#txt_sizes ~= 1 and #txt_sizes ~= #texts) then return "Text coordinates or sizes missing!" end
	
	local img = mw.html.create('div'):cssText('position:relative;width:'..bg_size):wikitext(lib.format('[[File:%s|%s|link=]]', bg, bg_size))
	
	for k,v in ipairs(files or {}) do
		local c = lib.split(coords[k], ',')
		if not c or not c[1] or not c[2] then return "Icon coordinates error" end
		img
			:tag('span')
			:cssText(lib.format('%sposition:absolute;left:%s%%;bottom:%s%%', (tt and tt[k] and tt[k] ~= '') and 'cursor:help;' or '', c[1], c[2]))
			:attr('title', tt and (tt[k] or '') or '')
			:wikitext(lib.format('[[File:%s|%s|link=]]', files[k], sizes[k] or sizes[1]))
			:done()
	end
	for k,v in ipairs(texts or {}) do
		local c = lib.split(txt_coords[k], ',')
		if not c or not c[1] or not c[2] then return "Text coordinates error" end
		img
			:tag('span')
			:cssText(lib.format('%sposition:absolute;left:%s%%;bottom:%s%%;font-size:%s;line-height:1', (ttt and ttt[k] and ttt[k] ~= '') and 'cursor:help;' or '', c[1], c[2], txt_sizes[k] or txt_sizes[1]))
			:attr('title', ttt and (ttt[k] or '') or '')
			:wikitext(v)
			:done()
	end
	
	return img:allDone()
end

return p
-- [[Category:Modules]]