Module:Navbox: Difference between revisions

m
87 revisions imported from wikipedia:Module:Navbox: this is nice to have
(update from Module:Navbox/sandbox per talk; this implements automatic striping that applies to a navbox and any subgroup (child) navboxes it contains)
m (87 revisions imported from wikipedia:Module:Navbox: this is nice to have)
 
(22 intermediate revisions by 11 users not shown)
Line 9:
 
local args
local tableRowAdded = false
local border
local listnums = {}
local ODD_EVEN_MARKER = '\127_ODDEVEN_\127'
local RESTART_MARKER = '\127_ODDEVEN0_\127'
Line 57 ⟶ 56:
end
 
local function addNewlineprocessItem(sitem, nowrapitems)
if sitem:matchsub('^[*:;#]'1, 2) or== s:match('^{|') then
-- Applying nowrap to lines in a table does not make sense.
return '\n' .. s ..'\n'
-- Add newlines to compensate for trim of x in |parm=x in a template.
else
return s'\n' .. item ..'\n'
end
if nowrapitems == 'yes' then
end
local lines = {}
 
for line in (item .. '\n'):gmatch('([^\n]*)\n') do
local function addTableRow(tbl)
local prefix, content = line:match('^([*:;#]+)%s*(.*)')
-- If any other rows have already been added, then we add a 2px gutter row.
if prefix and not content:match('^<span class="nowrap">') then
if tableRowAdded then
line = prefix .. '<span class="nowrap">' .. content .. '</span>'
tbl
:tag('tr')end
:csstable.insert('height'lines, '2px'line)
end
:tag('td')
item = table.concat(lines, '\n')
:attr('colspan',2)
end
if item:match('^[*:;#]') then
 
return '\n' .. sitem ..'\n'
tableRowAdded = true
end
 
return tbl:tag('tr')item
end
 
Line 86 ⟶ 85:
args.name,
mini = 1,
fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') .. ';background:none transparent;border:none;box-shadow:none;padding:0;'
})
end
Line 98 ⟶ 97:
if not args.title then return end
 
local titleRow = addTableRow(tbl:tag('tr')
 
if args.titlegroup then
Line 135 ⟶ 134:
titleCell
:tag('div')
-- id for aria-labelledby attribute
:attr('id', mw.uri.anchorEncode(args.title))
:addClass(args.titleclass)
:css('font-size', '114%')
:css('margin', '0 4em')
:wikitext(addNewlineprocessItem(args.title))
end
 
Line 156:
if not args.above then return end
 
tbl:tag('tdtr')
addTableRow(tbl)
:tag('td')
:addClass('navbox-abovebelow')
Line 164:
:attr('colspan', getAboveBelowColspan())
:tag('div')
-- id for aria-labelledby attribute, if no title
:wikitext(addNewline(args.above))
nav :attr('aria-labelledbyid', args.title and nil or mw.uri.anchorEncode(args.titleabove))
:wikitext(addNewlineprocessItem(args.above, args.nowrapitems))
end
 
Line 170 ⟶ 172:
if not args.below then return end
 
tbl:tag('tr')
addTableRow(tbl)
:tag('td')
:addClass('navbox-abovebelow')
Line 178 ⟶ 180:
:attr('colspan', getAboveBelowColspan())
:tag('div')
:wikitext(addNewlineprocessItem(args.below, args.nowrapitems))
end
 
Line 185 ⟶ 187:
--
local function renderListRow(tbl, index, listnum)
local row = addTableRow(tbl:tag('tr')
 
if index == 1 and args.imageleft then
row
:tag('td')
:addClass('noviewer')
:addClass('navbox-image')
:addClass(args.imageclass)
:css('width', '0%1px') -- Minimize width
:css('padding', '0px 2px 0px 0px')
:cssText(args.imageleftstyle)
:attr('rowspan', 2 * #listnums - 1)
:tag('div')
:wikitext(addNewlineprocessItem(args.imageleft))
end
 
if args['group' .. listnum] then
local groupCell = row:tag('th')
 
-- id for aria-labelledby attribute, if lone group with no title or above
if listnum == 1 and not (args.title or args.above or args.group2) then
groupCell
nav :attr('aria-labelledbyid', mw.uri.anchorEncode(args.titlegroup1))
end
 
groupCell
Line 208 ⟶ 217:
:addClass(args.groupclass)
:cssText(args.basestyle)
:css('width', args.groupwidth or '1%') -- If groupwidth not specified, minimize width
 
if args.groupwidth then
groupCell:css('width', args.groupwidth)
end
 
groupCell
Line 255 ⟶ 261:
:addClass('navbox-' .. oddEven)
:addClass(args.listclass)
:addClass(args['list' .. listnum .. 'class'])
:tag('div')
:css('padding', (index == 1 and args.list1padding) or args.listpadding or '0em 0.25em')
:wikitext(addNewlineprocessItem(listText, args.nowrapitems))
 
if index == 1 and args.image then
row
:tag('td')
:addClass('noviewer')
:addClass('navbox-image')
:addClass(args.imageclass)
:css('width', '0%1px') -- Minimize width
:css('padding', '0px 0px 0px 2px')
:cssText(args.imagestyle)
:attr('rowspan', 2 * #listnums - 1)
:tag('div')
:wikitext(addNewlineprocessItem(args.image))
end
end
Line 291 ⟶ 299:
 
local function hasBackgroundColors()
for _, key in ipairs({'titlestyle', 'groupstyle', 'basestyle', 'abovestyle', 'belowstyle'}) do
if tostring(args[key]):find('background', 1, true) then
return true
end
end
end
 
local function addTableRowhasBorders(tbl)
for _, key in ipairs({'groupstyle', 'basestyle', 'abovestyle', 'belowstyle'}) do
if tostring(args[key]):find('border', 1, true) then
return true
end
Line 316 ⟶ 332:
if hasBackgroundColors() then table.insert(cats, 'Navboxes using background colours') end
if isIllegible() then table.insert(cats, 'Potentially illegible navboxes') end
if hasBorders() then table.insert(cats, 'Navboxes using borders') end
return cats
end
Line 325 ⟶ 342:
if subpage == 'doc' or subpage == 'sandbox' or subpage == 'testcases' then return end
 
for i_, cat in ipairs(getTrackingCategories()) do
builder:wikitext('[[Category:' .. cat .. ']]')
end
Line 339 ⟶ 356:
 
if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
if args.state == 'collapsed' then args.state = 'mw-collapsed' end
tbl
:addClass('mw-collapsible')
:addClass(args.state or 'autocollapse')
end
Line 370 ⟶ 388:
function p._navbox(navboxArgs)
args = navboxArgs
listnums = {}
 
for k, v_ in pairs(args) do
if type(k) == 'string' then
local listnum = ('' .. k):match('^list(%d+)$')
if local listnum then= table.insertk:match(listnums, tonumber'^list(listnum%d+)$') end
if listnum then table.insert(listnums, tonumber(listnum)) end
end
end
table.sort(listnums)
Line 391 ⟶ 412:
:attr('role', 'navigation')
:node(tbl)
-- aria-labelledby title, otherwise above, otherwise lone group
if args.title then
if args.title or args.above or (args.group1 and not args.group2) then
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title))
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above or args.group1))
else
nav:attr('aria-label', 'Navbox')
Line 408 ⟶ 430:
:attr('role', 'navigation')
:addClass('navbox')
:addClass(args.navboxclass)
:cssText(args.bodystyle)
:cssText(args.style)
:css('padding', '3px')
:node(tbl)
-- aria-labelledby title, otherwise above, otherwise lone group
if args.title then
if args.title or args.above or (args.group1 and not args.group2) then
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title))
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above or args.group1))
else
nav:attr('aria-label', 'Navbox')
Line 419 ⟶ 443:
end
 
if (args.nocat or 'false'):lower() == 'false' then
renderTrackingCategories(res)
 
end
return striped(tostring(res))
end
Line 428 ⟶ 453:
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.