Module:Navbox: Difference between revisions
put back todo; found a way to differentiate headerless data rows and dataless header rows; also add todo for possible code refactoring |
refactor row helper function; only the row helper needed immediate refactoring |
||
| Line 13: | Line 13: | ||
-- and dataless rows. | -- and dataless rows. | ||
-- | -- Navbox row helper function | ||
-- Function was refactored to use table.concat(), like with the "main" function, | |||
-- | -- since it's being called multiple times. The other helper functions don't need | ||
-- to be refactored (currently) because they only need to be called a fixed | |||
-- number of times. | |||
-- | |||
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 = "|- | |||
table.insert(row, "|-") | |||
if #row_content == 1 then | if #row_content == 1 then | ||
-- | -- Row is a headerless row... | ||
if is_navbox then | if is_navbox then | ||
-- | -- ...and row content is a nested navbox | ||
row | table.insert(row, '| style="padding: 0;" colspan="2" |') | ||
table.insert(row, row_content[1]) | |||
else | else | ||
-- | -- ... and row content is normal content (text and links) | ||
row | table.insert(row, '| style="font-size: 0.9em; padding: 0.25em 0.5em;" colspan="2" | ' .. row_content[1]) | ||
end | |||
else | else | ||
-- Row | -- Row is a header+data row | ||
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;" |' | |||
local data_style = is_navbox | |||
and '| style="padding: 0;" |' -- For nested navboxes | |||
or '| style="font-size: 0.9em; padding: 0.25em 0.5em;" | ' -- For normal content | |||
table.insert(row, header_style .. row_content[1]) | |||
table.insert(row, data_style .. row_content[2]) | |||
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 | -- 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 | ||
-- Wrapper function for template-based navboxes | -- Wrapper function for template-based navboxes | ||
function p.navbox(frame) | function p.navbox(frame) | ||