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(frame_or_args)
local args = getArgs(frame_or_args)
local frame = is_maybe_frame(frame_or_args) and frame_or_args or mw.getCurrentFrame()
-- [REQUIRED]
local icon = args[1]
assert_not_nil(icon, "failed to generate labeled icon: icon not provided");
local icon_size = args[2]
assert_not_nil(icon, "failed to generate labeled icon: icon size not provided");
local label = args[3]
assert_not_nil(icon, "failed to generate labeled icon: label not provided");
-- [OPTIONAL]
local link = args[4]
local class = args.class
local zoom = args.zoom
-- ============
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
return p