Modulo:Navbox
Inpostasion de letura
--[[
* Modulo par implementasion dei modełi i Navbox e Navbox_subgroup.
]]--
require('strict')
local getArgs = require('Modulo:Arguments').getArgs
-- Nùmaro màsemo de liste e grupi par i modełi Navbox e Navbox_subgroup
local MAX_LIST_NAVBOX = 30
local MAX_LIST_NAVBOX_SUBGROUP = 20
-- =============================================================================
-- Funsion de utilità
-- =============================================================================
-- Restituise na sequence Lua ordenada tegnendo dentro i ID dei listN che ghe ze.
-- Se withGroup el ze true, controła anca i groupN.
--
-- @param {table} args
-- @param {boolean} withGroup
-- @return {table}
local function getIds(args, withGroup)
local ret, ids = {}, {}
for key, _ in pairs(args) do
if type(key) == 'string' then
local id = key:match('^list(%d+)$') or (withGroup and key:match('^group(%d+)$'))
if id and tonumber(id) <= (withGroup and MAX_LIST_NAVBOX or MAX_LIST_NAVBOX_SUBGROUP) then
ids[tonumber(id)] = true
end
end
end
for key, _ in pairs(ids) do
table.insert(ret, key)
end
table.sort(ret)
return ret
end
-- Cava eventuali spasi / caoversi torno dei {{,}}.
--
-- @param {string} list
-- @return {string}
local function trimSep(list)
local sep = mw.getCurrentFrame():expandTemplate{ title = "," }
local sepEsc = mw.ustring.gsub(sep, '-', '%-')
return mw.ustring.gsub(list, '%s*' .. sepEsc .. '%s*', sep)
end
-- Col debug redefinise el modo mw.html:css,
-- fazendo sì che se ezegua i test sensa controlar anca el CSS.
--
-- @param {table} tableNode
local function disableCSS(tableNode)
local mt = getmetatable(tableNode)
mt.__index.css = function(t, name, val) return t end
end
-- Varda se el modeło el ze ełaborà inte ła so voze
local function isTemplatePage(name)
local title = mw.title.getCurrentTitle().prefixedText
name = 'Template:' .. (name or '')
return name == title and true or false
end
-- Carga el CSS via TemplateStyles co ze justo
local function loadCSS(name)
local prefix = isTemplatePage(name) and 'mobile-' or ''
local styles = 'Modulo:Navbox/' .. prefix .. 'styles.css'
return mw.getCurrentFrame():extensionTag{
name = 'templatestyles',
args = {src = styles}
}
end
-- =============================================================================
-- Clase Navbox
-- =============================================================================
local Navbox = {}
-- Costrutor de ła clase Navbox.
--
-- @param {table} args - i argominti pasai al modulo
-- @return {table} un novo ojeto Navbox
function Navbox:new(args)
local self = {}
local thNode
setmetatable(self, { __index = Navbox })
self.args = args
-- costrusion tola HTML
self.tableNode = mw.html.create('table')
if self.args.debug then
disableCSS(self.tableNode)
end
self:_setupTableNode()
-- prima riga: tien dentro la navbar e el titulo
thNode = self.tableNode:tag('tr')
:tag('th')
:attr('colspan', self.args.image and '3' or '2')
:cssText(self.args.titlestyle)
self:_addTnavbar(thNode)
if self.args.title then
self:_addTitle(thNode)
end
-- eventuale riga par l'above
if self.args.above then
self:_addAboveOrBelow(self.args.above, self.args.abovestyle)
end
-- altre righe
self:_addLists()
-- eventuale riga finale par el below
if self.args.below then
self:_addAboveOrBelow(self.args.below, self.args.belowstyle)
end
return self
end
-- restituise toła HTML.
--
-- @return {string}
function Navbox:getHTML()
return tostring(self.tableNode)
end
-- Configura i stili CSS de ła toła
function Navbox:_setupTableNode()
self.tableNode
:addClass(isTemplatePage(self.args.name) and 'navbox_mobile' or 'navbox')
:addClass('mw-collapsible')
:addClass(isTemplatePage(self.args.name) and 'autocollapse' or
self.args.state == 'collapsed' and 'mw-collapsed' or
self.args.state == 'autocollapse' and 'autocollapse' or
not self.args.state and 'autocollapse' or nil)
:addClass('noprint metadata')
:attr('id', 'navbox-' .. (self.args.name or ''))
:cssText(self.args.style)
:cssText(self.args.bodystyle)
end
-- Zonta el Tnavbar (ligaminti a ła voze modeło, de discusion e cànbio).
--
-- @param {table} node
function Navbox:_addTnavbar(node)
local tnavbar = mw.getCurrentFrame():expandTemplate {
title = 'Tnavbar',
args = {
[1] = self.args.name,
['mini'] = 1
}
}
node:tag('div'):addClass('navbox_navbar'):wikitext(tnavbar)
end
-- Inposta el tìtulo del navbox dal paràmetro "title".
--
-- @param {table} node
function Navbox:_addTitle(node)
node:tag('span'):addClass('navbox_title'):wikitext(self.args.title)
end
-- Zonta la riga par i paràmetri "above" e "below".
--
-- @param {string} arg
-- @param {string} argStyle
function Navbox:_addAboveOrBelow(arg, argStyle)
self.tableNode
:tag('tr')
:tag('th')
:attr('colspan', self.args.image and '3' or '2')
:addClass('navbox_abovebelow')
:cssText(argStyle)
:wikitext(arg)
end
-- Zonta na cołona par l'imàzene.
--
-- @param {table} trNode
-- @param {number} rowspan
function Navbox:_addImage(trNode, rowspan)
trNode
:tag('td')
:attr('rowspan', rowspan)
:addClass('navbox_image')
:cssText(self.args.imagestyle)
:wikitext(self.args.image)
end
-- Zonta na nova riga par ogni groupN/listN
function Navbox:_addLists()
local rowIds, altStyle, altBackground
-- crea na riga par ogni groupN/listN
rowIds = getIds(self.args, true)
for i, id in ipairs(rowIds) do
local trNode = self.tableNode:tag('tr')
-- groupN
if self.args['group' .. id] then
trNode:tag('th')
:attr('colspan', self.args['list' .. id] and '1' or '2')
:addClass('navbox_group')
:cssText(self.args.groupstyle)
:cssText(self.args['group' .. id .. 'style'])
:wikitext(self.args['group' .. id])
end
-- listN
if self.args['list' .. id] then
local list = trimSep(self.args['list' .. id])
if (i % 2) == 0 then
altStyle = self.args.evenstyle
altBackground = 'navbox_even'
else
altStyle = self.args.oddstyle
altBackground = 'navbox_odd'
end
trNode:tag('td')
:attr('colspan', self.args['group' .. id] and '1' or '2')
:addClass('navbox_list')
:addClass(not self.args['group' .. id] and 'navbox_center' or nil)
:addClass(altBackground)
:cssText(self.args.liststyle)
:cssText(altStyle)
:cssText(self.args['list' .. id .. 'style'])
:wikitext(list)
end
if id == 1 and self.args.image then
self:_addImage(trNode, #rowIds)
end
end
end
-- =============================================================================
-- Clase NavboxSubgroup
-- =============================================================================
local NavboxSubgroup = {}
-- Costrutor de la clase NavboxSubgroup.
--
-- @param {table} args - i argominti pasati al modulo
-- @return {table} un novo ojeto NavboxSubgroup
function NavboxSubgroup:new(args)
local self = {}
setmetatable(self, { __index = NavboxSubgroup })
self.args = args
-- costrusion tabela HTML
self.tableNode = mw.html.create('table')
if self.args.debug then
disableCSS(self.tableNode)
end
self:_setupTableNode()
self:_addLists()
return self
end
-- Restituise la toła HTML.
--
-- @return {string}
function NavboxSubgroup:getHTML()
return tostring(self.tableNode)
end
-- Configura i stili CSS de ła toła.
function NavboxSubgroup:_setupTableNode()
self.tableNode
:addClass('subnavbox')
:cssText(self.args.bodystyle)
end
-- zonta na nova riga par ogni groupN/listN.
function NavboxSubgroup:_addLists()
local listIds, altStyle
-- crea na row par ogni listN
listIds = getIds(self.args)
for _, id in ipairs(listIds) do
local trNode = self.tableNode:tag('tr')
local list = trimSep(self.args['list' .. id])
-- i groupN i se vede solché se ghe ze la rispetiva listN
if self.args['group' .. id] then
trNode:tag('th')
:addClass('subnavbox_group')
:cssText(self.args.groupstyle)
:wikitext(self.args['group' .. id])
end
if (id % 2) == 0 then
altStyle = self.args.evenstyle
else
altStyle = self.args.oddstyle
end
trNode:tag('td')
:attr('colspan', self.args['group' .. id] and '1' or '2')
:addClass(not self.args['group' .. id] and 'navbox_center' or nil)
:cssText(self.args.liststyle)
:cssText(altStyle)
:wikitext(list)
end
end
-- =============================================================================
-- Funsion esportae
-- =============================================================================
local p = {}
-- Funsion par doparasion da n'altro modulo.
function p._navbox(args)
return loadCSS(args.name) .. Navbox:new(args):getHTML()
end
-- Funsion par la so doparasion da n'altro mòdulo.
function p._navbox_subgroup(args)
return NavboxSubgroup:new(args):getHTML()
end
-- Funsion par el modeło {{Navbox}}.
function p.navbox(frame)
return p._navbox(getArgs(frame, { parentOnly = true }))
end
-- Funsion par el modeło {{Navbox subgroup}}.
function p.navbox_subgroup(frame)
return p._navbox_subgroup(getArgs(frame, { parentOnly = true }))
end
return p