Catch errors in style definition

Do not replace the layer if the style is invalid.
Thrown errors are caught and the layer will not be changed.
This commit is contained in:
Maximilian Krög
2019-10-03 00:47:23 +02:00
parent dee114d4c4
commit b4a996e760

View File

@@ -62,15 +62,23 @@ const map = new Map({
let literalStyle; let literalStyle;
let pointsLayer; let pointsLayer;
function refreshLayer() { function refreshLayer(newStyle) {
if (pointsLayer) { try {
map.removeLayer(pointsLayer); const previousLayer = pointsLayer;
}
pointsLayer = new WebGLPointsLayer({ pointsLayer = new WebGLPointsLayer({
source: vectorSource, source: vectorSource,
style: literalStyle style: newStyle
}); });
map.addLayer(pointsLayer); map.addLayer(pointsLayer);
if (previousLayer) {
map.removeLayer(previousLayer);
}
literalStyle = newStyle;
return true;
} catch (e) {
return false;
}
} }
function setStyleStatus(valid) { function setStyleStatus(valid) {
@@ -83,11 +91,8 @@ editor.addEventListener('input', function() {
const textStyle = editor.value; const textStyle = editor.value;
try { try {
const newLiteralStyle = JSON.parse(textStyle); const newLiteralStyle = JSON.parse(textStyle);
if (JSON.stringify(newLiteralStyle) !== JSON.stringify(literalStyle)) { const same = JSON.stringify(newLiteralStyle) === JSON.stringify(literalStyle);
literalStyle = newLiteralStyle; setStyleStatus(same || refreshLayer(newLiteralStyle));
refreshLayer();
}
setStyleStatus(true);
} catch (e) { } catch (e) {
setStyleStatus(false); setStyleStatus(false);
} }
@@ -97,9 +102,10 @@ const select = document.getElementById('style-select');
select.value = 'circles'; select.value = 'circles';
function onSelectChange() { function onSelectChange() {
const style = select.value; const style = select.value;
literalStyle = predefinedStyles[style]; const newLiteralStyle = predefinedStyles[style];
editor.value = JSON.stringify(literalStyle, null, 2); if (refreshLayer(newLiteralStyle)) {
refreshLayer(); editor.value = JSON.stringify(newLiteralStyle, null, 2);
}
setStyleStatus(); setStyleStatus();
} }
onSelectChange(); onSelectChange();