UNPKG

4.08 kBJavaScriptView Raw
1
2import {Layer} from './Layer';
3import * as Util from '../core/Util';
4
5/*
6 * @class LayerGroup
7 * @aka L.LayerGroup
8 * @inherits Layer
9 *
10 * Used to group several layers and handle them as one. If you add it to the map,
11 * any layers added or removed from the group will be added/removed on the map as
12 * well. Extends `Layer`.
13 *
14 * @example
15 *
16 * ```js
17 * L.layerGroup([marker1, marker2])
18 * .addLayer(polyline)
19 * .addTo(map);
20 * ```
21 */
22
23export var LayerGroup = Layer.extend({
24
25 initialize: function (layers, options) {
26 Util.setOptions(this, options);
27
28 this._layers = {};
29
30 var i, len;
31
32 if (layers) {
33 for (i = 0, len = layers.length; i < len; i++) {
34 this.addLayer(layers[i]);
35 }
36 }
37 },
38
39 // @method addLayer(layer: Layer): this
40 // Adds the given layer to the group.
41 addLayer: function (layer) {
42 var id = this.getLayerId(layer);
43
44 this._layers[id] = layer;
45
46 if (this._map) {
47 this._map.addLayer(layer);
48 }
49
50 return this;
51 },
52
53 // @method removeLayer(layer: Layer): this
54 // Removes the given layer from the group.
55 // @alternative
56 // @method removeLayer(id: Number): this
57 // Removes the layer with the given internal ID from the group.
58 removeLayer: function (layer) {
59 var id = layer in this._layers ? layer : this.getLayerId(layer);
60
61 if (this._map && this._layers[id]) {
62 this._map.removeLayer(this._layers[id]);
63 }
64
65 delete this._layers[id];
66
67 return this;
68 },
69
70 // @method hasLayer(layer: Layer): Boolean
71 // Returns `true` if the given layer is currently added to the group.
72 // @alternative
73 // @method hasLayer(id: Number): Boolean
74 // Returns `true` if the given internal ID is currently added to the group.
75 hasLayer: function (layer) {
76 return !!layer && (layer in this._layers || this.getLayerId(layer) in this._layers);
77 },
78
79 // @method clearLayers(): this
80 // Removes all the layers from the group.
81 clearLayers: function () {
82 return this.eachLayer(this.removeLayer, this);
83 },
84
85 // @method invoke(methodName: String, …): this
86 // Calls `methodName` on every layer contained in this group, passing any
87 // additional parameters. Has no effect if the layers contained do not
88 // implement `methodName`.
89 invoke: function (methodName) {
90 var args = Array.prototype.slice.call(arguments, 1),
91 i, layer;
92
93 for (i in this._layers) {
94 layer = this._layers[i];
95
96 if (layer[methodName]) {
97 layer[methodName].apply(layer, args);
98 }
99 }
100
101 return this;
102 },
103
104 onAdd: function (map) {
105 this.eachLayer(map.addLayer, map);
106 },
107
108 onRemove: function (map) {
109 this.eachLayer(map.removeLayer, map);
110 },
111
112 // @method eachLayer(fn: Function, context?: Object): this
113 // Iterates over the layers of the group, optionally specifying context of the iterator function.
114 // ```js
115 // group.eachLayer(function (layer) {
116 // layer.bindPopup('Hello');
117 // });
118 // ```
119 eachLayer: function (method, context) {
120 for (var i in this._layers) {
121 method.call(context, this._layers[i]);
122 }
123 return this;
124 },
125
126 // @method getLayer(id: Number): Layer
127 // Returns the layer with the given internal ID.
128 getLayer: function (id) {
129 return this._layers[id];
130 },
131
132 // @method getLayers(): Layer[]
133 // Returns an array of all the layers added to the group.
134 getLayers: function () {
135 var layers = [];
136 this.eachLayer(layers.push, layers);
137 return layers;
138 },
139
140 // @method setZIndex(zIndex: Number): this
141 // Calls `setZIndex` on every layer contained in this group, passing the z-index.
142 setZIndex: function (zIndex) {
143 return this.invoke('setZIndex', zIndex);
144 },
145
146 // @method getLayerId(layer: Layer): Number
147 // Returns the internal ID for a layer
148 getLayerId: function (layer) {
149 return Util.stamp(layer);
150 }
151});
152
153
154// @factory L.layerGroup(layers?: Layer[], options?: Object)
155// Create a layer group, optionally given an initial set of layers and an `options` object.
156export var layerGroup = function (layers, options) {
157 return new LayerGroup(layers, options);
158};