Module:Navbox

Revision as of 19:45, 3 October 2022 by Jacmob (talk | contribs) (Created page with "-- <nowiki> -- -- Implements {{navbox}} -- local p = {} local tnavbar = require( 'Module:Tnavbar' ) local yesno = require( 'Module:Yesno' ) local page_title = mw.title.getCurrentTitle().fullText -- -- Helper for inserting a new row into the navbox -- -- @param tbl {mw.html table} -- @return tbl {mw.html table} -- local function insertRow( tbl ) return tbl:tag( 'tr' ) end -- -- Creates the navbox table -- -- @param args {table} -- @return tbl {mw.html table} -- local f...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation
This documentation is transcluded from Template:Navbox/doc. [edit] [history] [purge]
Module:Navbox's function navbox is invoked by Template:Navbox.
Module:Navbox requires Module:Tnavbar.
Module:Navbox requires Module:Yesno.
Module:Navbox is invoked by Template:Navbox.

Usage

This template can be used to make a standard navigation box. It is intended for use within another template that standardises the title and contents of the box (see Template:Rune equipment). This template should not be used directly within articles, as it creates a maintenance burden to do so whenever the contents of the box have to be updated.

The purpose of this template is to standardise the attributes of all navigation boxes, for example, their colours and other CSS attributes. The template is fairly simple, and could be extended to add other features in the future. Using this template allows changes to be implemented much quicker and easier than editing each template separately.

Syntax

{{Navbox
|state = uncollapsed, collapsed, plain, autocollapse (default)
|name = mandatory - name of the template
|style = optional - style for the whole table
|title = mandatory - navbox header
|gtitleN = optional - title of N-th group of items
|styleN = optional - style for groupN; i.e. text-align
|groupN = mandatory - list of groups of items
|fstyle = optional - style for footer ; i.e. text-align
|footer = optional - navbox footer
}}
  • Groups: Currently there is no technical limit to the maximum number of groups.
    • Groups of items can be added as parameters: group1, group2, group3, ... up to groupN.
  • Group title: Each group has its own title parameter: gtitle1, gtitle2, gtitle3, ... up to gtitleN. Note that the default width for the first column is 15%.
  • Group style: Style may also be customised to each group in: style1, style2, style3, ... up to styleN.
    • Note that neither the title nor the items have any wiki-links built into the template. As a result, users of the template are free to use wiki-links as they wish.
  • Line breaks: Line-breaks (<br>) are unnecessary, as text wrapping is done automatically by the template. This also includes links and text in gtitleN and groupN.
  • Superscript: It is preferred to use superscript (<sup>) to denote variants of items that are already contained in the navbox.
  • Bullets: Create unordered lists with lines beginning with *. The use of Template:* is deprecated

States

  • Autocollapse: By default, the state of a navbox is "autocollapsed". This means that if the number of navboxes in a page exceeds the maximum allocation of the collapsible navboxes, then the remaining navboxes are collapsed automatically. For more information (or to change the setting), see MediaWiki:Gadget-autocollapse.js.
    • Currently, the autocollapse allocation is 2. This means that if there are 3 navboxes in a page, the third and subsequent navboxes will be collapsed automatically.
  • Uncollapsed: Forces the navbox to be displayed.
    • All of the navboxes in this documentation are in the uncollapsed state.
  • Collapsed: The reverse of uncollapsed. Forces the navbox to be hidden instead.
    • Navboxes with a height of more than 300 pixels will also be collapsed automatically. For more information (or to change the setting), see MediaWiki:Gadget-autocollapse.js.
  • Plain: Removes the "show/hide" link.

Sub-groups

  • For larger navboxes, it may be necessary to include sub-groups within a particular group of items. It is also possible to collapsible navboxes within the parent navbox, see Collapsible sub-groups.
  • To create sub-groups within {{Navbox}}, certain parameters are to be included:
    • The parent navbox group (i.e. group2 in the example below) requires the type parameter (gtype2 = subgroup).
    • The nested navbox (sub-group) requires the inclusion of the sub-group parameter (subgroup = yes). This enables the template to differentiate between the sub-group and the parent navbox.
{{Navbox
|name = 
|title = 
|gtitle1 = 
|group1 =
|gtitle2 = 
|gtype2 = subgroup (mandatory)
|group2 =
  {{Navbox
  |subgroup = yes (mandatory)
  |gtitle1 = 
  |group1 =
  |gtitle2 = 
  |group2 =
  }}
}}

Collapsible sub-groups

  • For extremely long navboxes, it is recommended to use collapsible navboxes within the parent navbox. Further sub-groups can then be added to these collapsible navboxes.
  • To create collapsible sub-groups within {{Navbox}}, these parameters need to be included:
    • The parent navbox is almost always uncollapsed (state=uncollapsed).
    • The parent navbox group (i.e. group1 in the example below) requires the type parameter (gtype1 = subgroup).
    • The nested collapsible navbox requires the inclusion of the collapsible parameter (collapsible = yes). This enables the template to differentiate between the collapsible navbox and the parent navbox.
    • The nested collapsible navbox is forcefully collapsed (state=collapsed).
{{Navbox
|state = uncollapsed
|name = 
|gtype1 = subgroup
|group1 = 
  {{Navbox
  |collapsible = yes
  |state = collapsed
  |title = 
  |gtitle1 = 
  |group1 =
  |gtitle2 = 
  |group2 =
  }}
}}

Use of images

  • The use of images in navboxes is permitted, but please resize the images to keep them small. Inventory-sized images (25-30 pixels) are recommended.
  • Please use {{plink}} to create links with images. Use {{plinkp}} for any case where text is not desired after the link. It functions the same as {{plink}}, but it does not produce a wikilink in text. Use {{chatl}} to link chatheads. These templates create links for both the image and text (except for {{plinkp}}) using far less wikicode than the standard syntax. Use {{Emote link}} to link emote icons.
  • Avoid using images in the main header of the navbox.

Link and file share the same name

{{plink|Item}}

File name differs from the link

{{plink|Item|pic=File}}

Link and file share the same name, but different text is desired to display

{{plink|Item|txt=Text}}

File name differs from the link and different text is desired to display

{{plink|Item|pic=File|txt=Text}}
Examples
Code Standard syntax
{{plink|Abyssal whip}} [[File:Abyssal whip.png|link=Abyssal whip]] [[Abyssal whip]]
{{plink|Granite maul|pic=Abyssal whip}} [[File:Abyssal whip.png|link=Granite maul]] [[Granite maul]]
{{plink|Abyssal whip|txt=Whip}} [[File:Abyssal whip.png|link=Abyssal whip]] [[Abyssal whip|Whip]]
{{plink|Granite maul|pic=Abyssal whip|txt=Whip}} [[File:Abyssal whip.png|link=Granite maul]] [[Granite maul|Whip]]
{{plinkp|Abyssal whip}} [[File:Abyssal whip.png|link=Abyssal whip]]
{{plinkp|Granite maul|pic=Abyssal whip}} [[File:Abyssal whip.png|link=Granite maul]]

Categorising

Navboxes are very useful for transcluding categories onto a large number of pages that share both a trait and the navigation box. For example: Template:Rune equipment transcludes Category:Rune onto every page that uses it.

There are 2 methods of categorising pages with navboxes:

The first way is with {{Ctg}}. This is the preferred method, and can be used in most cases. The rules and operations for the Category handler template are very simple; however, they are enough to produce the desired results on most pages.

The other method is using {{Mainonly|rules}} and providing more complex rules with parser functions. Template:Potions is an example of this:

{{mainonly|{{#ifeq:{{lc:{{#sub:{{PAGENAME}}|-3}}}}|mix|[[Category:Barbarian mixes]]}}}}

This code tells the template to add Category:Barbarian mixes to pages only if their title ends with the string "mix". {{ctg|Barbarian mixes::ifmatches[mix]}} would not work here, because several pages include the substring "mix" in other locations, and they would be improperly categorised.

Categorisation should only be used on a template for large-scale addition. It is unhelpful, for example, to use a rule that only applies to 1 page.

Examples

Single line navbox with centered list

{{Navbox
|name = 
|title = 
|style1 = text-align:center
|group1 =
}}

Navbox with standard footer

{{Navbox
|name = 
|title = 
|gtitle1 = 
|group1 =
|footer = Centered footer
}}

Navbox with styled footer

{{Navbox
|name = 
|title = 
|gtitle1 = 
|group1 =
|fstyle = text-align:right
|footer = Right-aligned footer
}}

Multiple line navbox

{{Navbox
|name = 
|title = 
|gtitle1 = Group title 1
|group1 =
|gtitle2 = Group title 2
|group2 =
|gtitle3 = Group title 3
|group3 =
}}

Collapsed navbox

{{Navbox
|state = collapsed
|name = 
|title = 
|style1 = text-align:center
|group1 =
}}

Plain navbox with Tnavbar

{{Navbox
|state = plain
|name = 
|title = 
|style1 = text-align:center
|group1 =
}}

Plain navbox without Tnavbar

{{Navbox
|state = plain
|title = 
|style1 = text-align:center
|group1 =
}}

Complicated navbox

{{Navbox
|state = uncollapsed
|name = 
|title = 
|gtitle1 = Level 1 Title 1
|group1 =
|gtitle2 = Level 1 Title 2
|group2 =
|gtitle3 = Level 1 Title 3
|group3 =
|gtitle4 = Level 1 Title 4
|group4 =
|gtitle5 = Level 1 Title 5
|gtype5 = subgroup
|group5 =
  {{Navbox
  |subgroup = yes
  |gtitle1 = Level 2 Title 1
  |group1 =
  |gtitle2 = Level 2 Title 2
  |group2 =
  |gtitle3 = Level 2 Title 3
  |gtype3 = subgroup
  |group3 =
    {{Navbox
    |subgroup = yes
    |gtitle1 = Level 3 Title 1
    |group1 =
    |gtitle2 = Level 3 Title 2
    |group2 =
    }}
  |gtitle4 = Level 2 Title 4
  |group4 =
  }}
|gtitle6 = Level 1 Title 6
|group6 =
|gtitle7 = Level 1 Title 7
|group7 =
|fstyle = 
|footer = 
}}

Preventing unwanted documentation

The Navbox template's documentation is automatically transcluded under the navbox on any Template namespace page. It is not transcluded when used as a sub-group or collapsible sub-group template (subgroup= yes or collapsible=yes).

Should the documentation appear when it is unwanted, it can be hidden by setting the doc parameter doc=no on the template. In most cases, the doc parameter is not used and the parameter is removed completely.

On a similar note, the automatically-added category Category:Navbox templates can be suppressed by adding hidecat=yes.


-- <nowiki>
--
-- Implements {{navbox}}
--

local p = {}
local tnavbar = require( 'Module:Tnavbar' )
local yesno = require( 'Module:Yesno' )
local page_title = mw.title.getCurrentTitle().fullText
--
-- Helper for inserting a new row into the navbox
--
-- @param tbl {mw.html table}
-- @return tbl {mw.html table}
--
local function insertRow( tbl )
	return tbl:tag( 'tr' )
end

--
-- Creates the navbox table
--
-- @param args {table}
-- @return tbl {mw.html table}
--
local function createTbl( args )

	local tbl = mw.html.create( 'table' )

	tbl
		:addClass( yesno(args.subgroup, false) and 'navbox-subgroup' or 'navbox' )
		:addClass( 'nowraplinks' )

	if not yesno(args.subgroup, false) and
		( args.state == 'collapsed' or
		  args.state == 'uncollapsed' or
		  args.state == 'autocollapse' or
		  -- defaults to autocollapse
		  args.state == nil )
	then
		tbl:addClass( 'mw-collapsible' )

		if args.state == 'collapsed' then
			tbl:addClass( 'mw-collapsed' )
		elseif args.state == 'uncollapsed' then
			tbl:addClass('navbox-uncollapsed')
		else
			tbl:addClass( 'mw-collapsed' )
			tbl:addClass( 'navbox-autocollapse' )
		end
	end

	if yesno(args.collapsible, false) then
		tbl:addClass( 'navbox-collapsible' )
	end

	if args.style then
		tbl:cssText( args.style )
	end

	-- used by [[MediaWiki:Gadget-navbox-tracking.js]] for tracking purposes
	tbl:attr( { ['data-navbox-name'] = args.name } )

	return tbl
end

--
-- Wrapper for [[Module:Tnavbar]]
--
-- @param args {table}
-- @return {string}
--
local function navbar( args )

	local div = mw.html.create( 'div' )
		:css( {
			float = 'left',
			['text-align'] = 'left'
		} )
		:addClass( 'navbar' )
		:wikitext( tnavbar._navbar( { [1] = args.name } ) )
	return div
end

--
-- Creates the header (what you see when the navbox is collapsed)
--
-- @param tbl {mw.html table}
-- @param args {table}
-- @return {mw.html table}
--
local function header( tbl, args )
	local div = insertRow( tbl )
		:tag( 'th' )
			:attr( 'colspan', '2' )
			:addClass( 'navbox-title' )
				:wikitext( args.name and tostring( navbar( args ) ) )
				:tag('div')
					:addClass( 'navbox-title-name' )
					:wikitext( args.title )

	return div:allDone()
end

--
-- Inserts a row into the navbox
--
-- @param tbl {mw.html table}
-- @param gtitle {string}
-- @param group {string}
-- @param gtype {string}
-- @param style {string}
-- @return {mw.html table}
--
local function row( tbl, gtitle, group, gtype, style, _name, subgroup )
	local tr = insertRow( tbl )
	local td

	if gtitle then
		td = tr
			:addClass( 'navbox-group' )
			:tag( 'td' )
				:addClass( 'navbox-group-title' )
				:wikitext( gtitle )
				:done()
			:tag( 'td' )
	else
		td = tr
			:addClass( 'navbox-group' )
			:addClass( 'navbox-group-split' )
			:tag( 'td' )
				:addClass( 'navbox-group-title-hidden' )
				:attr( 'colspan', '0' )
				:css( 'display', 'none' )
				:done()
			:tag( 'td' )
				:attr( 'colspan', '2' )
	end

	--[[
	   List styling
	   This is unlikely to be implemented in the near future due to it requiring extra css to work
	   and mobile currently not supporting that css.
	   As an example, it lets you do the following instead if using {{*}} all the time
	   | group3 =
	   * {{plink|foo}}
	   * {{plink|bar}}
	   * {{plink|baz}}
	]]
	if mw.ustring.match( group, '^%s*%*' ) then
		td:newline()

		-- trim whitespace on bullets
		local spl = mw.text.split( group, '\n' )

		for i = 1, #spl do
			spl[i] = mw.text.trim( spl[i] )
		end

		group = '\n' .. table.concat( spl, '\n' )		
	end

	--local group2 = group
	--local group3 = group2
	-- analytics

	--if _name then
	--	local name = mw.ustring.gsub(_name,' ','_')
	--	for v in mw.ustring.gmatch(group,'%[%[[^%]]+%]%]') do
	--		if mw.ustring.match(v,'%[%[File:.+|link=') then
	--			local link = mw.ustring.match(v,'|link=([^%]|]+)')
	--			if link then
	--				local linkrep = mw.ustring.gsub(link,'([%%%]%[%-^$*()+?])','%%%1')
	--				local _link = mw.ustring.gsub(link,' ','_')
	--				local newfile = mw.ustring.gsub(v,'|link='..linkrep,string.format('|link=https://oldschool.runescape.wiki/w/%s?f=%s',_link,name))
	--				local w = mw.ustring.gsub(v,'([%%%]%[%-^$*()+?])','%%%1')
	--				group2 = mw.ustring.gsub(group2,w,newfile)
	--			end
	--		elseif mw.ustring.match(v,'%[%[Category:') then
				-- nothing
	--		else
	--			local link = mw.ustring.match(v,'%[%[([^%]|]+)')
	--			local txt = mw.ustring.match(v,'%|([^%]|]+)') or link

	--			local newlink = ''

				-- black links if current page
	--			if link == page_title then
	--				newlink = string.format('<b>%s</b>',txt)
	--			else
	--				local _link = mw.ustring.gsub(link or '',' ','_')
	--				newlink = string.format('[https://oldschool.runescape.wiki.com/w/%s?n=%s %s]',_link,name,txt)
	--			end
	--			local w = mw.ustring.gsub(v,'([%%%]%[%-^$*()+?])','%%%1')
	--			group2 = mw.ustring.gsub(group2,w,newlink)
	--		end
	--	end

		--[==[
			fix [[these kind]]s of [[link]]s post analytics parse
			]==]
	--	group3 = group2

	--	for v in mw.ustring.gmatch(group2,'%[https://oldschool.runescape.wiki.com/w[^%]]-%]%a') do
	--		local rep = mw.ustring.gsub(v,'%]','')
	--		rep = rep..']'
	--		local w = mw.ustring.gsub(v,'([%%%]%[%-^$*()+?])','%%%1')

	--		group3 = mw.ustring.gsub(group2,w,rep)
	--	end
	--end

	td
		:addClass( 'navbox-list' )
		:wikitext( group ) --group3

	if gtype and mw.ustring.lower( gtype ) == 'subgroup' then
		td
			:addClass( 'navbox-parent' )
			:css( {
				padding = '0'
			} )
	end

	if style then
		td:cssText( style )
	end

	return td:allDone()
end

--
-- Inserts a footer into the navbox
--
-- @param tbl {mw.html table}
-- @param args {table}
-- @return {mw.html table}
--
local function footer( tbl, args )
	local th = insertRow( tbl )
		:tag( 'th' )
			:attr( 'colspan', '2' )
			:addClass( 'navbox-footer' )

	if args.fstyle then
		th:cssText( args.fstyle )
	end

	if mw.ustring.match( args.footer, '^%s*%*' ) then
		th:newline()

		-- trim whitespace on bullets
		local spl = mw.text.split( args.footer, '\n' )

		for i = 1, #spl do
			spl[i] = mw.text.trim( spl[i] )
		end

		args.footer = table.concat( spl, '\n' )

		th:addClass( 'navbox-list' )
	end

	th:wikitext( args.footer )

	return th:allDone()
end

--
-- Adds [[Category:Navbox templates]] to navbox template pages
--
-- @return {string}
--
local function categories()
	local title = mw.title.getCurrentTitle()
	local page = title.text
	local ns = title.nsText

	if ns == 'Template' then
		-- sort in category by pagename
		return '[[Category:Navbox templates| ' .. page .. ']]'
	else
		return ''
	end

end

--
-- Adds [[Template:Navbox/doc]] to navbox template pages
--
-- @param args {table}
-- @return {string}
--
local function docs( args )
	local frame = mw.getCurrentFrame()
	local title = mw.title.getCurrentTitle()
	local base = title.baseText
	local ns = title.nsText

		-- not if a subpage of [[Template:Navbox]]
	if base ~= 'Navbox' and
		-- in template ns
		ns == 'Template' and
		-- not a navbox group within a navbox
		not yesno(args.subgroup, false) and
		-- not a collapsible navbox within a navbox
		not yesno(args.collapsible, false) and
		-- not if the doc argument is not set to "yes"
		yesno(args.doc, false)
	then
		return frame:expandTemplate{ title = 'Navbox/doc' }
	else
		return ''
	end

end

--
-- Navbox method to allow it to be called by other modules
--
-- @param _args {table}
-- @return {string}
--
function p._navbox( _args )
	local args = {}
	local wkCss = ''
	local wkDiv = ''
	local j
	
	-- preserves parser function behaviour where an empty string is considered undefined
	-- or nil in lua's case
	for k, v in pairs( _args ) do
		if v ~= '' then
			args[k] = v
		end
	end

	local tbl = createTbl( args )

	if not yesno(args.subgroup, false) then
		tbl = header( tbl, args )
	end

	-- insert up to 25 rows
	for i = 1, 25 do
		j = tostring( i )

		if args['group' .. j] then
			tbl = row( tbl, args['gtitle' .. j], args['group' .. j], args['gtype' .. j], args['style' .. j], args.name, args.subgroup )
		else
			break
		end
	end

	if args.footer then
		tbl = footer( tbl, args )
	end

	tbl = tostring( tbl )

	local cats = ''
	if not yesno(args.subgroup, false) and not yesno(args.hidecat, false) then
		cats = categories()
	end
	local docs = docs( args )

	return tbl .. cats .. docs
end

--
-- Main navbox method accessed through #invoke
--
-- @param frame {table}
-- @return {string}
--
function p.navbox( frame )
	local args = frame:getParent().args
	return p._navbox( args )
end

return p
-- </nowiki>