Module:NavboxMobile: Difference between revisions

No edit summary
No edit summary
 
(133 intermediate revisions by the same user not shown)
Line 50: Line 50:


local function add_list_styles()
local function add_list_styles()
local frame = mw.getCurrentFrame()
    local frame = mw.getCurrentFrame()
local function add_list_templatestyles(htmlclass, templatestyles)
    local function add_list_templatestyles(htmlclass, templatestyles)
if has_list_class(htmlclass) then
        if has_list_class(htmlclass) then
return frame:extensionTag{
            return frame:extensionTag{
name = 'templatestyles', args = { src = templatestyles }
                name = 'templatestyles', args = { src = templatestyles }
}
            }
else
        else
return ''
            return ''
end
        end
end
    end
       
local hlist_styles = add_list_templatestyles('hlist', cfg.hlist_templatestyles)
    local hlist_styles = add_list_templatestyles('hlist', cfg.hlist_templatestyles)
local plainlist_styles = add_list_templatestyles('plainlist', cfg.plainlist_templatestyles)
    local plainlist_styles = add_list_templatestyles('plainlist', cfg.plainlist_templatestyles)
   
-- a second workaround for [[phab:T303378]]
    -- a second workaround for [[phab:T303378]]
-- when that issue is fixed, we can actually use has_navbar not to emit the
    -- when that issue is fixed, we can actually use has_navbar not to emit the
-- tag here if we want
    -- tag here if we want
if has_navbar() and hlist_styles == '' then
    if has_navbar() and hlist_styles == '' then
hlist_styles = frame:extensionTag{
        hlist_styles = frame:extensionTag{
name = 'templatestyles', args = { src = cfg.hlist_templatestyles }
            name = 'templatestyles', args = { src = cfg.hlist_templatestyles }
}
        }
end
    end
   
-- hlist -> plainlist is best-effort to preserve old Common.css ordering.
    -- hlist -> plainlist is best-effort to preserve old Common.css ordering.
-- this ordering is not a guarantee because most navboxes will emit only
    -- this ordering is not a guarantee because most navboxes will emit only
-- one of these classes [hlist_note]
    -- one of these classes [hlist_note]
return hlist_styles .. plainlist_styles
    return hlist_styles .. plainlist_styles
end
end


Line 95: Line 95:


     -- Combine list styles with base and user styles
     -- Combine list styles with base and user styles
     local list_styles = add_list_styles() -- Ensure this function is defined or required appropriately
     local list_styles = add_list_styles()
     local base_templatestyles = cfg.templatestyles
     local base_templatestyles = cfg.templatestyles
     local templatestyles = add_user_styles(args[cfg.arg.templatestyles])
     local templatestyles = add_user_styles(args[cfg.arg.templatestyles])
Line 141: Line 141:
     end
     end
end
end


local function processItem(item, nowrapitems)
local function processItem(item, nowrapitems)
Line 149: Line 150:
         -- Applying nowrap to lines in a table does not make sense.
         -- Applying nowrap to lines in a table does not make sense.
         -- Add newlines to compensate for trim of x in |parm=x in a template.
         -- Add newlines to compensate for trim of x in |parm=x in a template.
        item = item:gsub('|%s*width%s*=%s*"[%d%w]+"', '| width="100%"')
         return '\n' .. item .. '\n'
         return '\n' .. item .. '\n'
     end
     end
     if nowrapitems == cfg.keyword.nowrapitems_yes then
     if nowrapitems == cfg.keyword.nowrapitems_yes or true then
         local lines = {}
         local lines = {}
         for line in (item .. '\n'):gmatch('([^\n]*)\n') do
         for line in (item .. '\n'):gmatch('([^\n]*)\n') do
Line 165: Line 167:
         return '\n' .. item .. '\n'
         return '\n' .. item .. '\n'
     end
     end
    -- Ensure no excessive whitespace
    item = mw.ustring.gsub(item, "%s+", " ")
     return item
     return item
end
end




-- Function to add a new table row with optional gutter
-- Function to add a new table row with optional gutter
local function addTableRow(tbl)
local function addTableRow(tbl)
     if tableRowAdded then
    -- Check if the spacer rows are necessary based on the listclass or bodyclass
         tbl
    local suppressSpacer = false
            :tag('tr')
    if args.listclass then
                :css('height', '2px')
        suppressSpacer = suppressSpacer or args.listclass:find("hlist")
                :tag('td')
    end
                    :attr('colspan', 2)
    if args.bodyclass then
        -- Note: You can add additional gutter styling here if needed
        suppressSpacer = suppressSpacer or args.bodyclass:find("hlist")
    end
 
    -- Only add spacer rows if they are not explicitly suppressed
     if tableRowAdded and not suppressSpacer then
         tbl:tag('tr')
            :css('height', '2px')
            :tag('td')
                :attr('colspan', 2)
                :css('background-color', 'transparent') -- Ensure spacer is invisible
     end
     end
     tableRowAdded = true
     tableRowAdded = true
     return tbl:tag('tr')
     return tbl:tag('tr')
Line 185: Line 198:


-- Function to render the navigation bar
-- Function to render the navigation bar
local function renderNavBar(titleCell)
local function renderNavBar(cell)
    local spacerSide = nil
 
     if args.navbar == 'off' then
     if args.navbar == 'off' then
         if args.state == 'plain' then spacerSide = 'right' end
         return -- Don't render anything if Navbar is off
    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
    else
        if args.state == 'plain' then spacerSide = 'right' end
 
        titleCell:wikitext(navbar{
            args.name,
            mini = 1,
            fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') ..  ';background:none transparent;border:none;'
        })
     end
     end


     if spacerSide then
     -- Render the Navbar
         titleCell
    cell:wikitext(navbar{
            :tag('span')
        args.name,
                :css('float', spacerSide)
         mini = 1,
                :css('width', '6em')
        fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') .. ';background:none transparent;border:none;'
                :wikitext(' ')
     })
     end
end
end


--
--
Line 218: Line 219:


     local titleRow = addTableRow(tbl)
     local titleRow = addTableRow(tbl)
    if args.titlegroup then
        titleRow
            :tag('th')
                :attr('scope', 'row')
                :addClass('navboxMobile-group-content')
                :addClass(args.titlegroupclass)
                :cssText(args.basestyle)
                :cssText(args.groupstyle)
                :cssText(args.titlegroupstyle)
                :wikitext(args.titlegroup)
    end
     local titleCell = titleRow:tag('th'):attr('scope', 'col')
     local titleCell = titleRow:tag('th'):attr('scope', 'col')
    if args.titlegroup then
        titleCell
            :css('border-left', '2px solid #fdfdfd')
            :css('width', '100%')
    end
    local titleColspan = 2
    if args.titlegroup then titleColspan = titleColspan - 1 end
    titleCell
         :addClass('navboxMobile-title')
         :addClass('navboxMobile-title')
         :addClass(cfg.class.navbox_list) -- Add consistent list styling
         :addClass(cfg.class.navbox_list)
         :addClass(args[cfg.arg.titleclass]) -- Additional title-specific class
         :addClass(args[cfg.arg.titleclass])
         :attr('colspan', titleColspan)
        :attr('data-level', 1)
         :attr('colspan', 2)
         :cssText(args.basestyle)
         :cssText(args.basestyle)
         :cssText(args.titlestyle)
         :cssText(args.titlestyle)
        :css('position', 'relative')  -- Ensure relative positioning for absolute children


     renderNavBar(titleCell)
     -- Add the title content, centered across the full width
 
     titleCell
     titleCell
         :tag('div')
         :tag('div')
             :css('font-size', '114%')
             :css('margin', '0 auto')
             :css('padding', args[cfg.arg.list1padding] or args[cfg.arg.listpadding] or '0 0.25em')
             :css('text-align', 'center')
             :wikitext(addNewline(args.title))
             :wikitext(addNewline(args.title))
    -- Add the navbar, positioned absolutely in the top-right corner
    if has_navbar() then
        titleCell
            :tag('div')
                :addClass('navboxMobile-navbar')
                :css('position', 'absolute')
                :css('top', '0')
                :css('right', '0')
                :css('padding', '0.2em')  -- Optional: Adjust padding for alignment
                :wikitext(navbar{
                    args.name,
                    mini = 1,
                    fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') .. ';background:none transparent;border:none;'
                })
    end
end
end


--
--
Line 283: Line 276:
                 :wikitext(addNewline(args.above))
                 :wikitext(addNewline(args.above))
end
end




Line 303: Line 297:




local function renderListRow(tbl, listnum)
local function renderListRow(tbl, path, parentLevel)
     -- Add a new row for the group
     -- Initialize path and parentLevel
     local row = addTableRow(tbl)
    path = path or ""
     parentLevel = parentLevel or 2 -- Start at level 2 for child groups


     -- Process group cell
     -- Keep track of rendered children to avoid duplicates
     if args['group' .. listnum] then
     local renderedChildren = {}
        local groupCell = row:tag('th')
        groupCell
            :attr('scope', 'row')
            :addClass('navboxMobile-group-content')
            :cssText(args.basestyle)
            :cssText(args.groupstyle)
            :cssText(args['group' .. listnum .. 'style'])
            :wikitext(processItem(args['group' .. listnum]))
    end


     -- Add a new row for the list
     for i = 1, 10 do
    row = addTableRow(tbl)
        local groupKey = (path ~= "" and "child" .. path .. "_" or "") .. "group" .. i
    local listCell = row:tag('td')
        local listKey = (path ~= "" and "child" .. path .. "_" or "") .. "list" .. i
    listCell:attr('colspan', 2) -- Span across the full width
    listCell:css('width', '100%')


    -- Determine odd/even styling for the row
        -- Skip if this group has already been rendered
    local rowStyle = (listnum % 2 == 1) and args.oddstyle or args.evenstyle
        if not renderedChildren[listKey] and args[listKey] then
            renderedChildren[listKey] = true -- Track rendered lists


    -- Add classes for styling and dynamic list attributes
            -- Render the group (heading)
    listCell
            local groupRow = addTableRow(tbl)
        :addClass('navboxMobile-list-content')
            local groupCell = groupRow:tag('th')
        :addClass('navboxMobile-list')
                :attr('scope', 'row')
        :addClass((listnum % 2 == 1) and 'navboxMobile-odd' or 'navboxMobile-even')
                :addClass('navboxMobile-group-content')
        :cssText(args.liststyle)
                :addClass('navboxMobile-group-level' .. parentLevel) -- Correct level
        :cssText(args['list' .. listnum .. 'style'])
                :attr('data-level', parentLevel) -- Optional attribute for CSS targeting
        :cssText(rowStyle)
                :wikitext(processItem(args[groupKey]))


    -- Handle nested subgroups dynamically
            -- Check if the list is non-empty before rendering content
    if args['list' .. listnum] == 'child' then
            if args[listKey] and args[listKey] ~= "" then
        local childArgs = { border = 'subgroup', navbar = 'plain' }
                if args[listKey] == "child" then
        for k, v in pairs(args) do
                    -- Check if the child contains valid groups or lists
            local prefix = 'child' .. listnum .. '_'
                    local childHasContent = false
            if k:sub(1, #prefix) == prefix then
                    for j = 1, 10 do
                childArgs[k:sub(#prefix + 1)] = v -- Remove the "childX_" prefix
                        local childGroupKey = "child" .. (path ~= "" and path .. "_" or "") .. i .. "_group" .. j
            end
                        local childListKey = "child" .. (path ~= "" and path .. "_" or "") .. i .. "_list" .. j
        end
                        if args[childGroupKey] or args[childListKey] then
        local nestedContent = p._navboxMobile(childArgs) -- Recursively render the child content
                            childHasContent = true
        listCell:wikitext(nestedContent)
                            break
    else
                        end
        -- Check for inline lists
                    end
        local listContent = processItem(args['list' .. listnum], args.nowrapitems)


        -- Check for nested lists or subgroups
                    if childHasContent then
        local nestedTbl = nil
                        -- Recursively render child lists
        for i = 1, 10 do -- Assume a maximum of 10 nested groups/lists
                        local newPath = (path ~= "" and path .. "_" or "") .. i
            local nestedGroup = args['child' .. listnum .. '_group' .. i]
                        renderListRow(tbl, tostring(newPath), parentLevel + 1)
            local nestedList = args['child' .. listnum .. '_list' .. i]
                    end
 
                else
            if not nestedGroup and not nestedList then break end -- Stop if no more nested items
                    -- Render flat lists
 
                    local listRow = addTableRow(tbl)
            if not nestedTbl then
                     local listCell = listRow:tag('td')
                nestedTbl = mw.html.create('table')
                        :attr('colspan', 2)
                     :addClass('navboxMobile-subgroup')
                        :addClass('navboxMobile-list-content') -- Base content class
                    :css('width', '100%')
                        :addClass('navboxMobile-list-level' .. parentLevel) -- Correct level
            end
                        :addClass((i % 2 == 1) and 'navboxMobile-odd' or 'navboxMobile-even') -- Odd/even class
 
                        :addClass(args.listclass or '') -- Apply `listclass` if defined in the template
            local nestedRow = addTableRow(nestedTbl)
                        :wikitext(processItem(args[listKey], args.nowrapitems))
 
                end
            -- Add group to the nested row
            if nestedGroup then
                nestedRow:tag('th')
                    :addClass('navboxMobile-group-content')
                    :cssText(args.basestyle)
                    :cssText(args['child' .. listnum .. '_group' .. i .. 'style'])
                    :wikitext(processItem(nestedGroup))
            end
 
            -- Add list to the nested row
            if nestedList then
                nestedRow:tag('td')
                    :addClass('navboxMobile-list-content')
                    :addClass('hlist') -- Horizontal list styling
                    :cssText(args['child' .. listnum .. '_list' .. i .. 'style'])
                    :wikitext(processItem(nestedList))
             end
             end
        end
        -- Add nested table if created
        if nestedTbl then
            listCell:node(nestedTbl)
        else
            -- Render the flat list content
            listCell
                :addClass('hlist')
                :tag('div')
                    :css('padding', args.listpadding or '0 0.25em')
                    :wikitext(listContent)
         end
         end
     end
     end
end
end




Line 470: Line 424:
     local tbl = mw.html.create('table')
     local tbl = mw.html.create('table')
         :addClass('nowraplinks')
         :addClass('nowraplinks')
         :addClass(cfg.class.navbox_list)
         :addClass(cfg.class.navbox)
         :addClass(args.bodyclass)
         :addClass(args.bodyclass)
        :addClass(args.listclass or '') -- Apply the `listclass` argument globally if applicable
         :cssText(args.bodystyle)
         :cssText(args.bodystyle)
         :cssText(args.style)
         :cssText(args.style)
        :css('margin-top', '0')
        :css('margin-bottom', '0')


     if border == 'subgroup' or border == 'child' or border == 'none' then
     -- Add the title row
        tbl
     renderTitleRow(tbl)
            :addClass('navboxMobile-subgroup')
            :css('width', '100%')
            :css('border-spacing', '0')
     else
        tbl
            :addClass('navboxMobile-inner')
            :css('background', 'transparent')
            :css('color', 'inherit')
            :css('border-spacing', '0')
    end


     tbl:cssText(args.innerstyle)
     -- Add the above row
    renderAboveRow(tbl)


     renderTitleRow(tbl)
     -- Render all group and list rows
    renderAboveRow(tbl)
     for _, listnum in ipairs(listnums) do
     for _, listnum in ipairs(listnums) do
         renderListRow(tbl, listnum)
         local listKey = "list" .. listnum
        local groupKey = "group" .. listnum
 
        if args[listKey] then
            -- Render group row
            local groupRow = addTableRow(tbl)
            local groupCell = groupRow:tag('th')
                :attr('scope', 'row')
                :addClass('navboxMobile-group-content')
                :addClass('navboxMobile-group-level1')
                :addClass(args.listclass or '') -- Apply `listclass` for group rows
                :wikitext(processItem(args[groupKey]))
 
            -- Render list row
            if args[listKey] == 'child' then
                renderListRow(tbl, tostring(listnum), 2)
            else
                local listRow = addTableRow(tbl)
                listRow:tag('td')
                    :attr('colspan', 2)
                    :addClass('navboxMobile-list-content')
                    :addClass((listnum % 2 == 1) and 'navboxMobile-odd' or 'navboxMobile-even')
                    :addClass(args.listclass or '') -- Apply `listclass` to flat rows
                    :wikitext(processItem(args[listKey]))
            end
        end
     end
     end
    -- Add the below row
     renderBelowRow(tbl)
     renderBelowRow(tbl)


Line 501: Line 475:




--
--  Main NavboxMobile Function
--
-- In the main navboxMobile function
function p._navboxMobile(navboxMobileArgs)
function p._navboxMobile(navboxMobileArgs)
     args = navboxMobileArgs
     args = navboxMobileArgs
Line 511: Line 490:
     local hiding_templatestyles = move_hiding_templatestyles(args)
     local hiding_templatestyles = move_hiding_templatestyles(args)


     -- Collect list numbers
     -- Collect top-level list numbers (list1, list2, etc.)
     for k, v in pairs(args) do
     for k, v in pairs(args) do
         if type(k) == 'string' then
         if type(k) == 'string' then
Line 530: Line 509:
     -- Create the final HTML with styles
     -- Create the final HTML with styles
     local res = mw.html.create()
     local res = mw.html.create()
    -- Inject styles analogous to Module:Navbox
     res:node(add_navbox_mobile_styles(hiding_templatestyles))
     res:node(add_navbox_mobile_styles(hiding_templatestyles))
 
     res:node(tbl)
     -- Handle wrapping based on border parameter
    if border == 'none' then
        -- Wrap the table within a navigation div with ARIA attributes
        local navWrapper = mw.html.create('div')
            :addClass('navboxMobile-container') -- Add consistent container
            :attr('role', 'navigation')
        if args.title or args.above or (args.group1 and not args.group2) then
            navWrapper:attr(
                'aria-labelledby',
                mw.uri.anchorEncode(args.title or args.above or args.group1)
            )
        else
            navWrapper:attr('aria-label', cfg.aria_label)
        end
        navWrapper:node(tbl)
        res:node(navWrapper)
    elseif border == 'subgroup' or border == 'child' then
        -- Assume this navboxMobile is inside a parent navboxMobile's list cell
        -- Insert closing and opening divs to manage padding
        local subgroupWrapper = mw.html.create('div')
            :addClass('navboxMobile-container') -- Consistent wrapping container
            :node(tbl) -- Add the table as a child
 
        res:node(subgroupWrapper)
    else
        -- Wrap the table within a navigation div with additional classes and styles
        local navWrapper = mw.html.create('div')
            :addClass('navboxMobile-container') -- Add consistent container
            :attr('role', 'navigation')
            :addClass('navboxMobile') -- Add appropriate classes
            :cssText(args.bodystyle)
            :cssText(args.style)
            :css('padding', '2px') -- Adjust padding as needed
        if args.title or args.above or (args.group1 and not args.group2) then
            navWrapper:attr(
                'aria-labelledby',
                mw.uri.anchorEncode(args.title or args.above or args.group1)
            )
        else
            navWrapper:attr('aria-label', cfg.aria_label)
        end
        navWrapper:node(tbl)
        res:node(navWrapper)
    end
 
 
    -- Render tracking categories
    renderTrackingCategories(res)


     return tostring(res)
     return tostring(res)
end
end


--
--  Main NavboxMobile Function
--
function p.navboxMobile(frame)
function p.navboxMobile(frame)
     if not getArgs then
     if not getArgs then
Line 604: Line 537:
     return p._navboxMobile(args)
     return p._navboxMobile(args)
end
end
return p
return p