Module:BaseTable

Revision as of 19:26, 23 March 2015 by com>Ckwng (Common base class for table generators.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local BaseData = require('Module:BaseData')

local BaseTable = BaseData{
	_table_start = [[{| class="wikitable"]],
	_row_starter = "|-",
	_header_template = "",
	_column_header_cell_template = "! ${value}",
	_cell_color = {
	},
	_cell = [[| colspan="1" rowspan="${rowspan}" style="text-align: ${text_align}; background-color: ${bg_color}; padding:5px 5px 5px 5px;" |${values.value}]],
	_column_cell_templates = {
	},
	_good_stat_color = "#ffff40",
	_outstanding_stat_color = "#73ff4d",
	_transparent = "transparent",
	_start_align = "start",
	_center_align = "center",
	_notes_suffix = "_notes",
	_highlight_suffix = "_highlight",
	_table_end = [[|}]],
	_operators = {
		["<"] = function(x,y) return x < y end,
		[">"] = function(x,y) return x > y end,
	},
}

function BaseTable:Table(args)
	return self{
		_args = args,
	}:create_table()
end

function BaseTable:get_bg_color(default, value, operator, outstanding, good)
	if not value then
		return default or self._transparent
	end
	local comparison = self._operators[operator or ">"]
	if outstanding and comparison(value, tonumber(outstanding)) then
		return self._outstanding_stat_color
	elseif good and comparison(value, tonumber(good)) then
		return self._good_stat_color
	else
		return default or self._transparent
	end
end

function BaseTable.compare_values(val1, val2)
	if #val1 == #val2 then
		for value_type, value in pairs(val1) do
			if value ~= val2[value_type] then
				return false
			end
		end
	else
		return false
	end
	return true
end