Skip to content

Commit

Permalink
Merge pull request #38 from Leaflet/remember-overridden-styles
Browse files Browse the repository at this point in the history
Remember overridden styles between draw calls
  • Loading branch information
IvanSanchez committed Dec 2, 2016
2 parents fd327e6 + c7a09df commit 330acf2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
24 changes: 19 additions & 5 deletions src/Leaflet.VectorGrid.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand All @@ -70,8 +81,6 @@ L.VectorGrid = L.GridLayer.extend({
}

if (storeFeatures) {
var id = this.options.getFeatureId(feat);

renderer._features[id] = {
layerName: layerName,
feature: featureLayer
Expand All @@ -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) :
Expand All @@ -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;
Expand Down

0 comments on commit 330acf2

Please sign in to comment.