Jump to content

Module:Mscore resizable

From Wikipedia

Documentation for this module may be created at Module:Mscore resizable/doc

function setdefaultcss( defaultwidth, size, step, attr )
	if defaultwidth >= size and defaultwidth < size + step then
		if attr == "visibility" then
			return "visible"
		else
			return "auto"
		end
	else
		if attr == "visibility" then
			return "hidden"
		else
			return "0px"
		end
	end
end

function genscoreset( frame, parsedargs, defaultwidth, minwidth, maxwidth, step )
	local div
	local result = ''
	for size = minwidth, maxwidth, step do
		parsedargs['width'] = size
		div = mw.html.create( 'div' )
		div
			:attr( "class", "score-resizable" )
			:css( "width", tostring( 4 * size + 15 ) .. "px" )
			:css( "visibility", setdefaultcss( defaultwidth, size, step, "visibility" ) )
			:css( "height", setdefaultcss( defaultwidth, size, step, "height" ) )
			:wikitext( mscore.genscore( frame, parsedargs ) )
		result = result .. tostring( div ) .. '\n'
	end
	return result
end

function genlist( frame )
	local parsedargs = { }
	local minwidth = 100
	local maxwidth = 400
	local defaultwidth = 180
	local step = 20
	local args = frame:getParent().args
	for k, v in pairs( args ) do
		if type( k ) == "number" then
			return error('unnamed template parameter encountered; maybe | was used instead of {{!}}')
		elseif type( k ) == "string" and k == "minwidth" and tonumber( args[k] ) > minwidth then
			if tonumber( args[k] ) < maxwidth then
				minwidth = tonumber( args[k] )
			else
				minwidth = maxwidth
			end
		elseif type( k ) == "string" and k == "maxwidth" and tonumber( args[k] ) > minwidth and tonumber( args[k] ) < maxwidth then
			maxwidth = tonumber( args[k] )
		else
			parsedargs[k] = v
		end
	end
	if (maxwidth - maxwidth) / 9 > 20 then
		step = toInt((maxwidth - maxwidth) / 9)
	end
	if defaultwidth < minwidth then
		defaultwidth = minwidth
	elseif defaultwidth > maxwidth then
		defaultwidth = maxwidth - step + 1
	end
	mscore = require( 'Module:Mscore' )
	local div = mw.html.create( 'div' )
		:attr( "class", "score-resizable-container" )
		:wikitext( genscoreset( frame, parsedargs, defaultwidth, minwidth, maxwidth, step ) )
	return tostring( div )
end

local p = {}
function p.genscorelist( frame )
	return genlist( frame )
end
return p