2011-06-10 17:38:24 +08:00
|
|
|
/*
|
2012-12-20 18:37:40 +08:00
|
|
|
* L.FeatureGroup extends L.LayerGroup by introducing mouse events and additional methods
|
|
|
|
* shared between a group of interactive layers (like vectors or markers).
|
2011-06-10 17:38:24 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
L.FeatureGroup = L.LayerGroup.extend({
|
|
|
|
includes: L.Mixin.Events,
|
|
|
|
|
2012-11-08 00:46:59 +08:00
|
|
|
statics: {
|
2013-06-24 23:49:10 +08:00
|
|
|
EVENTS: 'click dblclick mouseover mouseout mousemove contextmenu popupclose'
|
2012-11-08 00:46:59 +08:00
|
|
|
},
|
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
addLayer: function (layer) {
|
2013-04-11 00:28:27 +08:00
|
|
|
if (this.hasLayer(layer)) {
|
2012-07-12 13:01:19 +08:00
|
|
|
return this;
|
|
|
|
}
|
2012-07-26 19:56:21 +08:00
|
|
|
|
2012-11-08 00:46:59 +08:00
|
|
|
layer.on(L.FeatureGroup.EVENTS, this._propagateEvent, this);
|
2012-02-15 19:17:25 +08:00
|
|
|
|
2011-06-10 17:38:24 +08:00
|
|
|
L.LayerGroup.prototype.addLayer.call(this, layer);
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2011-06-10 21:24:49 +08:00
|
|
|
if (this._popupContent && layer.bindPopup) {
|
2013-01-14 06:23:04 +08:00
|
|
|
layer.bindPopup(this._popupContent, this._popupOptions);
|
2011-12-09 22:35:15 +08:00
|
|
|
}
|
2012-07-05 19:45:06 +08:00
|
|
|
|
2012-11-08 01:21:21 +08:00
|
|
|
return this.fire('layeradd', {layer: layer});
|
2011-06-10 17:38:24 +08:00
|
|
|
},
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2012-07-03 07:49:09 +08:00
|
|
|
removeLayer: function (layer) {
|
2013-05-05 05:24:02 +08:00
|
|
|
if (layer in this._layers) {
|
|
|
|
layer = this._layers[layer];
|
|
|
|
}
|
|
|
|
|
2012-11-08 00:46:59 +08:00
|
|
|
layer.off(L.FeatureGroup.EVENTS, this._propagateEvent, this);
|
2012-07-03 07:49:09 +08:00
|
|
|
|
|
|
|
L.LayerGroup.prototype.removeLayer.call(this, layer);
|
|
|
|
|
2012-08-01 09:40:43 +08:00
|
|
|
if (this._popupContent) {
|
2012-11-08 01:21:21 +08:00
|
|
|
this.invoke('unbindPopup');
|
2012-08-01 09:40:43 +08:00
|
|
|
}
|
2012-11-08 01:21:21 +08:00
|
|
|
|
|
|
|
return this.fire('layerremove', {layer: layer});
|
2012-07-03 07:49:09 +08:00
|
|
|
},
|
|
|
|
|
2013-01-14 06:23:04 +08:00
|
|
|
bindPopup: function (content, options) {
|
2011-06-10 21:24:49 +08:00
|
|
|
this._popupContent = content;
|
2013-01-14 06:23:04 +08:00
|
|
|
this._popupOptions = options;
|
|
|
|
return this.invoke('bindPopup', content, options);
|
2011-12-15 19:25:25 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
setStyle: function (style) {
|
|
|
|
return this.invoke('setStyle', style);
|
2011-06-10 17:38:24 +08:00
|
|
|
},
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2012-08-03 20:09:02 +08:00
|
|
|
bringToFront: function () {
|
|
|
|
return this.invoke('bringToFront');
|
|
|
|
},
|
|
|
|
|
|
|
|
bringToBack: function () {
|
|
|
|
return this.invoke('bringToBack');
|
|
|
|
},
|
|
|
|
|
2012-03-04 13:54:50 +08:00
|
|
|
getBounds: function () {
|
|
|
|
var bounds = new L.LatLngBounds();
|
2012-11-08 00:46:59 +08:00
|
|
|
|
2012-07-26 19:56:21 +08:00
|
|
|
this.eachLayer(function (layer) {
|
2012-03-04 13:54:50 +08:00
|
|
|
bounds.extend(layer instanceof L.Marker ? layer.getLatLng() : layer.getBounds());
|
2012-11-08 00:46:59 +08:00
|
|
|
});
|
|
|
|
|
2012-03-04 13:54:50 +08:00
|
|
|
return bounds;
|
|
|
|
},
|
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
_propagateEvent: function (e) {
|
2013-02-08 09:44:28 +08:00
|
|
|
if (!e.layer) {
|
|
|
|
e.layer = e.target;
|
|
|
|
}
|
2011-06-10 17:38:24 +08:00
|
|
|
e.target = this;
|
2012-02-15 19:17:25 +08:00
|
|
|
|
2011-06-10 17:38:24 +08:00
|
|
|
this.fire(e.type, e);
|
|
|
|
}
|
2011-12-09 22:35:15 +08:00
|
|
|
});
|
2012-07-05 19:45:06 +08:00
|
|
|
|
|
|
|
L.featureGroup = function (layers) {
|
|
|
|
return new L.FeatureGroup(layers);
|
2012-07-26 19:56:21 +08:00
|
|
|
};
|