From e04a02921ea015d4f4fe69c1c3f1813a07be9cf4 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 7 Jun 2017 13:37:51 +0500 Subject: Combine simple formatters in one factory --- web/app/AttributeFormatter.js | 48 ++++++++++++------------------------------- 1 file changed, 13 insertions(+), 35 deletions(-) (limited to 'web') diff --git a/web/app/AttributeFormatter.js b/web/app/AttributeFormatter.js index 423492d..3f7c127 100644 --- a/web/app/AttributeFormatter.js +++ b/web/app/AttributeFormatter.js @@ -18,6 +18,14 @@ Ext.define('Traccar.AttributeFormatter', { singleton: true, + numberFormatterFactory: function (precision, suffix) { + return function (value) { + if (value !== undefined) { + return Number(value.toFixed(precision)) + ' ' + suffix; + } + }; + }, + coordinateFormatter: function (key, value) { return Ext.getStore('CoordinateFormats').formatValue(key, value, Traccar.app.getPreference('coordinateFormat')); }, @@ -43,36 +51,6 @@ Ext.define('Traccar.AttributeFormatter', { return Ext.getStore('DistanceUnits').convertValue(value, Traccar.app.getPreference('distanceUnit')); }, - voltageFormatter: function (value) { - if (value !== undefined) { - return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' ' + Strings.sharedVoltAbbreviation; - } - }, - - percentageFormatter: function (value) { - if (value !== undefined) { - return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' %'; - } - }, - - temperatureFormatter: function (value) { - if (value !== undefined) { - return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' °C'; - } - }, - - volumeFormatter: function (value) { - if (value !== undefined) { - return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' ' + Strings.sharedLiterAbbreviation; - } - }, - - consumptionFormatter: function (value) { - if (value !== undefined) { - return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' ' + Strings.sharedLiterPerHourAbbreviation; - } - }, - hoursFormatter: function (value) { var hours = Math.round(value / 3600000); return (hours + ' ' + Strings.sharedHourAbbreviation); @@ -171,15 +149,15 @@ Ext.define('Traccar.AttributeFormatter', { } else if (dataType === 'speed') { return this.speedFormatter; } else if (dataType === 'voltage') { - return this.voltageFormatter; + return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedVoltAbbreviation); } else if (dataType === 'percentage') { - return this.percentageFormatter; + return this.numberFormatterFactory(Traccar.Style.numberPrecision, '%'); } else if (dataType === 'temperature') { - return this.temperatureFormatter; + return this.numberFormatterFactory(Traccar.Style.numberPrecision, '°C'); } else if (dataType === 'volume') { - return this.volumeFormatter; + return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedLiterAbbreviation); } else if (dataType === 'consumption') { - return this.consumptionFormatter; + return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedLiterPerHourAbbreviation); } else { return this.defaultFormatter; } -- cgit v1.2.3