UNPKG

4.11 kBJavaScriptView Raw
1
2import {Layer} from './Layer';
3import * as Util from '../core/Util';
4
5/*
6 * @class LayerGroup
7 * @aka L.LayerGroup
8 * @inherits Interactive 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 var layerId = typeof layer === 'number' ? layer : this.getLayerId(layer);
77 return layerId in this._layers;
78 },
79
80 // @method clearLayers(): this
81 // Removes all the layers from the group.
82 clearLayers: function () {
83 return this.eachLayer(this.removeLayer, this);
84 },
85
86 // @method invoke(methodName: String, …): this
87 // Calls `methodName` on every layer contained in this group, passing any
88 // additional parameters. Has no effect if the layers contained do not
89 // implement `methodName`.
90 invoke: function (methodName) {
91 var args = Array.prototype.slice.call(arguments, 1),
92 i, layer;
93
94 for (i in this._layers) {
95 layer = this._layers[i];
96
97 if (layer[methodName]) {
98 layer[methodName].apply(layer, args);
99 }
100 }
101
102 return this;
103 },
104
105 onAdd: function (map) {
106 this.eachLayer(map.addLayer, map);
107 },
108
109 onRemove: function (map) {
110 this.eachLayer(map.removeLayer, map);
111 },
112
113 // @method eachLayer(fn: Function, context?: Object): this
114 // Iterates over the layers of the group, optionally specifying context of the iterator function.
115 // ```js
116 // group.eachLayer(function (layer) {
117 // layer.bindPopup('Hello');
118 // });
119 // ```
120 eachLayer: function (method, context) {
121 for (var i in this._layers) {
122 method.call(context, this._layers[i]);
123 }
124 return this;
125 },
126
127 // @method getLayer(id: Number): Layer
128 // Returns the layer with the given internal ID.
129 getLayer: function (id) {
130 return this._layers[id];
131 },
132
133 // @method getLayers(): Layer[]
134 // Returns an array of all the layers added to the group.
135 getLayers: function () {
136 var layers = [];
137 this.eachLayer(layers.push, layers);
138 return layers;
139 },
140
141 // @method setZIndex(zIndex: Number): this
142 // Calls `setZIndex` on every layer contained in this group, passing the z-index.
143 setZIndex: function (zIndex) {
144 return this.invoke('setZIndex', zIndex);
145 },
146
147 // @method getLayerId(layer: Layer): Number
148 // Returns the internal ID for a layer
149 getLayerId: function (layer) {
150 return Util.stamp(layer);
151 }
152});
153
154
155// @factory L.layerGroup(layers?: Layer[], options?: Object)
156// Create a layer group, optionally given an initial set of layers and an `options` object.
157export var layerGroup = function (layers, options) {
158 return new LayerGroup(layers, options);
159};