-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFeatureGroup.LoadEvents.js
121 lines (95 loc) · 2.46 KB
/
FeatureGroup.LoadEvents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(function () {
// Public API
var FG = L.FeatureGroup
, FGp = FG.prototype;
FG.LoadEvents = FG.extend({
includes : L.Mixin.Events,
addLayer : FGLE_addLayer,
removeLayer : FGLE_removeLayer
});
FG.loadEvents = function (layerarr) {
return new FG.LoadEvents(layerarr);
}
// Public API implementation
function FGLE_addLayer(layer)
{
_FGLE_ensure_init(this);
var id = this.getLayerId(layer);
if (!(id in this._id2ctx))
{
delete this._id2ldg[id];
var ctx = this._id2ctx[id] = {
lg : this,
id : id
};
layer.on({
loading : _FGLE_on_loading,
load : _FGLE_on_load
}, ctx);
}
return FGp.addLayer.apply(this, arguments);
}
function FGLE_removeLayer(layer)
{
_FGLE_ensure_init(this);
var id = this.getLayerId(layer);
if (id in this._id2ctx)
{
var ctx = this._id2ctx[id];
layer.off({
loading : _FGLE_on_loading,
load : _FGLE_on_load
}, ctx);
delete this._id2ctx[id];
delete this._id2ldg[id];
}
return FGp.removeLayer.apply(this.arguments);
}
// Private details
function _FGLE_ensure_init(that)
{
if (!that._id2ctx)
{
that._id2ctx = {};
that._id2ldg = {};
}
}
function _FGLE_on_loading()
{
var ctx = this,
lg = this.lg,
id = this.id,
id2ldg = lg._id2ldg;
if (!(id in id2ldg))
{
var is_starting = !_FGLE_is_loading_some(id2ldg);
// Update
id2ldg[id] = true;
// Notify
if (is_starting)
lg.fire('loading');
}
}
function _FGLE_on_load()
{
var ctx = this,
lg = this.lg,
id = this.id,
id2ldg = lg._id2ldg;
if (id in id2ldg)
{
// Update
delete id2ldg[id];
// Notify
if (!_FGLE_is_loading_some(id2ldg))
lg.fire('load');
}
}
function _FGLE_is_loading_some(id2ldg)
{
for (var id in id2ldg)
if (id2ldg.hasOwnProperty(id))
return true;
return false;
}
})();