Module:Navbox
Thử mô khối đích văn đương khả dĩ tạiModule:Navbox/docSang kiến
--
-- This module will implement {{Navbox}}
--
localp={}
localnavbar=require('Module:Navbar')._navbar
localgetArgs-- lazily initialized
localargs
localtableRowAdded=false
localborder
locallistnums={}
localfunctiontrim(s)
return(mw.ustring.gsub(s,"^%s*(.-)%s*$","%1"))
end
localfunctionaddNewline(s)
ifs:match('^[*:;#]')ors:match('^{|')then
return'\n'..s..'\n'
else
returns
end
end
localfunctionaddTableRow(tbl)
-- If any other rows have already been added, then we add a 2px gutter row.
iftableRowAddedthen
tbl
:tag('tr')
:css('height','2px')
:tag('td')
:attr('colspan',3)
end
tableRowAdded=true
returntbl:tag('tr')
end
localfunctionrenderNavBar(titleCell)
-- Depending on the presence of the navbar and/or show/hide link, we may need to add a spacer div on the left
-- or right to keep the title centered.
localspacerSide=nil
ifargs.navbar=='off'then
-- No navbar, and client wants no spacer, i.e. wants the title to be shifted to the left. If there's
-- also no show/hide link, then we need a spacer on the right to achieve the left shift.
ifargs.state=='plain'thenspacerSide='right'end
elseifargs.navbar=='plain'or(notargs.nameandmw.getCurrentFrame():getParent():getTitle()=='Template:Navbox'and(border=='subgroup'orborder=='child'orborder=='none'))then
-- No navbar. Need a spacer on the left to balance out the width of the show/hide link.
ifargs.state~='plain'thenspacerSide='left'end
else
-- Will render navbar (or error message). If there's no show/hide link, need a spacer on the right
-- to balance out the width of the navbar.
ifargs.state=='plain'thenspacerSide='right'end
titleCell:wikitext(navbar{
args.name,
mini=1,
fontstyle=(args.basestyleor'')..';'..(args.titlestyleor'')..';background:none transparent;border:none;'
})
end
-- Render the spacer div.
ifspacerSidethen
titleCell
:tag('span')
:css('float',spacerSide)
:css('width','8em')
:css('font-size','80%')
:css('margin-'..(spacerSide=='left'and'right'or'left'),'0.5em')
:wikitext(' ')
end
end
--
-- Title row
--
localfunctionrenderTitleRow(tbl)
ifnotargs.titlethenreturnend
localtitleRow=addTableRow(tbl)
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')
:addClass(args.titleclass)
:attr('colspan',titleColspan)
renderNavBar(titleCell)
titleCell
:tag('div')
:css('font-size','110%')
:wikitext(addNewline(args.title))
end
--
-- Above/Below rows
--
localfunctiongetAboveBelowColspan()
localret=2
ifargs.imageleftthenret=ret+1end
ifargs.imagethenret=ret+1end
returnret
end
localfunctionrenderAboveRow(tbl)
ifnotargs.abovethenreturnend
addTableRow(tbl)
:tag('td')
:addClass('navbox-abovebelow')
:addClass(args.aboveclass)
:cssText(args.basestyle)
:cssText(args.abovestyle)
:attr('colspan',getAboveBelowColspan())
:tag('div')
:wikitext(addNewline(args.above))
end
localfunctionrenderBelowRow(tbl)
ifnotargs.belowthenreturnend
addTableRow(tbl)
:tag('td')
:addClass('navbox-abovebelow')
:addClass(args.belowclass)
:cssText(args.basestyle)
:cssText(args.belowstyle)
:attr('colspan',getAboveBelowColspan())
:tag('div')
:wikitext(addNewline(args.below))
end
--
-- List rows
--
localfunctionrenderListRow(tbl,listnum)
localrow=addTableRow(tbl)
iflistnum==1andargs.imageleftthen
row
:tag('td')
:addClass('navbox-image')
:addClass(args.imageclass)
:css('width','0%')
:css('padding','0px 2px 0px 0px')
:cssText(args.imageleftstyle)
:attr('rowspan',2*#listnums-1)
:tag('div')
:wikitext(addNewline(args.imageleft))
end
ifargs['group'..listnum]then
localgroupCell=row:tag('th')
groupCell
:attr('scope','row')
:addClass('navbox-group')
:addClass(args.groupclass)
:cssText(args.basestyle)
ifargs.groupwidththen
groupCell:css('width',args.groupwidth)
end
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
localisOdd=(listnum%2)==1
localrowstyle=args.evenstyle
ifisOddthenrowstyle=args.oddstyleend
localevenOdd
ifargs.evenodd=='swap'then
ifisOddthenevenOdd='even'elseevenOdd='odd'end
else
ifisOddthenevenOdd=args.evenoddor'odd'elseevenOdd=args.evenoddor'even'end
end
listCell
:css('padding','0px')
:cssText(args.liststyle)
:cssText(rowstyle)
:cssText(args['list'..listnum..'style'])
:addClass('navbox-list')
:addClass('navbox-'..evenOdd)
:addClass(args.listclass)
:tag('div')
:css('padding',(listnum==1andargs.list1padding)orargs.listpaddingor'0em 0.25em')
:wikitext(addNewline(args['list'..listnum]))
iflistnum==1andargs.imagethen
row
:tag('td')
:addClass('navbox-image')
:addClass(args.imageclass)
:css('width','0%')
:css('padding','0px 0px 0px 2px')
:cssText(args.imagestyle)
:attr('rowspan',2*#listnums-1)
:tag('div')
:wikitext(addNewline(args.image))
end
end
--
-- Tracking categories
--
localfunctionneedsHorizontalLists()
ifborder=='child'orborder=='subgroup'orargs.tracking=='no'thenreturnfalseend
locallistClasses={'plainlist','hlist','hlist hnum','hlist hwrap','hlist vcard','vcard hlist','hlist vevent'}
fori,clsinipairs(listClasses)do
ifargs.listclass==clsorargs.bodyclass==clsthen
returnfalse
end
end
returntrue
end
localfunctionhasBackgroundColors()
returnmw.ustring.match(args.titlestyleor'','background')ormw.ustring.match(args.groupstyleor'','background')ormw.ustring.match(args.basestyleor'','background')
end
localfunctionargNameAndRealTitleAreDifferent()
ifborder=='child'orborder=='subgroup'orargs.tracking=='no'thenreturnfalseend
ifargs.name~=mw.title.getCurrentTitle().textthen
returntrue
end
returnfalse
end
localfunctiongetTrackingCategories()
localcats={}
ifneedsHorizontalLists()thentable.insert(cats,' một hữu sử dụng thủy bình liệt biểu đích đạo hàng khuông ')end
ifhasBackgroundColors()thentable.insert(cats,' sử dụng bối cảnh nhan sắc đích đạo hàng khuông ')end
ifargNameAndRealTitleAreDifferent()thentable.insert(cats,'name tham sổ hòa thật tế bất đồng đích đạo hàng khuông ')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
fori,catinipairs(getTrackingCategories())do
builder:wikitext('[[Category:'..cat..']]')
end
end
--
-- Main navbox tables
--
localfunctionrenderMainTable()
localtbl=mw.html.create('table')
:attr('cellspacing',0)
: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=='child'orborder=='none'then
tbl
:addClass('navbox-subgroup')
:cssText(args.bodystyle)
:cssText(args.style)
else-- regular navobx - 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,listnum)
end
renderBelowRow(tbl)
returntbl
end
functionp._navbox(navboxArgs)
args=navboxArgs
fork,vinpairs(args)do
locallistnum=(''..k):match('^list(%d+)$')
iflistnumthentable.insert(listnums,tonumber(listnum))end
end
table.sort(listnums)
border=trim(args.borderorargs[1]or'')
-- 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
res:node(tbl)
elseifborder=='subgroup'orborder=='child'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>')-- mw.html vị chi trì unclosed
:node(tbl)
:wikitext('<div>')-- mw.html vị chi trì unclosed
else
res
:tag('table')
:attr('cellspacing',0)
:addClass('navbox')
:css('border-spacing',0)
:cssText(args.bodystyle)
:cssText(args.style)
:tag('tr')
:tag('td')
:css('padding','2px')
:node(tbl)
end
renderTrackingCategories(res)
returntostring(res)
end
functionp.navbox(frame)
ifnotgetArgsthen
getArgs=require('Module:Arguments').getArgs
end
args=getArgs(frame,{wrappers='Template:Navbox'})
-- 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,35do
_=args["group"..tostring(i)]
_=args["list"..tostring(i)]
end
_=args.below
returnp._navbox(args)
end
returnp