[76] | 1 | /** |
---|
| 2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
| 3 | * |
---|
| 4 | * Published under the BSD license. |
---|
| 5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
| 6 | * of the license. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | Ext.namespace("GeoExt.tree"); |
---|
| 10 | |
---|
| 11 | /** private: constructor |
---|
| 12 | * .. class:: LayerNodeUI |
---|
| 13 | * |
---|
| 14 | * Place in a separate file if this should be documented. |
---|
| 15 | */ |
---|
| 16 | GeoExt.tree.LayerNodeUI = Ext.extend(Ext.tree.TreeNodeUI, { |
---|
| 17 | |
---|
| 18 | /** private: method[constructor] |
---|
| 19 | */ |
---|
| 20 | constructor: function(config) { |
---|
| 21 | GeoExt.tree.LayerNodeUI.superclass.constructor.apply(this, arguments); |
---|
| 22 | }, |
---|
| 23 | |
---|
| 24 | /** private: method[render] |
---|
| 25 | * :param bulkRender: ``Boolean`` |
---|
| 26 | */ |
---|
| 27 | render: function(bulkRender) { |
---|
| 28 | var a = this.node.attributes; |
---|
| 29 | if (a.checked === undefined) { |
---|
| 30 | a.checked = this.node.layer.getVisibility(); |
---|
| 31 | } |
---|
| 32 | GeoExt.tree.LayerNodeUI.superclass.render.apply(this, arguments); |
---|
| 33 | var cb = this.checkbox; |
---|
| 34 | if(a.checkedGroup) { |
---|
| 35 | // replace the checkbox with a radio button |
---|
| 36 | var radio = Ext.DomHelper.insertAfter(cb, |
---|
| 37 | ['<input type="radio" name="', a.checkedGroup, |
---|
| 38 | '_checkbox" class="', cb.className, |
---|
| 39 | cb.checked ? '" checked="checked"' : '', |
---|
| 40 | '"></input>'].join("")); |
---|
| 41 | radio.defaultChecked = cb.defaultChecked; |
---|
| 42 | Ext.get(cb).remove(); |
---|
| 43 | this.checkbox = radio; |
---|
| 44 | } |
---|
| 45 | this.enforceOneVisible(); |
---|
| 46 | }, |
---|
| 47 | |
---|
| 48 | /** private: method[onClick] |
---|
| 49 | * :param e: ``Object`` |
---|
| 50 | */ |
---|
| 51 | onClick: function(e) { |
---|
| 52 | if(e.getTarget('.x-tree-node-cb', 1)) { |
---|
| 53 | this.toggleCheck(this.isChecked()); |
---|
| 54 | } else { |
---|
| 55 | GeoExt.tree.LayerNodeUI.superclass.onClick.apply(this, arguments); |
---|
| 56 | } |
---|
| 57 | }, |
---|
| 58 | |
---|
| 59 | /** private: method[toggleCheck] |
---|
| 60 | * :param value: ``Boolean`` |
---|
| 61 | */ |
---|
| 62 | toggleCheck: function(value) { |
---|
| 63 | value = (value === undefined ? !this.isChecked() : value); |
---|
| 64 | GeoExt.tree.LayerNodeUI.superclass.toggleCheck.call(this, value); |
---|
| 65 | |
---|
| 66 | this.enforceOneVisible(); |
---|
| 67 | }, |
---|
| 68 | |
---|
| 69 | /** private: method[enforceOneVisible] |
---|
| 70 | * |
---|
| 71 | * Makes sure that only one layer is visible if checkedGroup is set. |
---|
| 72 | */ |
---|
| 73 | enforceOneVisible: function() { |
---|
| 74 | var attributes = this.node.attributes; |
---|
| 75 | var group = attributes.checkedGroup; |
---|
| 76 | // If we are in the baselayer group, the map will take care of |
---|
| 77 | // enforcing visibility. |
---|
| 78 | if(group && group !== "gx_baselayer") { |
---|
| 79 | var layer = this.node.layer; |
---|
| 80 | var checkedNodes = this.node.getOwnerTree().getChecked(); |
---|
| 81 | var checkedCount = 0; |
---|
| 82 | // enforce "not more than one visible" |
---|
| 83 | Ext.each(checkedNodes, function(n){ |
---|
| 84 | var l = n.layer |
---|
| 85 | if(!n.hidden && n.attributes.checkedGroup === group) { |
---|
| 86 | checkedCount++; |
---|
| 87 | if(l != layer && attributes.checked) { |
---|
| 88 | l.setVisibility(false); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | }); |
---|
| 92 | // enforce "at least one visible" |
---|
| 93 | if(checkedCount === 0 && attributes.checked == false) { |
---|
| 94 | layer.setVisibility(true); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | }, |
---|
| 98 | |
---|
| 99 | /** private: method[appendDDGhost] |
---|
| 100 | * :param ghostNode ``DOMElement`` |
---|
| 101 | * |
---|
| 102 | * For radio buttons, makes sure that we do not use the option group of |
---|
| 103 | * the original, otherwise only the original or the clone can be checked |
---|
| 104 | */ |
---|
| 105 | appendDDGhost : function(ghostNode){ |
---|
| 106 | var n = this.elNode.cloneNode(true); |
---|
| 107 | var radio = Ext.DomQuery.select("input[type='radio']", n); |
---|
| 108 | Ext.each(radio, function(r) { |
---|
| 109 | r.name = r.name + "_clone"; |
---|
| 110 | }); |
---|
| 111 | ghostNode.appendChild(n); |
---|
| 112 | } |
---|
| 113 | }); |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | /** api: (define) |
---|
| 117 | * module = GeoExt.tree |
---|
| 118 | * class = LayerNode |
---|
| 119 | * base_link = `Ext.tree.TreeNode <http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreeNode>`_ |
---|
| 120 | */ |
---|
| 121 | |
---|
| 122 | /** api: constructor |
---|
| 123 | * .. class:: LayerNode(config) |
---|
| 124 | * |
---|
| 125 | * A subclass of ``Ext.tree.TreeNode`` that is connected to an |
---|
| 126 | * ``OpenLayers.Layer`` by setting the node's layer property. Checking or |
---|
| 127 | * unchecking the checkbox of this node will directly affect the layer and |
---|
| 128 | * vice versa. The default iconCls for this node's icon is |
---|
| 129 | * "gx-tree-layer-icon", unless it has children. |
---|
| 130 | * |
---|
| 131 | * Setting the node's layer property to a layer name instead of an object |
---|
| 132 | * will also work. As soon as a layer is found, it will be stored as layer |
---|
| 133 | * property in the attributes hash. |
---|
| 134 | * |
---|
| 135 | * The node's text property defaults to the layer name. |
---|
| 136 | * |
---|
| 137 | * If the node has a checkedGroup attribute configured, it will be |
---|
| 138 | * rendered with a radio button instead of the checkbox. The value of |
---|
| 139 | * the checkedGroup attribute is a string, identifying the options group |
---|
| 140 | * for the node. |
---|
| 141 | * |
---|
| 142 | * To use this node type in a ``TreePanel`` config, set ``nodeType`` to |
---|
| 143 | * "gx_layer". |
---|
| 144 | */ |
---|
| 145 | GeoExt.tree.LayerNode = Ext.extend(Ext.tree.AsyncTreeNode, { |
---|
| 146 | |
---|
| 147 | /** api: config[layer] |
---|
| 148 | * ``OpenLayers.Layer or String`` |
---|
| 149 | * The layer that this layer node will |
---|
| 150 | * be bound to, or the name of the layer (has to match the layer's |
---|
| 151 | * name property). If a layer name is provided, ``layerStore`` also has |
---|
| 152 | * to be provided. |
---|
| 153 | */ |
---|
| 154 | |
---|
| 155 | /** api: property[layer] |
---|
| 156 | * ``OpenLayers.Layer`` |
---|
| 157 | * The layer this node is bound to. |
---|
| 158 | */ |
---|
| 159 | layer: null, |
---|
| 160 | |
---|
| 161 | /** api: config[layerStore] |
---|
| 162 | * :class:`GeoExt.data.LayerStore` ``or "auto"`` |
---|
| 163 | * The layer store containing the layer that this node represents. If set |
---|
| 164 | * to "auto", the node will query the ComponentManager for a |
---|
| 165 | * :class:`GeoExt.MapPanel`, take the first one it finds and take its layer |
---|
| 166 | * store. This property is only required if ``layer`` is provided as a |
---|
| 167 | * string. |
---|
| 168 | */ |
---|
| 169 | layerStore: null, |
---|
| 170 | |
---|
| 171 | /** api: config[loader] |
---|
| 172 | * ``Ext.tree.TreeLoader|Object`` If provided, subnodes will be added to |
---|
| 173 | * this LayerNode. Obviously, only loaders that process an |
---|
| 174 | * ``OpenLayers.Layer`` or :class:`GeoExt.data.LayerRecord` (like |
---|
| 175 | * :class:`GeoExt.tree.LayerParamsLoader`) will actually generate child |
---|
| 176 | * nodes here. If provided as ``Object``, a |
---|
| 177 | * :class:`GeoExt.tree.LayerParamLoader` instance will be created, with |
---|
| 178 | * the provided object as configuration. |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | /** private: method[constructor] |
---|
| 182 | * Private constructor override. |
---|
| 183 | */ |
---|
| 184 | constructor: function(config) { |
---|
| 185 | config.leaf = config.leaf || !(config.children || config.loader); |
---|
| 186 | |
---|
| 187 | if(!config.iconCls && !config.children) { |
---|
| 188 | config.iconCls = "gx-tree-layer-icon"; |
---|
| 189 | } |
---|
| 190 | if(config.loader && !(config.loader instanceof Ext.tree.TreeLoader)) { |
---|
| 191 | config.loader = new GeoExt.tree.LayerParamLoader(config.loader); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | this.defaultUI = this.defaultUI || GeoExt.tree.LayerNodeUI; |
---|
| 195 | |
---|
| 196 | Ext.apply(this, { |
---|
| 197 | layer: config.layer, |
---|
| 198 | layerStore: config.layerStore |
---|
| 199 | }); |
---|
| 200 | if (config.text) { |
---|
| 201 | this.fixedText = true; |
---|
| 202 | } |
---|
| 203 | GeoExt.tree.LayerNode.superclass.constructor.apply(this, arguments); |
---|
| 204 | }, |
---|
| 205 | |
---|
| 206 | /** private: method[render] |
---|
| 207 | * :param bulkRender: ``Boolean`` |
---|
| 208 | */ |
---|
| 209 | render: function(bulkRender) { |
---|
| 210 | var layer = this.layer instanceof OpenLayers.Layer && this.layer; |
---|
| 211 | if(!layer) { |
---|
| 212 | // guess the store if not provided |
---|
| 213 | if(!this.layerStore || this.layerStore == "auto") { |
---|
| 214 | this.layerStore = GeoExt.MapPanel.guess().layers; |
---|
| 215 | } |
---|
| 216 | // now we try to find the layer by its name in the layer store |
---|
| 217 | var i = this.layerStore.findBy(function(o) { |
---|
| 218 | return o.get("title") == this.layer; |
---|
| 219 | }, this); |
---|
| 220 | if(i != -1) { |
---|
| 221 | // if we found the layer, we can assign it and everything |
---|
| 222 | // will be fine |
---|
| 223 | layer = this.layerStore.getAt(i).getLayer(); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | if (!this.rendered || !layer) { |
---|
| 227 | var ui = this.getUI(); |
---|
| 228 | |
---|
| 229 | if(layer) { |
---|
| 230 | this.layer = layer; |
---|
| 231 | // no DD and radio buttons for base layers |
---|
| 232 | if(layer.isBaseLayer) { |
---|
| 233 | this.draggable = false; |
---|
| 234 | Ext.applyIf(this.attributes, { |
---|
| 235 | checkedGroup: "gx_baselayer" |
---|
| 236 | }); |
---|
| 237 | } |
---|
| 238 | if(!this.text) { |
---|
| 239 | this.text = layer.name; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | ui.show(); |
---|
| 243 | this.addVisibilityEventHandlers(); |
---|
| 244 | } else { |
---|
| 245 | ui.hide(); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | if(this.layerStore instanceof GeoExt.data.LayerStore) { |
---|
| 249 | this.addStoreEventHandlers(layer); |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | GeoExt.tree.LayerNode.superclass.render.apply(this, arguments); |
---|
| 253 | }, |
---|
| 254 | |
---|
| 255 | /** private: method[addVisibilityHandlers] |
---|
| 256 | * Adds handlers that sync the checkbox state with the layer's visibility |
---|
| 257 | * state |
---|
| 258 | */ |
---|
| 259 | addVisibilityEventHandlers: function() { |
---|
| 260 | this.layer.events.on({ |
---|
| 261 | "visibilitychanged": this.onLayerVisibilityChanged, |
---|
| 262 | scope: this |
---|
| 263 | }); |
---|
| 264 | this.on({ |
---|
| 265 | "checkchange": this.onCheckChange, |
---|
| 266 | scope: this |
---|
| 267 | }); |
---|
| 268 | }, |
---|
| 269 | |
---|
| 270 | /** private: method[onLayerVisiilityChanged |
---|
| 271 | * handler for visibilitychanged events on the layer |
---|
| 272 | */ |
---|
| 273 | onLayerVisibilityChanged: function() { |
---|
| 274 | if(!this._visibilityChanging) { |
---|
| 275 | this.getUI().toggleCheck(this.layer.getVisibility()); |
---|
| 276 | } |
---|
| 277 | }, |
---|
| 278 | |
---|
| 279 | /** private: method[onCheckChange] |
---|
| 280 | * :param node: ``GeoExt.tree.LayerNode`` |
---|
| 281 | * :param checked: ``Boolean`` |
---|
| 282 | * |
---|
| 283 | * handler for checkchange events |
---|
| 284 | */ |
---|
| 285 | onCheckChange: function(node, checked) { |
---|
| 286 | if(checked != this.layer.getVisibility()) { |
---|
| 287 | this._visibilityChanging = true; |
---|
| 288 | var layer = this.layer; |
---|
| 289 | if(checked && layer.isBaseLayer && layer.map) { |
---|
| 290 | layer.map.setBaseLayer(layer); |
---|
| 291 | } else { |
---|
| 292 | layer.setVisibility(checked); |
---|
| 293 | } |
---|
| 294 | delete this._visibilityChanging; |
---|
| 295 | } |
---|
| 296 | }, |
---|
| 297 | |
---|
| 298 | /** private: method[addStoreEventHandlers] |
---|
| 299 | * Adds handlers that make sure the node disappeares when the layer is |
---|
| 300 | * removed from the store, and appears when it is re-added. |
---|
| 301 | */ |
---|
| 302 | addStoreEventHandlers: function() { |
---|
| 303 | this.layerStore.on({ |
---|
| 304 | "add": this.onStoreAdd, |
---|
| 305 | "remove": this.onStoreRemove, |
---|
| 306 | "update": this.onStoreUpdate, |
---|
| 307 | scope: this |
---|
| 308 | }); |
---|
| 309 | }, |
---|
| 310 | |
---|
| 311 | /** private: method[onStoreAdd] |
---|
| 312 | * :param store: ``Ext.data.Store`` |
---|
| 313 | * :param records: ``Array(Ext.data.Record)`` |
---|
| 314 | * :param index: ``Number`` |
---|
| 315 | * |
---|
| 316 | * handler for add events on the store |
---|
| 317 | */ |
---|
| 318 | onStoreAdd: function(store, records, index) { |
---|
| 319 | var l; |
---|
| 320 | for(var i=0; i<records.length; ++i) { |
---|
| 321 | l = records[i].getLayer(); |
---|
| 322 | if(this.layer == l) { |
---|
| 323 | this.getUI().show(); |
---|
| 324 | break; |
---|
| 325 | } else if (this.layer == l.name) { |
---|
| 326 | // layer is a string, which means the node has not yet |
---|
| 327 | // been rendered because the layer was not found. But |
---|
| 328 | // now we have the layer and can render. |
---|
| 329 | this.render(); |
---|
| 330 | break; |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | }, |
---|
| 334 | |
---|
| 335 | /** private: method[onStoreRemove] |
---|
| 336 | * :param store: ``Ext.data.Store`` |
---|
| 337 | * :param record: ``Ext.data.Record`` |
---|
| 338 | * :param index: ``Number`` |
---|
| 339 | * |
---|
| 340 | * handler for remove events on the store |
---|
| 341 | */ |
---|
| 342 | onStoreRemove: function(store, record, index) { |
---|
| 343 | if(this.layer == record.getLayer()) { |
---|
| 344 | this.getUI().hide(); |
---|
| 345 | } |
---|
| 346 | }, |
---|
| 347 | |
---|
| 348 | /** private: method[onStoreUpdate] |
---|
| 349 | * :param store: ``Ext.data.Store`` |
---|
| 350 | * :param record: ``Ext.data.Record`` |
---|
| 351 | * :param operation: ``String`` |
---|
| 352 | * |
---|
| 353 | * Listener for the store's update event. |
---|
| 354 | */ |
---|
| 355 | onStoreUpdate: function(store, record, operation) { |
---|
| 356 | var layer = record.getLayer(); |
---|
| 357 | if(!this.fixedText && (this.layer == layer && this.text !== layer.name)) { |
---|
| 358 | this.setText(layer.name); |
---|
| 359 | } |
---|
| 360 | }, |
---|
| 361 | |
---|
| 362 | /** private: method[destroy] |
---|
| 363 | */ |
---|
| 364 | destroy: function() { |
---|
| 365 | var layer = this.layer; |
---|
| 366 | if (layer instanceof OpenLayers.Layer) { |
---|
| 367 | layer.events.un({ |
---|
| 368 | "visibilitychanged": this.onLayerVisibilityChanged, |
---|
| 369 | scope: this |
---|
| 370 | }); |
---|
| 371 | } |
---|
| 372 | delete this.layer; |
---|
| 373 | var layerStore = this.layerStore; |
---|
| 374 | if(layerStore) { |
---|
| 375 | layerStore.un("add", this.onStoreAdd, this); |
---|
| 376 | layerStore.un("remove", this.onStoreRemove, this); |
---|
| 377 | layerStore.un("update", this.onStoreUpdate, this); |
---|
| 378 | } |
---|
| 379 | delete this.layerStore; |
---|
| 380 | this.un("checkchange", this.onCheckChange, this); |
---|
| 381 | |
---|
| 382 | GeoExt.tree.LayerNode.superclass.destroy.apply(this, arguments); |
---|
| 383 | } |
---|
| 384 | }); |
---|
| 385 | |
---|
| 386 | /** |
---|
| 387 | * NodeType: gx_layer |
---|
| 388 | */ |
---|
| 389 | Ext.tree.TreePanel.nodeTypes.gx_layer = GeoExt.tree.LayerNode; |
---|