Module:Storm categories: Difference between revisions

Content deleted Content added
MTS
Add demo preview function
Line 408:
] or cats[defaultCategory]).sortkey
end
 
function count(_table)
local n = 0
for _ in pairs(_table) do
n = n + 1
end
return n
end
 
function p.demo(frame)
local out = mw.html.create("table")
:addClass("wikitable")
:addClass("sortable")
:attr("style", "width: 100%")
out:node(
mw.html.create("tr")
:node(mw.html.create("th"):wikitext("ID")
:attr("rowspan", "2"))
:node(mw.html.create("th"):wikitext("Name")
:attr("colspan", "2"))
:node(mw.html.create("th"):wikitext("Color")
:attr("rowspan", "2")
:attr("colspan", "2"))
:node(mw.html.create("th"):wikitext("Sortkey")
:attr("colspan", "2"))
):node(
mw.html.create("tr")
:node(mw.html.create("th"):wikitext("Basin"))
:node(mw.html.create("th"):wikitext("Name"))
:node(mw.html.create("th"):wikitext("Basin"))
:node(mw.html.create("th"):wikitext("Sortkey")
:attr("data-sort-type", "number"))
)
for name, cat in pairs(cats) do
local rows = { mw.html.create("tr") }
local row = rows[1]
local id = mw.html.create("td")
:wikitext(name)
local colorPreview = mw.html.create("td")
:attr("style", "background-color: #" .. cat["color"] .. "; padding: 0; width: 1.8em")
local color = mw.html.create("td")
:wikitext("#" .. cat["color"])
local sortkeyCategory = mw.html.create("td")
:attr("data-sort-value", cat["sortkey"])
local sortkey = mw.html.create("td")
:attr("data-sort-value", cat["sortkey"])
:wikitext(cat["sortkey"])
if cat["sortkey"] < 0 then
sortkeyCategory:wikitext("Invalid")
elseif cat["sortkey"] < 20000 then
sortkeyCategory:wikitext("Global")
elseif cat["sortkey"] < 30000 then
sortkeyCategory:wikitext("Historical")
elseif cat["sortkey"] < 40000 then
sortkeyCategory:wikitext("SWIO")
elseif cat["sortkey"] < 50000 then
sortkeyCategory:wikitext("Aus/Fiji")
elseif cat["sortkey"] < 60000 then
sortkeyCategory:wikitext("NIO")
elseif cat["sortkey"] < 80000 then
sortkeyCategory:wikitext("WPAC")
elseif cat["sortkey"] < 90000 then
sortkeyCategory:wikitext("Atl/EPac/SAtl")
elseif cat["sortkey"] < 100000 then
sortkeyCategory:attr("style", "color: gray")
sortkeyCategory:wikitext("''Global''")
else
sortkeyCategory:wikitext("Invalid")
end
if type(cat["name"]) == "string" then
local name = mw.html.create("td")
:attr("colspan", "2")
:wikitext(cat["name"])
row:node(id)
row:node(name)
else
local nameTableLength = count(cat["name"])
id:attr("rowspan", nameTableLength)
colorPreview:attr("rowspan", nameTableLength)
color:attr("rowspan", nameTableLength)
sortkeyCategory:attr("rowspan", nameTableLength)
sortkey:attr("rowspan", nameTableLength)
row:node(id)
local firstDone = false
for key, basinName in pairs(cat["name"]) do
if firstDone then
local nameRow = mw.html.create("tr")
nameRow
:node(mw.html.create("td"):wikitext(key))
:node(mw.html.create("td"):wikitext(basinName))
table.insert(rows, nameRow)
else
firstDone = true
row
:node(mw.html.create("td"):wikitext(key))
:node(mw.html.create("td"):wikitext(basinName))
end
end
end
row:node(colorPreview)
row:node(color)
row:node(sortkeyCategory)
row:node(sortkey)
for _, _row in pairs(rows) do
out:node(_row)
end
end
return tostring(out)
end
 
p.cats = cats
 
return p