Module:Labeled icon
From Space Station 14 Wiki
Module documentation
|
---|
View or edit this documentation • (about module documentation) |
Implements {{Labeled icon}}.
local p = {}
local getArgs = require('Module:Arguments').getArgs
-- ===========================
-- # Internal vars #
-- Whether template styles were added.
local were_template_styles_added = false
-- ===========================
local function assert_not_nil(value, error_message)
if value == nil then
if error_message == nil then
error("value is nil")
else
error(error_message)
end
end
end
local function numeric_table_length(t)
local count = 0
for _ in ipairs(t) do count = count + 1 end
return count
end
local function table_length(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
local function map_table(t, f)
local newt = {}
for k, v in pairs(t) do
newt[k] = f(k, v, t)
end
return newt
end
-- Check if given value is possibly a frame object.
local function is_maybe_frame(value)
return type(value) == 'table' and type(value.args) == "table"
end
-- ===================
local function generate_template_styles(frame)
return frame:extensionTag("templatestyles", "", { src = 'Template:Labeled icon/styles.css' })
end
-- ===================
function p.main_direct(icon, icon_size, label, link, class, zoom)
local frame = mw.getCurrentFrame()
assert_not_nil(icon, "failed to generate labeled icon: icon not provided");
assert_not_nil(icon, "failed to generate labeled icon: icon size not provided");
assert_not_nil(icon, "failed to generate labeled icon: label not provided");
-- ============
local container_el = mw.html.create('span')
:addClass("li-c")
if class ~= nil then
container_el:addClass(class)
end
local label_el = mw.html.create('span')
:addClass("lc-l")
:wikitext(link and "[[" .. link .. "|" .. label .. "]]" or label)
if not were_template_styles_added then
container_el:node(generate_template_styles(frame))
were_template_styles_added = true
end
if zoom then
container_el:css("--s", icon_size)
container_el:css("--z", zoom)
end
container_el
:wikitext("[[File:" ..
icon .. "|" .. icon_size .. "|link=" .. (link or "") .. "|class=lc-i " .. (zoom and "lc-z" or "") .. "]]")
:node(label_el)
return container_el
end
function p.main(frame)
local args = getArgs(frame)
return p.main_direct(args[1], args[2], args[3], args[4], args.class, args.zoom)
end
return p