Module:columns

Definition from Wiktionary, the free dictionary
Jump to navigation Jump to search

This module creates a list with automatically balanced columns. It should not be used directly in entries, but in templates such as {{der2}} or {{der3}}. List entries are given as parameters to the template.

Examples[edit]

  • {{#invoke:columns|display|sort=1|collapse=1|columns=3|title=Derived terms}} -> {{der3|z|y|x|w|v|u|t|lang=en}}
  • {{#invoke:columns|display|sort=1|collapse=1|columns=2|title=Derived terms}} -> {{der2|a|b|c|d|e|f|g|lang=nl}}

Parameters[edit]

create_list[edit]

export.create_list {
	column_count = number,
	content = list, alphabetize = boolean,
	background_color = string, collapse = boolean,
	toggle_category = string,
	class = string, lang = language_object,
}
content
A list of terms: { "term1", "term2", "term3", ... }.
lang
The language of the terms in the list. (Must be a language object from Module:languages.)
collapse
If true, table will be collapsed if it has enough items.
column_count
Number of columns in the table. Defaults to 1.
sort
Toggle sorting of the entries in the table. Defaults to false.
toggle_category
Determines the text for the "Show <toggle_category>" or "Hide <toggle category>" button in the "visibility" part of the toolbar. The default is "derived terms".
class
HTML class to add to the div tag that contains the list. Defaults to derivedterms.
background_color
A HTML color value for the list.

create_table[edit]

The old name for the main function. It is now just a wrapper for create_list.

display[edit]

The template-invokable function.


local export = {}

local m_links = require("Module:links")
local m_languages = require("Module:languages")


local function format_list_items(items, lang)
	local result = {}
	
	for i, item in ipairs(items) do
		if lang and not string.find(item, "<span") then
			item = m_links.full_link({lang = lang, term = item})
		end
		
		result[i] = '\n* ' .. item
	end
	
	return table.concat(result)
end

local collapse_header =
	[[<div class="list-switcher" data-toggle-category="{{{toggle_category}}}">]]
local column_header = [[<div class="{{{class}}} term-list ul-column-count" ]]
	.. [[data-column-count="{{{column_count}}}" ]]
	.. [[style="background-color: {{{background_color}}};">]]
local button = [[<div class="list-switcher-element" ]]
	.. [[data-showtext="&nbsp;show more ▼&nbsp;" ]]
	.. [[data-hidetext="&nbsp;show less ▲&nbsp;" style="display: none;">&nbsp;</div>]]

function export.create_list(args)
	-- Fields in args that are used:
	-- args.column_count, args.content, args.alphabetize, args.background_color,
	-- args.collapse, args.toggle_category, args.class, args.lang
	-- Check for required fields?
	if type(args) ~= "table" then
		error("expected table, got " .. type(args))
	end
	
	args.class = args.class or "derivedterms"
	args.column_count = args.column_count or 1
	args.toggle_category = args.toggle_category or "derived terms"
	
	local output = {}
	
	if args.header then
		table.insert(output, args.header)
	end
	if args.collapse then
		table.insert(output, (collapse_header:gsub('{{{(.-)}}}', args)))
	end
	table.insert(output, (column_header:gsub('{{{(.-)}}}', args)))
	
    if args.alphabetize then
		require("Module:collation").sort(args.content, args.lang)
	end
	table.insert(output, format_list_items(args.content, args.lang))
	
	table.insert(output, '</div>')
	if args.collapse then
		table.insert(output, button .. '</div>')
	end
	
	return table.concat(output)
end


-- This function is for compatibility with earlier version of [[Module:columns]]
-- (now found in [[Module:columns/old]]).
function export.create_table(...)
	-- Earlier arguments to create_table:
	-- n_columns, content, alphabetize, bg, collapse, class, title, column_width, line_start, lang
	local args = {}
	args.column_count, args.content, args.alphabetize, args.background_color,
		args.collapse, args.class, args.title, args.column_width,
		args.line_start, args.lang = ...
	
	return export.create_list(args)
end


function export.display(frame)
	local params = {
		["class"] = {},
		["collapse"] = {type = "boolean"},
		["columns"] = {type = "number"},
		["lang"] = {},
		["sort"] = {type = "boolean"},
		["title"] = {default = ""},
		["toggle_category"] = {},
	}
	
	local frame_args = require("Module:parameters").process(frame.args, params)
	
	params = {
		[1] = {list = true},
		
		["title"] = {},
		["lang"] = not frame_args["lang"] and {required = true, default = "und"} or nil,
		["collapse"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = frame_args["lang"] or args["lang"]
	lang = m_languages.getByCode(lang) or m_languages.err(lang, "lang")
	
	local header
	if args["title"] then
		header = '<div class="term-list-header">' .. args["title"] .. "</div>"
	end
	
	return export.create_list { column_count = frame_args["columns"],
		content = args[1], alphabetize = frame_args["sort"],
		header = header, background_color = "#F8F8FF",
		collapse = frame_args["collapse"],
		toggle_category = frame_args["toggle_category"],
		class = frame_args["class"], lang = lang, }
end


return export