Module:Navbox

From Xenharmonic Wiki
Jump to navigation Jump to search

Documentation transcluded from /doc
Note: Do not invoke this module directly; use the corresponding template instead: Template:Navbox.
Icon-Todo.png Todo: Documentation

-- 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; background-color:#eaecf0; white-space:nowrap; padding:0em; border:1px solid white" colspan="2">\n'
	        	.. row_data
	    		.. '</td>\n'
		else
			-- Row data is normal data
			row = row
				.. '<td style="width:5%; text-align:center; background-color:#eaecf0; white-space:nowrap; padding:0em; border:1px solid white" 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; white-space:nowrap; 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; white-space:nowrap; padding:0.25em 0.5em; border:1px solid white">' .. row_header .. '</th>\n'
				.. '<td style="padding:0em">\n'
				.. '<div style="padding:0.25em 0.5em">' .. row_data .. '</div>\n'
				.. '</td>\n'
		end
	end
	local row = row .. '</tr>\n'
	
	return row
end

-- Navbox title
-- Child navboxes used for subcategories don't need a title or collapse options.
function p.navbox_title(title, is_collapsible, is_collapsed)
	local is_collapsible = is_collapsible or true		-- Not implemented
	local is_collapsed = is_collapsed or true			-- Not working?
	local navbox_type = navbox_type or "Normal"
	
	local navbox_title = ''
	if title == nil then
		navbox_title = '<table style="width:100%; border-spacing:0px">\n'
	else
		navbox_title = navbox_title
			.. '<table class="mw-collapsible' .. (is_collapsed and ' mw-collapsed ' or '') .. 'nowraplinks" style="width: 100%; border-spacing:0px">\n'
			.. '<tr>\n'
			.. '<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'
			.. '</tr>\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 = p.navbox_title(title, is_collapsible, is_collapsed)
	
	for i = 1, #rows do
		navbox = navbox .. p.navbox_row(rows[i])
	end
	
	navbox = navbox
		.. '</table>\n'
		
	if navbox_type == "Normal" then
		navbox = '<div class="wikitable">\n' .. navbox .. '</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