Module:Navbox

Revision as of 07:54, 24 November 2024 by Ganaram inukshuk (talk | contribs) (Undo revision 167423 by Ganaram inukshuk (talk))
Module documentation[view] [edit] [history] [purge]
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.

This template allows a navigation box to be set up relatively quickly by supplying it with one or more lists of links. It comes equipped with default styles that should work for most navigational templates. Changing the default styles is possible, but not recommended. The default styling for our navigation boxes is modelled off of RuneScape Wiki's navigation box, instead of Wikipedia's.

See also


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

-- TODO:
-- - Address navbox nesting further by introducing navbox options:
-- -- Navbox with outer div --> Root navbox; has collapse options
-- -- Navbox without outer div --> Nested navbox for headerless row; has collapse options
-- -- Navbox without title --> Nested navbox for subcategories; no collapse options
-- - Header and data interactions
-- -- Header and normal data --> Normal row
-- -- No header and normal data --> Headerless row (used for a header, footer, or spacer)
-- -- Header and nested table --> Category with subcategories (nested table has no title and no collapse options)
-- -- No header and nested table --> Nested navboxes (both navboxes have collapse options)
-- - Add a navbox type option to specify whether a navbox has a title bar (normal or nested navbox) or not (navbox for subcategories)

-- Navbox row
function p.navbox_row(row_content)
	local row_header    = row_content["Header"]
	local row_data      = row_content["Data"]
	local is_row_navbox = row_content["Is Navbox"]

	local row = '<tr>\n'
	if row_header == nil then
		-- Headerless row
		if is_row_navbox then
			-- Row data is a child navbox
			row = row
				--.. '<td style="width:5%; text-align:center; white-space:nowrap; padding:0em; border:1px solid white" colspan="2">\n'
				.. '<td style="width:5%; padding:0em;" colspan="2">\n'
	        	.. row_data
	    		.. '</td>\n'
		else
			-- Row data is normal data
			row = row
				--.. '<td style="width:5%; text-align:center; white-space:nowrap; padding:0em; border:1px solid white" colspan="2">\n'
				.. '<td style="width:5%; padding:0em;" colspan="2">\n'
	        	.. '<div style="padding:0.25em 0.5em">' .. row_data .. '</div>\n'
	    		.. '</td>\n'
    	end
	else
		-- Simple row with header and data
		if is_row_navbox then
			-- Row data is a child navbox
			row = row
				.. '<th style="width:5%; text-align:right; background-color:#eaecf0; padding:0.25em 0.5em; border:1px solid white">' .. row_header .. '</th>\n'
				.. '<td style="padding:0em">' .. row_data .. '</td>\n'
		else
			-- Row data is normal data
			row = row
				.. '<th style="width:5%; text-align:right; background-color:#eaecf0; padding:0.25em 0.5em; border:1px solid white">' .. row_header .. '</th>\n'
				.. '<td style="text-alight:right; padding:0.25em 0.5em">\n' .. row_data .. '</td>\n'
		end
	end
	local row = row .. '</tr>\n'
	
	return row
end

-- Navbox title
function p.navbox_title(title)
	local navbox_title = ''
	if title ~= nil then
		navbox_title = '<th style="width:5%; text-align:center; background-color:#eaecf0; white-space:nowrap; padding:0.25em 0.5em; border:1px solid white" colspan="2"><b>' .. title .. '</b></th>\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 rows            = args["Rows"]
	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"])
	
	-- Start of table
	local navbox = ""
	if navbox_type == "Nested" then
		-- Navbox has a title and collapse options, and a white border. The
		-- white border matches with that of the header cells.
		-- This navbox is meant to be a sub-navbox, placed on a headerless row.
		navbox = '<div class="wikitable" style="margin:0; border:1px solid white">\n'
			.. '<table class="mw-collapsible' .. (is_collapsed and ' mw-collapsed ' or '') .. 'nowraplinks" style="width: 100%; border-spacing:0px">\n'
			.. p.navbox_title(title)
			
		for i = 1, #rows do
			navbox = navbox .. p.navbox_row(rows[i])
		end
		
		navbox = navbox
			.. '</table>\n'
			.. '</div>'
			
	elseif navbox_type == "Subheader" then
		-- Navbox has no title, border, or collapse options.
		-- This navbox is meant to display subheaders.
		navbox = '<table style="width:100%; border-spacing:0px">\n'
			
		for i = 1, #rows do
			navbox = navbox .. p.navbox_row(rows[i])
		end
		
		navbox = navbox .. '</table>'
	else
		-- Navbox has a title, wikitable border, and collapse options.
		-- This navbox is a normal navbox or a nested navbox.
		navbox = '<div class="wikitable">\n'
			.. '<table class="mw-collapsible' .. (is_collapsed and ' mw-collapsed ' or '') .. 'nowraplinks" style="width: 100%; border-spacing:0px">\n'
			.. p.navbox_title(title)
			
		for i = 1, #rows do
			navbox = navbox .. p.navbox_row(rows[i])
		end
		
		navbox = navbox
			.. '</table>\n'
			.. '</div>'
	end	
	
	return navbox
end

-- Navbox to be #invoke'd
-- 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 = {}
	for i = 1, 20 do
		local header = args["Header " .. i]
		local data   = args["Data "   .. i]
		local is_navbox = yesno(args["Is Data " .. i .. " Navbox"])
		
		local row = nil
		if (header ~= nil or data ~= nil or is_navbox ~= nil) then
			row = {
				["Header"] = header,
				["Data"] = ((header ~= nil and data == nil) and "" or data),
				["Is Navbox"] = is_navbox
			}
		end
		if row ~= nil then
			table.insert(rows, row)
		end
		
		-- Remove original entries as cleanup
		args["Header " .. i] = nil
		args["Data "   .. i] = nil
		args["Is Data " .. i .. " Navbox"] = nil
	end
	args["Rows"] = rows
	
	return p._navbox(args)
	
end

return p