Module:NavboxMobile: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
 
(45 intermediate revisions by the same user not shown)
Line 150: 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 166: 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
Line 194: 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 231: Line 223:
         :addClass(cfg.class.navbox_list)
         :addClass(cfg.class.navbox_list)
         :addClass(args[cfg.arg.titleclass])
         :addClass(args[cfg.arg.titleclass])
         :attr('data-level', 1) -- Level 1 for the main title
         :attr('data-level', 1)
         :attr('colspan', 2)
         :attr('colspan', 2)
         :cssText(args.basestyle)
         :cssText(args.basestyle)
         :cssText(args.titlestyle)
         :cssText(args.titlestyle)
        :css('position', 'relative')  -- Ensure relative positioning for absolute children
    -- Add the title content, centered across the full width
    titleCell
         :tag('div')
         :tag('div')
             :css('font-size', '114%')
             :css('margin', '0 auto')
             :css('margin', '0')
             :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 265: Line 276:
                 :wikitext(addNewline(args.above))
                 :wikitext(addNewline(args.above))
end
end




Line 282: Line 294:
                 :wikitext(addNewline(args.below))
                 :wikitext(addNewline(args.below))
end
end


local function renderListRow(tbl, path, parentLevel)
local function renderListRow(tbl, path, parentLevel)
    -- Initialize path and parentLevel
     path = path or ""
     path = path or ""
     parentLevel = parentLevel or 2 -- Start at level 2 for child groups
     parentLevel = parentLevel or 2 -- Start at level 2 for child groups
    -- Keep track of rendered children to avoid duplicates
    local renderedChildren = {}


     for i = 1, 10 do
     for i = 1, 10 do
Line 291: Line 309:
         local listKey = (path ~= "" and "child" .. path .. "_" or "") .. "list" .. i
         local listKey = (path ~= "" and "child" .. path .. "_" or "") .. "list" .. i


         if args[groupKey] then
        -- Skip if this group has already been rendered
             -- Define styles for different levels
         if not renderedChildren[listKey] and args[listKey] then
            local groupStyles = {
             renderedChildren[listKey] = true -- Track rendered lists
                [2] = "padding: 0.5em; background-color: #e6e6ff; box-shadow: inset 5px 0 #fff, inset -5px 0 #fff;",
 
                [3] = "padding: 0.5em; background-color: #f0f0ff; box-shadow: inset 5px 0 #ddd, inset -5px 0 #ddd;"
             -- Render the group (heading)
             }
            -- Add a level-specific class to the group row
             local groupRow = addTableRow(tbl)
             local groupRow = addTableRow(tbl)
             local groupCell = groupRow:tag('th')
             local groupCell = groupRow:tag('th')
                 :attr('scope', 'row')
                 :attr('scope', 'row')
                 :addClass('navboxMobile-group-content')
                 :addClass('navboxMobile-group-content')
                 :addClass('navboxMobile-group-level' .. parentLevel) -- Level-specific class
                 :addClass('navboxMobile-group-level' .. parentLevel) -- Correct level
                 :attr('data-level', parentLevel) -- Optional attribute for CSS targeting
                 :attr('data-level', parentLevel) -- Optional attribute for CSS targeting
                :cssText(groupStyles[parentLevel] or "") -- Apply inline styles based on level
                 :wikitext(processItem(args[groupKey]))
                 :wikitext(processItem(args[groupKey]))


             if args[listKey] then
            -- Check if the list is non-empty before rendering content
                 local listRow = addTableRow(tbl)
             if args[listKey] and args[listKey] ~= "" then
                local listCell = listRow:tag('td')
                 if args[listKey] == "child" then
                    :attr('scope', 'row')
                     -- Check if the child contains valid groups or lists
                     :addClass('navboxMobile-group-content')
                     local childHasContent = false
                     :addClass('navboxMobile-group-level' .. parentLevel) -- Level-specific class
                     for j = 1, 10 do
                     :attr('data-level', parentLevel) -- Optional attribute for CSS targeting
                        local childGroupKey = "child" .. (path ~= "" and path .. "_" or "") .. i .. "_group" .. j
                    :css('padding', '0.5em') -- Add padding
                        local childListKey = "child" .. (path ~= "" and path .. "_" or "") .. i .. "_list" .. j
                    :css('background-color', parentLevel == 2 and '#e6e6ff' or '#f0f0ff') -- Different colors for levels
                        if args[childGroupKey] or args[childListKey] then
                    :css('box-shadow', parentLevel == 2 and 'inset 5px 0 #fff, inset -5px 0 #fff' or 'inset 3px 0 #ccc, inset -3px 0 #ccc') -- Custom shadows
                            childHasContent = true
                    :wikitext(processItem(args[groupKey]))
                            break
                        end
                    end


                if args[listKey] == 'child' then
                    if childHasContent then
                    local newPath = (path ~= "" and path .. "_" or "") .. i
                        -- Recursively render child lists
                    renderListRow(tbl, tostring(newPath), parentLevel + 1)
                        local newPath = (path ~= "" and path .. "_" or "") .. i
                        renderListRow(tbl, tostring(newPath), parentLevel + 1)
                    end
                 else
                 else
                     listCell
                     -- Render flat lists
                         :addClass('hlist')
                    local listRow = addTableRow(tbl)
                    local listCell = listRow:tag('td')
                        :attr('colspan', 2)
                        :addClass('navboxMobile-list-content') -- Base content class
                        :addClass('navboxMobile-list-level' .. parentLevel) -- Correct level
                        :addClass((i % 2 == 1) and 'navboxMobile-odd' or 'navboxMobile-even') -- Odd/even class
                         :addClass(args.listclass or '') -- Apply `listclass` if defined in the template
                         :wikitext(processItem(args[listKey], args.nowrapitems))
                         :wikitext(processItem(args[listKey], args.nowrapitems))
                 end
                 end
Line 331: Line 356:
     end
     end
end
end




Line 396: 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)
Line 403: Line 432:
         :css('margin-bottom', '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%')
    -- Add the above row
            :css('border-spacing', '0')
    renderAboveRow(tbl)
    else
 
        tbl
    -- Render all group and list rows
            :addClass('navboxMobile-inner')
    for _, listnum in ipairs(listnums) do
            :css('background', 'transparent')
        local listKey = "list" .. listnum
            :css('color', 'inherit')
        local groupKey = "group" .. listnum
            :css('border-spacing', '0')
 
        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


    tbl:cssText(args.innerstyle)
     -- Add the below row
 
    renderTitleRow(tbl)
    renderAboveRow(tbl)
     -- listnums are now processed separately
     renderBelowRow(tbl)
     renderBelowRow(tbl)


     return tbl
     return tbl
end
end


--
--
Line 456: Line 506:
     -- Render the main body of the navboxMobile
     -- Render the main body of the navboxMobile
     local tbl = renderMainTable()
     local tbl = renderMainTable()
    -- Iterate through list numbers and handle child and flat lists separately
    for _, listnum in ipairs(listnums) do
        local listKey = "list" .. listnum
        local groupKey = "group" .. listnum
        if args[listKey] then
            -- Always render the parent group
            local groupRow = addTableRow(tbl)
            local groupCell = groupRow:tag('th')
                :attr('scope', 'row')
                :addClass('navboxMobile-group-content')
                :addClass('navboxMobile-group-level1') -- Top-level groups use level1
                :wikitext(processItem(args[groupKey]))
            if args[listKey] == 'child' then
                -- Render child lists recursively
                renderListRow(tbl, tostring(listnum), 2) -- Start at level 2 for children
            else
                -- Render flat lists directly
                local listRow = addTableRow(tbl)
                local listCell = listRow:tag('td')
                    :attr('colspan', 2)
                    :addClass('navboxMobile-list-content')
                    :addClass((listnum % 2 == 1) and 'navboxMobile-odd' or 'navboxMobile-even')
                    :css('padding', '0 0.25em')
                    :css('width', '100%')
                    :cssText(args.liststyle)
                    :cssText(args[listKey .. 'style'])
                    :addClass('hlist') -- Ensure hlist class is added for proper shading
                    :wikitext(processItem(args[listKey], args.nowrapitems))
            end
        end
    end


     -- 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')
            :attr('role', 'navigation')
            :addClass('navboxMobile')
        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') -- Add consistent container
            :attr('role', 'navigation')
            :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


--
--