Module:WikiProjectBanner/Row

< Module:WikiProjectBanner
Revision as of 07:58, 16 July 2021 by Zoran (talk | contribs) (Pywikibot 6.4.0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:WikiProjectBanner/Row/doc

-------------------------------------------------------------------------------
--                Row class for Module:WikiProjectBanner                     --
--                                                                           --
-- This class is a parent class for all WikiProject banner rows, e.g.        --
-- quality ratings, importance ratings, task forces, notices and requests.   --
-------------------------------------------------------------------------------

-- Load required modules.
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType

-- Lazily load modules we might not need.
local mShared

local Row = {}
Row.__index = Row

function Row.new(args, bannerData, cfg, rowCfg)
	-- Validate the input.
	checkType('Row.new', 1, args, 'table')
	checkType('Row.new', 2, bannerData, 'table')
	checkType('Row.new', 3, cfg, 'table')
	checkType('Row.new', 4, rowCfg, 'table', true)

	-- Create the object.
	local obj = {}
	setmetatable(obj, Row)

	-- Set the data we were passed.
	obj.args = args
	obj.bannerData = bannerData
	obj.cfg = cfg
	obj.rowCfg = rowCfg or {}

	-- Define the object structure.
	obj.categories = {}

	return obj
end

function Row:isActive()
	-- To be used only by hooks.
	mShared = mShared or require('Module:WikiProjectBanner/shared')
	return mShared.isActiveRow(
		self.args,
		self.bannerData,
		self.cfg,
		self.rowCfg
	)
end

function Row:_setField(field, val, options)
	options = options or {}
	local valType = type(val)

	if valType == 'function' then
		val = val{
			args = self.args,
			bannerData = self.bannerData,
			cfg = self.cfg,
			rowCfg = self.rowCfg
		}
		valType = type(val)
	end

	if valType == 'string' then
		self[field] = val
	elseif val ~= nil then
		error(string.format(
			'the %s value must be a string (received %s)',
			field,
			valType
		), 3)
	end

	self[field .. 'Style'] = options.style
	self[field .. 'Class'] = options.class
end

function Row:setIcon(...)
	self:_setField('icon', ...)
end

function Row:setText(...)
	self:_setField('text', ...)
end

function Row:addCategory(cat)
	table.insert(self.categories, cat)
end

function Row:exportHtml()
	local row = mw.html.create('tr')
	row
		:tag('td')
			:addClass(self.iconClass)
			:css(self.iconStyle or {})
			:wikitext(self.icon)
			:done()
		:tag('td')
			:addClass('mbox-text')
			:addClass(self.textClass)
			:attr('colspan', '2')
			:css(self.textStyle or {})
			:wikitext(self.text)
	return row
end

function Row:exportCategories()
	return self.categories
end

return Row