Sari la conținut

Modul:Navbox

Documentația acestui modul poate fi creată laModul:Navbox/doc

--
-- Acest modul implementează {{Navbox}}
--

localp={}

localnavbar=require('Modul:Navbar')._navbar
localgetArgs-- lazily initialized

localargs
localborder
locallistnums={}
localODD_EVEN_MARKER='\127_ODDEVEN_\127'
localRESTART_MARKER='\127_ODDEVEN0_\127'
localREGEX_MARKER='\127_ODDEVEN(%d?)_\127'

localfunctionstriped(wikitext)
-- Return wikitext with markers replaced for odd/even striping.
-- Child (subgroup) navboxes are flagged with a category that is removed
-- by parent navboxes. The result is that the category shows all pages
-- where a child navbox is not contained in a parent navbox.
localorphanCat='[[Category:Navbox orphans]]'
ifborder=='subgroup'andargs.orphan~='yes'then
-- No change; striping occurs in outermost navbox.
returnwikitext..orphanCat
end
localfirst,second='odd','even'
ifargs.evenoddthen
ifargs.evenodd=='swap'then
first,second=second,first
else
first=args.evenodd
second=first
end
end
localchanger
iffirst==secondthen
changer=first
else
localindex=0
changer=function(code)
ifcode=='0'then
-- Current occurrence is for a group before a nested table.
-- Set it to first as a valid although pointless class.
-- The next occurrence will be the first row after a title
-- in a subgroup and will also be first.
index=0
returnfirst
end
index=index+1
returnindex%2==1andfirstorsecond
end
end
localregex=orphanCat:gsub('([%[%]])','%%%1')
return(wikitext:gsub(regex,''):gsub(REGEX_MARKER,changer))-- () omits gsub count
end

localfunctionprocessItem(item,nowrapitems)
ifitem:sub(1,2)=='{|'then
-- Applying nowrap to lines in a table does not make sense.
-- Add newlines to compensate for trim of x in |parm=x in a template.
return'\n'..item..'\n'
end
ifnowrapitems=='yes'then
locallines={}
forlinein(item..'\n'):gmatch('([^\n]*)\n')do
localprefix,content=line:match('^([*:;#]+)%s*(.*)')
ifprefixandnotcontent:match('^<span class= "nowrap" >')then
line=prefix..'<span class= "nowrap" >'..content..'</span>'
end
table.insert(lines,line)
end
item=table.concat(lines,'\n')
end
ifitem:match('^[*:;#]')then
return'\n'..item..'\n'
end
returnitem
end

localfunctionrenderNavBar(titleCell)

ifargs.navbar~='off'andargs.navbar~='plain'andnot(notargs.nameandmw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$','')=='Template:Navbox')then
titleCell:wikitext(navbar{
args.name,
mini=1,
fontstyle=(args.basestyleor'')..';'..(args.titlestyleor'')..';background:none transparent;border:none;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;'
})
end

end

--
-- Title row
--
localfunctionrenderTitleRow(tbl)
ifnotargs.titlethenreturnend

localtitleRow=tbl:tag('tr')

ifargs.titlegroupthen
titleRow
:tag('th')
:attr('scope','row')
:addClass('navbox-group')
:addClass(args.titlegroupclass)
:cssText(args.basestyle)
:cssText(args.groupstyle)
:cssText(args.titlegroupstyle)
:wikitext(args.titlegroup)
end

localtitleCell=titleRow:tag('th'):attr('scope','col')

ifargs.titlegroupthen
titleCell
:css('border-left','2px solid #fdfdfd')
:css('width','100%')
end

localtitleColspan=2
ifargs.imageleftthentitleColspan=titleColspan+1end
ifargs.imagethentitleColspan=titleColspan+1end
ifargs.titlegroupthentitleColspan=titleColspan-1end

titleCell
:cssText(args.basestyle)
:cssText(args.titlestyle)
:addClass('navbox-title')
:attr('colspan',titleColspan)

renderNavBar(titleCell)

titleCell
:tag('div')
:attr('id',mw.uri.anchorEncode(args.title))
:addClass(args.titleclass)
:css('font-size','114%')
:css('margin','0 4em')
:wikitext(processItem(args.title))
end

--
-- Above/Below rows
--

localfunctiongetAboveBelowColspan()
localret=2
ifargs.imageleftthenret=ret+1end
ifargs.imagethenret=ret+1end
returnret
end

localfunctionrenderAboveRow(tbl)
ifnotargs.abovethenreturnend

tbl:tag('tr')
:tag('td')
:addClass('navbox-abovebelow')
:addClass(args.aboveclass)
:cssText(args.basestyle)
:cssText(args.abovestyle)
:attr('colspan',getAboveBelowColspan())
:tag('div')
:wikitext(processItem(args.above,args.nowrapitems))
end

localfunctionrenderBelowRow(tbl)
ifnotargs.belowthenreturnend

tbl:tag('tr')
:tag('td')
:addClass('navbox-abovebelow')
:addClass(args.belowclass)
:cssText(args.basestyle)
:cssText(args.belowstyle)
:attr('colspan',getAboveBelowColspan())
:tag('div')
:wikitext(processItem(args.below,args.nowrapitems))
end

--
-- List rows
--
localfunctionrenderListRow(tbl,index,listnum)
localrow=tbl:tag('tr')

ifindex==1andargs.imageleftthen
row
:tag('td')
:addClass('navbox-image')
:addClass(args.imageclass)
:css('width','1px')-- Minimize width
:css('padding','0px 2px 0px 0px')
:cssText(args.imageleftstyle)
:attr('rowspan',#listnums)
:tag('div')
:wikitext(processItem(args.imageleft))
end

ifargs['group'..listnum]then
localgroupCell=row:tag('th')

groupCell
:attr('scope','row')
:addClass('navbox-group')
:addClass(args.groupclass)
:cssText(args.basestyle)
:css('width',args.groupwidthor'1%')-- If groupwidth not specified, minimize width

groupCell
:cssText(args.groupstyle)
:cssText(args['group'..listnum..'style'])
:wikitext(args['group'..listnum])
end

locallistCell=row:tag('td')

ifargs['group'..listnum]then
listCell
:css('text-align','left')
:css('border-left-width','2px')
:css('border-left-style','solid')
else
listCell:attr('colspan',2)
end

ifnotargs.groupwidththen
listCell:css('width','100%')
end

localrowstyle-- usually nil so cssText(rowstyle) usually adds nothing
ifindex%2==1then
rowstyle=args.oddstyle
else
rowstyle=args.evenstyle
end

locallistText=args['list'..listnum]
localoddEven=ODD_EVEN_MARKER
iflistText:sub(1,12)=='</div><table'then
-- Assume list text is for a subgroup navbox so no automatic striping for this row.
oddEven=listText:find('<th[^>]* "navbox%-title" ')andRESTART_MARKERor'odd'
end
listCell
:css('padding','0px')
:cssText(args.liststyle)
:cssText(rowstyle)
:cssText(args['list'..listnum..'style'])
:addClass('navbox-list')
:addClass('navbox-'..oddEven)
:addClass(args.listclass)
:tag('div')
:css('padding',(index==1andargs.list1padding)orargs.listpaddingor'0em 0.25em')
:wikitext(processItem(listText,args.nowrapitems))

ifindex==1andargs.imagethen
row
:tag('td')
:addClass('navbox-image')
:addClass(args.imageclass)
:css('width','1px')-- Minimize width
:css('padding','0px 0px 0px 2px')
:cssText(args.imagestyle)
:attr('rowspan',#listnums)
:tag('div')
:wikitext(processItem(args.image))
end
end


--
-- Tracking categories
--

localfunctionneedsHorizontalLists()
ifborder=='subgroup'orargs.tracking=='no'then
returnfalse
end
locallistClasses={
['plainlist']=true,['hlist']=true,['hlist hnum']=true,
['hlist hwrap']=true,['hlist vcard']=true,['vcard hlist']=true,
['hlist vevent']=true,
}
returnnot(listClasses[args.listclass]orlistClasses[args.bodyclass])
end

localfunctionhasBackgroundColors()
for_,keyinipairs({'titlestyle','groupstyle','basestyle','abovestyle','belowstyle'})do
iftostring(args[key]):find('background',1,true)then
returntrue
end
end
end

localfunctionhasBorders()
for_,keyinipairs({'groupstyle','basestyle','abovestyle','belowstyle'})do
iftostring(args[key]):find('border',1,true)then
returntrue
end
end
end

localfunctionisIllegible()
localstyleratio=require('Module:Color contrast')._styleratio

forkey,styleinpairs(args)do
iftostring(key):match("style$")then
ifstyleratio{mw.text.unstripNoWiki(style)}<4.5then
returntrue
end
end
end
returnfalse
end

localfunctiongetTrackingCategories()
localcats={}
ifneedsHorizontalLists()thentable.insert(cats,'Navigational boxes without horizontal lists')end
ifhasBackgroundColors()thentable.insert(cats,'Navboxes using background colours')end
ifisIllegible()thentable.insert(cats,'Potentially illegible navboxes')end
ifhasBorders()thentable.insert(cats,'Navboxes using borders')end
returncats
end

localfunctionrenderTrackingCategories(builder)
localtitle=mw.title.getCurrentTitle()
iftitle.namespace~=10thenreturnend-- not in template space
localsubpage=title.subpageText
ifsubpage=='doc'orsubpage=='sandbox'orsubpage=='testcases'thenreturnend

for_,catinipairs(getTrackingCategories())do
builder:wikitext('[[Category:'..cat..']]')
end
end

--
-- Main navbox tables
--
localfunctionrenderMainTable()
localtbl=mw.html.create('table')
:addClass('nowraplinks')
:addClass(args.bodyclass)

ifargs.titleand(args.state~='plain'andargs.state~='off')then
tbl
:addClass('collapsible')
:addClass(args.stateor'autocollapse')
end

tbl:css('border-spacing',0)
ifborder=='subgroup'orborder=='none'then
tbl
:addClass('navbox-subgroup')
:cssText(args.bodystyle)
:cssText(args.style)
else-- regular navbox - bodystyle and style will be applied to the wrapper table
tbl
:addClass('navbox-inner')
:css('background','transparent')
:css('color','inherit')
end
tbl:cssText(args.innerstyle)

renderTitleRow(tbl)
renderAboveRow(tbl)
fori,listnuminipairs(listnums)do
renderListRow(tbl,i,listnum)
end
renderBelowRow(tbl)

returntbl
end

functionp._navbox(navboxArgs)
args=navboxArgs

fork,_inpairs(args)do
iftype(k)=='string'then
locallistnum=k:match('^list(%d+)$')
iflistnumthentable.insert(listnums,tonumber(listnum))end
end
end
table.sort(listnums)

border=mw.text.trim(args.borderorargs[1]or'')
ifborder=='child'then
border='subgroup'
end

-- render the main body of the navbox
localtbl=renderMainTable()

-- render the appropriate wrapper around the navbox, depending on the border param
localres=mw.html.create()
ifborder=='none'then
localnav=res:tag('div')
:attr('role','navigation')
:node(tbl)
ifargs.titlethen
nav:attr('aria-labelledby',mw.uri.anchorEncode(args.title))
else
nav:attr('aria-label','Navbox')
end
elseifborder=='subgroup'then
-- We assume that this navbox is being rendered in a list cell of a parent navbox, and is
-- therefore inside a div with padding:0em 0.25em. We start with a </div> to avoid the
-- padding being applied, and at the end add a <div> to balance out the parent's </div>
res
:wikitext('</div>')
:node(tbl)
:wikitext('<div>')
else
localnav=res:tag('div')
:attr('role','navigation')
:addClass('navbox')
:cssText(args.bodystyle)
:cssText(args.style)
:css('padding','3px')
:node(tbl)
ifargs.titlethen
nav:attr('aria-labelledby',mw.uri.anchorEncode(args.title))
else
nav:attr('aria-label','Navbox')
end
end

renderTrackingCategories(res)

returnstriped(tostring(res))
end

functionp.navbox(frame)
ifnotgetArgsthen
getArgs=require('Module:Arguments').getArgs
end
args=getArgs(frame,{wrappers={'Template:Navbox','Template:Navbox subgroup'}})
ifframe.args.borderthen
-- This allows Template:Navbox_subgroup to use {{#invoke:Navbox|navbox|border=...}}.
args.border=frame.args.border
end

-- Read the arguments in the order they'll be output in, to make references number in the right order.
local_
_=args.title
_=args.above
fori=1,20do
_=args["group"..tostring(i)]
_=args["list"..tostring(i)]
end
_=args.below

returnp._navbox(args)
end

returnp