From c40271071c2a8caae5d3727d0160fb6aae1178f9 Mon Sep 17 00:00:00 2001 From: Per Liedman Date: Fri, 2 Dec 2016 13:19:59 +0100 Subject: [PATCH 1/2] Remember overridden styles between draw calls Close #33. --- src/Leaflet.VectorGrid.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Leaflet.VectorGrid.js b/src/Leaflet.VectorGrid.js index 39ccb7a..13e1f96 100644 --- a/src/Leaflet.VectorGrid.js +++ b/src/Leaflet.VectorGrid.js @@ -13,6 +13,7 @@ L.VectorGrid = L.GridLayer.extend({ L.GridLayer.prototype.initialize.apply(this, arguments); if (this.options.getFeatureId) { this._vectorTiles = {}; + this._overriddenStyles = {}; this.on('tileunload', function(e) { delete this._vectorTiles[this._tileCoordsToKey(e.coords)]; }, this); @@ -44,10 +45,20 @@ L.VectorGrid = L.GridLayer.extend({ for (var i in layer.features) { var feat = layer.features[i]; + var id; - var styleOptions = (layerStyle instanceof Function) ? - layerStyle(feat.properties, coords.z) : - layerStyle; + var styleOptions = layerStyle; + if (storeFeatures) { + id = this.options.getFeatureId(feat); + var styleOverride = this._overriddenStyles[id]; + if (styleOverride) { + styleOptions = styleOverride[layerName]; + } + } + + if (styleOptions instanceof Function) { + styleOptions = styleOptions(feat.properties, coords.z); + } if (!(styleOptions instanceof Array)) { styleOptions = [styleOptions]; @@ -70,8 +81,6 @@ L.VectorGrid = L.GridLayer.extend({ } if (storeFeatures) { - var id = this.options.getFeatureId(feat); - renderer._features[id] = { layerName: layerName, feature: featureLayer @@ -88,11 +97,14 @@ L.VectorGrid = L.GridLayer.extend({ }, setFeatureStyle: function(id, layerStyle) { + var styleOverride = this._overriddenStyles[id] = {} + for (var tileKey in this._vectorTiles) { var tile = this._vectorTiles[tileKey]; var features = tile._features; var data = features[id]; if (data) { + styleOverride[data.layerName] = layerStyle; var feat = data.feature; var styleOptions = (layerStyle instanceof Function) ? layerStyle(feat.properties, tile.getCoord().z) : @@ -103,6 +115,8 @@ L.VectorGrid = L.GridLayer.extend({ }, resetFeatureStyle: function(id) { + delete this._overriddenStyles[id]; + for (var tileKey in this._vectorTiles) { var tile = this._vectorTiles[tileKey]; var features = tile._features; From c7a09dfacca28c3dad2e669c6250446dd5a0cd28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Fri, 2 Dec 2016 14:37:24 +0100 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b976f04..60d5abf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Refactored the code modules into ES6 modules * Switched the build system and dependencies to use RollupJS +* Store styles set through `setFeatureStyle` so the changes persist after zooming/panning ## 1.1.0