Module:NavboxMobile: Difference between revisions
EnWikiAdmin (talk | contribs) No edit summary |
EnWikiAdmin (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
-- Module:NavboxMobile | |||
-- This module implements {{navbox}} for MobileFrontend | -- This module implements {{navbox}} for MobileFrontend | ||
Line 6: | Line 7: | ||
local cfg = mw.loadData('Module:NavboxMobile/configuration') | local cfg = mw.loadData('Module:NavboxMobile/configuration') | ||
local navbar = require('Module:Navbar')._navbar | local navbar = require('Module:Navbar')._navbar | ||
local getArgs -- | local getArgs -- Lazily initialized | ||
local inArray = require('Module:TableTools').inArray | local inArray = require('Module:TableTools').inArray | ||
local format = string.format | local format = string.format | ||
Line 15: | Line 16: | ||
local listnums = {} | local listnums = {} | ||
-- Function to inject styles from styles.css | |||
local function addNavboxMobileStyles() | local function addNavboxMobileStyles() | ||
return mw.html.create('style') | |||
:attr('data-mw-deduplicate', 'TemplateStyles-navboxmobile') -- Ensures styles are not duplicated | |||
:wikitext(mw.loadData('Module:NavboxMobile/styles.css')) | |||
:done() | |||
end | end | ||
-- Helper function to trim whitespace | |||
local function trim(s) | local function trim(s) | ||
return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1")) | return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1")) | ||
end | end | ||
-- Helper function to add newlines based on content | |||
local function addNewline(s) | local function addNewline(s) | ||
if s:match('^[*:;#]') or s:match('^{|') then | if s:match('^[*:;#]') or s:match('^{|') then | ||
Line 35: | Line 38: | ||
end | end | ||
-- Function to add a new table row with optional gutter | |||
local function addTableRow(tbl) | local function addTableRow(tbl) | ||
if tableRowAdded then | if tableRowAdded then | ||
tbl | tbl | ||
Line 42: | Line 45: | ||
:css('height', '2px') | :css('height', '2px') | ||
:tag('td') | :tag('td') | ||
:attr('colspan',2) | :attr('colspan', 2) | ||
-- Note: You can add additional gutter styling here if needed | |||
end | end | ||
tableRowAdded = true | tableRowAdded = true | ||
return tbl:tag('tr') | return tbl:tag('tr') | ||
end | end | ||
-- Function to render the navigation bar | |||
local function renderNavBar(titleCell) | local function renderNavBar(titleCell) | ||
local spacerSide = nil | local spacerSide = nil | ||
if args.navbar == 'off' then | if args.navbar == 'off' then | ||
if args.state == 'plain' then spacerSide = 'right' end | if args.state == 'plain' then spacerSide = 'right' end | ||
elseif args.navbar == 'plain' or (not args.name and mw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$', '') == 'Template:navbox') then | elseif args.navbar == 'plain' or (not args.name and mw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$', '') == 'Template:navbox') then | ||
if args.state ~= 'plain' then spacerSide = 'left' end | if args.state ~= 'plain' then spacerSide = 'left' end | ||
else | else | ||
if args.state == 'plain' then spacerSide = 'right' end | if args.state == 'plain' then spacerSide = 'right' end | ||
Line 74: | Line 70: | ||
end | end | ||
if spacerSide then | if spacerSide then | ||
titleCell | titleCell | ||
Line 113: | Line 108: | ||
local titleColspan = 2 | local titleColspan = 2 | ||
-- | -- Adjust colspan based on image presence if needed | ||
if args.titlegroup then titleColspan = titleColspan - 1 end | if args.titlegroup then titleColspan = titleColspan - 1 end | ||
Line 137: | Line 131: | ||
local function getAboveBelowColspan() | local function getAboveBelowColspan() | ||
return 2 | |||
end | end | ||
Line 196: | Line 187: | ||
listCell:cssText(args.liststyle) | listCell:cssText(args.liststyle) | ||
listCell:addClass('navboxMobile-list') | listCell:addClass('navboxMobile-list') | ||
-- Check for nested lists (e.g., child1_groupX) | -- Check for nested lists (e.g., child1_groupX) | ||
local nestedGroup = args['child' .. listnum .. '_group1'] | local nestedGroup = args['child' .. listnum .. '_group1'] | ||
Line 235: | Line 226: | ||
end | end | ||
end | end | ||
-- | -- | ||
Line 245: | Line 235: | ||
local listClasses = {'plainlist', 'hlist', 'hlist hnum', 'hlist hwrap', 'hlist vcard', 'vcard hlist', 'hlist vevent'} | local listClasses = {'plainlist', 'hlist', 'hlist hnum', 'hlist hwrap', 'hlist vcard', 'vcard hlist', 'hlist vevent'} | ||
for | for _, cls in ipairs(listClasses) do | ||
if args.listclass == cls or args.bodyclass == cls then | if args.listclass == cls or args.bodyclass == cls then | ||
return false | return false | ||
Line 255: | Line 245: | ||
local function hasBackgroundColors() | local function hasBackgroundColors() | ||
return mw.ustring.match(args.titlestyle or '','background') or mw.ustring.match(args.groupstyle or '','background') or mw.ustring.match(args.basestyle or '','background') | return mw.ustring.match(args.titlestyle or '', 'background') | ||
or mw.ustring.match(args.groupstyle or '', 'background') | |||
or mw.ustring.match(args.basestyle or '', 'background') | |||
end | end | ||
Line 281: | Line 273: | ||
local function renderTrackingCategories(builder) | local function renderTrackingCategories(builder) | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
if title.namespace ~= 10 then return end -- | if title.namespace ~= 10 then return end -- Not in template space | ||
local subpage = title.subpageText | local subpage = title.subpageText | ||
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 | for _, cat in ipairs(getTrackingCategories()) do | ||
builder:wikitext('[[Category:' .. cat .. ']]') | builder:wikitext('[[Category:' .. cat .. ']]') | ||
end | end | ||
Line 298: | Line 290: | ||
:addClass(args.bodyclass) | :addClass(args.bodyclass) | ||
-- Uncomment and implement collapsibility if needed | |||
--[[if args.title and (args.state ~= 'plain' and args.state ~= 'off') then | --[[if args.title and (args.state ~= 'plain' and args.state ~= 'off') then | ||
tbl | tbl | ||
Line 310: | Line 303: | ||
:cssText(args.bodystyle) | :cssText(args.bodystyle) | ||
:cssText(args.style) | :cssText(args.style) | ||
else -- | else -- Regular navbox - bodystyle and style will be applied to the wrapper table | ||
tbl | tbl | ||
:addClass('navboxMobile-inner') | :addClass('navboxMobile-inner') | ||
Line 320: | Line 313: | ||
renderTitleRow(tbl) | renderTitleRow(tbl) | ||
renderAboveRow(tbl) | renderAboveRow(tbl) | ||
for | for _, listnum in ipairs(listnums) do | ||
renderListRow(tbl, listnum) | renderListRow(tbl, listnum) | ||
end | end | ||
Line 328: | Line 321: | ||
end | end | ||
-- | |||
-- Main function to render NavboxMobile | |||
-- | |||
function p._navboxMobile(navboxMobileArgs) | function p._navboxMobile(navboxMobileArgs) | ||
args = navboxMobileArgs | args = navboxMobileArgs | ||
Line 343: | Line 339: | ||
border = trim(args.border or args[1] or '') | border = trim(args.border or args[1] or '') | ||
-- | -- Render the main body of the navboxMobile | ||
local tbl = renderMainTable() | local tbl = renderMainTable() | ||
Line 357: | Line 353: | ||
local navWrapper = mw.html.create('div') | local navWrapper = mw.html.create('div') | ||
:attr('role', 'navigation') | :attr('role', 'navigation') | ||
:attr('aria-label', cfg.aria_label) | :attr('aria-label', cfg.aria_label) -- Ensure cfg.aria_label is defined appropriately | ||
:node(tbl) | :node(tbl) | ||
res:node(navWrapper) | res:node(navWrapper) | ||
elseif border == 'subgroup' or border == 'child' then | elseif border == 'subgroup' or border == 'child' then | ||
-- Assume this navboxMobile is | -- Assume this navboxMobile is being rendered in a list cell of a parent navboxMobile | ||
-- Insert closing and opening divs to manage padding | -- Insert closing and opening divs to manage padding | ||
res | res | ||
Line 375: | Line 371: | ||
:cssText(args.bodystyle) | :cssText(args.bodystyle) | ||
:cssText(args.style) | :cssText(args.style) | ||
:node(tbl) | |||
res:node(navWrapper) | res:node(navWrapper) | ||
end | end | ||
Line 388: | Line 381: | ||
end | end | ||
-- | |||
-- Entry point function for the template | |||
-- | |||
function p.navboxMobile(frame) | function p.navboxMobile(frame) | ||
if not getArgs then | if not getArgs then | ||
Line 395: | Line 391: | ||
-- 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. | ||
-- This ensures that list numbers are processed sequentially. | |||
local _ | local _ | ||
_ = args.title | _ = args.title |