| | 1 | /* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. |
|---|
| | 2 | * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt |
|---|
| | 3 | * for the 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 happening |
|---|
| | 10 | * while tiles are loading. This control displays a div across the map when this is |
|---|
| | 11 | * going on. |
|---|
| | 12 | * |
|---|
| | 13 | * Inherits from: |
|---|
| | 14 | * - <OpenLayers.Control> |
|---|
| | 15 | */ |
|---|
| | 16 | OpenLayers.Control.LoadingPanel = OpenLayers.Class.create(); |
|---|
| | 17 | OpenLayers.Control.LoadingPanel.prototype = |
|---|
| | 18 | OpenLayers.Class.inherit( OpenLayers.Control, { |
|---|
| | 19 | |
|---|
| | 20 | /** |
|---|
| | 21 | * Property: counter |
|---|
| | 22 | * {Integer} A counter for the number of layers loading. |
|---|
| | 23 | */ |
|---|
| | 24 | counter: 0, |
|---|
| | 25 | |
|---|
| | 26 | /** |
|---|
| | 27 | * Property: maximized |
|---|
| | 28 | * {Boolean} A boolean indicating whether or not the control is maximized |
|---|
| | 29 | */ |
|---|
| | 30 | maximized: false, |
|---|
| | 31 | |
|---|
| | 32 | /** |
|---|
| | 33 | * Property: layer |
|---|
| | 34 | * {OpenLayers.Layer} |
|---|
| | 35 | */ |
|---|
| | 36 | layer: null, |
|---|
| | 37 | |
|---|
| | 38 | /** |
|---|
| | 39 | * Constructor: OpenLayers.Control.LoadingPanel |
|---|
| | 40 | * Display a panel across the map that says 'loading'. |
|---|
| | 41 | * |
|---|
| | 42 | * Parameters: |
|---|
| | 43 | * layer - {<OpenLayers.Layer.Grid>} Some layer which implements the loadstart and loadend |
|---|
| | 44 | * properties. At the moment, this is only subclasses of the Grid layer. |
|---|
| | 45 | * options - {Object} additional options. |
|---|
| | 46 | */ |
|---|
| | 47 | initialize: function(layer, options) { |
|---|
| | 48 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
|---|
| | 49 | |
|---|
| | 50 | if (!layer) { |
|---|
| | 51 | this.map.events.register('addlayer', this, this.addLayer); |
|---|
| | 52 | for (var i = 0; i < this.map.layers.length; i++) { |
|---|
| | 53 | var layer = this.map.layers[i]; |
|---|
| | 54 | layer.events.register('loadstart', this, this.increaseCounter); |
|---|
| | 55 | layer.events.register('loadend', this, this.decreaseCounter); |
|---|
| | 56 | } |
|---|
| | 57 | } else { |
|---|
| | 58 | this.layer = layer; |
|---|
| | 59 | layer.events.register('loadstart', this, this.maximizeControl); |
|---|
| | 60 | layer.events.register('loadend', this, this.minimizeControl); |
|---|
| | 61 | } |
|---|
| | 62 | }, |
|---|
| | 63 | |
|---|
| | 64 | /** |
|---|
| | 65 | * Method: addLayer |
|---|
| | 66 | * Attach event handlers when new layer gets added to the map |
|---|
| | 67 | */ |
|---|
| | 68 | addLayer: function(evt) { |
|---|
| | 69 | var layer = this.map.layers[this.map.layers.length-1]; |
|---|
| | 70 | layer.events.register('loadstart', this, this.increaseCounter); |
|---|
| | 71 | layer.events.register('loadend', this, this.decreaseCounter); |
|---|
| | 72 | }, |
|---|
| | 73 | |
|---|
| | 74 | /** |
|---|
| | 75 | * Method: increaseCounter |
|---|
| | 76 | * Increase the counter and show control |
|---|
| | 77 | */ |
|---|
| | 78 | increaseCounter: function() { |
|---|
| | 79 | this.counter++; |
|---|
| | 80 | if (this.counter > 0) { |
|---|
| | 81 | if (!this.maximized) { |
|---|
| | 82 | this.maximizeControl(); |
|---|
| | 83 | } |
|---|
| | 84 | } |
|---|
| | 85 | }, |
|---|
| | 86 | |
|---|
| | 87 | /** |
|---|
| | 88 | * Method: decreaseCounter |
|---|
| | 89 | * Decrease the counter and hide the control if finished |
|---|
| | 90 | */ |
|---|
| | 91 | decreaseCounter: function() { |
|---|
| | 92 | if (this.counter > 0) { |
|---|
| | 93 | this.counter--; |
|---|
| | 94 | } |
|---|
| | 95 | if (this.counter == 0) { |
|---|
| | 96 | if (this.maximized) { |
|---|
| | 97 | this.minimizeControl(); |
|---|
| | 98 | } |
|---|
| | 99 | } |
|---|
| | 100 | }, |
|---|
| | 101 | |
|---|
| | 102 | /** |
|---|
| | 103 | * Method: draw |
|---|
| | 104 | * Create and return the element to be splashed over the map. |
|---|
| | 105 | */ |
|---|
| | 106 | draw: function () { |
|---|
| | 107 | if (this.div == null) { |
|---|
| | 108 | var viewSize = this.map.getSize(); |
|---|
| | 109 | |
|---|
| | 110 | msgW = Math.max(viewSize.w, 300); |
|---|
| | 111 | msgH = Math.max(viewSize.h, 200); |
|---|
| | 112 | |
|---|
| | 113 | var centerPx = new OpenLayers.Pixel(viewSize.w/2, viewSize.h/2); |
|---|
| | 114 | var size = new OpenLayers.Size(msgW, msgH); |
|---|
| | 115 | var centerPx2 = centerPx.add(-msgW/2, -msgH/2); |
|---|
| | 116 | this.div = OpenLayers.Util.createDiv('OpenLayers_Control_LoadingPanel' + "_warning", |
|---|
| | 117 | centerPx2, |
|---|
| | 118 | size, |
|---|
| | 119 | null, |
|---|
| | 120 | null, |
|---|
| | 121 | null, |
|---|
| | 122 | "auto", |
|---|
| | 123 | .75); |
|---|
| | 124 | this.div.className = 'loading'; |
|---|
| | 125 | } |
|---|
| | 126 | |
|---|
| | 127 | return this.div; |
|---|
| | 128 | }, |
|---|
| | 129 | |
|---|
| | 130 | /** |
|---|
| | 131 | * Method: minimizeControl |
|---|
| | 132 | * Set the display properties of the control to make it disappear. |
|---|
| | 133 | */ |
|---|
| | 134 | minimizeControl: function(e) { |
|---|
| | 135 | this.div.style.width = "0px"; |
|---|
| | 136 | this.div.style.height = "0px"; |
|---|
| | 137 | this.maximized = false; |
|---|
| | 138 | |
|---|
| | 139 | if (e != null) { |
|---|
| | 140 | OpenLayers.Event.stop(e); |
|---|
| | 141 | } |
|---|
| | 142 | }, |
|---|
| | 143 | |
|---|
| | 144 | /** |
|---|
| | 145 | * Method: maximizeControl |
|---|
| | 146 | * Make the control visible. |
|---|
| | 147 | */ |
|---|
| | 148 | maximizeControl: function(e) { |
|---|
| | 149 | var viewSize = this.map.getSize(); |
|---|
| | 150 | |
|---|
| | 151 | msgW = Math.max(viewSize.w, 300); |
|---|
| | 152 | msgH = Math.max(viewSize.h, 200); |
|---|
| | 153 | |
|---|
| | 154 | this.div.style.width = msgW + "px"; |
|---|
| | 155 | this.div.style.height = msgH + "px"; |
|---|
| | 156 | this.maximized = true; |
|---|
| | 157 | |
|---|
| | 158 | if (e != null) { |
|---|
| | 159 | OpenLayers.Event.stop(e); |
|---|
| | 160 | } |
|---|
| | 161 | }, |
|---|
| | 162 | |
|---|
| | 163 | /** |
|---|
| | 164 | * Method: destroy |
|---|
| | 165 | * Destroy control. |
|---|
| | 166 | */ |
|---|
| | 167 | destroy: function() { |
|---|
| | 168 | if (this.layer) { |
|---|
| | 169 | this.layer.events.unregister('loadstart', this, this.maximizeControl); |
|---|
| | 170 | this.layer.events.unregister('loadend', this, this.minimizeControl); |
|---|
| | 171 | } else { |
|---|
| | 172 | if (this.map) { |
|---|
| | 173 | this.map.events.unregister('addlayer', this, this.addLayer); |
|---|
| | 174 | if (this.map.layers) { |
|---|
| | 175 | for (var i = 0; i < this.map.layers.length; i++) { |
|---|
| | 176 | var layer = this.map.layers[i]; |
|---|
| | 177 | layer.events.unregister('loadstart', this, this.increaseCounter); |
|---|
| | 178 | layer.events.unregister('loadend', this, this.decreaseCounter); |
|---|
| | 179 | } |
|---|
| | 180 | } |
|---|
| | 181 | } |
|---|
| | 182 | } |
|---|
| | 183 | OpenLayers.Control.prototype.destroy.apply(this, arguments); |
|---|
| | 184 | }, |
|---|
| | 185 | |
|---|
| | 186 | CLASS_NAME: "OpenLayers.Control.LoadingPanel" |
|---|
| | 187 | |
|---|
| | 188 | }); |