diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-08-22 14:11:55 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-22 14:11:55 +1200 |
commit | 0401e755936df29af9eeb129f8cc99e3b151f05c (patch) | |
tree | 1fa898c25d4fb3695c34f1e96507e61b6d3da7b8 /web/app | |
parent | e0bb9b92d07176677b3043530660af3bc30774d7 (diff) | |
parent | 7e2fb28b558f404442bec67a7551e75fc90ff100 (diff) | |
download | etbsa-traccar-web-0401e755936df29af9eeb129f8cc99e3b151f05c.tar.gz etbsa-traccar-web-0401e755936df29af9eeb129f8cc99e3b151f05c.tar.bz2 etbsa-traccar-web-0401e755936df29af9eeb129f8cc99e3b151f05c.zip |
Merge pull request #553 from tsmgeek/optimise-ifs
Optimise ifs and loops
Diffstat (limited to 'web/app')
-rw-r--r-- | web/app/AttributeFormatter.js | 136 | ||||
-rw-r--r-- | web/app/view/edit/AttributesController.js | 22 | ||||
-rw-r--r-- | web/app/view/map/BaseMap.js | 225 | ||||
-rw-r--r-- | web/app/view/map/MapMarkerController.js | 37 |
4 files changed, 223 insertions, 197 deletions
diff --git a/web/app/AttributeFormatter.js b/web/app/AttributeFormatter.js index 87689a2..957992d 100644 --- a/web/app/AttributeFormatter.js +++ b/web/app/AttributeFormatter.js @@ -139,86 +139,90 @@ Ext.define('Traccar.AttributeFormatter', { getFormatter: function (key) { var self = this; - if (key === 'latitude' || key === 'longitude') { - return function (value) { - return self.coordinateFormatter(key, value); - }; - } else if (key === 'speed') { - return this.speedFormatter; - } else if (key === 'course') { - return this.courseFormatter; - } else if (key === 'distance' || key === 'accuracy') { - return this.distanceFormatter; - } else if (key === 'duration') { - return this.durationFormatter; - } else if (key === 'deviceId') { - return this.deviceIdFormatter; - } else if (key === 'groupId') { - return this.groupIdFormatter; - } else if (key === 'geofenceId') { - return this.geofenceIdFormatter; - } else if (key === 'lastUpdate') { - return this.lastUpdateFormatter; - } else if (key === 'spentFuel') { - return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedLiterAbbreviation); - } else if (key === 'driverUniqueId') { - return this.driverUniqueIdFormatter; - } else { - return this.defaultFormatter; + + switch (key) { + case 'latitude': + case 'longitude': + return function (value) { + return self.coordinateFormatter(key, value); + }; + case 'speed': + return this.speedFormatter; + case 'course': + return this.courseFormatter; + case 'distance': + case 'accuracy': + return this.distanceFormatter; + case 'duration': + return this.durationFormatter; + case 'deviceId': + return this.deviceIdFormatter; + case 'groupId': + return this.groupIdFormatter; + case 'geofenceId': + return this.geofenceIdFormatter; + case 'lastUpdate': + return this.lastUpdateFormatter; + case 'spentFuel': + return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedLiterAbbreviation); + case 'driverUniqueId': + return this.driverUniqueIdFormatter; + default: + return this.defaultFormatter; } }, getConverter: function (key) { - if (key === 'speed') { - return this.speedConverter; - } else if (key === 'distance' || key === 'accuracy') { - return this.distanceConverter; - } else { - return function (value) { - return value; - }; + switch (key) { + case 'speed': + return this.speedConverter; + case 'distance': + case 'accuracy': + return this.distanceConverter; + default: + return function (value) { + return value; + }; } }, getAttributeFormatter: function (key) { var dataType = Ext.getStore('PositionAttributes').getAttributeDataType(key); - if (!dataType) { - return this.defaultFormatter; - } else if (dataType === 'distance') { - return this.distanceFormatter; - } else if (dataType === 'speed') { - return this.speedFormatter; - } else if (dataType === 'driverUniqueId') { - return this.driverUniqueIdFormatter; - } else if (dataType === 'voltage') { - return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedVoltAbbreviation); - } else if (dataType === 'percentage') { - return this.numberFormatterFactory(Traccar.Style.numberPrecision, '%'); - } else if (dataType === 'temperature') { - return this.numberFormatterFactory(Traccar.Style.numberPrecision, '°C'); - } else if (dataType === 'volume') { - return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedLiterAbbreviation); - } else if (dataType === 'consumption') { - return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedLiterPerHourAbbreviation); - } else { - return this.defaultFormatter; + + switch (dataType) { + case 'distance': + return this.distanceFormatter; + case 'speed': + return this.speedFormatter; + case 'driverUniqueId': + return this.driverUniqueIdFormatter; + case 'voltage': + return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedVoltAbbreviation); + case 'percentage': + return this.numberFormatterFactory(Traccar.Style.numberPrecision, '%'); + case 'temperature': + return this.numberFormatterFactory(Traccar.Style.numberPrecision, '°C'); + case 'volume': + return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedLiterAbbreviation); + case 'consumption': + return this.numberFormatterFactory(Traccar.Style.numberPrecision, Strings.sharedLiterPerHourAbbreviation); + default: + return this.defaultFormatter; } }, getAttributeConverter: function (key) { var dataType = Ext.getStore('PositionAttributes').getAttributeDataType(key); - if (!dataType) { - return function (value) { - return value; - }; - } else if (dataType === 'distance') { - return this.distanceConverter; - } else if (dataType === 'speed') { - return this.speedConverter; - } else { - return function (value) { - return value; - }; + + switch (dataType) { + case 'distance': + return this.distanceConverter; + case 'speed': + return this.speedConverter; + default: + return function (value) { + return value; + }; } } }); diff --git a/web/app/view/edit/AttributesController.js b/web/app/view/edit/AttributesController.js index c12c196..84ff6ad 100644 --- a/web/app/view/edit/AttributesController.js +++ b/web/app/view/edit/AttributesController.js @@ -45,27 +45,31 @@ Ext.define('Traccar.view.edit.AttributesController', { } } store.addListener('add', function (store, records) { - var i; + var i, view; + view = this.getView(); for (i = 0; i < records.length; i++) { - this.getView().record.get('attributes')[records[i].get('name')] = records[i].get('value'); + view.record.get('attributes')[records[i].get('name')] = records[i].get('value'); } - this.getView().record.dirty = true; + view.record.dirty = true; }, this); store.addListener('update', function (store, record, operation) { + var view; + view = this.getView(); if (operation === Ext.data.Model.EDIT) { if (record.modified.name !== record.get('name')) { - delete this.getView().record.get('attributes')[record.modified.name]; + delete view.record.get('attributes')[record.modified.name]; } - this.getView().record.get('attributes')[record.get('name')] = record.get('value'); - this.getView().record.dirty = true; + view.record.get('attributes')[record.get('name')] = record.get('value'); + view.record.dirty = true; } }, this); store.addListener('remove', function (store, records) { - var i; + var i, view; + view = this.getView(); for (i = 0; i < records.length; i++) { - delete this.getView().record.get('attributes')[records[i].get('name')]; + delete view.record.get('attributes')[records[i].get('name')]; } - this.getView().record.dirty = true; + view.record.dirty = true; }, this); this.getView().setStore(store); diff --git a/web/app/view/map/BaseMap.js b/web/app/view/map/BaseMap.js index 91f3784..c6eb5b4 100644 --- a/web/app/view/map/BaseMap.js +++ b/web/app/view/map/BaseMap.js @@ -37,116 +37,125 @@ Ext.define('Traccar.view.map.BaseMap', { type = Traccar.app.getPreference('map', null); bingKey = server.get('bingKey'); - if (type === 'custom') { - layer = new ol.layer.Tile({ - source: new ol.source.XYZ({ - url: server.get('mapUrl'), - attributions: [ - new ol.Attribution({ - html: '' - }) - ] - }) - }); - } else if (type === 'bingRoad') { - layer = new ol.layer.Tile({ - source: new ol.source.BingMaps({ - key: bingKey, - imagerySet: 'Road' - }) - }); - } else if (type === 'bingAerial') { - layer = new ol.layer.Tile({ - source: new ol.source.BingMaps({ - key: bingKey, - imagerySet: 'Aerial' - }) - }); - } else if (type === 'bingHybrid') { - layer = new ol.layer.Tile({ - source: new ol.source.BingMaps({ - key: bingKey, - imagerySet: 'AerialWithLabels' - }) - }); - } else if (type === 'carto') { - layer = new ol.layer.Tile({ - source: new ol.source.XYZ({ - url: 'https://cartodb-basemaps-{a-d}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', - attributions: [ - new ol.Attribution({ - html: [ - '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> ' + - 'contributors, © <a href="https://carto.com/attributions">CARTO</a>' + switch (type) { + case 'custom': + layer = new ol.layer.Tile({ + source: new ol.source.XYZ({ + url: server.get('mapUrl'), + attributions: [ + new ol.Attribution({ + html: '' + }) + ] + }) + }); + break; + case 'bingRoad': + layer = new ol.layer.Tile({ + source: new ol.source.BingMaps({ + key: bingKey, + imagerySet: 'Road' + }) + }); + break; + case 'bingAerial': + layer = new ol.layer.Tile({ + source: new ol.source.BingMaps({ + key: bingKey, + imagerySet: 'Aerial' + }) + }); + break; + case 'bingHybrid': + layer = new ol.layer.Tile({ + source: new ol.source.BingMaps({ + key: bingKey, + imagerySet: 'AerialWithLabels' + }) + }); + break; + case 'carto': + layer = new ol.layer.Tile({ + source: new ol.source.XYZ({ + url: 'https://cartodb-basemaps-{a-d}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', + attributions: [ + new ol.Attribution({ + html: [ + '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> ' + + 'contributors, © <a href="https://carto.com/attributions">CARTO</a>' + ] + }) + ] + }) + }); + break; + case 'baidu': + layer = new ol.layer.Tile({ + source: new ol.source.XYZ({ + projection: 'BD-MC', + tileUrlFunction: function (tileCoord) { + var urlsLength = 5, z = tileCoord[0], x = tileCoord[1], y = tileCoord[2], hash, index; + + hash = (x << z) + y; + index = hash % urlsLength; + index = index < 0 ? index + urlsLength : index; + + if (x < 0) { + x = 'M' + -x; + } + if (y < 0) { + y = 'M' + -y; + } + return 'http://online{}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl' + .replace('{}', index).replace('{x}', x).replace('{y}', y).replace('{z}', z); + }, + tileGrid: new ol.tilegrid.TileGrid({ + extent: ol.proj.transformExtent([-180, -74, 180, 74], 'EPSG:4326', 'BD-MC'), + origin: [0, 0], + minZoom: 3, + resolutions: [ + 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, + 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.5 ] - }) - ] - }) - }); - } else if (type === 'baidu') { - layer = new ol.layer.Tile({ - source: new ol.source.XYZ({ - projection: 'BD-MC', - tileUrlFunction: function (tileCoord) { - var urlsLength = 5, z = tileCoord[0], x = tileCoord[1], y = tileCoord[2], hash, index; - - hash = (x << z) + y; - index = hash % urlsLength; - index = index < 0 ? index + urlsLength : index; - - if (x < 0) { - x = 'M' + -x; - } - if (y < 0) { - y = 'M' + -y; - } - return 'http://online{}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl' - .replace('{}', index).replace('{x}', x).replace('{y}', y).replace('{z}', z); - }, - tileGrid: new ol.tilegrid.TileGrid({ - extent: ol.proj.transformExtent([-180, -74, 180, 74], 'EPSG:4326', 'BD-MC'), - origin: [0, 0], - minZoom: 3, - resolutions: [ - 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, - 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 0.5 + }), + attributions: [ + new ol.Attribution({ + html: '© <a href="http://map.baidu.com/">Baidu</a>' + }) ] - }), - attributions: [ - new ol.Attribution({ - html: '© <a href="http://map.baidu.com/">Baidu</a>' - }) - ] - }) - }); - } else if (type === 'yandexMap') { - layer = new ol.layer.Tile({ - source: new ol.source.XYZ({ - url: 'https://vec0{1-4}.maps.yandex.net/tiles?l=map&x={x}&y={y}&z={z}', - projection: 'EPSG:3395', - attributions: [ - new ol.Attribution({ - html: '© <a href="https://yandex.com/maps/">Yandex</a>' - }) - ] - }) - }); - } else if (type === 'yandexSat') { - layer = new ol.layer.Tile({ - source: new ol.source.XYZ({ - url: 'https://sat0{1-4}.maps.yandex.net/tiles?l=sat&x={x}&y={y}&z={z}', - projection: 'EPSG:3395', - attributions: [ - new ol.Attribution({ - html: '© <a href="https://yandex.com/maps/">Yandex</a>' - }) - ] - }) - }); - } else { - layer = new ol.layer.Tile({ - source: new ol.source.OSM({}) - }); + }) + }); + break; + case 'yandexMap': + layer = new ol.layer.Tile({ + source: new ol.source.XYZ({ + url: 'https://vec0{1-4}.maps.yandex.net/tiles?l=map&x={x}&y={y}&z={z}', + projection: 'EPSG:3395', + attributions: [ + new ol.Attribution({ + html: '© <a href="https://yandex.com/maps/">Yandex</a>' + }) + ] + }) + }); + break; + case 'yandexSat': + layer = new ol.layer.Tile({ + source: new ol.source.XYZ({ + url: 'https://sat0{1-4}.maps.yandex.net/tiles?l=sat&x={x}&y={y}&z={z}', + projection: 'EPSG:3395', + attributions: [ + new ol.Attribution({ + html: '© <a href="https://yandex.com/maps/">Yandex</a>' + }) + ] + }) + }); + break; + default: + layer = new ol.layer.Tile({ + source: new ol.source.OSM({}) + }); } lat = Traccar.app.getPreference('latitude', Traccar.Style.mapDefaultLat); diff --git a/web/app/view/map/MapMarkerController.js b/web/app/view/map/MapMarkerController.js index d3b813b..c46faac 100644 --- a/web/app/view/map/MapMarkerController.js +++ b/web/app/view/map/MapMarkerController.js @@ -156,27 +156,30 @@ Ext.define('Traccar.view.map.MapMarkerController', { }, removeDevice: function (store, data) { - var i, deviceId; + var i, deviceId, markersSource; if (!Ext.isArray(data)) { data = [data]; } + + markersSource = this.getView().getMarkersSource(); + for (i = 0; i < data.length; i++) { deviceId = data[i].get('id'); if (this.latestMarkers[deviceId]) { - if (this.getView().getMarkersSource().getFeatureById(this.latestMarkers[deviceId].getId())) { - this.getView().getMarkersSource().removeFeature(this.latestMarkers[deviceId]); + if (markersSource.getFeatureById(this.latestMarkers[deviceId].getId())) { + markersSource.removeFeature(this.latestMarkers[deviceId]); } delete this.latestMarkers[deviceId]; } if (this.accuracyCircles[deviceId]) { - if (this.getView().getAccuracySource().getFeatureById(this.accuracyCircles[deviceId].getId())) { - this.getView().getAccuracySource().removeFeature(this.accuracyCircles[deviceId]); + if (markersSource.getFeatureById(this.accuracyCircles[deviceId].getId())) { + markersSource.removeFeature(this.accuracyCircles[deviceId]); } delete this.accuracyCircles[deviceId]; } if (this.liveRoutes[deviceId]) { - if (this.getView().getLiveRouteSource().getFeatureById(this.liveRoutes[deviceId].getId())) { - this.getView().getLiveRouteSource().removeFeature(this.liveRoutes[deviceId]); + if (markersSource.getFeatureById(this.liveRoutes[deviceId].getId())) { + markersSource.removeFeature(this.liveRoutes[deviceId]); } delete this.liveRoutes[deviceId]; } @@ -184,15 +187,17 @@ Ext.define('Traccar.view.map.MapMarkerController', { }, updateLatest: function (store, data) { - var i, position, device; + var i, position, device, deviceStore; if (!Ext.isArray(data)) { data = [data]; } + deviceStore = Ext.getStore('Devices'); + for (i = 0; i < data.length; i++) { position = data[i]; - device = Ext.getStore('Devices').getById(position.get('deviceId')); + device = deviceStore.getById(position.get('deviceId')); if (device) { this.updateAccuracy(position, device); @@ -303,9 +308,10 @@ Ext.define('Traccar.view.map.MapMarkerController', { }, loadReport: function (store, data) { - var i, position, point; + var i, position, point, routeSource; if (data) { this.addReportMarkers(store, data); + routeSource = this.getView().getRouteSource(); this.reportRoute = []; for (i = 0; i < data.length; i++) { @@ -319,7 +325,7 @@ Ext.define('Traccar.view.map.MapMarkerController', { geometry: new ol.geom.LineString([]) })); this.reportRoute[this.reportRoute.length - 1].setStyle(this.getRouteStyle(data[i].get('deviceId'))); - this.getView().getRouteSource().addFeature(this.reportRoute[this.reportRoute.length - 1]); + routeSource.addFeature(this.reportRoute[this.reportRoute.length - 1]); } this.reportRoute[this.reportRoute.length - 1].getGeometry().appendCoordinate(point); } @@ -352,19 +358,22 @@ Ext.define('Traccar.view.map.MapMarkerController', { }, clearReport: function () { - var key, i; + var key, i, reportSource, markersSource; + + reportSource = this.getView().getRouteSource(); if (this.reportRoute) { for (i = 0; i < this.reportRoute.length; i++) { - this.getView().getRouteSource().removeFeature(this.reportRoute[i]); + reportSource.removeFeature(this.reportRoute[i]); } this.reportRoute = null; } if (this.reportMarkers) { + markersSource = this.getView().getMarkersSource(); for (key in this.reportMarkers) { if (this.reportMarkers.hasOwnProperty(key)) { - this.getView().getMarkersSource().removeFeature(this.reportMarkers[key]); + markersSource.removeFeature(this.reportMarkers[key]); } } this.reportMarkers = {}; |