diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-09-27 23:55:42 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-27 23:55:42 +1300 |
commit | a47bf8f3eb6807c4fc9094f6e1e6b62185354b2c (patch) | |
tree | 2030cd9ce92d4bcfede0257d27dd96190b8a7e4a /web/app/view/StateController.js | |
parent | e236e3b26434aee0016a7a743e6e3bb0233e0e8d (diff) | |
parent | b1715a760cde09ec69a6756d7067a8c077d0fe2b (diff) | |
download | trackermap-web-a47bf8f3eb6807c4fc9094f6e1e6b62185354b2c.tar.gz trackermap-web-a47bf8f3eb6807c4fc9094f6e1e6b62185354b2c.tar.bz2 trackermap-web-a47bf8f3eb6807c4fc9094f6e1e6b62185354b2c.zip |
Merge pull request #287 from Abyss777/attribute_aliases
Implement attributes aliases
Diffstat (limited to 'web/app/view/StateController.js')
-rw-r--r-- | web/app/view/StateController.js | 89 |
1 files changed, 74 insertions, 15 deletions
diff --git a/web/app/view/StateController.js b/web/app/view/StateController.js index d92201b9..8a194940 100644 --- a/web/app/view/StateController.js +++ b/web/app/view/StateController.js @@ -21,15 +21,19 @@ Ext.define('Traccar.view.StateController', { requires: [ 'Traccar.AttributeFormatter', - 'Traccar.model.Attribute' + 'Traccar.model.Attribute', + 'Traccar.model.AttributeAlias', + 'Traccar.view.AttributeAliasDialog' + ], config: { listen: { controller: { '*': { - selectDevice: 'selectDevice', - selectReport: 'selectReport' + selectdevice: 'selectDevice', + selectreport: 'selectReport', + updatealiases: 'updateAliases' } }, store: { @@ -39,11 +43,19 @@ Ext.define('Traccar.view.StateController', { }, '#Positions': { clear: 'clearReport' + }, + '#AttributeAliases': { + add: 'updateAliases', + update: 'updateAliases' } } } }, + init: function () { + this.aliasesStore = Ext.getStore('AttributeAliases'); + }, + keys: (function () { var i, list, result; result = {}; @@ -66,7 +78,8 @@ Ext.define('Traccar.view.StateController', { } for (i = 0; i < data.length; i++) { if (this.deviceId === data[i].get('deviceId')) { - this.updatePosition(data[i]); + this.position = data[i]; + this.updatePosition(); } } }, @@ -79,30 +92,42 @@ Ext.define('Traccar.view.StateController', { } }, - updatePosition: function (position) { - var attributes, store, key; + findAttribute: function (record) { + return record.get('deviceId') === this.position.get('deviceId') && record.get('attribute') === this.lookupAttribute; + }, + + updatePosition: function () { + var attributes, store, key, aliasIndex, name; store = Ext.getStore('Attributes'); store.removeAll(); - for (key in position.data) { - if (position.data.hasOwnProperty(key) && this.keys[key] !== undefined) { + for (key in this.position.data) { + if (this.position.data.hasOwnProperty(key) && this.keys[key] !== undefined) { store.add(Ext.create('Traccar.model.Attribute', { priority: this.keys[key].priority, name: this.keys[key].name, - value: Traccar.AttributeFormatter.getFormatter(key)(position.get(key)) + value: Traccar.AttributeFormatter.getFormatter(key)(this.position.get(key)) })); } } - attributes = position.get('attributes'); + attributes = this.position.get('attributes'); if (attributes instanceof Object) { for (key in attributes) { if (attributes.hasOwnProperty(key)) { + this.lookupAttribute = key; + aliasIndex = this.aliasesStore.findBy(this.findAttribute, this); + if (aliasIndex !== -1) { + name = this.aliasesStore.getAt(aliasIndex).get('alias'); + } else { + name = key.replace(/^./, function (match) { + return match.toUpperCase(); + }); + } store.add(Ext.create('Traccar.model.Attribute', { priority: 1024, - name: key.replace(/^./, function (match) { - return match.toUpperCase(); - }), + name: name, + attribute: key, value: Traccar.AttributeFormatter.getFormatter(key)(attributes[key]) })); } @@ -115,18 +140,52 @@ Ext.define('Traccar.view.StateController', { this.deviceId = device.get('id'); position = Ext.getStore('LatestPositions').findRecord('deviceId', this.deviceId, 0, false, false, true); if (position) { - this.updatePosition(position); + this.position = position; + this.updatePosition(); } else { + this.position = null; Ext.getStore('Attributes').removeAll(); } }, selectReport: function (position) { this.deviceId = null; - this.updatePosition(position); + this.position = position; + this.updatePosition(); }, clearReport: function (store) { + this.position = null; Ext.getStore('Attributes').removeAll(); + }, + + onSelectionChange: function (selected, records) { + var enabled = selected.getCount() > 0 && records[0].get('priority') === 1024; + this.lookupReference('aliasEditButton').setDisabled(!enabled); + }, + + onAliasEditClick: function () { + var attribute, aliasIndex, attributeAlias, dialog; + attribute = this.getView().getSelectionModel().getSelection()[0]; + this.lookupAttribute = attribute.get('attribute'); + aliasIndex = this.aliasesStore.findBy(this.findAttribute, this); + if (aliasIndex !== -1) { + attributeAlias = this.aliasesStore.getAt(aliasIndex); + } else { + attributeAlias = Ext.create('Traccar.model.AttributeAlias', { + deviceId: this.position.get('deviceId'), + attribute: attribute.get('attribute') + }); + attributeAlias.store = this.aliasesStore; + } + dialog = Ext.create('Traccar.view.AttributeAliasDialog'); + dialog.down('form').loadRecord(attributeAlias); + dialog.show(); + }, + + updateAliases: function () { + if (this.position !== null) { + this.updatePosition(); + } } }); |