Module:Navbox

From Zelda Wiki, the Zelda encyclopedia
Revision as of 01:55, 8 November 2022 by PhantomCaleb (talk | contribs)
This is the main module for the following templates:
local p = {}

local utilsArg = require("Module:UtilsArg")

local CATEGORY_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]"
local DEFAULT_IMG_SIZE = "150x150px"

function p.Main(frame)
	local args, err = utilsArg.parse(frame:getParent().args, p.Templates.Navbox)
	local categories = err and err.categoryText or ""
	
	if not args.title then
		return "", categories
	end
	
	-- [[MediaWiki:Gadget-Site.js]] automatically removes the "mw-collapsed" when there is only one navbox on the page.
	local navboxContent = mw.html.create("div")
		-- MediaWiki (Timeless?) automatically removes elements with class "navbox" on mobile
		-- we _want_ to show navboxes on mobile since we've made them mobile friendly, so we use a different class name
		:addClass("zw-navbox mw-collapsible mw-collapsed")
		:tag("div")
			:addClass("zw-navbox__header mw-collapsible-toggle")
			:tag("span")
				-- Used to center the heading - see Template:Navbox/Styles.css
				:addClass("zw-navbox__toggle-button-counterbalance")
				:done()
			:tag("span")
				:addClass("zw-navbox__title")
				:wikitext(args.title)
				:done()
			:tag("span")
				:addClass("zw-navbox__toggle-button")
				:tag("span")
					:addClass("zw-navbox__toggle-button-text mw-collapsible-text")
					:wikitext("hide ▲")
					:done()
				:done()
			:done()
		:done()
		:tag("div")
			:addClass("zw-navbox__content mw-collapsible-content")
			
	local body = navboxContent:tag("div")
		:addClass("zw-navbox__body")
	local rows = body:tag("div")
		:addClass("zw-navbox__rows")
	for i, row in ipairs(args.rows) do
		local links = table.concat(row.links or "", " • ")
		local evenOdd = (i % 2 == 0) and "even" or "odd"
		if row.group then
			rows:tag("div")
					:addClass("zw-navbox__row-header")
					:wikitext(row.group)
					:done()
		elseif i ~= 1 or #args.rows > 1 then
			local utilsError = require("Module:UtilsError")
			utilsError.warn(string.format("<code>group%d</code> parameter is required when there is more than one group.", i))
			categories = categories..CATEGORY_INVALID_ARGS
		end
		local rowModifiers = " zw-navbox__row-links--"..evenOdd
		if #args.rows == 1 and not args.rows.group1 then
			rowModifiers = rowModifiers.." zw-navbox__row-links--nogroups"
		end
		rows:tag("div")
				:addClass("zw-navbox__row-links"..rowModifiers)
				:tag("div")
					:addClass("zw-navbox__row-links-content")
					:wikitext(links)
					:done()
				:done()
	end
	
	if args.image then
		local filename = args.image
		if not string.find(filename, "^File:") then
			filename = "File:"..filename
		end
		local thumbnail = string.format("[[%s|%s]]", filename, DEFAULT_IMG_SIZE)
		body:tag("div")
			:addClass("zw-navbox__image")
			:wikitext(thumbnail)
	end
	if args.footer then
		navboxContent:tag("div")
			:addClass("zw-navbox__footer")
			:wikitext(args.footer)
	end
	
	local result = tostring(navboxContent:allDone())
	return result, categories
end

p.Templates = {
	["Navbox"] = {
		format = "block",
		purpose = "Creates [[:Category:Navbox Templates|navbox templates]].",
		categories = {"Metatemplates"},
		boilerplate = {
			separateRequiredParams = false,
		},
		paramOrder = {"title", "image", "group", "links", "footer"},
		repeatedGroup = {
			name = "rows",
			params = {"group", "links"},
			counts = {2, 3, 4, 5, 6, 7},
		},
		params = {
			title = {
				required = true,
				type = "content",
				desc = "<p>The navbox title.</p><p>It is recommended not to place links in the title as this can create confusion between the clickable navbox header and the link within it. Category links should be placed in the footer.</p>",
				trim = true,
				nilIfEmpty = true,
			},
			image = {
				type = "wiki-file-name",
				desc = "A file name, with the <code>File:</code> prefix.",
				trim = true,
				nilIfEmpty = true,
			},
			group = {
				type = "string",
				desc = "A header for the given row in the navbox. Required if there is more than one row.",
				trim = true,
				nilIfEmpty = true,
			},
			links = {
				type = "content",
				required = true,
				desc = "A comma-separated list of links for the given row.",
				trim = true,
				nilIfEmpty = true,
				split = true,
			},
			footer = {
				type = "content",
				desc = "The navbox footer. Usually contains links to relevant categories.",
				trim = true,
				nilIfEmpty = true,
			},
		},
	},
}

return p