Module:NavboxMobile: Difference between revisions

No edit summary
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 -- lazily initialized
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()
     local frame = mw.getCurrentFrame()
     return mw.html.create('style')
    return frame:extensionTag{
        :attr('data-mw-deduplicate', 'TemplateStyles-navboxmobile') -- Ensures styles are not duplicated
        name = 'templatestyles',
         :wikitext(mw.loadData('Module:NavboxMobile/styles.css'))
         args = { src = '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 any other rows have already been added, then we add a 2px gutter row.
     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)
    -- 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.
     local spacerSide = nil
     local spacerSide = nil


     if args.navbar == 'off' then
     if args.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.
         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
        -- No navbar. Need a spacer on the left to balance out the width of the show/hide link.
         if args.state ~= 'plain' then spacerSide = 'left' end
         if args.state ~= 'plain' then spacerSide = 'left' end
     else
     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.
         if args.state == 'plain' then spacerSide = 'right' end
         if args.state == 'plain' then spacerSide = 'right' end


Line 74: Line 70:
     end
     end


    -- Render the spacer div.
     if spacerSide then
     if spacerSide then
         titleCell
         titleCell
Line 113: Line 108:


     local titleColspan = 2
     local titleColspan = 2
     --[[if args.imageleft then titleColspan = titleColspan + 1 end
     -- Adjust colspan based on image presence if needed
    if args.image then titleColspan = titleColspan + 1 end]]
     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()
     local ret = 2
     return 2
    --[[if args.imageleft then ret = ret + 1 end
    if args.image then ret = ret + 1 end]]
    return ret
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 i, cls in ipairs(listClasses) do
     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 -- not in template space
     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 i, cat in ipairs(getTrackingCategories()) do
     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 -- regular navbox - bodystyle and style will be applied to the wrapper table
     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 i, listnum in ipairs(listnums) do
     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
     -- 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 inside a parent navboxMobile's list cell
         -- 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)
             :tag('tr')
             :node(tbl)
                :tag('td')
                    :css('padding', '2px')
                    :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