Jump to content

Module:Progress box

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 01:44, 15 May 2015 (flesh this out a bit and add a date-incrementing function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module implements [[Template:Progress box]]

local makePurgeLink = require('Module:Purge')._main
local lang = mw.language.getContentLanguage()
local START_DATE = '2005-03'

-------------------------------------------------------------------------------
-- Subtotal class
-------------------------------------------------------------------------------

local Subtotal = {}
Subtotal.__index = Subtotal

function Subtotal.new()
	local self = setmetatable({}, Subtotal)
	return self
end

function Subtotal.formatDate(date)
	return lang:formatDate('F Y', date)
end

function Subtotal:getDate()
end

function Subtotal:getFormattedDate()
	return self.formatDate(self:getDate())
end

function Subtotal:getSubtotal()
end

function Subtotal:makeCategoryLink(display)
end

-------------------------------------------------------------------------------
-- ProgressBox class
-------------------------------------------------------------------------------

local ProgressBox = {}
ProgressBox.__index = ProgressBox

function ProgressBox.new(args)
	args = args or {}
	local self = setmetatable({}, ProgressBox)

	-- Set data
	self.float = args.float or 'left'
	self.margin = args.float == 'none' and 'auto' or nil
	self.header = args[1]

	-- Make subtotal objects
	self.subtotals = {}

	return self
end

-- Increments a date in the format YYYY-MM
function ProgressBox.incrementDate(date)
	local year, month = date:match('^%d%d%d%d%-%d%d$')
	year = tonumber(year)
	month = tonumber(month)
	if not year or not month then
		error(string.format("error parsing date '%s'", tostring(date)), 2)
	end
	month = month + 1
	if month == 13 then
		month = 1
		year = year + 1
	end
	return string.format('%04d-%02d', year, month)
end

function ProgressBox:findEarliestCategoryDate()
	
end

function ProgressBox:isCollapsed()
end

function ProgressBox:__tostring()
	data = data or {}
	local root = mw.html.create('table')
	
	-- Base classes and styles
	root
		:addClass('infobox')
		:css('float', self.float)
		:css('clear', self.float)
		:css('margin', self.margin)
		:css('width', '22em')

	-- Header row
	root:tag('tr'):tag('th')
		:attr('colspan', 2)
		:addClass('navbox-title')
		:css('padding', '0.2em')
		:css('font-size', '125%')
		:wikitext(self.header)

	-- Refresh row
	root:tag('tr'):tag('th')
		:attr('colspan', 2)
		:css('text-align', 'center')
		:wikitext(makePurgeLink{'(refresh)'})

	-- Subtotals
	local subtotalTable = root
		:tag('tr')
			:tag('th')
				:attr('colspan', 2)
				:css('padding', 0)
				:tag('table')
					:addClass('collapsible')
					:addClass(self:isCollapsed() and 'collapsed' or nil)
					:css('width', '100%')
					:css('margin', 0)
	for i, subtotalObj in ipairs(self.subtotals) do
		subtotalTable
			:tag('tr')
				:tag('td')
					:wikitext(subtotalObj:makeCategoryLink(subtotalObj:getFormattedDate()))
					:done()
				:tag('td')
					:css('text-align', 'right')
					:wikitext(subtotalObj:getSubtotal())
	end

	-- Total
	root
		:tag('tr')
			:css('font-size', '110%')
			:tag('th')
				:wikitext(self:makeTotalLabel())
				:done()
			:tag('td')
				:css('text-align', 'right')
				:wikitext(string.format("'''%d'''", self:getTotalCount()))

	return tostring(root)
end

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

local p = {}

function p._main(args)
	return tostring(ProgressBox.new(args))
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Progress box'
	})
	return p._main(args)
end

return p