Added FeatureGroup setStyle and invoke methods, closed #353

This commit is contained in:
mourner 2011-12-15 13:25:25 +02:00
parent 8e65ac4c5e
commit e50c98863c
8 changed files with 49 additions and 28 deletions

View File

@ -34,6 +34,8 @@ Leaflet Changelog
* Added `Polyline` `closestLayerPoint` method that's can be useful for interaction features (by [@anru](https://github.com/anru)). [#186](https://github.com/CloudMade/Leaflet/pull/186)
* Added `setLatLngs` method to `MultiPolyline` and `MultiPolygon` (by [@anru](https://github.com/anru)). [#194](https://github.com/CloudMade/Leaflet/pull/194)
* Added `getBounds` method to `Polyline` and `Polygon` (by [@JasonSanford](https://github.com/JasonSanford)). [#253](https://github.com/CloudMade/Leaflet/pull/253)
* Added `FeatureGroup` `setStyle` method (also inherited by `MultiPolyline` and `MultiPolygon`). [#353](https://github.com/CloudMade/Leaflet/issues/353)
* Added `FeatureGroup` `invoke` method to call a particular method on all layers of the group with the given arguments.
* Added `ImageOverlay` `load` event. [#213](https://github.com/CloudMade/Leaflet/issues/213)
* Added `minWidth` option to `Popup` (by [@marphi](https://github.com/marphi)). [#214](https://github.com/CloudMade/Leaflet/pull/214)
* Improved `LatLng` constructor to be more tolerant (and throw descriptive error if latitude or longitude can't be interpreted as a number). [#136](https://github.com/CloudMade/Leaflet/issues/136)

View File

@ -44,6 +44,9 @@ var geojsonSample = {
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[[100.0, 1.5], [100.5, 1.5], [100.5, 2.0], [100.0, 2.0], [100.0, 1.5]]], [[[100.5, 2.0], [100.5, 2.5], [101.0, 2.5], [101.0, 2.0], [100.5, 2.0]]]]
},
"properties": {
"color": "purple"
}
}
]

View File

@ -37,10 +37,10 @@
});
*/
geojson.on('featureparse', function(e) {
// you can style features depending on their properties, etc.
var popupText = 'geometry type: ' + e.geometryType;
if (e.layer.setStyle && e.properties && e.properties.color) {
e.layer.setStyle({color: e.properties.color});
popupText += '<br/>color: ' + e.properties.color;

34
dist/leaflet-src.js vendored
View File

@ -2499,6 +2499,22 @@ L.LayerGroup = L.Class.extend({
return this;
},
invoke: function (methodName) {
var args = Array.prototype.slice.call(arguments, 1),
i, layer;
for (i in this._layers) {
if (this._layers.hasOwnProperty(i)) {
layer = this._layers[i];
if (layer[methodName]) {
layer[methodName].apply(layer, args);
}
}
}
return this;
},
onAdd: function (map) {
this._map = map;
this._iterateLayers(map.addLayer, map);
@ -2538,11 +2554,11 @@ L.FeatureGroup = L.LayerGroup.extend({
bindPopup: function (content) {
this._popupContent = content;
for (var i in this._layers) {
if (this._layers.hasOwnProperty(i) && this._layers[i].bindPopup) {
this._layers[i].bindPopup(content);
}
}
return this.invoke('bindPopup', content);
},
setStyle: function (style) {
return this.invoke('setStyle', style);
},
_events: ['click', 'dblclick', 'mouseover', 'mouseout'],
@ -3396,14 +3412,6 @@ L.Polygon = L.Polyline.extend({
this.setLatLngs(latlngs);
},
setStyle: function (style) {
for (var i in this._layers) {
if (this._layers.hasOwnProperty(i) && this._layers[i].setStyle) {
this._layers[i].setStyle(style);
}
}
},
setLatLngs: function (latlngs) {
var i = 0, len = latlngs.length;

2
dist/leaflet.js vendored

File diff suppressed because one or more lines are too long

View File

@ -17,11 +17,11 @@ L.FeatureGroup = L.LayerGroup.extend({
bindPopup: function (content) {
this._popupContent = content;
for (var i in this._layers) {
if (this._layers.hasOwnProperty(i) && this._layers[i].bindPopup) {
this._layers[i].bindPopup(content);
}
}
return this.invoke('bindPopup', content);
},
setStyle: function (style) {
return this.invoke('setStyle', style);
},
_events: ['click', 'dblclick', 'mouseover', 'mouseout'],

View File

@ -38,6 +38,22 @@ L.LayerGroup = L.Class.extend({
return this;
},
invoke: function (methodName) {
var args = Array.prototype.slice.call(arguments, 1),
i, layer;
for (i in this._layers) {
if (this._layers.hasOwnProperty(i)) {
layer = this._layers[i];
if (layer[methodName]) {
layer[methodName].apply(layer, args);
}
}
}
return this;
},
onAdd: function (map) {
this._map = map;
this._iterateLayers(map.addLayer, map);

View File

@ -11,14 +11,6 @@
this.setLatLngs(latlngs);
},
setStyle: function (style) {
for (var i in this._layers) {
if (this._layers.hasOwnProperty(i) && this._layers[i].setStyle) {
this._layers[i].setStyle(style);
}
}
},
setLatLngs: function (latlngs) {
var i = 0, len = latlngs.length;