Module:Definitions: Difference between revisions
From SpiritVale Wiki
More actions
No edit summary Tags: Reverted Mobile edit Mobile web edit |
No edit summary Tags: Mobile edit Mobile web edit |
||
| (8 intermediate revisions by the same user not shown) | |||
| Line 4: | Line 4: | ||
-- Data source (static): | -- Data source (static): | ||
-- Module:Definitions/Definitions.json | -- Module:Definitions/Definitions.json | ||
-- | -- | ||
-- Notes: | -- Notes: | ||
| Line 18: | Line 9: | ||
-- - CSS namespace: ONLY "sv-def" (plus sv-def--* modifiers). | -- - CSS namespace: ONLY "sv-def" (plus sv-def--* modifiers). | ||
-- - Output classes: sv-def, sv-def-icon, sv-def-icon-img, sv-def-text. | -- - Output classes: sv-def, sv-def-icon, sv-def-icon-img, sv-def-text. | ||
-- - Tooltip/link behavior is handled by Universal Popups (Common.js). | |||
local p = {} | local p = {} | ||
| Line 34: | Line 26: | ||
local function lc(s) | local function lc(s) | ||
return string.lower(tostring(s or "")) | return string.lower(tostring(s or "")) | ||
end | |||
local function truthy(v) | |||
v = lc(mw.text.trim(tostring(v or ""))) | |||
return v == "1" or v == "true" or v == "yes" or v == "y" | |||
end | end | ||
| Line 67: | Line 64: | ||
if DB ~= nil then return DB end | if DB ~= nil then return DB end | ||
local title = mw.title.new(DATA_TITLE) | local title = mw.title.new(DATA_TITLE) | ||
if not title then | if not title then | ||
| Line 87: | Line 76: | ||
end | end | ||
local | local ok, obj = pcall(mw.text.jsonDecode, raw) | ||
if not | if not ok or type(obj) ~= "table" then | ||
DB = {} | DB = {} | ||
return DB | return DB | ||
end | end | ||
DB = | DB = obj | ||
return DB | return DB | ||
end | end | ||
| Line 129: | Line 118: | ||
-- ============================================================================= | -- ============================================================================= | ||
-- Icon rendering | -- Icon rendering | ||
-- - Blank icon: render nothing | -- - Blank icon: render nothing | ||
-- - Missing file: render "?" badge (no redlink image) | -- - Missing file: render "?" badge (no redlink image) | ||
-- ============================================================================= | -- ============================================================================= | ||
| Line 150: | Line 139: | ||
end | end | ||
return '<span class="sv-def-icon-img">[[' .. fileTitle .. '| | return '<span class="sv-def-icon-img">[[' .. fileTitle .. '|14px|link=]]</span>' | ||
end | end | ||
-- ============================================================================= | -- ============================================================================= | ||
-- | -- Render | ||
-- ============================================================================= | -- ============================================================================= | ||
local function | local function render(domain, key, opts) | ||
opts = opts or {} | |||
local noicon = truthy(opts.noicon) | |||
local pill = truthy(opts.pill) | |||
local fill = truthy(opts.fill) | |||
local | |||
local | |||
local | |||
local db = load_db() | local db = load_db() | ||
domain = norm_domain(db, domain) | domain = norm_domain(db, domain) | ||
| Line 226: | Line 176: | ||
local d_lc = lc(domain) | local d_lc = lc(domain) | ||
-- Missing record: | -- Missing record: visible hint, no tooltip/link attributes. | ||
if rec == nil then | if rec == nil then | ||
local miss_icon = noicon and "" or '<span class="sv-def-icon sv-def-icon--missing" aria-hidden="true">?</span>' | |||
return | return | ||
'<span class="sv-def sv-def--missing sv-def--' .. enc_attr(d_lc) .. '"' .. | '<span class="sv-def sv-def--missing' .. | ||
(pill and ' sv-def--pill' or '') .. | |||
(fill and ' sv-def--fill' or '') .. | |||
' sv-def--' .. enc_attr(d_lc) .. '"' .. | |||
' data-sv-def-domain="' .. enc_attr(domain) .. '"' .. | ' data-sv-def-domain="' .. enc_attr(domain) .. '"' .. | ||
' data-sv-def-key="' .. enc_attr(key) .. '"' .. | ' data-sv-def-key="' .. enc_attr(key) .. '"' .. | ||
(pill and ' data-sv-def-pill="1"' or '') .. | |||
(fill and ' data-sv-def-fill="1"' or '') .. | |||
'>' .. | '>' .. | ||
miss_icon .. | |||
'<span class="sv-def-text">' .. mw.text.nowiki(name) .. '</span>' .. | '<span class="sv-def-text">' .. mw.text.nowiki(name) .. '</span>' .. | ||
'</span>' | '</span>' | ||
end | end | ||
local classes = 'sv-def sv-def--' .. enc_attr(d_lc) | |||
if noicon then classes = classes .. ' sv-def--noicon' end | |||
if pill then classes = classes .. ' sv-def--pill' end | |||
if fill then classes = classes .. ' sv-def--fill' end | |||
local attrs = { | local attrs = { | ||
'class=" | 'class="' .. classes .. '"', | ||
'data-sv-def-domain="' .. enc_attr(domain) .. '"', | 'data-sv-def-domain="' .. enc_attr(domain) .. '"', | ||
'data-sv-def-key="' .. enc_attr(key) .. '"', | 'data-sv-def-key="' .. enc_attr(key) .. '"', | ||
} | } | ||
if pill then attrs[#attrs + 1] = 'data-sv-def-pill="1"' end | |||
if fill then attrs[#attrs + 1] = 'data-sv-def-fill="1"' end | |||
-- Only include tooltip/link attributes when populated. | -- Only include tooltip/link attributes when populated. | ||
if defn ~= "" then | if defn ~= "" then | ||
attrs[#attrs + 1] = 'data-sv-def-tip="' .. enc_attr(defn) .. '"' | attrs[#attrs + 1] = 'data-sv-def-tip="' .. enc_attr(defn) .. '"' | ||
-- Interactive definitions are focusable for consistent UX. | |||
attrs[#attrs + 1] = 'tabindex="0"' | |||
end | end | ||
if link ~= "" then | if link ~= "" then | ||
| Line 252: | Line 218: | ||
end | end | ||
local | local ico = noicon and "" or icon_html(icon) | ||
return | return | ||
'<span ' .. table.concat(attrs, " ") | '<span ' .. table.concat(attrs, " ") .. '>' .. | ||
ico .. | |||
'<span class="sv-def-text">' .. mw.text.nowiki(name) .. '</span>' .. | |||
'</span>' | '</span>' | ||
end | end | ||
| Line 267: | Line 233: | ||
function p.def(frame) | function p.def(frame) | ||
local a = frame.args or {} | local a = frame.args or {} | ||
local domain = a[1] or a.Domain or a.domain | |||
local key = a[2] or a.Key or a.key | |||
local noicon = a.noicon or a.NoIcon or a[3] | |||
local pill = a.pill or a.Pill | |||
local fill = a.fill or a.Fill | |||
return render(domain, key, { noicon = noicon, pill = pill, fill = fill }) | |||
end | end | ||
function p.render(domain, key, opts) | |||
return render(domain, key, opts) | |||
end | end | ||
return p | return p | ||