Difference between revisions of "Module:Navbox"

2,720 bytes added ,  12:21, 10 October 2021
no edit summary
m (1 revision imported)
 
Line 10: Line 10:
local args
local args
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 77: Line 77:
end
end
return item
return item
end
-- Separate function so that we can evaluate properly whether hlist should
-- be added by the module
local function has_navbar()
return args.navbar ~= 'off' and args.navbar ~= 'plain' and
(args.name or mw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$', '') ~= 'Template:Navbox')
end
end


local function renderNavBar(titleCell)
local function renderNavBar(titleCell)


if args.navbar ~= 'off' and args.navbar ~= 'plain' and not (not args.name and mw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$', '') == 'Template:Navbox') then
if has_navbar() then
titleCell:wikitext(navbar{
titleCell:wikitext(navbar{
args.name,
args.name,
-- we depend on this being mini = 1 when the navbox module decides
-- to add hlist templatestyles. we also depend on navbar outputting
-- a copy of the hlist templatestyles.
mini = 1,
mini = 1,
fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') .. ';background:none transparent;border:none;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;'
fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') .. ';background:none transparent;border:none;box-shadow:none; padding:0;'
})
})
end
end
Line 115: Line 125:
if args.titlegroup then
if args.titlegroup then
titleCell
titleCell
:css('border-left', '2px solid #fdfdfd')
:addClass('navbox-title1')
:css('width', '100%')
end
end


Line 216: Line 225:
:addClass(args.groupclass)
:addClass(args.groupclass)
:cssText(args.basestyle)
:cssText(args.basestyle)
            :css('width', args.groupwidth or '1%') -- If groupwidth not specified, minimize width
:css('width', args.groupwidth or '1%') -- If groupwidth not specified, minimize width


groupCell
groupCell
Line 228: Line 237:
if args['group' .. listnum] then
if args['group' .. listnum] then
listCell
listCell
:css('text-align', 'left')
:addClass('navbox-list1')
:css('border-left-width', '2px')
:css('border-left-style', 'solid')
else
else
listCell:attr('colspan', 2)
listCell:attr('colspan', 2)
Line 260: Line 267:
: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')
Line 293: Line 301:
}
}
return not (listClasses[args.listclass] or listClasses[args.bodyclass])
return not (listClasses[args.listclass] or listClasses[args.bodyclass])
end
-- there are a lot of list classes in the wild, so we have a function to find
-- them and add their TemplateStyles
local function addListStyles()
local frame = mw.getCurrentFrame()
-- TODO?: Should maybe take a table of classes for e.g. hnum, hwrap as above
-- I'm going to do the stupid thing first though
-- Also not sure hnum and hwrap are going to live in the same TemplateStyles
-- as hlist
local function _addListStyles(htmlclass, templatestyles)
local class_args = { -- rough order of probability of use
'bodyclass', 'listclass', 'aboveclass', 'belowclass', 'titleclass',
'navboxclass', 'groupclass', 'titlegroupclass', 'imageclass'
}
local patterns = {
'^' .. htmlclass .. '$',
'%s' .. htmlclass .. '$',
'^' .. htmlclass .. '%s',
'%s' .. htmlclass .. '%s'
}
local found = false
for _, arg in ipairs(class_args) do
for _, pattern in ipairs(patterns) do
if mw.ustring.find(args[arg] or '', pattern) then
found = true
break
end
end
if found then break end
end
if found then
return frame:extensionTag{
name = 'templatestyles', args = { src = templatestyles }
}
else
return ''
end
end
local hlist_styles = ''
-- navbar always has mini = 1, so here (on this wiki) we can assume that
-- we don't need to output hlist styles in navbox again.
if not has_navbar() then
hlist_styles = _addListStyles('hlist', 'Flatlist/styles.css')
end
local plainlist_styles = _addListStyles('plainlist', 'Plainlist/styles.css')
return hlist_styles .. plainlist_styles
end
end


Line 312: Line 370:


local function isIllegible()
local function isIllegible()
local styleratio = require('Module:Color contrast')._styleratio
-- require('Module:Color contrast') absent on mediawiki.org
 
for key, style in pairs(args) do
if tostring(key):match("style$") then
if styleratio{mw.text.unstripNoWiki(style)} < 4.5 then
return true
end
end
end
return false
return false
end
end
Line 353: Line 403:


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 384: Line 435:
function p._navbox(navboxArgs)
function p._navbox(navboxArgs)
args = navboxArgs
args = navboxArgs
listnums = {}


for k, _ in pairs(args) do
for k, _ in pairs(args) do
Line 400: Line 452:
-- render the main body of the navbox
-- render the main body of the navbox
local tbl = renderMainTable()
local tbl = renderMainTable()
 
-- get templatestyles
local frame = mw.getCurrentFrame()
local base_templatestyles = frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Navbox/styles.css' }
}
local templatestyles = ''
if args.templatestyles and args.templatestyles ~= '' then
templatestyles = frame:extensionTag{
name = 'templatestyles', args = { src = args.templatestyles }
}
end
local res = mw.html.create()
-- 'navbox-styles' exists for two reasons:
--  1. To wrap the styles to work around phab: T200206 more elegantly. Instead
--   of combinatorial rules, this ends up being linear number of CSS rules.
--  2. To allow MobileFrontend to rip the styles out with 'nomobile' such that
--    they are not dumped into the mobile view.
res:tag('div')
:addClass('navbox-styles')
:addClass('nomobile')
:wikitext(base_templatestyles .. templatestyles)
:done()
-- render the appropriate wrapper around the navbox, depending on the border param
-- render the appropriate wrapper around the navbox, depending on the border param
local res = mw.html.create()
if border == 'none' then
if border == 'none' then
local nav = res:tag('div')
local nav = res:tag('div')
:attr('role', 'navigation')
:attr('role', 'navigation')
:wikitext(addListStyles())
:node(tbl)
:node(tbl)
-- aria-labelledby title, otherwise above, otherwise lone group
-- aria-labelledby title, otherwise above, otherwise lone group
Line 414: Line 490:
end
end
elseif border == 'subgroup' then
elseif border == 'subgroup' then
-- We assume that this navbox is being rendered in a list cell of a parent navbox, and is
-- We assume that this navbox is being rendered in a list cell of a
-- therefore inside a div with padding:0em 0.25em. We start with a </div> to avoid the
-- parent navbox, and is therefore inside a div with padding:0em 0.25em.
-- padding being applied, and at the end add a <div> to balance out the parent's </div>
-- 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
res
:wikitext('</div>')
:wikitext('</div>')
:wikitext(addListStyles())
:node(tbl)
:node(tbl)
:wikitext('<div>')
:wikitext('<div>')
Line 425: Line 503:
: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')
:wikitext(addListStyles())
:node(tbl)
:node(tbl)
-- aria-labelledby title, otherwise above, otherwise lone group
-- aria-labelledby title, otherwise above, otherwise lone group
Line 437: Line 517:
end
end


renderTrackingCategories(res)
if (args.nocat or 'false'):lower() == 'false' then
renderTrackingCategories(res)
end


return striped(tostring(res))
return striped(tostring(res))