Module:Navbox: Difference between revisions

m
1 revision
m (1 revision)
 
m (1 revision)
(One intermediate revision by the same user not shown)
Line 14: Line 14:
local listnums = {}
local listnums = {}
   
   
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


function addNewline(s)
local function addNewline(s)
     if s:match('^[*:;#]') or s:match('^{|') then
     if s:match('^[*:;#]') or s:match('^{|') then
         return '\n' .. s ..'\n'
         return '\n' .. s ..'\n'
Line 26: Line 26:
end
end


function addTableRow(tbl)
local function addTableRow(tbl)
     -- If any other rows have already been added, then we add a 2px gutter row.
     -- If any other rows have already been added, then we add a 2px gutter row.
     if tableRowAdded then
     if tableRowAdded then
Line 41: Line 41:
end
end


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
    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
    elseif args.navbar == 'plain' or args.navbar == 'off' or (not args.name and (border == 'subgroup' or border == 'child' or border == 'none')) 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
    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
        titleCell.wikitext(Navbar.navbar({
            args.name,
            mini = 1,
            fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') ..  ';background:none transparent;border:none;'
        }))
    end
   
    -- Render the spacer div.
    if spacerSide then
        titleCell
            .tag('span')
                .css('float', spacerSide)
                .css('width', '6em')
                .wikitext(' ')
    end
end


--
--
--  Title row
--  Title row
--
--
function renderTitleRow(tbl)
local function renderTitleRow(tbl)
     if not args.title then return end
     if not args.title then return end
   
   
Line 90: Line 123:
end
end


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
--   Above/Below rows
    -- or right to keep the title centered.
--
    local spacerSide = nil


     if args.navbar == 'off' then
local function getAboveBelowColspan()
        -- No navbar, and client wants no spacer, i.e. wants the title to be shifted to the left. If there's
     local ret = 2
        -- also no show/hide link, then we need a spacer on the right to achieve the left shift.
     if args.imageleft then ret = ret + 1 end
        if args.state == 'plain' then spacerSide = 'right' end
     if args.image then ret = ret + 1 end
     elseif args.navbar == 'plain' or args.navbar == 'off' or (not args.name and (border == 'subgroup' or border == 'child' or border == 'none')) then
     return ret
        -- 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
     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
 
        titleCell.wikitext(Navbar.navbar({
            args.name,
            mini = 1,
            fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') ..  ';background:none transparent;border:none;'
        }))
    end
      
    -- Render the spacer div.
    if spacerSide then
        titleCell
            .tag('span')
                .css('float', spacerSide)
                .css('width', '6em')
                .wikitext(' ')
    end
end
end


 
local function renderAboveRow(tbl)
--
--  Above/Below rows
--
function renderAboveRow(tbl)
     if not args.above then return end
     if not args.above then return end


Line 142: Line 148:
end
end


function renderBelowRow(tbl)
local function renderBelowRow(tbl)
     if not args.below then return end
     if not args.below then return end


Line 155: Line 161:
                 .wikitext(addNewline(args.below))
                 .wikitext(addNewline(args.below))
end
end
function getAboveBelowColspan()
    local ret = 2
    if args.imageleft then ret = ret + 1 end
    if args.image then ret = ret + 1 end
    return ret
end
   
   
--
--
--  List rows
--  List rows
--
--
function renderListRow(tbl, listnum)
local function renderListRow(tbl, listnum)
     local row = addTableRow(tbl)
     local row = addTableRow(tbl)
      
      
Line 258: Line 256:
--  Tracking categories
--  Tracking categories
--
--
function renderTrackingCategories(builder)
    local frame = mw.getCurrentFrame()
   
    if not frame then return end
   
    local s = frame:preprocess('{{#ifeq:{{NAMESPACE}}|{{ns:10}}|1|0}}{{SUBPAGENAME}}')
    if mw.ustring.sub(s, 1, 1) == '0' then return end -- not in template space
    local subpage = mw.ustring.lower(mw.ustring.sub(s, 2))
    if subpage == 'doc' or subpage == 'sandbox' or subpage == 'testcases' then return end
   
    for i, cat in ipairs(getTrackingCategories()) do
        builder.wikitext('[[Category:' .. cat .. ']]')
    end
end


function getTrackingCategories()
local function needsHorizontalLists()
    local cats = {}
    if needsHorizontalLists() then table.insert(cats, 'Navigational boxes without horizontal lists') end
    if hasBackgroundColors() then table.insert(cats, 'Navboxes using background colours') end
    return cats
end
 
function needsHorizontalLists()
     if border == 'child' or border == 'subgroup'  or args.tracking == 'no' then return false end
     if border == 'child' or border == 'subgroup'  or args.tracking == 'no' then return false end
      
      
Line 293: Line 270:
end
end


function hasBackgroundColors()
local function hasBackgroundColors()
     return args.titlestyle or args.groupstyle
     return args.titlestyle or args.groupstyle
end
end


local function getTrackingCategories()
    local cats = {}
    if needsHorizontalLists() then table.insert(cats, 'Navigational boxes without horizontal lists') end
    if hasBackgroundColors() then table.insert(cats, 'Navboxes using background colours') end
    return cats
end
local function renderTrackingCategories(builder)
    local frame = mw.getCurrentFrame()
   
    if not frame then return end
   
    local s = frame:preprocess('{{#ifeq:{{NAMESPACE}}|{{ns:10}}|1|0}}{{SUBPAGENAME}}')
    if mw.ustring.sub(s, 1, 1) == '0' then return end -- not in template space
    local subpage = mw.ustring.lower(mw.ustring.sub(s, 2))
    if subpage == 'doc' or subpage == 'sandbox' or subpage == 'testcases' then return end
   
    for i, cat in ipairs(getTrackingCategories()) do
        builder.wikitext('[[Category:' .. cat .. ']]')
    end
end


--
--
--  Main navbox tables
--  Main navbox tables
--
--
function renderMainTable()
local function renderMainTable()
     local tbl = HtmlBuilder.create('table')
     local tbl = HtmlBuilder.create('table')
         .attr('cellspacing', 0)
         .attr('cellspacing', 0)
14,061

edits