Fix saving default layer in config dialog

This commit is contained in:
Bartek Fabiszewski 2020-05-14 22:17:36 +02:00
parent e76b996d6b
commit f4ac9edad7
4 changed files with 30 additions and 24 deletions

View File

@ -37,7 +37,7 @@ export default class ConfigDialogModel extends ViewModel {
lang: config.lang, lang: config.lang,
mapApi: config.mapApi, mapApi: config.mapApi,
googleKey: config.googleKey, googleKey: config.googleKey,
layerId: config.olLayers.getPriorityLayer(), layerId: config.olLayers.getPriorityLayer().toString(),
layers: new uLayerCollection(new uLayer(0, 'OpenStreetMap', '', 0), ...config.olLayers), layers: new uLayerCollection(new uLayer(0, 'OpenStreetMap', '', 0), ...config.olLayers),
layerName: null, layerName: null,
layerUrl: null, layerUrl: null,
@ -75,8 +75,9 @@ export default class ConfigDialogModel extends ViewModel {
this.layerSelect = new uSelect(this.getBoundElement('layerId')); this.layerSelect = new uSelect(this.getBoundElement('layerId'));
this.layerSelect.setOptions(this.model.layers, this.currentLayer().listValue); this.layerSelect.setOptions(this.model.layers, this.currentLayer().listValue);
this.setPublicTracksActivity(this.model.requireAuth); this.setPublicTracksActivity(this.model.requireAuth);
this.toggleEditVisible();
this.onChanged('layerId', (listValue) => { 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.layerName = layer ? layer.name : '';
this.model.layerUrl = layer ? layer.url : ''; this.model.layerUrl = layer ? layer.url : '';
this.toggleEditVisible(); this.toggleEditVisible();
@ -102,7 +103,7 @@ export default class ConfigDialogModel extends ViewModel {
onSave() { onSave() {
if (this.validate()) { if (this.validate()) {
this.model.layers.setPriorityLayer(this.model.layerId); this.model.layers.setPriorityLayer(parseInt(this.model.layerId));
config.save(this.model) config.save(this.model)
.then(() => this.dialog.destroy()) .then(() => this.dialog.destroy())
.catch((e) => { uAlert.error(`${$._('actionfailure')}\n${e.message}`, e); }); .catch((e) => { uAlert.error(`${$._('actionfailure')}\n${e.message}`, e); });
@ -119,7 +120,7 @@ export default class ConfigDialogModel extends ViewModel {
} }
toggleEditVisible() { toggleEditVisible() {
if (this.model.layerId > 0) { if (parseInt(this.model.layerId) > 0) {
this.toggleEditEl.style.visibility = 'visible'; this.toggleEditEl.style.visibility = 'visible';
} else { } else {
this.toggleEditEl.style.visibility = 'hidden'; this.toggleEditEl.style.visibility = 'hidden';
@ -128,8 +129,8 @@ export default class ConfigDialogModel extends ViewModel {
} }
onLayerDelete() { onLayerDelete() {
this.model.layers.delete(this.model.layerId); this.model.layers.delete(parseInt(this.model.layerId));
this.model.layerId = 0; this.model.layerId = '0';
} }
onLayerEdit() { onLayerEdit() {
@ -144,7 +145,7 @@ export default class ConfigDialogModel extends ViewModel {
if (!this.model.layerName || !this.model.layerUrl) { if (!this.model.layerName || !this.model.layerUrl) {
return; return;
} }
if (this.model.layerId === -1) { if (this.model.layerId === '-1') {
this.model.layers.addNewLayer(this.model.layerName, this.model.layerUrl); this.model.layers.addNewLayer(this.model.layerName, this.model.layerUrl);
} else { } else {
const layer = this.currentLayer(); const layer = this.currentLayer();
@ -161,7 +162,7 @@ export default class ConfigDialogModel extends ViewModel {
} }
onLayerAdd() { onLayerAdd() {
this.model.layerId = -1; this.model.layerId = '-1';
this.onLayerEdit(); this.onLayerEdit();
} }
@ -174,7 +175,7 @@ export default class ConfigDialogModel extends ViewModel {
} }
currentLayer() { currentLayer() {
return this.model.layers.get(this.model.layerId); return this.model.layers.get(parseInt(this.model.layerId));
} }
/** /**

View File

@ -55,13 +55,10 @@ export default class uLayerCollection extends Array {
} }
/** /**
* @param {number|string} id Id or listValue * @param {number} id
* @return {uLayer} * @return {uLayer}
*/ */
get(id) { get(id) {
if (typeof id === 'string') {
return this.find((o) => o.listValue === id);
}
return this.find((o) => o.id === id); return this.find((o) => o.id === id);
} }

View File

@ -97,7 +97,7 @@ describe('ConfigDialogModel tests', () => {
// then // then
setTimeout(() => { setTimeout(() => {
expect(cm.layerEditEl.style.display).toBe('block'); expect(cm.layerEditEl.style.display).toBe('block');
expect(cm.model.layerId).toBe(-1); expect(cm.model.layerId).toBe('-1');
done(); done();
}, 100); }, 100);
}); });
@ -112,7 +112,7 @@ describe('ConfigDialogModel tests', () => {
// then // then
setTimeout(() => { setTimeout(() => {
expect(cm.layerEditEl.style.display).toBe('none'); expect(cm.layerEditEl.style.display).toBe('none');
expect(cm.model.layerId).toBe(-1); expect(cm.model.layerId).toBe('-1');
done(); done();
}, 100); }, 100);
}); });
@ -134,6 +134,22 @@ describe('ConfigDialogModel tests', () => {
}, 100); }, 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) => { it('should hide dialog on negative button clicked', (done) => {
// given // given
cm.init(); cm.init();
@ -192,7 +208,7 @@ describe('ConfigDialogModel tests', () => {
// then // then
expect(layers.length).toBe(1); expect(layers.length).toBe(1);
expect(layers[0].id).toBe(0); expect(layers[0].id).toBe(0);
expect(cm.model.layerId).toBe(0); expect(cm.model.layerId).toBe('0');
done(); done();
}, 100); }, 100);
}, 100); }, 100);

View File

@ -86,14 +86,6 @@ describe('LayerCollection tests', () => {
expect(layers.get(testId).id).toBe(testId); 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', () => { it('should get max id of all layers in array', () => {
// when // when
layers.addLayer(testId + 1, testName, testUrl, testPriority); layers.addLayer(testId + 1, testName, testUrl, testPriority);