Module:Navbox

From Xenharmonic Wiki
Jump to navigation Jump to search

Documentation transcluded from /doc
Note: While this module should not be invoked directly, the use of its corresponding template (Template:Navbox) is not absolutely necessary.

On other templates that use or generate navigation boxes, you can call _navbox (note the underscore in front). The navbox function (without the underscore) is used by Template:Navbox as a wrapper.

On other modules, you can include local navbox = require("Module:Navbox")._navbox at or near the top.

Icon-Todo.png Todo: Documentation

See also


-- Page is following provisonal style guide: User:Ganaram_inukshuk/Provisional_style_guide_for_Lua
-- Loosely modelled off of Runescape Wiki's navbox, not Wikipedia's
local getArgs = require("Module:Arguments").getArgs
local yesno = require("Module:Yesno")
local navbar = require("Module:Navbar")._navbar
local p = {}

-- Header/footer row
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 row
function p.navbox_row(row_content, is_navbox)
	local is_navbox = (is_navbox ~= nil and is_navbox or false)

	local row = "|-\n"
	if #row_content == 1 then
		-- Headerless row; takes up two cells
		if is_navbox then
			-- Row data is a child navbox; data cell has no padding
			row = row
				.. '| style="padding: 0;" colspan="2" | \n'
				.. row_content[1] .. "\n"
		else
			-- Row data is normal data
			row = row .. '| style="font-size: 0.9em; padding: 0.25em 0.5em;" colspan="2" | ' .. row_content[1] .. "\n"
    	end
	else
		-- Row with header and data
		if is_navbox then
			-- Row data is a child navbox; data cell has no padding
			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="padding: 0;" |\n' 
				.. row_content[2] .. "\n"
		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
	
	return row
end

-- Navbox title
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
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

-- Navbox to be called by other modules; also called by wrapper function
function p._navbox(args)
	local title           = args["Title"] or "Navbox 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"]
	
	-- Start of table
	local navbox = ''
	if navbox_type == "Nested" then
		-- Navbox has a title, wikitable border, collapse options, and extra
		-- margin.
		-- This navbox is meant to be a sub-navbox, placed on a headerless row.
		navbox = '<div class="wikitable" style="overflow-x: auto; padding: 0; margin: 2px;">\n'
			.. '{| width="100%" style="mw-border-collapse: collapse; border-spacing: 0; margin: 0;"'
			.. (is_collapsible and ('class="mw-collapsible' .. (is_collapsed and ' mw-collapsed"' or '"')) or '') .. '\n'
			.. p.nested_navbox_title(title, is_collapsible)
			
		-- Add header
		if header_row ~= nil then
			navbox = navbox .. p.navbox_header_footer(header_row)
		end
		
		-- Add rows
		for i = 1, #rows do
			navbox = navbox .. p.navbox_row(rows[i], is_data_navbox[i])
		end
		
		-- Add footer
		if footer_row ~= nil then
			navbox = navbox .. p.navbox_header_footer(footer_row)
		end
		
		-- End of table
		navbox = navbox
			.. '|}\n'
			.. '</div>'
			
	elseif navbox_type == "Subheader" then
		-- Navbox has no title, border, or collapse options.
		-- This navbox is meant to display subheaders.
		navbox = '{| width="100%" style="mw-border-collapse: collapse; border-spacing: 0; margin: 0;"\n'
		
		-- Add rows
		for i = 1, #rows do
			navbox = navbox .. p.navbox_row(rows[i], is_data_navbox[i])
		end
		
		-- End of table
		navbox = navbox .. '|}\n'
	else
		-- Navbox has a title, wikitable border, and collapse options.
		-- This navbox is a normal navbox or a nested navbox.
		navbox = '<div class="wikitable" style="overflow-x: auto; padding: 0">\n'
			.. '{| width="100%" style="mw-border-collapse: collapse; border-spacing: 0; margin: 0"'
			.. (is_collapsible and ('class="mw-collapsible' .. (is_collapsed and 'mw-collapsed"' or '"')) or '') .. '\n'
			.. p.navbox_title(title, is_collapsible, name)
			
		-- Add header
		if header_row ~= nil then
			navbox = navbox .. p.navbox_header_footer(header_row)
		end
		
		-- Add rows
		for i = 1, #rows do
			navbox = navbox .. p.navbox_row(rows[i], is_data_navbox[i])
		end
		
		-- Add header
		if footer_row ~= nil then
			navbox = navbox .. p.navbox_header_footer(footer_row)
		end
		
		-- End of table
		navbox = navbox
			.. '|}\n'
			.. '</div>'
	end	
	
	return navbox
end

-- Navbox to be invoked
-- 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.
	-- Both the Wikipedia and RsWiki navboxes go up to 20 rows so follow that.
	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
	
	return p._navbox(args)
	
end

return p