Module:Navbox: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
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)

Revision as of 08:27, 8 October 2025

Module documentation[view] [edit] [history] [purge]
This module implements a metatemplate, and may be invoked by templates using its corresponding template Template:Navbox, or used directly from other modules.

Module:Navbox is a module that implements the {{Navbox}} template. Navbox templates can be made by using the template or by calling the _navbox function from another module.

On templates, you can create a navbox by using {{Navbox}}, which calls this module's wrapper function.

On modules, you can include local navbox = require("Module:Navbox")._navbox to create a navbox.


Introspection summary for Module:Navbox 
Functions provided (6)
Line Function Params
20 navbox_row (row_content, is_navbox)
51 navbox_header_footer (row_content)
62 navbox_title (title, is_collapsible, name)
82 nested_navbox_title (title, is_collapsible)
97 _navbox (main) (args)
191 navbox (invokable) (frame)
Lua modules required (3)
Variable Module Functions used
getArgs Module:Arguments getArgs
navbar Module:Navbar _navbar
yesno Module:Yesno yesno

No function descriptions were provided. The Lua code may have further information.


-- Page is following provisonal style guide: User:Ganaram_inukshuk/Provisional_style_guide_for_Lua
local p = {}

local getArgs = require("Module:Arguments").getArgs
local navbar = require("Module:Navbar")._navbar
local yesno = require("Module:Yesno")

-- TODO: add an option for a dataless header row, to mirror the option of having
-- a headerless data row. Requires a change to how rows are processed: instead
-- of a table that is a one-element array (for headerless rows) or two-element
-- array (for header+data rows), it's an associative array containing entries
-- for both; the absence of one (as nil) differentiates between headerless 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)
	local is_navbox = (is_navbox ~= nil and is_navbox or false)
	local row = {}
	
	table.insert(row, "|-")
	if #row_content == 1 then
		-- Row is a headerless row...
		if is_navbox then
			-- ...and row content is a nested navbox
			table.insert(row, '| style="padding: 0;" colspan="2" |')
			table.insert(row, row_content[1])
		else
			-- ... and row content is normal content (text and links)
			table.insert(row, '| style="font-size: 0.9em; padding: 0.25em 0.5em;" colspan="2" | ' .. row_content[1])
		end
	else
		-- 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

	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
end

-- 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)
	local is_root_navbox = (is_root_navbox == nil and is_root_navbox or true)		-- If not specified, default to TRUE
	local has_navbar = name ~= nil
	local navbox_title = ''
	if title ~= nil then
		navbox_title = "|-\n"
			.. '! style="text-align: center; background-color: #eaecf0; white-space: nowrap; margin: 0em 4em 0em 4em;'
			.. 'padding: 0.25em 0.5em; border: 1px solid white;" colspan="2" | ' 
			.. '<span style="display: inline-block; float: left; text-align: left; font-weight: normal; font-style: normal; min-width: 4em; padding: 0px; margin: 0px;">'
			.. (has_navbar and navbar(name, "mini", "") or "") .. '</span>'
			.. '<span style="font-size: 1.05em;">' .. title .. "</span>"
			.. (is_collapsible and '' or '<span style="display: inline-block; float: right; font-size: 0.8em; width: 5em;">&nbsp;</span>')
			--.. '\n'
	end
	return navbox_title
end

-- 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)
	local navbox_title = ''
	if title ~= nil then
		navbox_title = "|-\n"
			.. '! style="text-align: center; background-color: #eaecf0; white-space: nowrap; margin: 0em 4em 0em 4em;'
			.. 'padding: 0.25em 0.5em; border: 1px solid white;" colspan="2" | ' 
			.. (is_collapsible and '<span style="display: inline-block; float: left; min-width: 4em; padding: 0px; margin: 0px;">&nbsp;</span>' or '')
			.. '<span style="font-size: 0.9em;">' .. title .. "</span>"
			--.. '\n'
	end
	return navbox_title
end

-- "Main" function
-- Navbox to be called by other modules; also called by wrapper function
function p._navbox(args)
	local title           = args["Title"]
	local name            = args["name"]
	local rows            = args["Rows"]
	local is_data_navbox  = args["Is Data Navbox"]
	local is_collapsible  = yesno(args["Is Collapsible"], true )
	local is_collapsed    = yesno(args["Is Collapsed"  ], false)
	local navbox_type     = ((args["Navbox Type"] == nil) and "Normal" or args["Navbox Type"])
	local header_row      = args["Header Row"]
	local footer_row      = args["Footer Row"]

	-- Table that holds individual lines
	local navbox = {}

	if navbox_type == "Nested" then
		-- Build a navbox that lies within another navbox
		table.insert(navbox, '<div class="wikitable" style="overflow-x: auto; padding: 0; margin: 2px;">')
		table.insert(navbox,
			'{| style="mw-border-collapse: collapse; border-spacing: 0; margin: 0; width: 100%;"'
			.. (is_collapsible and (' class="mw-collapsible' .. (is_collapsed and ' mw-collapsed"' or '"')) or '')
		)

		-- Title row
		table.insert(navbox, p.nested_navbox_title(title, is_collapsible))
		
		-- Header row
		if header_row then
			table.insert(navbox, p.navbox_header_footer(header_row))
		end

		-- Data rows
		for i = 1, #rows do
			table.insert(navbox, p.navbox_row(rows[i], is_data_navbox[i]))
		end

		-- Footer rows
		if footer_row then
			table.insert(navbox, p.navbox_header_footer(footer_row))
		end

		-- End of table
		table.insert(navbox, '|}')
		table.insert(navbox, '</div>')

	elseif navbox_type == "Subheader" then
		-- Build a navbox that lies within another navbox
		-- This one serves as subcategories for one row
		table.insert(navbox, '{| style="mw-border-collapse: collapse; border-spacing: 0; margin: 0; width: 100%;"')
		
		-- Title (optional)
		table.insert(navbox, p.nested_navbox_title(title, false))

		-- Rows
		for i = 1, #rows do
			table.insert(navbox, p.navbox_row(rows[i], is_data_navbox[i]))
		end

		-- End of table
		table.insert(navbox, '|}')

	else -- Normal
		table.insert(navbox, '<div class="wikitable" style="overflow-x: auto; padding: 0">')
		table.insert(navbox,
			'{| style="mw-border-collapse: collapse; border-spacing: 0; margin: 0; width: 100%;"'
			.. (is_collapsible and (' class="mw-collapsible' .. (is_collapsed and ' mw-collapsed"' or '"')) or '')
		)

		-- Title
		table.insert(navbox, p.navbox_title(title, is_collapsible, name))

		-- Header
		if header_row then
			table.insert(navbox, p.navbox_header_footer(header_row))
		end

		-- Rows
		for i = 1, #rows do
			table.insert(navbox, p.navbox_row(rows[i], is_data_navbox[i]))
		end
		
		-- Footer
		if footer_row then
			table.insert(navbox, p.navbox_header_footer(footer_row))
		end

		-- End of table
		table.insert(navbox, '|}')
		table.insert(navbox, '</div>')
	end

	return table.concat(navbox, '\n')
end

-- Wrapper function for template-based navboxes
function p.navbox(frame)
	local args = getArgs(frame)
	
	-- Preprocess individual entries for, headers, data, and is-row-child into
	-- one single table.
	local rows = {}
	local is_data_navbox = {}
	for i = 1, 30 do
		local header = args["Header " .. i]
		local data   = args["Data "   .. i]
		local is_navbox = args["Is Data " .. i .. " Navbox"] ~= nil and args["Is Data " .. i .. " Navbox"] or false
		
		local row = nil
		if (header == nil and data ~= nil) then
			-- Headerless row
			row = {}
			table.insert(row, data)
		elseif (header ~= nil and data == nil) then
			-- Dataless row; permitted for placeholder purposes
			row = {}
			table.insert(row, header)
			table.insert(row, "")
		elseif (header ~= nil and data ~= nil) then
			-- Row with header and data
			row = {}
			table.insert(row, header)
			table.insert(row, data)
		end
		if row ~= nil then
			table.insert(rows, row)
			table.insert(is_data_navbox, is_navbox)
		end
		
		-- Remove original entries as cleanup
		args["Header " .. i] = nil
		args["Data "   .. i] = nil
		args["Is Data " .. i .. " Navbox"] = nil
	end
	args["Rows"] = rows
	args["Is Data Navbox"] = is_data_navbox
	
	local result = p._navbox(args)
	
	-- Debugger option
	local debugg = yesno(args["debug"])
	if debugg == true then
		result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
	end
	
	return result
end

return p