Skip to content

Commit

Permalink
Basic implementation of filter API (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Feb 16, 2015
1 parent ce9f131 commit b1fb860
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
31 changes: 25 additions & 6 deletions js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,42 @@ Style.prototype = util.inherit(Evented, {
return this._layers[id];
},

setPaintProperty: function(layer, name, value, klass) {
this.getLayer(layer).setPaintProperty(name, value, klass);
getReferentLayer: function(id) {
var layer = this.getLayer(id);
if (layer.ref) {
layer = this.getLayer(layer.ref);
}
return layer;
},

getPaintProperty: function(layer, name, klass) {
return this.getLayer(layer).getPaintProperty(name, klass);
setFilter: function(layer, filter) {
layer = this.getReferentLayer(layer);
layer.filter = filter;
this._broadcastLayers();
this.sources[layer.source].reload();
},

getFilter: function(layer) {
return this.getReferentLayer(layer).filter;
},

setLayoutProperty: function(layer, name, value) {
layer = this.getLayer(layer);
layer = this.getReferentLayer(layer);
layer.setLayoutProperty(name, value);
this._broadcastLayers();
this.sources[layer.source].reload();
},

getLayoutProperty: function(layer, name) {
return this.getLayer(layer).getLayoutProperty(name);
return this.getReferentLayer(layer).getLayoutProperty(name);
},

setPaintProperty: function(layer, name, value, klass) {
this.getLayer(layer).setPaintProperty(name, value, klass);
},

getPaintProperty: function(layer, name, klass) {
return this.getLayer(layer).getPaintProperty(name, klass);
},

featuresAt: function(point, params, callback) {
Expand Down
9 changes: 9 additions & 0 deletions js/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ util.extend(Map.prototype, {
return this;
},

setFilter: function(layer, filter) {
this.style.setFilter(layer, filter);
return this;
},

getFilter: function(layer) {
return this.style.getFilter(layer);
},

setPaintProperty: function(layer, name, value, klass) {
this.style.setPaintProperty(layer, name, value, klass);
this.style._cascade(this._classes);
Expand Down
41 changes: 35 additions & 6 deletions test/js/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,30 @@ test('Style#removeSource', function(t) {
});
});

test('Style#setPaintProperty', function(t) {
t.test('sets property', function(t) {
test('Style#setFilter', function(t) {
t.test('sets a layer filter', function(t) {
var style = new Style({
"version": 7,
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": []
}
}
},
"layers": [{
"id": "background",
"type": "background"
"id": "symbol",
"type": "symbol",
"source": "geojson",
"filter": ["==", "id", 0]
}]
});

style.on('load', function() {
style.setPaintProperty('background', 'background-color', 'red');
t.deepEqual(style.getPaintProperty('background', 'background-color'), [1, 0, 0, 1]);
style.setFilter('symbol', ["==", "id", 1]);
t.deepEqual(style.getFilter('symbol'), ["==", "id", 1]);
t.end();
});
});
Expand Down Expand Up @@ -271,6 +282,24 @@ test('Style#setLayoutProperty', function(t) {
});
});

test('Style#setPaintProperty', function(t) {
t.test('sets property', function(t) {
var style = new Style({
"version": 7,
"layers": [{
"id": "background",
"type": "background"
}]
});

style.on('load', function() {
style.setPaintProperty('background', 'background-color', 'red');
t.deepEqual(style.getPaintProperty('background', 'background-color'), [1, 0, 0, 1]);
t.end();
});
});
});

test('Style#featuresAt', function(t) {
var style = new Style({
"version": 7,
Expand Down

0 comments on commit b1fb860

Please sign in to comment.