Module:Select

From The Wiki Camp 2
Jump to navigation Jump to search

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

local p = {}

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

local function addClasses(out, id, arg, i)
	out[#out + 1] = "mw-customtoggle-_"
	out[#out + 1] = id
	out[#out + 1] = i
	if arg ~= "" then
		out[#out + 1] = " "
		out[#out + 1] = arg
	end
end

function p.single(frame)
	local args = getArgs(frame)
	local argsmax = 0
	for k, v in pairs(args) do if type(k) == "number" and k > argsmax then argsmax = k end end
	if argsmax == 0 then return "{{:Damn seal}}" end
	local id = args[1]
	local final = math.floor((argsmax - 2) / 3)
	n = argsmax - final * 3
	if n == 2 then
		args[argsmax + 1] = ""
		args[argsmax + 2] = ""
	elseif n == 3 then
		args[argsmax + 1] = ""
	end
	local out = {}
	n = 3
	local s
	for i = 0, final do
		out[#out + 1] = "<div class=\"selection mw-collapsible"
		if i ~= 0 then out[#out + 1] = " mw-collapsed" end
		out[#out + 1] = "\" id=\"mw-customcollapsible-_"
		out[#out + 1] = id
		out[#out + 1] = i
		out[#out + 1] = "\"><ul class=\"selection\">"
		s = args[n + 1]
		for j = 0, final do
			if i == j then
				out[#out + 1] = "<li class=\"selected\">"
			else
				out[#out + 1] = "<li class=\""
				addClasses(out, id, s, i)
				out[#out + 1] = " "
				addClasses(out, id, args[j * 3 + 4], j)
				out[#out + 1] = "\">"
			end
			out[#out + 1] = args[j * 3 + 2]
			out[#out + 1] = "</li>"
		end
		out[#out + 1] = "</ul>"
		s = args[n]
		if s ~= "" then out[#out + 1] = s end
		out[#out + 1] = "</div>"
		n = n + 3
	end
	return table.concat(out)
end

return p