Module:Navbox

From Screamer Wiki
Revision as of 13:34, 6 September 2022 by Pmb1226 (talk | contribs)
Jump to: navigation, search

Script error: No such module "Message box". Script error: No such module "Message box". Lua error in package.lua at line 80: module 'Module:Message box' not found.

This module implements the {{Navbox}} template. Please see the template page for usage instructions.

Tracking/maintenance categories


-- Load necessary modules.
require('Module:No globals')
local getArgs

local p = { }

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

local function union(t1, t2)
	local temp = {}
	for _, v in pairs(t1) do
		temp[v] = true
	end
	for _, v in pairs(t2) do
		temp[v] = true
	end
	local ret = {}
	for k in pairs(temp) do
		table.insert(ret, k)
	end
	table.sort(ret)
	return ret
end

local function getArgNums(prefix)
	local ret = { }
	for k in pairs(mw.getCurrentFrame():getParent().args) do
		if string.match(k, "^" .. prefix .. "[0-9]%d*$") then
			table.insert(ret, tonumber(string.sub(k, string.len(prefix) + 1)))
		end
	end
	table.sort(ret)
	return ret
end

--------------------------------------------------------------------------------
-- Box class definition
--------------------------------------------------------------------------------

local Navbox = {}
Navbox.__index = Navbox

function Navbox.new(args)
	args = args or {}
	local obj = {}

	-- Set the arguments
	obj.args = args
	
	mw.logObject(obj)

	return setmetatable(obj, Navbox)
end

function Navbox:setParameters()
	local args = self.args
	
	self.title = args.title
	self.above = args.above

	self.rows = self:setRows()
	
	self.below = args.below
	
	self.image = args.image
	self.imageleft = args.imageleft
	
	self.subgroup = args[1] == 'child'
end

function Navbox:setRows()
	local args = self.args
	
	local nums = union(getArgNums('group'), getArgNums('list'))

	local t = { }
	
	for _,v in pairs(nums) do
		table.insert(t, 
			{
				group = args['group' .. v],
				list = args['list' .. v]
			})
	end
	
	return t
end

function Navbox:export()
	local root = mw.html.create():tag('div')

	if not self.subgroup then
		root:addClass('navbox')
		root:addClass('mw-collapsible')
	else
		root:addClass('navbox-subgroup')
	end
	
	if self.title then
		local title = root:tag('div')
		title
			:addClass('navbox-title')
			:wikitext(self.title)
	end
	
	local inner = root:tag('div')
	inner:addClass('navbox-inner')
	
	if not self.subgroup then
		inner:addClass('mw-collapsible-content')
	end
	
	if self.above then
		local above = inner:tag('div')
		above
			:addClass('navbox-above')
			:wikitext(self.above)
	end
	
	if self.imageleft then
		local imageleft = inner:tag('div')
		imageleft
			:addClass('navbox-image')
			:addClass('navbox-imageleft')
			:wikitext(self.imageleft)
	end
	
	if self.image then
		local image = inner:tag('div')
		image
			:addClass('navbox-image')
			:wikitext(self.image)
	end
	
	for _,v in ipairs(self.rows) do
		if v.list then
			if v.group then
				local group = inner:tag('div')
				group:addClass('navbox-group')
				group:wikitext(v.group)
			end
			
			local list = inner:tag('div')
			list:addClass('navbox-list')
			list:addClass('hlist')
			list:newline()
			list:wikitext(v.list)
			list:newline()
		end
	end
	
	if self.below then
		local below = inner:tag('div')
		below
			:addClass('navbox-below')
			:wikitext(self.below)
	end
	
	local navboxStyle = mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = 'Template:Navbox/styles.css' } );
	local hlistStyle = mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = 'Template:Hlist/styles.css' } );

	return navboxStyle .. hlistStyle .. tostring(root)
end

--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------

function p.navbox(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
		
	local box = Navbox.new(getArgs(frame))
	box:setParameters()
	return box:export()
end

if mw.site.siteName:byte() == 66 then
	return p
end