Module:Navbox: Difference between revisions

1,684 bytes added ,  2 years ago
m
12 revisions imported from wikipedia:Module:Navbox: see Topic:Vtixlm0q28eo6jtf
m>Johnuniq
(update from Module:Navbox/sandbox per talk; this implements automatic striping that applies to a navbox and any subgroup (child) navboxes it contains)
m (12 revisions imported from wikipedia:Module:Navbox: see Topic:Vtixlm0q28eo6jtf)
 
(25 intermediate revisions by 18 users not shown)
Line 9: Line 9:


local args
local args
local tableRowAdded = false
local border
local border
local listnums = {}
local listnums
local ODD_EVEN_MARKER = '\127_ODDEVEN_\127'
local ODD_EVEN_MARKER = '\127_ODDEVEN_\127'
local RESTART_MARKER = '\127_ODDEVEN0_\127'
local RESTART_MARKER = '\127_ODDEVEN0_\127'
Line 57: Line 56:
end
end


local function addNewline(s)
local function processItem(item, nowrapitems)
if s:match('^[*:;#]') or s:match('^{|') then
if item:sub(1, 2) == '{|' then
return '\n' .. s ..'\n'
-- Applying nowrap to lines in a table does not make sense.
else
-- Add newlines to compensate for trim of x in |parm=x in a template.
return s
return '\n' .. item ..'\n'
end
if nowrapitems == 'yes' then
local lines = {}
for line in (item .. '\n'):gmatch('([^\n]*)\n') do
local prefix, content = line:match('^([*:;#]+)%s*(.*)')
if prefix and not content:match('^<span class="nowrap">') then
line = prefix .. '<span class="nowrap">' .. content .. '</span>'
end
table.insert(lines, line)
end
item = table.concat(lines, '\n')
end
end
end
if item:match('^[*:;#]') then
 
return '\n' .. item ..'\n'
local function addTableRow(tbl)
-- If any other rows have already been added, then we add a 2px gutter row.
if tableRowAdded then
tbl
:tag('tr')
:css('height', '2px')
:tag('td')
:attr('colspan',2)
end
end
 
return item
tableRowAdded = true
 
return tbl:tag('tr')
end
end


Line 86: Line 85:
args.name,
args.name,
mini = 1,
mini = 1,
fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') .. ';background:none transparent;border:none;'
fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') .. ';background:none transparent;border:none;box-shadow:none;padding:0;'
})
})
end
end
Line 98: Line 97:
if not args.title then return end
if not args.title then return end


local titleRow = addTableRow(tbl)
local titleRow = tbl:tag('tr')


if args.titlegroup then
if args.titlegroup then
Line 135: Line 134:
titleCell
titleCell
:tag('div')
:tag('div')
-- id for aria-labelledby attribute
:attr('id', mw.uri.anchorEncode(args.title))
:attr('id', mw.uri.anchorEncode(args.title))
:addClass(args.titleclass)
:addClass(args.titleclass)
:css('font-size', '114%')
:css('font-size', '114%')
:css('margin', '0 4em')
:css('margin', '0 4em')
:wikitext(addNewline(args.title))
:wikitext(processItem(args.title))
end
end


Line 156: Line 156:
if not args.above then return end
if not args.above then return end


addTableRow(tbl)
tbl:tag('tr')
:tag('td')
:tag('td')
:addClass('navbox-abovebelow')
:addClass('navbox-abovebelow')
Line 164: Line 164:
:attr('colspan', getAboveBelowColspan())
:attr('colspan', getAboveBelowColspan())
:tag('div')
:tag('div')
:wikitext(addNewline(args.above))
-- id for aria-labelledby attribute, if no title
:attr('id', args.title and nil or mw.uri.anchorEncode(args.above))
:wikitext(processItem(args.above, args.nowrapitems))
end
end


Line 170: Line 172:
if not args.below then return end
if not args.below then return end


addTableRow(tbl)
tbl:tag('tr')
:tag('td')
:tag('td')
:addClass('navbox-abovebelow')
:addClass('navbox-abovebelow')
Line 178: Line 180:
:attr('colspan', getAboveBelowColspan())
:attr('colspan', getAboveBelowColspan())
:tag('div')
:tag('div')
:wikitext(addNewline(args.below))
:wikitext(processItem(args.below, args.nowrapitems))
end
end


Line 185: Line 187:
--
--
local function renderListRow(tbl, index, listnum)
local function renderListRow(tbl, index, listnum)
local row = addTableRow(tbl)
local row = tbl:tag('tr')


if index == 1 and args.imageleft then
if index == 1 and args.imageleft then
row
row
:tag('td')
:tag('td')
:addClass('noviewer')
:addClass('navbox-image')
:addClass('navbox-image')
:addClass(args.imageclass)
:addClass(args.imageclass)
:css('width', '0%')
:css('width', '1px')               -- Minimize width
:css('padding', '0px 2px 0px 0px')
:css('padding', '0px 2px 0px 0px')
:cssText(args.imageleftstyle)
:cssText(args.imageleftstyle)
:attr('rowspan', 2 * #listnums - 1)
:attr('rowspan', #listnums)
:tag('div')
:tag('div')
:wikitext(addNewline(args.imageleft))
:wikitext(processItem(args.imageleft))
end
end


if args['group' .. listnum] then
if args['group' .. listnum] then
local groupCell = row:tag('th')
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
:attr('id', mw.uri.anchorEncode(args.group1))
end


groupCell
groupCell
Line 208: Line 217:
:addClass(args.groupclass)
:addClass(args.groupclass)
:cssText(args.basestyle)
: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
groupCell
Line 255: Line 261:
:addClass('navbox-' .. oddEven)
:addClass('navbox-' .. oddEven)
:addClass(args.listclass)
:addClass(args.listclass)
:addClass(args['list' .. listnum .. 'class'])
:tag('div')
:tag('div')
:css('padding', (index == 1 and args.list1padding) or args.listpadding or '0em 0.25em')
:css('padding', (index == 1 and args.list1padding) or args.listpadding or '0em 0.25em')
:wikitext(addNewline(listText))
:wikitext(processItem(listText, args.nowrapitems))


if index == 1 and args.image then
if index == 1 and args.image then
row
row
:tag('td')
:tag('td')
:addClass('noviewer')
:addClass('navbox-image')
:addClass('navbox-image')
:addClass(args.imageclass)
:addClass(args.imageclass)
:css('width', '0%')
:css('width', '1px')               -- Minimize width
:css('padding', '0px 0px 0px 2px')
:css('padding', '0px 0px 0px 2px')
:cssText(args.imagestyle)
:cssText(args.imagestyle)
:attr('rowspan', 2 * #listnums - 1)
:attr('rowspan', #listnums)
:tag('div')
:tag('div')
:wikitext(addNewline(args.image))
:wikitext(processItem(args.image))
end
end
end
end
Line 291: Line 299:


local function hasBackgroundColors()
local function hasBackgroundColors()
for _, key in ipairs({'titlestyle', 'groupstyle', 'basestyle'}) do
for _, key in ipairs({'titlestyle', 'groupstyle', 'basestyle', 'abovestyle', 'belowstyle'}) do
if tostring(args[key]):find('background', 1, true) then
if tostring(args[key]):find('background', 1, true) then
return true
end
end
end
local function hasBorders()
for _, key in ipairs({'groupstyle', 'basestyle', 'abovestyle', 'belowstyle'}) do
if tostring(args[key]):find('border', 1, true) then
return true
return true
end
end
Line 316: Line 332:
if hasBackgroundColors() then table.insert(cats, 'Navboxes using background colours') end
if hasBackgroundColors() then table.insert(cats, 'Navboxes using background colours') end
if isIllegible() then table.insert(cats, 'Potentially illegible navboxes') end
if isIllegible() then table.insert(cats, 'Potentially illegible navboxes') end
if hasBorders() then table.insert(cats, 'Navboxes using borders') end
return cats
return cats
end
end
Line 325: Line 342:
if subpage == 'doc' or subpage == 'sandbox' or subpage == 'testcases' then return end
if subpage == 'doc' or subpage == 'sandbox' or subpage == 'testcases' then return end


for i, cat in ipairs(getTrackingCategories()) do
for _, cat in ipairs(getTrackingCategories()) do
builder:wikitext('[[Category:' .. cat .. ']]')
builder:wikitext('[[Category:' .. cat .. ']]')
end
end
Line 339: Line 356:


if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
if args.state == 'collapsed' then args.state = 'mw-collapsed' end
tbl
tbl
:addClass('collapsible')
:addClass('mw-collapsible')
:addClass(args.state or 'autocollapse')
:addClass(args.state or 'autocollapse')
end
end
Line 370: Line 388:
function p._navbox(navboxArgs)
function p._navbox(navboxArgs)
args = navboxArgs
args = navboxArgs
listnums = {}


for k, v in pairs(args) do
for k, _ in pairs(args) do
local listnum = ('' .. k):match('^list(%d+)$')
if type(k) == 'string' then
if listnum then table.insert(listnums, tonumber(listnum)) end
local listnum = k:match('^list(%d+)$')
if listnum then table.insert(listnums, tonumber(listnum)) end
end
end
end
table.sort(listnums)
table.sort(listnums)
Line 391: Line 412:
:attr('role', 'navigation')
:attr('role', 'navigation')
:node(tbl)
:node(tbl)
if args.title then
-- aria-labelledby title, otherwise above, otherwise lone group
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title))
if args.title or args.above or (args.group1 and not args.group2) then
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above or args.group1))
else
else
nav:attr('aria-label', 'Navbox')
nav:attr('aria-label', 'Navbox')
Line 408: Line 430:
:attr('role', 'navigation')
:attr('role', 'navigation')
:addClass('navbox')
:addClass('navbox')
:addClass(args.navboxclass)
:cssText(args.bodystyle)
:cssText(args.bodystyle)
:cssText(args.style)
:cssText(args.style)
:css('padding', '3px')
:css('padding', '3px')
:node(tbl)
:node(tbl)
if args.title then
-- aria-labelledby title, otherwise above, otherwise lone group
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title))
if args.title or args.above or (args.group1 and not args.group2) then
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above or args.group1))
else
else
nav:attr('aria-label', 'Navbox')
nav:attr('aria-label', 'Navbox')
Line 419: Line 443:
end
end


renderTrackingCategories(res)
if (args.nocat or 'false'):lower() == 'false' then
 
renderTrackingCategories(res)
end
return striped(tostring(res))
return striped(tostring(res))
end
end
Line 428: Line 453:
getArgs = require('Module:Arguments').getArgs
getArgs = require('Module:Arguments').getArgs
end
end
args = getArgs(frame, {wrappers = 'Template:Navbox'})
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.
-- Read the arguments in the order they'll be output in, to make references number in the right order.