UNPKG

@true-directive/base

Version:

The set of base classes for the TrueDirective Grid

80 lines (79 loc) 3.04 kB
/** * Copyright (c) 2018-2019 Aleksey Melnikov, True Directive Company. * @link https://truedirective.com/ * @license MIT */ import { ColumnType } from './enums'; import { Mask } from '../mask/mask.class'; import { NumberFormat } from '../numbers/number-format.class'; import { NumberParserFormatter } from '../numbers/number-parser-formatter.class'; import { DateParserFormatter } from '../dates/date-parser-formatter.class'; /** * Форматирование числовых значений и дат/времени для вывода в гриде */ var ValueFormatter = /** @class */ (function () { function ValueFormatter() { this._dateFormats = {}; this._numberFormats = {}; } /** * Установка требуемой локализации, из которой будут взяты разделители чисел * и шаблоны дат/времени по умолчанию * @param locale Локализация. */ ValueFormatter.prototype.setLocale = function (locale) { this._locale = locale; for (var fmt in this._dateFormats) { if (this._dateFormats.hasOwnProperty(fmt)) { this._dateFormats[fmt].setLocale(this._locale); } } }; ValueFormatter.prototype.getNumFormat = function (fmt) { fmt = fmt === '' ? '{1.2-10}' : fmt; // По умолчанию if (fmt === 'currency') { fmt = this._locale.currency; } if (!this._numberFormats[fmt]) { this._numberFormats[fmt] = NumberFormat.parseFormat(fmt); } return this._numberFormats[fmt]; }; ValueFormatter.prototype.getDateFormat = function (fmt) { if (!this._dateFormats[fmt]) { this._dateFormats[fmt] = new Mask(); this._dateFormats[fmt].setLocale(this._locale); this._dateFormats[fmt].pattern = fmt; } return this._dateFormats[fmt]; }; ValueFormatter.prototype.format = function (cType, format, value) { if (format === '') { return value; } var res = ''; if (cType === ColumnType.NUMBER) { var nf = this.getNumFormat(format); res = NumberParserFormatter.format(value, nf, this._locale.separators); } if (cType === ColumnType.DATETIME) { var m = this.getDateFormat(format); res = DateParserFormatter.format(value, m); } return res; }; ValueFormatter.prototype.displayedValue = function (c, value, row) { if (c.displayField !== '') { return row[c.displayField]; } if (value === null || value === undefined) { return ''; } if (!c || c.format === '') { return value; } return this.format(c.type, c.format, value); }; return ValueFormatter; }()); export { ValueFormatter };