Module:NavboxMobile: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 28: Line 28:
     end
     end
end
end
-- Function to add a new table row with optional gutter
local function addTableRow(tbl)
    -- Check if the spacer rows are necessary based on the listclass or bodyclass
    local suppressSpacer = false
    if args.listclass then
        suppressSpacer = suppressSpacer or args.listclass:find("hlist")
    end
    if args.bodyclass then
        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', '0.5em') -- Adjust spacer height as needed
            :tag('td')
                :attr('colspan', 2)
                :css('background-color', 'transparent') -- Ensure spacer is invisible
                :wikitext(' ')
    end
    tableRowAdded = true
    return tbl:tag('tr')
end
-- Function to check if the navbar is enabled
local function has_navbar()
    return args[cfg.arg.navbar] ~= cfg.keyword.navbar_off
        and args[cfg.arg.navbar] ~= cfg.keyword.navbar_plain
        and (
            args[cfg.arg.name]
            or mw.getCurrentFrame():getParent():getTitle():gsub(cfg.pattern.sandbox, '')
                ~= cfg.pattern.navbox
        )
end


local function processItem(item, nowrapitems)
local function processItem(item, nowrapitems)
Line 93: Line 55:
end
end


-- Function to add a new table row with optional gutter
local function addTableRow(tbl)
    -- Check if the spacer rows are necessary based on the listclass or bodyclass
    local suppressSpacer = false
    if args.listclass then
        suppressSpacer = suppressSpacer or args.listclass:find("hlist")
    end
    if args.bodyclass then
        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', '0.25em') -- Adjust spacer height as needed
            :tag('td')
                :attr('colspan', 2)
                :css('background-color', 'transparent') -- Ensure spacer is invisible
                :wikitext(' ')
    end
    tableRowAdded = true
    return tbl:tag('tr')
end


-- Function to render the navigation bar
-- Function to render the navigation bar
Line 122: Line 107:
     groupCell:css('text-align', 'center')
     groupCell:css('text-align', 'center')
      
      
     -- Optionally, add spacer below group row
     -- Add spacer below group row for consistent spacing
     tbl:tag('tr')
     tbl:tag('tr')
         :tag('td')
         :tag('td')
Line 210: Line 195:
-- Function to render list rows
-- Function to render list rows
local function renderListRow(tbl, listnum)
local function renderListRow(tbl, listnum)
     -- Render group row (heading) in its own row
    -- Determine level (2 for main groups, 3 for subgroups)
     renderGroupRow(tbl, listnum, 2) -- Level 2 for main groups
    local level = 2
 
    if border == 'subgroup' then
     -- Render list row below the group row
        level = 3
    end
   
     -- Render group row
     renderGroupRow(tbl, listnum, level)
   
     -- Continue with rendering the list
     local listRow = addTableRow(tbl)
     local listRow = addTableRow(tbl)
     local listCell = listRow:tag('td')
     local listCell = listRow:tag('td')
Line 223: Line 214:
         :cssText(args.liststyle)
         :cssText(args.liststyle)
         :cssText(args['list' .. listnum .. 'style'])
         :cssText(args['list' .. listnum .. 'style'])
 
   
     if args['list' .. listnum] == 'child' then
     if args['list' .. listnum] == 'child' then
         -- Render nested subgroup table
         -- Render nested subgroup as a separate table
         for i = 1, 10 do
         for i = 1, 10 do
             local nestedGroup = args['child' .. listnum .. '_group' .. i]
             local nestedGroup = args['child' .. listnum .. '_group' .. i]
Line 233: Line 224:


             renderGroupRow(tbl, i, 3) -- Level 3 for subgroups
             renderGroupRow(tbl, i, 3) -- Level 3 for subgroups
             local nestedListRow = addTableRow(tbl)
             local nestedListRow = addTableRow(tbl)
             local nestedListCell = nestedListRow:tag('td')
             local nestedListCell = nestedListRow:tag('td')
Line 241: Line 231:
                 :css('padding', '0 0.25em')
                 :css('padding', '0 0.25em')
                 :cssText(args['child' .. listnum .. '_list' .. i .. 'style'])
                 :cssText(args['child' .. listnum .. '_list' .. i .. 'style'])
                 :wikitext(addNewline(args['child' .. listnum .. '_list' .. i]))
                 :wikitext(processItem(nestedList))
         end
         end
     else
     else
Line 247: Line 237:
         listCell:tag('div')
         listCell:tag('div')
             :css('padding', args.listpadding or '0 0.25em')
             :css('padding', args.listpadding or '0 0.25em')
             :wikitext(addNewline(args['list' .. listnum], args.nowrapitems))
             :wikitext(processItem(args['list' .. listnum], args.nowrapitems))
    end
end
 
-- Function to check if the navbar is enabled
local function has_navbar()
    return args[cfg.arg.navbar] ~= cfg.keyword.navbar_off
        and args[cfg.arg.navbar] ~= cfg.keyword.navbar_plain
        and (
            args[cfg.arg.name]
            or mw.getCurrentFrame():getParent():getTitle():gsub(cfg.pattern.sandbox, '')
                ~= cfg.pattern.navbox
        )
end
 
-- Function to add list styles
local function add_list_styles()
    local frame = mw.getCurrentFrame()
    local function add_list_templatestyles(htmlclass, templatestyles)
        if has_list_class(htmlclass) then
            return frame:extensionTag{
                name = 'templatestyles', args = { src = templatestyles }
            }
        else
            return ''
        end
    end
       
    local hlist_styles = add_list_templatestyles('hlist', cfg.hlist_templatestyles)
    local plainlist_styles = add_list_templatestyles('plainlist', cfg.plainlist_templatestyles)
   
    -- Workaround for [[phab:T303378]]
    if has_navbar() and hlist_styles == '' then
        hlist_styles = frame:extensionTag{
            name = 'templatestyles', args = { src = cfg.hlist_templatestyles }
        }
     end
     end
   
    -- hlist -> plainlist is best-effort to preserve old Common.css ordering.
    return hlist_styles .. plainlist_styles
end
-- Function to add styles
local function add_navbox_mobile_styles(hiding_templatestyles)
    local frame = mw.getCurrentFrame()
   
    -- Function to add user-defined templatestyles
    local function add_user_styles(templatestyles)
        if templatestyles and templatestyles ~= '' then
            return frame:extensionTag{
                name = 'templatestyles',
                args = { src = templatestyles }
            }
        end
        return ''
    end
    -- Combine list styles with base and user styles
    local list_styles = add_list_styles()
    local base_templatestyles = cfg.templatestyles
    local templatestyles = add_user_styles(args[cfg.arg.templatestyles])
    local child_templatestyles = add_user_styles(args[cfg.arg.child_templatestyles])
    -- Combine all styles into a single <div>
    return mw.html.create('div')
        :addClass('navboxMobile-styles')
        :wikitext(
            list_styles ..
            base_templatestyles ..
            templatestyles ..
            child_templatestyles ..
            table.concat(hiding_templatestyles)
        )
        :done()
end
-- Function to move and collect hiding templatestyles
local function move_hiding_templatestyles(args)
    local gfind = string.gmatch
    local gsub = string.gsub
    local templatestyles_markers = {}
    local strip_marker_pattern = '(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)'
    for k, arg in pairs(args) do
        for marker in gfind(arg, strip_marker_pattern) do
            table.insert(templatestyles_markers, marker)
        end
        args[k] = gsub(arg, strip_marker_pattern, '')
    end
    return templatestyles_markers
end
end


Line 338: Line 415:


     return tbl
     return tbl
end
-- Function to add styles
local function add_navbox_mobile_styles(hiding_templatestyles)
    local frame = mw.getCurrentFrame()
   
    -- Function to add user-defined templatestyles
    local function add_user_styles(templatestyles)
        if templatestyles and templatestyles ~= '' then
            return frame:extensionTag{
                name = 'templatestyles',
                args = { src = templatestyles }
            }
        end
        return ''
    end
    -- Combine list styles with base and user styles
    local list_styles = add_list_styles()
    local base_templatestyles = cfg.templatestyles
    local templatestyles = add_user_styles(args[cfg.arg.templatestyles])
    local child_templatestyles = add_user_styles(args[cfg.arg.child_templatestyles])
    -- Combine all styles into a single <div>
    return mw.html.create('div')
        :addClass('navboxMobile-styles')
        :wikitext(
            list_styles ..
            base_templatestyles ..
            templatestyles ..
            child_templatestyles ..
            table.concat(hiding_templatestyles)
        )
        :done()
end
-- Function to move and collect hiding templatestyles
local function move_hiding_templatestyles(args)
    local gfind = string.gmatch
    local gsub = string.gsub
    local templatestyles_markers = {}
    local strip_marker_pattern = '(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)'
    for k, arg in pairs(args) do
        for marker in gfind(arg, strip_marker_pattern) do
            table.insert(templatestyles_markers, marker)
        end
        args[k] = gsub(arg, strip_marker_pattern, '')
    end
    return templatestyles_markers
end
end