Module:Navbox: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
condense args for navbox into one arg; separate navbox title into its own helper function to later support child navboxes
Line 5: Line 5:


-- Navbox row
-- Navbox row
-- A single table representing the row is entered, where:
function p.navbox_row(row_content, is_row_child_navbox)
-- - If it has one entry, then it's for a headerless row
local row_content = row_content or { "Header", "Content" }
-- - If it has two entry, then it's a header-data pair; if the second entry
local is_row_child_navbox = is_row_child_navbox or false -- Not implemented
--  is another jagged array, then the data is a subtable
function p.navbox_row(row_content)
local row_content = row_content or { "Header", {{"Subheadher 1", "Content 1"}, {"Subheader 2", "Content 2"}}}


local row = '<tr>\n'
local row = '<tr>\n'
Line 20: Line 17:
     .. '</td>\n'
     .. '</td>\n'
elseif #row_content == 2 then
elseif #row_content == 2 then
if type(row_content[2]) == "table" then
-- Simple row with header and data
-- Row with subcategories
row = row
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_content[1] .. '</th>\n'
.. '<th style="width:5%; text-align:right; background-color:#eaecf0; white-space:nowrap; padding:0.25em 0.5em; border:1px solid white">' .. row_content[1] .. '</th>\n'
.. '<td style="padding:0em">\n'
.. '<td style="padding:0em">\n'
.. '<div style="padding:0.25em 0.5em">' .. row_content[2] .. '</div>\n'
.. p.navbox_subtable(row_content[2])
.. '</td>\n'
.. '</td>\n'
else
-- Simple row with header and 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_content[1] .. '</th>\n'
.. '<td style="padding:0em">\n'
.. '<div style="padding:0.25em 0.5em">' .. row_content[2] .. '</div>\n'
.. '</td>\n'
end
end
end
local row = row .. '</tr>\n'
local row = row .. '</tr>\n'
Line 41: Line 29:
end
end


-- Navbox subtable
-- Navbox title
function p.navbox_subtable(subtable_content)
function p.navbox_title(title, is_collapsible, is_collapsed, is_child_navbox)
local subtable_content = subtable_content or {{"Subheadher 1", "Content 1"}, {"Subheader 2", "Content 2"}}
local title = title or "Example Navbox"
local is_collapsible = is_collapsible or true -- Not implemented
local is_collapsed = is_collapsed or true -- Not working?
local is_child_navbox = is_child_navbox or false
local subtable = '<table style="width:100%; border-spacing:0px"\n'
local navbox_title = ''
for i = 1, #subtable_content do
if is_child_navbox then
subtable = subtable
navbox_title = navbox_title
.. p.navbox_row(subtable_content[i])
.. '<div class="wikitable">'
.. '<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'
else
navbox_title = '<table style="width:100%; border-spacing:0px">\n'
end
end
subtable = subtable .. '</table>\n'
 
return navbox_title
return subtable
end
end


-- Navbox to be called by other modules
-- Navbox to be called by other modules; also called by wrapper function
-- Rows are entered as a jagged array; one subtable per row
function p._navbox(args)
function p._navbox(title, rows, is_collapsed)
local title           = args["Title"]
local title = title or "Navbox Title"
local is_child_navbox = args["Is Child Navbox"]
local rows = rows or {{"Header", {{"Subheadher 1", "Content 1"}, {"Subheader 2", "Content 2"}}}}
local rows            = args["Rows"]
local is_collapsed = is_collapsed or true
local row_is_subtable = args["Is Row Child Navbox"] -- Not implemented
local is_collapsible  = args["Is Collapsible"] -- Not implemented
local is_collapsed   = args["Is Collapsible"] -- Not working?
-- Start of table
-- Start of table
local navbox = '<div class="wikitable">'
local navbox = p.navbox_title(title, is_collapsible, is_collapsed, is_subtable)
.. '<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'
for i = 1, #rows do
for i = 1, #rows do
Line 84: Line 78:
local args = getArgs(frame)
local args = getArgs(frame)
local is_collapsed = args["Is Collapsed"]
-- Preprocess individual entries for, headers, data, and is-row-child into
local is_subtable = args["Is Subtable"] -- "subgroup" already means something else here
-- two separate tables.
local title = args["Title"]
-- Both the Wikipedia and RsWiki navboxes go up to 20 rows so follow that
-- Both the Wikipedia and RsWiki navboxes go up to 20 rows so follow that
local rows = {}
local rows = {}
local is_row_child_navbox = {}
for i = 1, 20 do
for i = 1, 20 do
local row = {}
local row = {}
local header = args["Header " .. i]
local header = args["Header " .. i]
local data  = args["Data "  .. i]
local data  = args["Data "  .. i]
local is_child_navbox = args["Is Row Child Navbox" .. i]
if header ~= nil and data ~= nil then
if header ~= nil and data ~= nil then
Line 111: Line 105:
table.insert(rows, row)
table.insert(rows, row)
end
end
if is_child_navbox ~= nil then
table.insert(is_row_child_navbox, is_child_navbox)
end
args["Header " .. i] = nil
args["Data "  .. i] = nil
args["Is Row Child Navbox" .. i] = nil
end
end
return p._navbox(title, rows, is_collapsed)
args["Rows"] = rows
args["Is Row Child Navbox"] = is_row_child_navbox
return p._navbox(args)
end
end


return p
return p

Revision as of 10:28, 23 November 2024

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 (4)
Line Function Params
7 navbox_row (row_content, is_row_child_navbox)
32 navbox_title (title, is_collapsible, is_collapsed, is_child_navbox)
54 _navbox (main) (args)
77 navbox (invokable) (frame)
Lua modules required (1)
Variable Module Functions used
getArgs Module:Arguments getArgs

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
-- Loosely modeled off of Runescape Wiki's navbox, not Wikipedia's
local getArgs = require('Module:Arguments').getArgs
local p = {}

-- Navbox row
function p.navbox_row(row_content, is_row_child_navbox)
	local row_content = row_content or { "Header", "Content" }
	local is_row_child_navbox = is_row_child_navbox or false	-- Not implemented

	local row = '<tr>\n'
	if #row_content == 1 then
		-- Headerless row
		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_content[1] .. '</div>\n'
    		.. '</td>\n'
	elseif #row_content == 2 then
		-- Simple row with header and 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_content[1] .. '</th>\n'
			.. '<td style="padding:0em">\n'
			.. '<div style="padding:0.25em 0.5em">' .. row_content[2] .. '</div>\n'
			.. '</td>\n'
	end
	local row = row .. '</tr>\n'
	
	return row
end

-- Navbox title
function p.navbox_title(title, is_collapsible, is_collapsed, is_child_navbox)
	local title = title or "Example Navbox"
	local is_collapsible = is_collapsible or true		-- Not implemented
	local is_collapsed = is_collapsed or true			-- Not working?
	local is_child_navbox = is_child_navbox or false
	
	local navbox_title = ''
	if is_child_navbox then
		navbox_title = navbox_title
			.. '<div class="wikitable">'
			.. '<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'
	else
		navbox_title = '<table style="width:100%; border-spacing:0px">\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"]
	local is_child_navbox = args["Is Child Navbox"]
	local rows            = args["Rows"]
	local row_is_subtable = args["Is Row Child Navbox"]		-- Not implemented
	local is_collapsible  = args["Is Collapsible"]			-- Not implemented
	local is_collapsed    = args["Is Collapsible"]			-- Not working?
	
	-- Start of table
	local navbox = p.navbox_title(title, is_collapsible, is_collapsed, is_subtable)
	
	for i = 1, #rows do
		navbox = navbox .. p.navbox_row(rows[i])
	end
	
	navbox = navbox
		.. '</table>\n'
		.. '</div>'
	
	return navbox
end

-- Navbox to be #invoke'd
function p.navbox(frame)
	local args = getArgs(frame)
	
	-- Preprocess individual entries for, headers, data, and is-row-child into
	-- two separate tables.
	-- Both the Wikipedia and RsWiki navboxes go up to 20 rows so follow that
	local rows = {}
	local is_row_child_navbox = {}
	for i = 1, 20 do
		local row = {}
		local header = args["Header " .. i]
		local data   = args["Data "   .. i]
		local is_child_navbox = args["Is Row Child Navbox" .. i]
		
		if header ~= nil and data ~= nil then
			-- If there's both a header data, add both
			table.insert(row, header)
			table.insert(row, data)
		elseif header == nil and data ~= nil then
			-- Headerless row
			table.insert(row, data)
		elseif header ~= nil and data == nil then
			-- Dataless row with header; this is allowed
			table.insert(row, header)
			table.insert(row, "")
		end
		
		if #row >= 1 then
			table.insert(rows, row)
		end
		
		if is_child_navbox ~= nil then
			table.insert(is_row_child_navbox, is_child_navbox)
		end
		
		args["Header " .. i] = nil
		args["Data "   .. i] = nil
		args["Is Row Child Navbox" .. i] = nil
	end
	
	args["Rows"] = rows
	args["Is Row Child Navbox"] = is_row_child_navbox
	
	return p._navbox(args)
	
end

return p