| 1 |
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD |
|---|
| 2 |
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
|---|
| 3 |
* full text of the license. */ |
|---|
| 4 |
|
|---|
| 5 |
/** |
|---|
| 6 |
* @requires OpenLayers/Control.js |
|---|
| 7 |
* |
|---|
| 8 |
* Class: OpenLayers.Control.LoadingPanel |
|---|
| 9 |
* In some applications, it makes sense to alert the user that something is |
|---|
| 10 |
* happening while tiles are loading. This control displays a div across the |
|---|
| 11 |
* map when this is going on. |
|---|
| 12 |
* |
|---|
| 13 |
* Inherits from: |
|---|
| 14 |
* - <OpenLayers.Control> |
|---|
| 15 |
*/ |
|---|
| 16 |
OpenLayers.Control.LoadingPanel = OpenLayers.Class(OpenLayers.Control, { |
|---|
| 17 |
|
|---|
| 18 |
/** |
|---|
| 19 |
* Property: counter |
|---|
| 20 |
* {Integer} A counter for the number of layers loading |
|---|
| 21 |
*/ |
|---|
| 22 |
counter: 0, |
|---|
| 23 |
|
|---|
| 24 |
/** |
|---|
| 25 |
* Property: maximized |
|---|
| 26 |
* {Boolean} A boolean indicating whether or not the control is maximized |
|---|
| 27 |
*/ |
|---|
| 28 |
maximized: false, |
|---|
| 29 |
|
|---|
| 30 |
/** |
|---|
| 31 |
* Property: visible |
|---|
| 32 |
* {Boolean} A boolean indicating whether or not the control is visible |
|---|
| 33 |
*/ |
|---|
| 34 |
visible: true, |
|---|
| 35 |
|
|---|
| 36 |
/** |
|---|
| 37 |
* Constructor: OpenLayers.Control.LoadingPanel |
|---|
| 38 |
* Display a panel across the map that says 'loading'. |
|---|
| 39 |
* |
|---|
| 40 |
* Parameters: |
|---|
| 41 |
* options - {Object} additional options. |
|---|
| 42 |
*/ |
|---|
| 43 |
initialize: function(options) { |
|---|
| 44 |
OpenLayers.Control.prototype.initialize.apply(this, [options]); |
|---|
| 45 |
}, |
|---|
| 46 |
|
|---|
| 47 |
/** |
|---|
| 48 |
* Function: setVisible |
|---|
| 49 |
* Set the visibility of this control |
|---|
| 50 |
* |
|---|
| 51 |
* Parameters: |
|---|
| 52 |
* visible - {Boolean} should the control be visible or not? |
|---|
| 53 |
*/ |
|---|
| 54 |
setVisible: function(visible) { |
|---|
| 55 |
this.visible = visible; |
|---|
| 56 |
if (visible) { |
|---|
| 57 |
OpenLayers.Element.show(this.div); |
|---|
| 58 |
} else { |
|---|
| 59 |
OpenLayers.Element.hide(this.div); |
|---|
| 60 |
} |
|---|
| 61 |
}, |
|---|
| 62 |
|
|---|
| 63 |
/** |
|---|
| 64 |
* Function: getVisible |
|---|
| 65 |
* Get the visibility of this control |
|---|
| 66 |
* |
|---|
| 67 |
* Returns: |
|---|
| 68 |
* {Boolean} the current visibility of this control |
|---|
| 69 |
*/ |
|---|
| 70 |
getVisible: function() { |
|---|
| 71 |
return this.visible; |
|---|
| 72 |
}, |
|---|
| 73 |
|
|---|
| 74 |
/** |
|---|
| 75 |
* APIMethod: hide |
|---|
| 76 |
* Hide the loading panel control |
|---|
| 77 |
*/ |
|---|
| 78 |
hide: function() { |
|---|
| 79 |
this.setVisible(false); |
|---|
| 80 |
}, |
|---|
| 81 |
|
|---|
| 82 |
/** |
|---|
| 83 |
* APIMethod: show |
|---|
| 84 |
* Show the loading panel control |
|---|
| 85 |
*/ |
|---|
| 86 |
show: function() { |
|---|
| 87 |
this.setVisible(true); |
|---|
| 88 |
}, |
|---|
| 89 |
|
|---|
| 90 |
/** |
|---|
| 91 |
* APIMethod: toggle |
|---|
| 92 |
* Toggle the visibility of the loading panel control |
|---|
| 93 |
*/ |
|---|
| 94 |
toggle: function() { |
|---|
| 95 |
this.setVisible(!this.getVisible()); |
|---|
| 96 |
}, |
|---|
| 97 |
|
|---|
| 98 |
/** |
|---|
| 99 |
* Method: addLayer |
|---|
| 100 |
* Attach event handlers when new layer gets added to the map |
|---|
| 101 |
* |
|---|
| 102 |
* Parameters: |
|---|
| 103 |
* evt - {Event} |
|---|
| 104 |
*/ |
|---|
| 105 |
addLayer: function(evt) { |
|---|
| 106 |
if (evt.layer) { |
|---|
| 107 |
evt.layer.events.register('loadstart', this, this.increaseCounter); |
|---|
| 108 |
evt.layer.events.register('loadend', this, this.decreaseCounter); |
|---|
| 109 |
} |
|---|
| 110 |
}, |
|---|
| 111 |
|
|---|
| 112 |
/** |
|---|
| 113 |
* Method: setMap |
|---|
| 114 |
* Set the map property for the control and all handlers. |
|---|
| 115 |
* |
|---|
| 116 |
* Parameters: |
|---|
| 117 |
* map - {<OpenLayers.Map>} The control's map. |
|---|
| 118 |
*/ |
|---|
| 119 |
setMap: function(map) { |
|---|
| 120 |
OpenLayers.Control.prototype.setMap.apply(this, arguments); |
|---|
| 121 |
this.map.events.register('preaddlayer', this, this.addLayer); |
|---|
| 122 |
for (var i = 0; i < this.map.layers.length; i++) { |
|---|
| 123 |
var layer = this.map.layers[i]; |
|---|
| 124 |
layer.events.register('loadstart', this, this.increaseCounter); |
|---|
| 125 |
layer.events.register('loadend', this, this.decreaseCounter); |
|---|
| 126 |
} |
|---|
| 127 |
}, |
|---|
| 128 |
|
|---|
| 129 |
/** |
|---|
| 130 |
* Method: increaseCounter |
|---|
| 131 |
* Increase the counter and show control |
|---|
| 132 |
*/ |
|---|
| 133 |
increaseCounter: function() { |
|---|
| 134 |
this.counter++; |
|---|
| 135 |
if (this.counter > 0) { |
|---|
| 136 |
if (!this.maximized && this.visible) { |
|---|
| 137 |
this.maximizeControl(); |
|---|
| 138 |
} |
|---|
| 139 |
} |
|---|
| 140 |
}, |
|---|
| 141 |
|
|---|
| 142 |
/** |
|---|
| 143 |
* Method: decreaseCounter |
|---|
| 144 |
* Decrease the counter and hide the control if finished |
|---|
| 145 |
*/ |
|---|
| 146 |
decreaseCounter: function() { |
|---|
| 147 |
if (this.counter > 0) { |
|---|
| 148 |
this.counter--; |
|---|
| 149 |
} |
|---|
| 150 |
if (this.counter == 0) { |
|---|
| 151 |
if (this.maximized && this.visible) { |
|---|
| 152 |
this.minimizeControl(); |
|---|
| 153 |
} |
|---|
| 154 |
} |
|---|
| 155 |
}, |
|---|
| 156 |
|
|---|
| 157 |
/** |
|---|
| 158 |
* Method: draw |
|---|
| 159 |
* Create and return the element to be splashed over the map. |
|---|
| 160 |
*/ |
|---|
| 161 |
draw: function () { |
|---|
| 162 |
OpenLayers.Control.prototype.draw.apply(this, arguments); |
|---|
| 163 |
return this.div; |
|---|
| 164 |
}, |
|---|
| 165 |
|
|---|
| 166 |
/** |
|---|
| 167 |
* Method: minimizeControl |
|---|
| 168 |
* Set the display properties of the control to make it disappear. |
|---|
| 169 |
* |
|---|
| 170 |
* Parameters: |
|---|
| 171 |
* evt - {Event} |
|---|
| 172 |
*/ |
|---|
| 173 |
minimizeControl: function(evt) { |
|---|
| 174 |
this.div.style.display = "none"; |
|---|
| 175 |
this.maximized = false; |
|---|
| 176 |
|
|---|
| 177 |
if (evt != null) { |
|---|
| 178 |
OpenLayers.Event.stop(evt); |
|---|
| 179 |
} |
|---|
| 180 |
}, |
|---|
| 181 |
|
|---|
| 182 |
/** |
|---|
| 183 |
* Method: maximizeControl |
|---|
| 184 |
* Make the control visible. |
|---|
| 185 |
* |
|---|
| 186 |
* Parameters: |
|---|
| 187 |
* evt - {Event} |
|---|
| 188 |
*/ |
|---|
| 189 |
maximizeControl: function(evt) { |
|---|
| 190 |
this.div.style.display = "block"; |
|---|
| 191 |
var viewSize = this.map.getSize(); |
|---|
| 192 |
var msgW = viewSize.w; |
|---|
| 193 |
var msgH = viewSize.h; |
|---|
| 194 |
this.div.style.left = msgW/2-this.div.offsetWidth/2 + "px"; |
|---|
| 195 |
this.div.style.top = msgH/2-this.div.offsetHeight/2 + "px"; |
|---|
| 196 |
this.maximized = true; |
|---|
| 197 |
|
|---|
| 198 |
if (evt != null) { |
|---|
| 199 |
OpenLayers.Event.stop(evt); |
|---|
| 200 |
} |
|---|
| 201 |
}, |
|---|
| 202 |
|
|---|
| 203 |
/** |
|---|
| 204 |
* Method: destroy |
|---|
| 205 |
* Destroy control. |
|---|
| 206 |
*/ |
|---|
| 207 |
destroy: function() { |
|---|
| 208 |
if (this.map) { |
|---|
| 209 |
this.map.events.unregister('preaddlayer', this, this.addLayer); |
|---|
| 210 |
if (this.map.layers) { |
|---|
| 211 |
for (var i = 0; i < this.map.layers.length; i++) { |
|---|
| 212 |
var layer = this.map.layers[i]; |
|---|
| 213 |
layer.events.unregister('loadstart', this, |
|---|
| 214 |
this.increaseCounter); |
|---|
| 215 |
layer.events.unregister('loadend', this, |
|---|
| 216 |
this.decreaseCounter); |
|---|
| 217 |
} |
|---|
| 218 |
} |
|---|
| 219 |
} |
|---|
| 220 |
OpenLayers.Control.prototype.destroy.apply(this, arguments); |
|---|
| 221 |
}, |
|---|
| 222 |
|
|---|
| 223 |
CLASS_NAME: "OpenLayers.Control.LoadingPanel" |
|---|
| 224 |
|
|---|
| 225 |
}); |
|---|