Moduł:Lepsze główne: Różnice pomiędzy wersjami
Z Nonsensopedii, polskiej encyklopedii humoru
Polskacafe (dyskusja • edycje) M (obsługa linku) |
Polskacafe (dyskusja • edycje) M (yyy naprawiony bug chyba) |
||
Linia 23: | Linia 23: | ||
local final = allowOnly(value, {"desktoponly", "mobileonly"}, '') |
local final = allowOnly(value, {"desktoponly", "mobileonly"}, '') |
||
if final == "desktoponly" then |
if final == "desktoponly" then |
||
return {desktoponly=true, mobileonly=false} |
return {desktoponly=true, mobileonly=false, duplicateDesktoponly=false, duplicateMobileonly=false} |
||
elseif final == "mobileonly" then |
elseif final == "mobileonly" then |
||
return {desktoponly=false, mobileonly=true} |
return {desktoponly=false, mobileonly=true, duplicateDesktoponly=false, duplicateMobileonly=false} |
||
else |
else |
||
return {desktoponly=false, mobileonly=false} |
return {desktoponly=false, mobileonly=false, duplicateDesktoponly=false, duplicateMobileonly=false} |
||
end |
end |
||
end |
end |
||
Linia 33: | Linia 33: | ||
function showStatusToString(stat) |
function showStatusToString(stat) |
||
local val = "" |
local val = "" |
||
if stat["desktoponly"] then |
if stat["desktoponly"] or stat["duplicateDesktoponly"] then |
||
val = val .. "desktoponly " |
val = val .. "desktoponly " |
||
end |
end |
||
if stat["mobileonly"] then |
if stat["mobileonly"] or stat["duplicateMobileonly"] then |
||
val = val .. "mobileonly" |
val = val .. "mobileonly" |
||
end |
end |
||
return mw.text.trim(val) |
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 |
end |
||
Linia 68: | Linia 80: | ||
curRight = 0 |
curRight = 0 |
||
else |
else |
||
cur.showstatus. |
cur.showstatus.duplicateDesktoponly = true |
||
if willEverBeUsed(cur) then |
|||
if not ((cur.showstatus.desktoponly == cur.showstatus.mobileonly) and (cur.showstatus.desktoponly == true)) then |
|||
table.insert(second, cur) |
table.insert(second, cur) |
||
end |
end |
||
local cur2 = makeElementTable(args, i) |
local cur2 = makeElementTable(args, i) |
||
cur2.showstatus. |
cur2.showstatus.duplicateMobileonly = true |
||
if willEverBeUsed(cur2) then |
|||
if not ((cur2.showstatus.desktoponly == cur2.showstatus.mobileonly) and (cur2.showstatus.desktoponly == true)) then |
|||
table.insert(first, cur2) |
table.insert(first, cur2) |
||
curRight = curRight + 1 |
curRight = curRight + 1 |
||
Linia 87: | Linia 99: | ||
for i = 0, (curRight - 1) do |
for i = 0, (curRight - 1) do |
||
table.remove(first, maxFirst-i) |
table.remove(first, maxFirst-i) |
||
second[maxSecond-i].showstatus. |
second[maxSecond-i].showstatus.duplicateDesktoponly = false |
||
end |
end |
||
end |
end |
Wersja z 21:09, 27 mar 2022
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 = {}
cur['title'] = sanitizeOrElse(args['ptytuł' .. num], 'Nie podano tytułu')
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 curRight = 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)
curRight = 0
else
cur.showstatus.duplicateDesktoponly = true
if willEverBeUsed(cur) then
table.insert(second, cur)
end
local cur2 = makeElementTable(args, i)
cur2.showstatus.duplicateMobileonly = true
if willEverBeUsed(cur2) then
table.insert(first, cur2)
curRight = curRight + 1
end
end
end
else break end
end
if curRight > 0 then
local maxFirst = table.maxn(first)
local maxSecond = table.maxn(second)
for i = 0, (curRight - 1) do
table.remove(first, maxFirst-i)
second[maxSecond-i].showstatus.duplicateDesktoponly = false
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