Moduł:Lepsze główne

Z Nonsensopedii, polskiej encyklopedii humoru

Moduł automatycznie generujący semi-responsywny kolumnowy układ stron głównych. Zobacz też: Szablon:Generator głównych


local tools = require('Moduł:Narzędzia')
local p = {}

function sanitizeOrElse(text, alt)
	if text ~= nil then
		return mw.text.nowiki(text)
	else
		return alt
	end
end

function allowOnly(value, allowed, alt) 
	local sanitized = mw.text.trim(sanitizeOrElse(value, alt))
	for k, v in ipairs(allowed) do
		if v == sanitized then
			return sanitized
		end
	end
	return alt
end

function makeShowStatus(value)
	local final = allowOnly(value, {"desktoponly", "mobileonly"}, '')
	if final == "desktoponly" then
		return {desktoponly=true, mobileonly=false, duplicateDesktoponly=false, duplicateMobileonly=false}
	elseif final == "mobileonly" then
		return {desktoponly=false, mobileonly=true, duplicateDesktoponly=false, duplicateMobileonly=false}
	else
		return {desktoponly=false, mobileonly=false, duplicateDesktoponly=false, duplicateMobileonly=false}
	end
end

function showStatusToString(stat)
	local val = ""
	if stat["desktoponly"] or stat["duplicateDesktoponly"] then
		val = val .. "desktoponly "
	end
	if stat["mobileonly"] or stat["duplicateMobileonly"] then
		val = val .. "mobileonly"
	end
	return mw.text.trim(val)
end

function getShowStatusForPlatform(stat, platform)
	if platform == "desktop" then
		return stat.desktoponly or stat.duplicateDesktoponly
	elseif platform == "mobile" then
		return stat.mobileonly or stat.duplicateMobileonly
	end
end

function willEverBeUsed(cur)
	return not ((getShowStatusForPlatform(cur.showstatus, "desktop") == getShowStatusForPlatform(cur.showstatus, "mobile")) and (getShowStatusForPlatform(cur.showstatus, "desktop") == true))
end

function makeElementTable(args, num)
	local cur = {}
	if args['ptytułszablon' .. num] ~= nil then
		cur['title'] = '{{' .. mw.text.nowiki(args['ptytułszablon' .. num]) .. '}}'
	else
		cur['title'] = sanitizeOrElse(args['ptytuł' .. num], 'Nie podano tytułu')
	end
	cur['text'] = sanitizeOrElse(args['panel' .. num], '')
	cur['num'] = num
	cur['showstatus'] = makeShowStatus(args["pwarunki" .. num])
	cur['icon'] = sanitizeOrElse(args['pikona' .. num], '')
	cur['iconlink'] = sanitizeOrElse(args["pikonalink" .. num], '')
	cur['titlelink'] = sanitizeOrElse(args["plink" .. num], '')
	return cur
end

-- robi listę elementów wg kolumn, ignoruje - i puste
function makeElementList(args)
	-- kolumny
	local first = {}
	local second = {}
	local curLeft, curRight = 0, 0
	for i = 1, 50 do
		if args['panel' .. i] ~= nil then
			if args['panel' .. i] == '-' or args['panel' .. i] == '' then else
				local cur = makeElementTable(args, i)
				if i % 2 == 1 then
					table.insert(first, cur)
					curLeft, curRight = 0, 0
				else
					cur.showstatus.duplicateDesktoponly = true
					if willEverBeUsed(cur) then
						table.insert(second, cur)
						curRight = curRight + 1
					end
					local cur2 = makeElementTable(args, i)
					cur2.showstatus.duplicateMobileonly = true
					if willEverBeUsed(cur2) then
						table.insert(first, cur2)
						curLeft = curLeft + 1
					end
				end
			end
		else break end
	end
	if curRight > 0 then
		local maxSecond = table.maxn(second)
		for i = 0, (curRight - 1) do
			second[maxSecond-i].showstatus.duplicateDesktoponly = false
		end
	end
	if curLeft > 0 then
		local maxFirst = table.maxn(first)
		for i = 0, (curLeft - 1) do
			table.remove(first, maxFirst-i)
		end
	end
	return first, second
end


function makePanel(tab)
	local panelname = tab["title"]
	local titlelink = tab["titlelink"]
	local template = tab["text"]
	local mobiledesktop = showStatusToString(tab["showstatus"])
	local icon = tab["icon"]
	local iconlink = tab["iconlink"]
	local id = "panel-" .. tab["num"] .. "-" .. mw.text.encode(panelname)
	local wikitext = "{{User:Polskacafe/panel|" .. panelname .. "|{{" .. template .. "}}|ikona=" .. icon .. "|link=" .. iconlink .. "|tytuł link=" .. titlelink .. "}}"
	local panel = mw.html.create("div")
		:addClass("glowna-panel")
		:attr("id", id)
		:wikitext(wikitext)
	if (mobiledesktop ~= nil) then
		panel = panel:addClass(mobiledesktop)
	end
	return panel:done()
end
		

function makeColumn(elements)
	column = mw.html.create("div")
		:addClass("glowna-kolumna")
	for k,v in ipairs(elements) do
		local elem = makePanel(v)
		column = column:node(elem)
	end
	return column:done()
end

function makeAll(args)
	local first, second = makeElementList(args)
	local mainList = mw.html.create("div")
		:addClass("glowna-lista")
		:node(makeColumn(first))
		:node(makeColumn(second))
		:done()
	return mainList
end

function p.kolumny(frame)
	local args = tools.getArgs(frame) -- argumenty szablonu to jest
	local mainList = makeAll(args)
	return frame:preprocess(tostring(mainList))
end

return p