Module:collapsible category tree

From Linguifex
Jump to navigation Jump to search

Documentation for this module may be created at Module:collapsible category tree/doc

local export = {}

local m_utilities = require("Module:utilities")

function export.make(args)
	local lang = args.lang
	local sc = args.sc
	
	-- Add a span which records information to be used by the catfix gadget.
	local catfix_info = lang and m_utilities.catfix(lang, sc) or ""
	
	-- Only provide collapsibility if 5 or more elements
	local pages_in_cat = mw.site.stats.pagesInCategory(args.category, "pages")
	local collapsible = pages_in_cat >= 5
	
	-- CategoryTree only shows the first 200 categories. How many pages are left over?
	local pages_left_over = ((pages_in_cat <= 200) and 0 or (pages_in_cat - 200))
	
	local output = mw.getCurrentFrame():callParserFunction{
		name = "#categorytree",
		args = {
			args.category,
			type = "pages",
			depth = 1,
			class = "\"columns-bg term-list" .. (sc and " " .. sc:getCode() or "") .. "\"",
			style = "counter-reset: pagesleftover " .. pages_left_over,
			namespaces = "-" .. (mw.title.getCurrentTitle().nsText == "Reconstruction" and " Reconstruction" or ""),
			["data-pages-left-over"] = pages_left_over,
		}
	}
	
	if collapsible then
		output = mw.html.create("div")
			:addClass("list-switcher-wrapper")
			:node(
				mw.html.create("div")
					:addClass("list-switcher list-switcher-category-tree")
					:wikitext(output)
			)
	else
		output = mw.html.create("div")
			:addClass("list-switcher-category-tree")
			:wikitext(output)
	end
	
	-- Maintenance categories
	local categories = ""

	return require("Module:TemplateStyles")("Module:collapsible category tree/style.css") ..
		catfix_info .. tostring(output) .. categories
end

-- for testing via wikitext ("wt")
function export.make_wt_test(frame)
	return export.make(frame.args)
end

return export