diff --git a/js/src/configdialogmodel.js b/js/src/configdialogmodel.js index b698880..1404ba8 100644 --- a/js/src/configdialogmodel.js +++ b/js/src/configdialogmodel.js @@ -37,7 +37,7 @@ export default class ConfigDialogModel extends ViewModel { lang: config.lang, mapApi: config.mapApi, googleKey: config.googleKey, - layerId: config.olLayers.getPriorityLayer(), + layerId: config.olLayers.getPriorityLayer().toString(), layers: new uLayerCollection(new uLayer(0, 'OpenStreetMap', '', 0), ...config.olLayers), layerName: null, layerUrl: null, @@ -75,8 +75,9 @@ export default class ConfigDialogModel extends ViewModel { this.layerSelect = new uSelect(this.getBoundElement('layerId')); this.layerSelect.setOptions(this.model.layers, this.currentLayer().listValue); this.setPublicTracksActivity(this.model.requireAuth); + this.toggleEditVisible(); this.onChanged('layerId', (listValue) => { - const layer = this.model.layers.get(listValue); + const layer = this.model.layers.get(parseInt(listValue)); this.model.layerName = layer ? layer.name : ''; this.model.layerUrl = layer ? layer.url : ''; this.toggleEditVisible(); @@ -102,7 +103,7 @@ export default class ConfigDialogModel extends ViewModel { onSave() { if (this.validate()) { - this.model.layers.setPriorityLayer(this.model.layerId); + this.model.layers.setPriorityLayer(parseInt(this.model.layerId)); config.save(this.model) .then(() => this.dialog.destroy()) .catch((e) => { uAlert.error(`${$._('actionfailure')}\n${e.message}`, e); }); @@ -119,7 +120,7 @@ export default class ConfigDialogModel extends ViewModel { } toggleEditVisible() { - if (this.model.layerId > 0) { + if (parseInt(this.model.layerId) > 0) { this.toggleEditEl.style.visibility = 'visible'; } else { this.toggleEditEl.style.visibility = 'hidden'; @@ -128,8 +129,8 @@ export default class ConfigDialogModel extends ViewModel { } onLayerDelete() { - this.model.layers.delete(this.model.layerId); - this.model.layerId = 0; + this.model.layers.delete(parseInt(this.model.layerId)); + this.model.layerId = '0'; } onLayerEdit() { @@ -144,7 +145,7 @@ export default class ConfigDialogModel extends ViewModel { if (!this.model.layerName || !this.model.layerUrl) { return; } - if (this.model.layerId === -1) { + if (this.model.layerId === '-1') { this.model.layers.addNewLayer(this.model.layerName, this.model.layerUrl); } else { const layer = this.currentLayer(); @@ -161,7 +162,7 @@ export default class ConfigDialogModel extends ViewModel { } onLayerAdd() { - this.model.layerId = -1; + this.model.layerId = '-1'; this.onLayerEdit(); } @@ -174,7 +175,7 @@ export default class ConfigDialogModel extends ViewModel { } currentLayer() { - return this.model.layers.get(this.model.layerId); + return this.model.layers.get(parseInt(this.model.layerId)); } /** diff --git a/js/src/layercollection.js b/js/src/layercollection.js index 4c03556..31c3c33 100644 --- a/js/src/layercollection.js +++ b/js/src/layercollection.js @@ -55,13 +55,10 @@ export default class uLayerCollection extends Array { } /** - * @param {number|string} id Id or listValue + * @param {number} id * @return {uLayer} */ get(id) { - if (typeof id === 'string') { - return this.find((o) => o.listValue === id); - } return this.find((o) => o.id === id); } diff --git a/js/test/configdialogmodel.test.js b/js/test/configdialogmodel.test.js index 80e22a6..00d27ea 100644 --- a/js/test/configdialogmodel.test.js +++ b/js/test/configdialogmodel.test.js @@ -97,7 +97,7 @@ describe('ConfigDialogModel tests', () => { // then setTimeout(() => { expect(cm.layerEditEl.style.display).toBe('block'); - expect(cm.model.layerId).toBe(-1); + expect(cm.model.layerId).toBe('-1'); done(); }, 100); }); @@ -112,7 +112,7 @@ describe('ConfigDialogModel tests', () => { // then setTimeout(() => { expect(cm.layerEditEl.style.display).toBe('none'); - expect(cm.model.layerId).toBe(-1); + expect(cm.model.layerId).toBe('-1'); done(); }, 100); }); @@ -134,6 +134,22 @@ describe('ConfigDialogModel tests', () => { }, 100); }); + it('should set priority layer on save', (done) => { + // given + spyOn(cm, 'validate').and.returnValue(true); + spyOn(config, 'save').and.returnValue(Promise.resolve()); + cm.model.layerId = '1'; + cm.init(); + const button = cm.dialog.element.querySelector("[data-bind='onSave']"); + // when + button.click(); + // then + setTimeout(() => { + expect(cm.model.layers[1].priority).toBe(1); + done(); + }, 100); + }); + it('should hide dialog on negative button clicked', (done) => { // given cm.init(); @@ -192,7 +208,7 @@ describe('ConfigDialogModel tests', () => { // then expect(layers.length).toBe(1); expect(layers[0].id).toBe(0); - expect(cm.model.layerId).toBe(0); + expect(cm.model.layerId).toBe('0'); done(); }, 100); }, 100); diff --git a/js/test/layercollection.test.js b/js/test/layercollection.test.js index d73fdee..58bf82e 100644 --- a/js/test/layercollection.test.js +++ b/js/test/layercollection.test.js @@ -86,14 +86,6 @@ describe('LayerCollection tests', () => { expect(layers.get(testId).id).toBe(testId); }); - it('should get layer by id (string)', () => { - // when - layers.addLayer(testId + 1, testName, testUrl, testPriority); - layers.addLayer(testId, testName, testUrl, testPriority); - // then - expect(layers.get(testId.toString()).id).toBe(testId); - }); - it('should get max id of all layers in array', () => { // when layers.addLayer(testId + 1, testName, testUrl, testPriority);