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