Module:Collapsible

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
This is the main module for the following templates:
local p = {}

local utilsArg = require("Module:UtilsArg")

function p.Main(frame)
	local args, err = utilsArg.parse(frame:getParent().args, p.Templates["Collapsible"])
	local categories = err and err.categoryText or ""
	
	if args.content == nil or args.content == "" then
		return "", categories
	end
	
	local expanded = args.collapse ~= true and not (args.frame and args.collapse ~= false)
	local expandText = args.buttonShow.." ▼"
	local collapseText = args.buttonHide.." ▲"
	
	-- This module depends on the jQuery.makeCollapsible that ships with MediaWiki
	-- See https://www.mediawiki.org/wiki/Manual:Collapsible_elements
	local html = mw.html.create("div")
		:addClass("zw-collapsible mw-collapsible")
		:attr("data-expandtext", expandText)
		:attr("data-collapsetext", collapseText)
		
	if not expanded then
		html:addClass("mw-collapsed")
	end
	if args.frame then
		html:addClass("zw-collapsible--framed")
	end
	if args.stretch and args.frame then
		html:addClass("zw-collapsible--stretch")
	end

	if not args.header then
		html:wikitext(args.content)
	else
		html:attr("id", args.id or args.header)
			:tag("div")
				:addClass("zw-collapsible__header mw-collapsible-toggle")
				:tag("span")
					:addClass("zw-collapsible__header-text")
					:wikitext(args.header or "")
					:done()
				:tag("span")
					:addClass("zw-collapsible__toggle-button")
					:tag("span")
						:addClass("zw-collapsible__toggle-button-text mw-collapsible-text")
						:wikitext(expanded and collapseText or nil) -- for some reason MediaWiki doesn't show the collapsetext when content is expanded by default
						:done()
					:done()
				:done()
			:tag("div")
				:addClass("zw-collapsible__content mw-collapsible-content")
				:wikitext("\n"..args.content)
				:done()
	end

	return tostring(html), categories
end

function p.Infofield(frame)
	local args, err = utilsArg.parse(frame:getParent().args, p.Templates["Infofield Collapsible"])
	local categories = err and err.categoryText or ""

	if not args.header or not args.content then
		return categories
	end
	
	local html = mw.html.create("div")
		:addClass("zw-infofield-collapsible mw-collapsible mw-collapsed")
		:attr("data-expandtext", "show ▼")
		:attr("data-collapsetext", "hide ▲")
		:tag("span")
			:addClass("zw-infofield-collapsible__header")
			:wikitext(args.header.." ")
			:done()
		:tag("span")
			:addClass("mw-collapsible-toggle")
			:tag("span")
				:addClass("zw-infofield-collapsible__toggle-button")
				:tag("span")
					:addClass("zw-infofield-collapsible__toggle-button-text mw-collapsible-text")
					:done()
				:done()
			:done()
		:tag("div")
			:addClass("mw-collapsible-content")
			:wikitext(args.content)
			:done()
			
	return tostring(html), categories
end

p.Templates = {
	["Collapsible"] = {
		format = "block",
		description = "Makes content collapsible.",
		purpose = "Makes content collapsible. Used for large lists and tables as well as other supplementary content such as [[:Category:Comment Templates|companion comments]]. It has also been used to describe plots with forking paths (see [[The Crystal Trap]] and [[The Subspace Emissary]]).",
		paramOrder = {"id", "header", "frame", "collapse", "stretch", "buttonHide", "buttonShow", "content"},
		params = {
			header = {
				suggested = true,
				type = "content",
				desc = "The title of the collapsible content.",
				trim = true,
				nilIfEmpty = true,
			},
			content = {
				required = true,
				type = "content",
				desc = "The content to be made collapsible.",
				trim = true,
			},
			id = {
				type = "string",
				desc = "Sets the [https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id ID attribute] of the collapsible content so that it can be linked as a [[Template:Section|section]]. Defaults to <code>header</code>.",
				trim = true,
				nilIfEmpty = true,
			},
			frame = {
				type = "boolean",
				desc = "If present, a border is added around the collapsible content and header.",
			},
			collapse = {
				type = "boolean",
				desc = "<p>If present, the content starts off in the collapsed state on page load.</p><p>Content is collapsed by default when <code>frame</code> is set. To override this, set <code>collapse</code> to <code>false</code>.</p>",
			},
			stretch = {
				type = "boolean",
				desc = "If present, the header stretches to fill the screen rather than fitting itself to its content. Works only when <code>frame</code> is enabled.",
			},
			buttonHide = {
				type = "string",
				default = "hide",
				desc = 'Text to use for the "hide" button label',
			},
			buttonShow = {
				type = "string",
				default = "show",
				desc = 'Text to use for the "show" button label',
			},
		}
	},
	["Infofield Collapsible"] = {
		format = "block",
		params = {
			[1] = {
				name = "header",
				type = "content",
				required = true,
				desc = "A title for the collapsible content.",
				trim = true,
				nilIfEmpty = true,
			},
			[2] = {
				name = "content",
				type = "content",
				required = true,
				desc = "The content to be made collapsible.",
				trim = true,
				nilfIfEmpty = true,
			},
		},
	},
}

return p