Module:Navbox: Difference between revisions

Ganaram inukshuk (talk | contribs)
put back todo; found a way to differentiate headerless data rows and dataless header rows; also add todo for possible code refactoring
Ganaram inukshuk (talk | contribs)
refactor row helper function; only the row helper needed immediate refactoring
Line 13: Line 13:
-- and dataless rows.
-- and dataless rows.


-- TODO: possible code refactoring for helper functions.
-- Navbox row helper function
 
-- Function was refactored to use table.concat(), like with the "main" function,
-- Header/footer row
-- since it's being called multiple times. The other helper functions don't need
function p.navbox_header_footer(row_content)
-- to be refactored (currently) because they only need to be called a fixed
local row = "|-\n"
-- number of times.
.. '| colspan="2" style="font-size: 0.8em; text-align: center; background-color: #eaecf0; padding: 0em; border: 1px solid white;" | '
.. row_content --.. "\n"
 
return row
end
 
-- Navbox row
function p.navbox_row(row_content, is_navbox)
function p.navbox_row(row_content, is_navbox)
local is_navbox = (is_navbox ~= nil and is_navbox or false)
local is_navbox = (is_navbox ~= nil and is_navbox or false)
 
local row = {}
local row = "|-\n"
table.insert(row, "|-")
if #row_content == 1 then
if #row_content == 1 then
-- Headerless row; takes up two cells
-- Row is a headerless row...
if is_navbox then
if is_navbox then
-- Row data is a child navbox; data cell has no padding
-- ...and row content is a nested navbox
row = row
table.insert(row, '| style="padding: 0;" colspan="2" |')
.. '| style="padding: 0;" colspan="2" | \n'
table.insert(row, row_content[1])
.. row_content[1] .. "\n"
else
else
-- Row data is normal data
-- ... and row content is normal content (text and links)
row = row .. '| style="font-size: 0.9em; padding: 0.25em 0.5em;" colspan="2" | ' .. row_content[1] .. "\n"
table.insert(row, '| style="font-size: 0.9em; padding: 0.25em 0.5em;" colspan="2" | ' .. row_content[1])
    end
end
else
else
-- Row with header and data
-- Row is a header+data row
if is_navbox then
local header_style = '! style="white-space: nowrap; font-size: 0.9em; width: 5%; text-align: right; background-color: #eaecf0; padding: 0.25em 0.5em; border: 1px solid white;" |'
-- Row data is a child navbox; data cell has no padding
local data_style = is_navbox
row = row
and '| style="padding: 0;" |' -- For nested navboxes
.. '! style="white-space: nowrap; font-size: 0.9em; width: 5%; text-align: right; background-color: #eaecf0; padding: 0.25em 0.5em; border: 1px solid white;" |'
or  '| style="font-size: 0.9em; padding: 0.25em 0.5em;" | ' -- For normal content
..  row_content[1] .. "\n"
 
.. '| style="padding: 0;" |\n'  
table.insert(row, header_style .. row_content[1])
.. row_content[2] --.. "\n"
table.insert(row, data_style .. row_content[2])
else
-- Row data is normal data
row = row
.. '! style="white-space: nowrap; font-size: 0.9em; width: 5%; text-align: right; background-color: #eaecf0; padding: 0.25em 0.5em; border: 1px solid white;" |'
.. row_content[1] .. "\n"
.. '| style="font-size: 0.9em; padding: 0.25em 0.5em;" | ' .. row_content[2] --.. "\n"
end
end
end
 
return table.concat(row, "\n")
end
 
-- Header/footer row helper function
-- Called up to twice on a table.
function p.navbox_header_footer(row_content)
local row = "|-\n"
.. '| colspan="2" style="font-size: 0.8em; text-align: center; background-color: #eaecf0; padding: 0em; border: 1px solid white;" | '
.. row_content --.. "\n"
 
return row
return row
end
end


-- Navbox title
-- Navbox title helper function
-- Up to one title helper is called per table; this is expected to be called in
-- most cases, however.
function p.navbox_title(title, is_collapsible, name)
function p.navbox_title(title, is_collapsible, name)
local is_root_navbox = (is_root_navbox == nil and is_root_navbox or true) -- If not specified, default to TRUE
local is_root_navbox = (is_root_navbox == nil and is_root_navbox or true) -- If not specified, default to TRUE
Line 79: Line 77:
end
end


-- Navbox title for nested navboxes
-- Navbox title for nested navboxes or subheader (subcategory) navboxes
-- Up to one title helper is called per table; not always necessray if the table
-- is used for subcategories.
function p.nested_navbox_title(title, is_collapsible)
function p.nested_navbox_title(title, is_collapsible)
local navbox_title = ''
local navbox_title = ''
Line 93: Line 93:
end
end


-- "Main" function
-- Navbox to be called by other modules; also called by wrapper function
-- Navbox to be called by other modules; also called by wrapper function
function p._navbox(args)
function p._navbox(args)
Line 140: Line 141:
elseif navbox_type == "Subheader" then
elseif navbox_type == "Subheader" then
-- Build a navbox that lies within another navbox
-- Build a navbox that lies within another navbox
-- This one serves as subrows for one row
-- This one serves as subcategories for one row
table.insert(navbox, '{| style="mw-border-collapse: collapse; border-spacing: 0; margin: 0; width: 100%;"')
table.insert(navbox, '{| style="mw-border-collapse: collapse; border-spacing: 0; margin: 0; width: 100%;"')
Line 187: Line 188:
end
end


-- Navbox to be invoked
-- Wrapper function for template-based navboxes
-- Wrapper function for template-based navboxes
function p.navbox(frame)
function p.navbox(frame)