Editing Module:Navbox with collapsible groups
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
-- This module implements {{Navbox with collapsible groups}} | -- This module implements {{Navbox with collapsible groups}} | ||
local | local p = {} | ||
local Navbox = require('Module:Navbox') | local Navbox = require('Module:Navbox') | ||
local getArgs -- lazily initialized | |||
-- helper functions | -- helper functions | ||
local function concatstrings( | local function concatstrings(t) | ||
local | local res = table.concat(t, '') | ||
if | if res:match('^%s*$') then res = nil end | ||
return | return res | ||
end | end | ||
local function concatstyles( | local function concatstyles(t) | ||
local | local res = table.concat(t, ';') | ||
while | while res:match(';%s*;') do | ||
res = mw.ustring.gsub(res, ';%s*;', ';') | |||
end | end | ||
if | if res:match('^%s*;%s*$') then res = nil end | ||
return | return res | ||
end | end | ||
function | function p._navbox(args) | ||
-- table for args passed to navbox | -- table for args passed to navbox | ||
local targs = {} | local targs = {} | ||
Line 25: | Line 27: | ||
-- process args | -- process args | ||
local passthrough = { | local passthrough = { | ||
['name']= | ['name']=1,['navbar']=1,['state']=1,['border']=1, | ||
['bodyclass']= | ['bodyclass']=1,['groupclass']=1,['listclass']=1, | ||
['style']= | ['style']=1,['bodystyle']=1,['basestyle']=1, | ||
['title']= | ['title']=1,['titleclass']=1,['titlestyle']=1, | ||
['above']= | ['above']=1,['aboveclass']=1,['abovestyle']=1, | ||
['below']= | ['below']=1,['belowclass']=1,['belowstyle']=1, | ||
['image']= | ['image']=1,['imageclass']=1,['imagestyle']=1, | ||
['imageleft']= | ['imageleft']=1,['imageleftstyle']=1 | ||
} | } | ||
for k,v in pairs( | for k,v in pairs(frame:getParent().args) do | ||
if k and type(k) == 'string' then | if k and type(k) == 'string' then | ||
if passthrough[k] then | if passthrough[k] then | ||
Line 41: | Line 43: | ||
or k:match('^content[0-9][0-9]*$') ) then | or k:match('^content[0-9][0-9]*$') ) then | ||
local n = mw.ustring.gsub(k, '^[a-z]*([0-9]*)$', '%1') | local n = mw.ustring.gsub(k, '^[a-z]*([0-9]*)$', '%1') | ||
if (targs['list' .. n] == nil and | if (targs['list' .. n] == nil and args['group' .. n] == nil | ||
and | and args['sect' .. n] == nil and args['section' .. n] == nil) then | ||
targs['list' .. n] = | targs['list' .. n] = (args['list' .. n] or '') .. (args['content' .. n] or '') | ||
end | end | ||
elseif (k:match('^group[0-9][0-9]*$') | elseif (k:match('^group[0-9][0-9]*$') | ||
Line 52: | Line 53: | ||
if targs['list' .. n] == nil then | if targs['list' .. n] == nil then | ||
local titlestyle = concatstyles( | local titlestyle = concatstyles( | ||
{ | {args['groupstyle'] or '',args['secttitlestyle'] or '', | ||
args['group' .. n .. 'style'] or '', | |||
args['section' .. n ..'titlestyle'] or ''}) | |||
local liststyle = concatstyles( | local liststyle = concatstyles( | ||
{ | {args['liststyle'] or '', args['contentstyle'] or '', | ||
args['list' .. n .. 'style'] or '', | |||
args['content' .. n .. 'style'] or ''}) | |||
local title = concatstrings( | local title = concatstrings( | ||
{ | {args['group' .. n] or '', | ||
args['sect' .. n] or '', | |||
args['section' .. n] or ''}) | |||
local list = concatstrings( | local list = concatstrings( | ||
{ | {args['list' .. n] or '', | ||
args['content' .. n] or ''}) | |||
local state = ( | local state = (args['abbr' .. n] and args['abbr' .. n] == args['selected']) | ||
and 'uncollapsed' or | and 'uncollapsed' or args['state' .. n] or 'collapsed' | ||
targs['list' .. n] = Navbox._navbox( | targs['list' .. n] = Navbox._navbox( | ||
{ | {navbar = 'plain', state = state, | ||
basestyle = | basestyle = args['basestyle'], | ||
title = title, titlestyle = titlestyle, | title = title, titlestyle = titlestyle, | ||
list = list, liststyle = liststyle, | |||
listclass = | listclass = args['list' .. n .. 'class'], | ||
image = | image = args['image' .. n], | ||
imageleft = | imageleft = args['imageleft' .. n], | ||
listpadding = | listpadding = args['listpadding']}) | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
-- child or subgroup | -- child or subgroup | ||
if targs['border'] == nil then targs['border'] = | if targs['border'] == nil then targs['border'] = args[1] end | ||
return Navbox._navbox(targs) | return Navbox._navbox(targs) | ||
end | end | ||
function | function p.navbox(frame) | ||
if not getArgs then | |||
getArgs = require('Module:Arguments').getArgs | |||
end | |||
args = getArgs(frame, {wrappers = {'Template:Navbox with collapsible groups'}}) | |||
-- Read the arguments in the order they'll be output in, to make references number in the right order. | -- Read the arguments in the order they'll be output in, to make references number in the right order. | ||
local _ | local _ | ||
_ = | _ = args.title | ||
_ = | _ = args.above | ||
for i = 1, 20 do | for i = 1, 20 do | ||
_ = | _ = args["group" .. tostring(i)] | ||
_ = | _ = args["list" .. tostring(i)] | ||
end | end | ||
_ = | _ = args.below | ||
return | return p._navbox(args) | ||
end | end | ||
return | return p |