Module:Navbox with collapsible groups: Difference between revisions

    From Nonbinary Wiki
    No edit summary
     
    (35 intermediate revisions by 3 users not shown)
    Line 1: Line 1:
    -- This module implements {{Navbox with collapsible groups}}
    -- This module implements {{Navbox with collapsible groups}}
    local p = {}
    local q = {}
     
    local Navbox = require('Module:Navbox')
    local Navbox = require('Module:Navbox')
    local getArgs -- lazily initialized


    -- helper functions
    -- helper functions
    local function concatstrings(t)
    local function concatstrings(s)
    local res = table.concat(t, '')
    local r = table.concat(s, '')
    if res:match('^%s*$') then res = nil end
    if r:match('^%s*$') then r = nil end
    return res
    return r
    end
    end


    local function concatstyles(t)
    local function concatstyles(s)
    local res = table.concat(t, ';')
    local r = table.concat(s, ';')
    while res:match(';%s*;') do
    while r:match(';%s*;') do
    res = mw.ustring.gsub(res, ';%s*;', ';')
    r = mw.ustring.gsub(r, ';%s*;', ';')
    end
    end
    if res:match('^%s*;%s*$') then res = nil end
    if r:match('^%s*;%s*$') then r = nil end
    return res
    return r
    end
    end


    function p._navbox(args)
    function q._navbox(pargs)
    -- table for args passed to navbox
    -- table for args passed to navbox
    local targs = {}
    local targs = {}
    Line 27: Line 25:
    -- process args
    -- process args
    local passthrough = {
    local passthrough = {
    ['name']=1,['navbar']=1,['state']=1,['border']=1,
    ['name']=true,['navbar']=true,['state']=true,['border']=true,
    ['bodyclass']=1,['groupclass']=1,['listclass']=1,
    ['bodyclass']=true,['groupclass']=true,['listclass']=true,
    ['style']=1,['bodystyle']=1,['basestyle']=1,
    ['style']=true,['bodystyle']=true,['basestyle']=true,
    ['title']=1,['titleclass']=1,['titlestyle']=1,
    ['title']=true,['titleclass']=true,['titlestyle']=true,
    ['above']=1,['aboveclass']=1,['abovestyle']=1,
    ['above']=true,['aboveclass']=true,['abovestyle']=true,
    ['below']=1,['belowclass']=1,['belowstyle']=1,
    ['below']=true,['belowclass']=true,['belowstyle']=true,
    ['image']=1,['imageclass']=1,['imagestyle']=1,
    ['image']=true,['imageclass']=true,['imagestyle']=true,
    ['imageleft']=1,['imageleftstyle']=1
    ['imageleft']=true,['imageleftstyle']=true
    }
    }
    for k,v in pairs(args) do
    for k,v in pairs(pargs) do
    if k and type(k) == 'string' then
    if k and type(k) == 'string' then
    if passthrough[k] then
    if passthrough[k] then
    Line 43: Line 41:
    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 args['group' .. n] == nil
    if (targs['list' .. n] == nil and pargs['group' .. n] == nil
    and args['sect' .. n] == nil and args['section' .. n] == nil) then
    and pargs['sect' .. n] == nil and pargs['section' .. n] == nil) then
    targs['list' .. n] = (args['list' .. n] or '') .. (args['content' .. n] or '')
    targs['list' .. n] = concatstrings(
    {pargs['list' .. n] or '', pargs['content' .. n] or ''})
    end
    end
    elseif (k:match('^group[0-9][0-9]*$')  
    elseif (k:match('^group[0-9][0-9]*$')  
    Line 53: Line 52:
    if targs['list' .. n] == nil then
    if targs['list' .. n] == nil then
    local titlestyle = concatstyles(
    local titlestyle = concatstyles(
    {args['groupstyle'] or '',args['secttitlestyle'] or '',  
    {pargs['groupstyle'] or '',pargs['secttitlestyle'] or '',  
    args['group' .. n .. 'style'] or '',  
    pargs['group' .. n .. 'style'] or '',  
    args['section' .. n ..'titlestyle'] or ''})
    pargs['section' .. n ..'titlestyle'] or ''})
    local liststyle = concatstyles(
    local liststyle = concatstyles(
    {args['liststyle'] or '', args['contentstyle'] or '',  
    {pargs['liststyle'] or '', pargs['contentstyle'] or '',  
    args['list' .. n .. 'style'] or '',  
    pargs['list' .. n .. 'style'] or '',  
    args['content' .. n .. 'style'] or ''})
    pargs['content' .. n .. 'style'] or ''})
    local title = concatstrings(
    local title = concatstrings(
    {args['group' .. n] or '',  
    {pargs['group' .. n] or '',  
    args['sect' .. n] or '',
    pargs['sect' .. n] or '',
    args['section' .. n] or ''})
    pargs['section' .. n] or ''})
    local list = concatstrings(
    local list = concatstrings(
    {args['list' .. n] or '',  
    {pargs['list' .. n] or '',  
    args['content' .. n] or ''})
    pargs['content' .. n] or ''})
    local state = (args['abbr' .. n] and args['abbr' .. n] == args['selected'])  
    local state = (pargs['abbr' .. n] and pargs['abbr' .. n] == pargs['selected'])  
    and 'uncollapsed' or args['state' .. n] or 'collapsed'
    and 'uncollapsed' or pargs['state' .. n] or 'collapsed'
     
    targs['list' .. n] = Navbox._navbox(
    targs['list' .. n] = Navbox._navbox(
    {'child', navbar = 'plain', state = state,
    {'child', navbar = 'plain', state = state,
    basestyle = args['basestyle'],
    basestyle = pargs['basestyle'],
    title = title, titlestyle = titlestyle,
    title = title, titlestyle = titlestyle,
    list1 = list, liststyle = liststyle,
    list1 = list, liststyle = liststyle,
    listclass = args['list' .. n .. 'class'],
    listclass = pargs['list' .. n .. 'class'],
    image = args['image' .. n],
    image = pargs['image' .. n],
    imageleft = args['imageleft' .. n],
    imageleft = pargs['imageleft' .. n],
    listpadding = args['listpadding']})
    listpadding = pargs['listpadding']})
    end
    end
    end
    end
    end
    end
    end
    end
    -- ordering of style and bodystyle
    targs['style'] = concatstyles({targs['style'] or '', targs['bodystyle'] or ''})
    targs['bodystyle'] = nil
    -- child or subgroup
    -- child or subgroup
    if targs['border'] == nil then targs['border'] = args[1] end
    if targs['border'] == nil then targs['border'] = pargs[1] end


    return Navbox._navbox(targs)
    return Navbox._navbox(targs)
    end
    end


    function p.navbox(frame)
    function q.navbox(frame)
    if not getArgs then
    local pargs = require('Module:Arguments').getArgs(frame, {wrappers = {'Template:Navbox with collapsible groups'}})
    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
    _ = pargs.title
    _ = args.above
    _ = pargs.above
    for i = 1, 20 do
    for i = 1, 20 do
    _ = args["group" .. tostring(i)]
    _ = pargs["group" .. tostring(i)]
    _ = args["list" .. tostring(i)]
    _ = pargs["list" .. tostring(i)]
    end
    end
    _ = args.below
    _ = pargs.below


    return p._navbox(args)
    return q._navbox(pargs)
    end
    end


    return p
    return q

    Latest revision as of 11:42, 21 May 2021

    Documentation for this module may be created at Module:Navbox with collapsible groups/doc

    -- This module implements {{Navbox with collapsible groups}}
    local q = {}
    local Navbox = require('Module:Navbox')
    
    -- helper functions
    local function concatstrings(s)
    	local r = table.concat(s, '')
    	if r:match('^%s*$') then r = nil end
    	return r
    end
    
    local function concatstyles(s)
    	local r = table.concat(s, ';')
    	while r:match(';%s*;') do
    		r = mw.ustring.gsub(r, ';%s*;', ';')
    	end
    	if r:match('^%s*;%s*$') then r = nil end
    	return r
    end
    
    function q._navbox(pargs)
    	-- table for args passed to navbox
    	local targs = {}
    
    	-- process args
    	local passthrough = {
    		['name']=true,['navbar']=true,['state']=true,['border']=true,
    		['bodyclass']=true,['groupclass']=true,['listclass']=true,
    		['style']=true,['bodystyle']=true,['basestyle']=true,
    		['title']=true,['titleclass']=true,['titlestyle']=true,
    		['above']=true,['aboveclass']=true,['abovestyle']=true,
    		['below']=true,['belowclass']=true,['belowstyle']=true,
    		['image']=true,['imageclass']=true,['imagestyle']=true,
    		['imageleft']=true,['imageleftstyle']=true
    	}
    	for k,v in pairs(pargs) do
    		if k and type(k) == 'string' then
    			if passthrough[k] then
    				targs[k] = v
    			elseif (k:match('^list[0-9][0-9]*$') 
    					or k:match('^content[0-9][0-9]*$') ) then
    				local n = mw.ustring.gsub(k, '^[a-z]*([0-9]*)$', '%1')
    				if (targs['list' .. n] == nil and pargs['group' .. n] == nil
    					and pargs['sect' .. n] == nil and pargs['section' .. n] == nil) then
    					targs['list' .. n] = concatstrings(
    						{pargs['list' .. n] or '', pargs['content' .. n] or ''})
    				end
    			elseif (k:match('^group[0-9][0-9]*$') 
    					or k:match('^sect[0-9][0-9]*$') 
    					or k:match('^section[0-9][0-9]*$') ) then
    				local n = mw.ustring.gsub(k, '^[a-z]*([0-9]*)$', '%1')
    				if targs['list' .. n] == nil then
    					local titlestyle = concatstyles(
    						{pargs['groupstyle'] or '',pargs['secttitlestyle'] or '', 
    							pargs['group' .. n .. 'style'] or '', 
    							pargs['section' .. n ..'titlestyle'] or ''})
    					local liststyle = concatstyles(
    						{pargs['liststyle'] or '', pargs['contentstyle'] or '', 
    							pargs['list' .. n .. 'style'] or '', 
    							pargs['content' .. n .. 'style'] or ''})
    					local title = concatstrings(
    						{pargs['group' .. n] or '', 
    							pargs['sect' .. n] or '',
    							pargs['section' .. n] or ''})
    					local list = concatstrings(
    						{pargs['list' .. n] or '', 
    							pargs['content' .. n] or ''})
    					local state = (pargs['abbr' .. n] and pargs['abbr' .. n] == pargs['selected']) 
    						and 'uncollapsed' or pargs['state' .. n] or 'collapsed'
    					
    					targs['list' .. n] = Navbox._navbox(
    						{'child', navbar = 'plain', state = state,
    						basestyle = pargs['basestyle'],
    						title = title, titlestyle = titlestyle,
    						list1 = list, liststyle = liststyle,
    						listclass = pargs['list' .. n .. 'class'],
    						image = pargs['image' .. n],
    						imageleft = pargs['imageleft' .. n],
    						listpadding = pargs['listpadding']})
    				end
    			end
    		end
    	end
    	-- ordering of style and bodystyle
    	targs['style'] = concatstyles({targs['style'] or '', targs['bodystyle'] or ''})
    	targs['bodystyle'] = nil
    	
    	-- child or subgroup
    	if targs['border'] == nil then targs['border'] = pargs[1] end
    
    	return Navbox._navbox(targs)
    end
    
    function q.navbox(frame)
    	local pargs = require('Module:Arguments').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.
    	local _
    	_ = pargs.title
    	_ = pargs.above
    	for i = 1, 20 do
    		_ = pargs["group" .. tostring(i)]
    		_ = pargs["list" .. tostring(i)]
    	end
    	_ = pargs.below
    
    	return q._navbox(pargs)
    end
    
    return q