Modulo:Navbox: differenze tra le versioni
Vai alla navigazione
Vai alla ricerca
Contenuto cancellato Contenuto aggiunto
m ops |
migro da CSS in linea a fogli di stile esterni: Modulo:Navbox/styles.css e Modulo:Navbox/mobile-styles.css |
||
Riga 48: | Riga 48: | ||
-- Con il debug ridefinisce il metodo mw.html:css, |
-- Con il debug ridefinisce il metodo mw.html:css, |
||
-- permettendo di eseguire i test senza controllare anche |
-- permettendo di eseguire i test senza controllare anche il CSS. |
||
-- |
-- |
||
-- @param {table} tableNode |
-- @param {table} tableNode |
||
Riga 65: | Riga 65: | ||
-- Carica il CSS via TemplateStyles quando opportuno |
-- Carica il CSS via TemplateStyles quando opportuno |
||
local function loadCSS(name) |
local function loadCSS(name) |
||
⚫ | |||
local styles = 'Modulo:Navbox/styles.css' |
local styles = 'Modulo:Navbox/' .. prefix .. 'styles.css' |
||
if isTemplatePage(name) then |
|||
return mw.getCurrentFrame():extensionTag{ |
|||
name = 'templatestyles', |
|||
args = {src = styles} |
|||
} |
|||
else |
|||
return '' |
|||
end |
|||
end |
end |
||
Riga 89: | Riga 86: | ||
local self = {} |
local self = {} |
||
local thNode |
local thNode |
||
local thStyle = { |
|||
['text-align'] = 'center', |
|||
width = '100%', |
|||
background = '#ccf', |
|||
['font-size'] = '90%' |
|||
} |
|||
setmetatable(self, { __index = Navbox }) |
setmetatable(self, { __index = Navbox }) |
||
Riga 108: | Riga 99: | ||
:tag('th') |
:tag('th') |
||
:attr('colspan', self.args.image and '3' or '2') |
:attr('colspan', self.args.image and '3' or '2') |
||
:css(thStyle) |
|||
:cssText(self.args.titlestyle) |
:cssText(self.args.titlestyle) |
||
if self.args.navbar ~= 'plain' then |
if self.args.navbar ~= 'plain' then |
||
Riga 139: | Riga 129: | ||
-- Configura gli stili CSS della tabella |
-- Configura gli stili CSS della tabella |
||
function Navbox:_setupTableNode() |
function Navbox:_setupTableNode() |
||
local tableStyle = { |
|||
margin = 'auto', |
|||
width = '100%', |
|||
clear = 'both', |
|||
border = '1px solid #aaa', |
|||
padding = '2px' |
|||
} |
|||
self.tableNode |
self.tableNode |
||
:addClass(isTemplatePage(self.args.name) and 'navbox_mobile' or 'navbox') |
:addClass(isTemplatePage(self.args.name) and 'navbox_mobile' or 'navbox') |
||
Riga 153: | Riga 136: | ||
self.args.state == 'autocollapse' and 'autocollapse' or |
self.args.state == 'autocollapse' and 'autocollapse' or |
||
not self.args.state and 'autocollapse' or nil) |
not self.args.state and 'autocollapse' or nil) |
||
:addClass('nowraplinks') |
:addClass('nowraplinks noprint metadata') |
||
⚫ | |||
⚫ | |||
:attr('id', 'navbox-' .. (self.args.name or '')) |
:attr('id', 'navbox-' .. (self.args.name or '')) |
||
:css(tableStyle) |
|||
:cssText(self.args.style) |
:cssText(self.args.style) |
||
:cssText(self.args.bodystyle) |
:cssText(self.args.bodystyle) |
||
Riga 166: | Riga 146: | ||
-- @param {table} node |
-- @param {table} node |
||
function Navbox:_addTnavbar(node) |
function Navbox:_addTnavbar(node) |
||
local divStyle = { |
|||
float = 'left', |
|||
width = '6em', |
|||
['text-align'] = 'left', |
|||
padding = '0 10px 0 0', |
|||
margin = '0px' |
|||
} |
|||
local tnavbar = mw.getCurrentFrame():expandTemplate { |
local tnavbar = mw.getCurrentFrame():expandTemplate { |
||
title = 'Tnavbar', |
title = 'Tnavbar', |
||
Riga 180: | Riga 153: | ||
} |
} |
||
} |
} |
||
node:tag('div'): |
node:tag('div'):addClass('navbox_navbar'):wikitext(tnavbar) |
||
end |
end |
||
Riga 187: | Riga 160: | ||
-- @param {table} node |
-- @param {table} node |
||
function Navbox:_addTitle(node) |
function Navbox:_addTitle(node) |
||
node:tag('span'): |
node:tag('span'):addClass('navbox_title'):wikitext(self.args.title) |
||
end |
end |
||
Riga 195: | Riga 168: | ||
-- @param {string} argStyle |
-- @param {string} argStyle |
||
function Navbox:_addAboveOrBelow(arg, argStyle) |
function Navbox:_addAboveOrBelow(arg, argStyle) |
||
local tdStyle = { |
|||
background = '#ddf', |
|||
['text-align'] = 'center', |
|||
['font-size'] = '90%' |
|||
} |
|||
self.tableNode |
self.tableNode |
||
:tag('tr') |
:tag('tr') |
||
:tag(' |
:tag('th') |
||
:attr('colspan', self.args.image and '3' or '2') |
:attr('colspan', self.args.image and '3' or '2') |
||
: |
:addClass('navbox_abovebelow') |
||
:cssText(argStyle) |
:cssText(argStyle) |
||
:wikitext(arg) |
:wikitext(arg) |
||
Riga 214: | Riga 182: | ||
-- @param {number} rowspan |
-- @param {number} rowspan |
||
function Navbox:_addImage(trNode, rowspan) |
function Navbox:_addImage(trNode, rowspan) |
||
local tdStyle = { |
|||
['vertical-align'] = 'middle', |
|||
['padding-left'] = '7px', |
|||
width = '0%' |
|||
} |
|||
trNode |
trNode |
||
:tag('td') |
:tag('td') |
||
:attr('rowspan', rowspan) |
:attr('rowspan', rowspan) |
||
: |
:addClass('navbox_image') |
||
:cssText(self.args.imagestyle) |
:cssText(self.args.imagestyle) |
||
:wikitext(self.args.image) |
:wikitext(self.args.image) |
||
Riga 230: | Riga 193: | ||
function Navbox:_addLists() |
function Navbox:_addLists() |
||
local rowIds, altStyle, altBackground |
local rowIds, altStyle, altBackground |
||
local thStyle = { |
|||
background = '#ddf', |
|||
['white-space'] = 'nowrap', |
|||
padding = '0 10px', |
|||
['font-size'] = '90%' |
|||
} |
|||
-- crea una riga per ogni groupN/listN |
-- crea una riga per ogni groupN/listN |
||
rowIds = getIds(self.args, true) |
rowIds = getIds(self.args, true) |
||
Riga 244: | Riga 201: | ||
trNode:tag('th') |
trNode:tag('th') |
||
:attr('colspan', self.args['list' .. id] and '1' or '2') |
:attr('colspan', self.args['list' .. id] and '1' or '2') |
||
: |
:addClass('navbox_group') |
||
:cssText(self.args.groupstyle) |
:cssText(self.args.groupstyle) |
||
:cssText(self.args['group' .. id .. 'style']) |
:cssText(self.args['group' .. id .. 'style']) |
||
Riga 254: | Riga 211: | ||
if (i % 2) == 0 then |
if (i % 2) == 0 then |
||
altStyle = self.args.evenstyle |
altStyle = self.args.evenstyle |
||
altBackground = ' |
altBackground = 'navbox_even' |
||
else |
else |
||
altStyle = self.args.oddstyle |
altStyle = self.args.oddstyle |
||
altBackground = |
altBackground = 'navbox_odd' |
||
end |
end |
||
trNode:tag('td') |
trNode:tag('td') |
||
:attr('colspan', self.args['group' .. id] and '1' or '2') |
:attr('colspan', self.args['group' .. id] and '1' or '2') |
||
: |
:addClass('navbox_list') |
||
⚫ | |||
:css('font-size', '90%') |
|||
⚫ | |||
⚫ | |||
:css('background', altBackground) |
|||
:cssText(self.args.liststyle) |
:cssText(self.args.liststyle) |
||
:cssText(altStyle) |
:cssText(altStyle) |
||
Riga 311: | Riga 267: | ||
-- Configura gli stili CSS della tabella. |
-- Configura gli stili CSS della tabella. |
||
function NavboxSubgroup:_setupTableNode() |
function NavboxSubgroup:_setupTableNode() |
||
local tableStyle = { |
|||
background = 'transparent', |
|||
['font-size'] = '100%', |
|||
padding = '0', |
|||
border = '0', |
|||
margin = '-3px', |
|||
width = '100%' |
|||
} |
|||
self.tableNode |
self.tableNode |
||
⚫ | |||
⚫ | |||
:addClass('nowraplinks') |
|||
:css(tableStyle) |
|||
:cssText(self.args.bodystyle) |
:cssText(self.args.bodystyle) |
||
end |
end |
||
Riga 329: | Riga 275: | ||
function NavboxSubgroup:_addLists() |
function NavboxSubgroup:_addLists() |
||
local listIds, altStyle |
local listIds, altStyle |
||
local thStyle = { |
|||
background = '#ddf', |
|||
padding = '0 10px', |
|||
} |
|||
-- crea una row per ogni listN |
-- crea una row per ogni listN |
||
listIds = getIds(self.args) |
listIds = getIds(self.args) |
||
Riga 341: | Riga 283: | ||
if self.args['group' .. id] then |
if self.args['group' .. id] then |
||
trNode:tag('th') |
trNode:tag('th') |
||
: |
:addClass('subnavbox_group') |
||
:cssText(self.args.groupstyle) |
:cssText(self.args.groupstyle) |
||
:wikitext(self.args['group' .. id]) |
:wikitext(self.args['group' .. id]) |
||
Riga 352: | Riga 294: | ||
trNode:tag('td') |
trNode:tag('td') |
||
:attr('colspan', self.args['group' .. id] and '1' or '2') |
: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(self.args.liststyle) |
||
:cssText(altStyle) |
:cssText(altStyle) |
||
Riga 372: | Riga 314: | ||
-- Funzione per l'utilizzo da un altro modulo. |
-- Funzione per l'utilizzo da un altro modulo. |
||
function p._navbox_subgroup(args) |
function p._navbox_subgroup(args) |
||
return |
return NavboxSubgroup:new(args):getHTML() |
||
end |
end |
||
Versione delle 12:45, 7 ott 2018
Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Navbox/man (modifica · cronologia)
Sandbox: Modulo:Navbox/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Navbox/test (modifica · cronologia · Esegui)
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Navbox/man (modifica · cronologia)
Sandbox: Modulo:Navbox/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Navbox/test (modifica · cronologia · Esegui)
Il modulo Navbox implementa le funzionalità dei template {{Navbox}} e {{Navbox subgroup}}.
Ha due sottopagine CSS: Modulo:Navbox/styles.css e Modulo:Navbox/mobile-styles.css.
--[[
* Modulo che implementa i template Navbox e Navbox_subgroup.
]]--
require('Modulo:No globals')
local getArgs = require('Modulo:Arguments').getArgs
-- Numero massimo di liste e gruppi per i template Navbox e Navbox_subgroup
local MAX_LIST_NAVBOX = 30
local MAX_LIST_NAVBOX_SUBGROUP = 20
-- =============================================================================
-- Funzioni di utilità
-- =============================================================================
-- Restituisce una sequence Lua ordinata contenente gli ID dei listN presenti.
-- Se withGroup è true, controlla anche 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
-- Rimuove eventuali spazi/a capo attorno ai {{,}}.
--
-- @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
-- Con il debug ridefinisce il metodo mw.html:css,
-- permettendo di eseguire i test senza controllare anche il CSS.
--
-- @param {table} tableNode
local function disableCSS(tableNode)
local mt = getmetatable(tableNode)
mt.__index.css = function(t, name, val) return t end
end
-- Verifica se il template è elaborato nella sua pagina
local function isTemplatePage(name)
local title = mw.title.getCurrentTitle().prefixedText
name = 'Template:' .. (name or '')
return name == title and true or false
end
-- Carica il CSS via TemplateStyles quando opportuno
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
-- =============================================================================
-- Classe Navbox
-- =============================================================================
local Navbox = {}
-- Costruttore della classe Navbox.
--
-- @param {table} args - gli argomenti passati al modulo
-- @return {table} un nuovo oggetto Navbox
function Navbox:new(args)
local self = {}
local thNode
setmetatable(self, { __index = Navbox })
self.args = args
-- costruzione tabella HTML
self.tableNode = mw.html.create('table')
if self.args.debug then
disableCSS(self.tableNode)
end
self:_setupTableNode()
-- prima riga: contiene la navbar e il titolo
thNode = self.tableNode:tag('tr')
:tag('th')
:attr('colspan', self.args.image and '3' or '2')
:cssText(self.args.titlestyle)
if self.args.navbar ~= 'plain' then
self:_addTnavbar(thNode)
end
if self.args.title then
self:_addTitle(thNode)
end
-- eventuale riga per l'above
if self.args.above then
self:_addAboveOrBelow(self.args.above, self.args.abovestyle)
end
-- altre righe
self:_addLists()
-- eventuale riga finale per il below
if self.args.below then
self:_addAboveOrBelow(self.args.below, self.args.belowstyle)
end
return self
end
-- Restituisce la tabella HTML.
--
-- @return {string}
function Navbox:getHTML()
return tostring(self.tableNode)
end
-- Configura gli stili CSS della tabella
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('nowraplinks noprint metadata')
:attr('id', 'navbox-' .. (self.args.name or ''))
:cssText(self.args.style)
:cssText(self.args.bodystyle)
end
-- Aggiunge il Tnavbar (collegamenti alla pagina del template, di discussione e modifica).
--
-- @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
-- Imposta il titolo del navbox dal parametro "title".
--
-- @param {table} node
function Navbox:_addTitle(node)
node:tag('span'):addClass('navbox_title'):wikitext(self.args.title)
end
-- Aggiunge la riga per i parametri "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
-- Aggiunge una colonna per l'immagine.
--
-- @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
-- Aggiunge una nuova riga per ogni groupN/listN
function Navbox:_addLists()
local rowIds, altStyle, altBackground
-- crea una riga per 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
-- =============================================================================
-- Classe NavboxSubgroup
-- =============================================================================
local NavboxSubgroup = {}
-- Costruttore della classe NavboxSubgroup.
--
-- @param {table} args - gli argomenti passati al modulo
-- @return {table} un nuovo oggetto NavboxSubgroup
function NavboxSubgroup:new(args)
local self = {}
setmetatable(self, { __index = NavboxSubgroup })
self.args = args
-- costruzione tabella HTML
self.tableNode = mw.html.create('table')
if self.args.debug then
disableCSS(self.tableNode)
end
self:_setupTableNode()
self:_addLists()
return self
end
-- Restituisce la tabella HTML.
--
-- @return {string}
function NavboxSubgroup:getHTML()
return tostring(self.tableNode)
end
-- Configura gli stili CSS della tabella.
function NavboxSubgroup:_setupTableNode()
self.tableNode
:addClass('subnavbox nowraplinks')
:cssText(self.args.bodystyle)
end
-- Aggiunge una nuova riga per ogni groupN/listN.
function NavboxSubgroup:_addLists()
local listIds, altStyle
-- crea una row per 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 sono visibili solo se c'è la corrispettiva 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
-- =============================================================================
-- Funzioni esportate
-- =============================================================================
local p = {}
-- Funzione per l'utilizzo da un altro modulo.
function p._navbox(args)
return loadCSS(args.name) .. Navbox:new(args):getHTML()
end
-- Funzione per l'utilizzo da un altro modulo.
function p._navbox_subgroup(args)
return NavboxSubgroup:new(args):getHTML()
end
-- Funzione per il template {{Navbox}}.
function p.navbox(frame)
return p._navbox(getArgs(frame, { parentOnly = true }))
end
-- Funzione per il template {{Navbox subgroup}}.
function p.navbox_subgroup(frame)
return p._navbox_subgroup(getArgs(frame, { parentOnly = true }))
end
return p