/
LoadingPanel.js
220 lines (198 loc) · 5.59 KB
/
LoadingPanel.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Control.js
*
* Class: OpenLayers.Control.LoadingPanel
* In some applications, it makes sense to alert the user that something is
* happening while tiles are loading. This control displays a div across the
* map when this is going on.
*
* Inherits from:
* - <OpenLayers.Control>
*/
OpenLayers.Control.LoadingPanel = OpenLayers.Class(OpenLayers.Control, {
/**
* Property: counter
* {Integer} A counter for the number of layers loading
*/
counter: 0,
/**
* Property: maximized
* {Boolean} A boolean indicating whether or not the control is maximized
*/
maximized: false,
/**
* Property: visible
* {Boolean} A boolean indicating whether or not the control is visible
*/
visible: true,
/**
* Constructor: OpenLayers.Control.LoadingPanel
* Display a panel across the map that says 'loading'.
*
* Parameters:
* options - {Object} additional options.
*/
initialize: function(options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
},
/**
* Function: setVisible
* Set the visibility of this control
*
* Parameters:
* visible - {Boolean} should the control be visible or not?
*/
setVisible: function(visible) {
this.visible = visible;
if (visible) {
OpenLayers.Element.show(this.div);
} else {
OpenLayers.Element.hide(this.div);
}
},
/**
* Function: getVisible
* Get the visibility of this control
*
* Returns:
* {Boolean} the current visibility of this control
*/
getVisible: function() {
return this.visible;
},
/**
* APIMethod: hide
* Hide the loading panel control
*/
hide: function() {
this.setVisible(false);
},
/**
* APIMethod: show
* Show the loading panel control
*/
show: function() {
this.setVisible(true);
},
/**
* APIMethod: toggle
* Toggle the visibility of the loading panel control
*/
toggle: function() {
this.setVisible(!this.getVisible());
},
/**
* Method: addLayer
* Attach event handlers when new layer gets added to the map
*
* Parameters:
* evt - {Event}
*/
addLayer: function(evt) {
if (evt.layer) {
evt.layer.events.register('loadstart', this, this.increaseCounter);
evt.layer.events.register('loadend', this, this.decreaseCounter);
}
},
/**
* Method: setMap
* Set the map property for the control and all handlers.
*
* Parameters:
* map - {<OpenLayers.Map>} The control's map.
*/
setMap: function(map) {
OpenLayers.Control.prototype.setMap.apply(this, arguments);
this.map.events.register('preaddlayer', this, this.addLayer);
for (var i = 0; i < this.map.layers.length; i++) {
var layer = this.map.layers[i];
layer.events.register('loadstart', this, this.increaseCounter);
layer.events.register('loadend', this, this.decreaseCounter);
}
},
/**
* Method: increaseCounter
* Increase the counter and show control
*/
increaseCounter: function() {
this.counter++;
if (this.counter > 0) {
if (!this.maximized && this.visible) {
this.maximizeControl();
}
}
},
/**
* Method: decreaseCounter
* Decrease the counter and hide the control if finished
*/
decreaseCounter: function() {
if (this.counter > 0) {
this.counter--;
}
if (this.counter == 0) {
if (this.maximized && this.visible) {
this.minimizeControl();
}
}
},
/**
* Method: draw
* Create and return the element to be splashed over the map.
*/
draw: function () {
OpenLayers.Control.prototype.draw.apply(this, arguments);
return this.div;
},
/**
* Method: minimizeControl
* Set the display properties of the control to make it disappear.
*
* Parameters:
* evt - {Event}
*/
minimizeControl: function(evt) {
this.div.style.display = "none";
this.maximized = false;
if (evt != null) {
OpenLayers.Event.stop(evt);
}
},
/**
* Method: maximizeControl
* Make the control visible.
*
* Parameters:
* evt - {Event}
*/
maximizeControl: function(evt) {
this.div.style.display = "block";
this.maximized = true;
if (evt != null) {
OpenLayers.Event.stop(evt);
}
},
/**
* Method: destroy
* Destroy control.
*/
destroy: function() {
if (this.map) {
this.map.events.unregister('preaddlayer', this, this.addLayer);
if (this.map.layers) {
for (var i = 0; i < this.map.layers.length; i++) {
var layer = this.map.layers[i];
layer.events.unregister('loadstart', this,
this.increaseCounter);
layer.events.unregister('loadend', this,
this.decreaseCounter);
}
}
}
OpenLayers.Control.prototype.destroy.apply(this, arguments);
},
CLASS_NAME: "OpenLayers.Control.LoadingPanel"
});