[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 | Ext.namespace("GeoExt.tree"); |
---|
| 9 | |
---|
| 10 | /** api: (define) |
---|
| 11 | * module = GeoExt.tree |
---|
| 12 | * class = LayerParamNode |
---|
| 13 | * base_link = `Ext.tree.TreeNode <http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreeNode>`_ |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | /** api: constructor |
---|
| 17 | * .. class:: LayerParamNode |
---|
| 18 | * |
---|
| 19 | * A subclass of ``Ext.tree.TreeNode`` that represents a value of a list of |
---|
| 20 | * values provided as one of an ``OpenLayers.Layer.HTTPRequest``'s params. |
---|
| 21 | * The default iconCls for this node's icon is "gx-tree-layerparam-icon". |
---|
| 22 | * |
---|
| 23 | * To use this node type in a ``TreePanel`` config, set ``nodeType`` to |
---|
| 24 | * "gx_layerparam". |
---|
| 25 | */ |
---|
| 26 | GeoExt.tree.LayerParamNode = Ext.extend(Ext.tree.TreeNode, { |
---|
| 27 | |
---|
| 28 | /** api: config[layer] |
---|
| 29 | * ``OpenLayers.Layer.HTTPRequest|String`` The layer that this node |
---|
| 30 | * represents a subnode of. If provided as string, the string has to |
---|
| 31 | * match the title of one of the records in the ``layerStore``. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** private: property[layer] |
---|
| 35 | * ``OpenLayers.Layer.HTTPRequest`` |
---|
| 36 | */ |
---|
| 37 | layer: null, |
---|
| 38 | |
---|
| 39 | /** api: config[layerStore] |
---|
| 40 | * :class:`GeoExt.data.LayerStore` Only used if layer is provided as |
---|
| 41 | * string. The store where we can find the layer. If not provided, the |
---|
| 42 | * store of a map panel found by ``GeoExt.MapPanel::guess`` will be used. |
---|
| 43 | */ |
---|
| 44 | |
---|
| 45 | /** api: config[param] |
---|
| 46 | * ``String`` Key for a param (key-value pair in the params object of the |
---|
| 47 | * layer) that this node represents an item of. The value can either be an |
---|
| 48 | * ``Array`` or a ``String``, delimited by the character (or string) |
---|
| 49 | * provided as ``delimiter`` config option. |
---|
| 50 | */ |
---|
| 51 | |
---|
| 52 | /** private: property[param] |
---|
| 53 | * ``String`` |
---|
| 54 | */ |
---|
| 55 | param: null, |
---|
| 56 | |
---|
| 57 | /** api: config[item] |
---|
| 58 | * ``String`` The param's value's item that this node represents. |
---|
| 59 | */ |
---|
| 60 | |
---|
| 61 | /** private: property[item] |
---|
| 62 | * ``String`` |
---|
| 63 | */ |
---|
| 64 | item: null, |
---|
| 65 | |
---|
| 66 | /** api: config[delimiter] |
---|
| 67 | * ``String`` Delimiter of the ``param``'s value's items. Default is |
---|
| 68 | * ``,`` (comma). If the ``param``'s value is an array, this property |
---|
| 69 | * has no effect. |
---|
| 70 | */ |
---|
| 71 | |
---|
| 72 | /** private: property[delimiter] |
---|
| 73 | * ``String`` |
---|
| 74 | */ |
---|
| 75 | delimiter: null, |
---|
| 76 | |
---|
| 77 | /** private: property[allItems] |
---|
| 78 | * ``Array`` All items in the param value. |
---|
| 79 | */ |
---|
| 80 | allItems: null, |
---|
| 81 | |
---|
| 82 | /** private: method[constructor] |
---|
| 83 | * Private constructor override. |
---|
| 84 | */ |
---|
| 85 | constructor: function(attributes) { |
---|
| 86 | var config = attributes || {}; |
---|
| 87 | config.iconCls = config.iconCls || "gx-tree-layerparam-icon"; |
---|
| 88 | config.text = config.text || config.item; |
---|
| 89 | |
---|
| 90 | this.param = config.param; |
---|
| 91 | this.item = config.item; |
---|
| 92 | this.delimiter = config.delimiter || ","; |
---|
| 93 | this.allItems = config.allItems; |
---|
| 94 | |
---|
| 95 | GeoExt.tree.LayerParamNode.superclass.constructor.apply(this, arguments); |
---|
| 96 | |
---|
| 97 | this.getLayer(); |
---|
| 98 | |
---|
| 99 | if(this.layer) { |
---|
| 100 | |
---|
| 101 | // read items from layer if allItems isn't set |
---|
| 102 | // in the attributes |
---|
| 103 | if(!this.allItems) { |
---|
| 104 | this.allItems = this.getItemsFromLayer(); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // if the "checked" attribute isn't set we derive |
---|
| 108 | // it from what we have in the layer. Else, we need |
---|
| 109 | // to update the layer param based on the value of |
---|
| 110 | // the "checked" attribute |
---|
| 111 | if(this.attributes.checked == null) { |
---|
| 112 | this.attributes.checked = |
---|
| 113 | this.layer.getVisibility() && |
---|
| 114 | this.getItemsFromLayer().indexOf(this.item) >= 0; |
---|
| 115 | } else { |
---|
| 116 | this.onCheckChange(this, this.attributes.checked); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | this.layer.events.on({ |
---|
| 120 | "visibilitychanged": this.onLayerVisibilityChanged, |
---|
| 121 | scope: this |
---|
| 122 | }); |
---|
| 123 | |
---|
| 124 | this.on({ |
---|
| 125 | "checkchange": this.onCheckChange, |
---|
| 126 | scope: this |
---|
| 127 | }); |
---|
| 128 | } |
---|
| 129 | }, |
---|
| 130 | |
---|
| 131 | /** private: method[getLayer] |
---|
| 132 | * :return: ``OpenLayers.Layer.HTTPRequest`` the layer |
---|
| 133 | * |
---|
| 134 | * Sets this.layer and returns the layer. |
---|
| 135 | */ |
---|
| 136 | getLayer: function() { |
---|
| 137 | if(!this.layer) { |
---|
| 138 | var layer = this.attributes.layer; |
---|
| 139 | if(typeof layer == "string") { |
---|
| 140 | var store = this.attributes.layerStore || |
---|
| 141 | GeoExt.MapPanel.guess().layers; |
---|
| 142 | var i = store.findBy(function(o) { |
---|
| 143 | return o.get("title") == layer; |
---|
| 144 | }); |
---|
| 145 | layer = i != -1 ? store.getAt(i).getLayer() : null; |
---|
| 146 | } |
---|
| 147 | this.layer = layer; |
---|
| 148 | } |
---|
| 149 | return this.layer; |
---|
| 150 | }, |
---|
| 151 | |
---|
| 152 | /** private: method[getItemsFromLayer] |
---|
| 153 | * :return: ``Array`` the items of this node's layer's param |
---|
| 154 | */ |
---|
| 155 | getItemsFromLayer: function() { |
---|
| 156 | var paramValue = this.layer.params[this.param]; |
---|
| 157 | return paramValue instanceof Array ? |
---|
| 158 | paramValue : |
---|
| 159 | (paramValue ? paramValue.split(this.delimiter) : []); |
---|
| 160 | }, |
---|
| 161 | |
---|
| 162 | /** private: method[createParams] |
---|
| 163 | * :param items: ``Array`` |
---|
| 164 | * :return: ``Object`` The params object to pass to mergeNewParams |
---|
| 165 | */ |
---|
| 166 | createParams: function(items) { |
---|
| 167 | var params = {}; |
---|
| 168 | params[this.param] = this.layer.params[this.param] instanceof Array ? |
---|
| 169 | items : |
---|
| 170 | items.join(this.delimiter); |
---|
| 171 | return params; |
---|
| 172 | }, |
---|
| 173 | |
---|
| 174 | /** private: method[onLayerVisibilityChanged] |
---|
| 175 | * Handler for visibilitychanged events on the layer. |
---|
| 176 | */ |
---|
| 177 | onLayerVisibilityChanged: function() { |
---|
| 178 | if(this.getItemsFromLayer().length === 0) { |
---|
| 179 | this.layer.mergeNewParams(this.createParams(this.allItems)); |
---|
| 180 | } |
---|
| 181 | var visible = this.layer.getVisibility(); |
---|
| 182 | if(visible && this.getItemsFromLayer().indexOf(this.item) !== -1) { |
---|
| 183 | this.getUI().toggleCheck(true); |
---|
| 184 | } |
---|
| 185 | if(!visible) { |
---|
| 186 | this.layer.mergeNewParams(this.createParams([])); |
---|
| 187 | this.getUI().toggleCheck(false); |
---|
| 188 | } |
---|
| 189 | }, |
---|
| 190 | |
---|
| 191 | /** private: method[onCheckChange] |
---|
| 192 | * :param node: :class:`GeoExt.tree.LayerParamNode`` |
---|
| 193 | * :param checked: ``Boolean`` |
---|
| 194 | * |
---|
| 195 | * Handler for checkchange events. |
---|
| 196 | */ |
---|
| 197 | onCheckChange: function(node, checked) { |
---|
| 198 | var layer = this.layer; |
---|
| 199 | |
---|
| 200 | var newItems = []; |
---|
| 201 | var curItems = this.getItemsFromLayer(); |
---|
| 202 | // if the layer is invisible, and a subnode is checked for the first |
---|
| 203 | // time, we need to pretend that no subnode param items are set. |
---|
| 204 | if(checked === true && layer.getVisibility() === false && |
---|
| 205 | curItems.length === this.allItems.length) { |
---|
| 206 | curItems = []; |
---|
| 207 | |
---|
| 208 | } |
---|
| 209 | Ext.each(this.allItems, function(item) { |
---|
| 210 | if((item !== this.item && curItems.indexOf(item) !== -1) || |
---|
| 211 | (checked === true && item === this.item)) { |
---|
| 212 | newItems.push(item); |
---|
| 213 | } |
---|
| 214 | }, this); |
---|
| 215 | |
---|
| 216 | var visible = (newItems.length > 0); |
---|
| 217 | // if there is something to display, we want to update the params |
---|
| 218 | // before the layer is turned on |
---|
| 219 | visible && layer.mergeNewParams(this.createParams(newItems)); |
---|
| 220 | if(visible !== layer.getVisibility()) { |
---|
| 221 | layer.setVisibility(visible); |
---|
| 222 | } |
---|
| 223 | // if there is nothing to display, we want to update the params |
---|
| 224 | // when the layer is turned off, so we don't fire illegal requests |
---|
| 225 | // (i.e. param value being empty) |
---|
| 226 | (!visible) && layer.mergeNewParams(this.createParams([])); |
---|
| 227 | }, |
---|
| 228 | |
---|
| 229 | /** private: method[destroy] |
---|
| 230 | */ |
---|
| 231 | destroy: function() { |
---|
| 232 | var layer = this.layer; |
---|
| 233 | if (layer instanceof OpenLayers.Layer) { |
---|
| 234 | layer.events.un({ |
---|
| 235 | "visibilitychanged": this.onLayerVisibilityChanged, |
---|
| 236 | scope: this |
---|
| 237 | }); |
---|
| 238 | } |
---|
| 239 | delete this.layer; |
---|
| 240 | |
---|
| 241 | this.un("checkchange", this.onCheckChange, this); |
---|
| 242 | |
---|
| 243 | GeoExt.tree.LayerParamNode.superclass.destroy.apply(this, arguments); |
---|
| 244 | } |
---|
| 245 | }); |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * NodeType: gx_layerparam |
---|
| 249 | */ |
---|
| 250 | Ext.tree.TreePanel.nodeTypes.gx_layerparam = GeoExt.tree.LayerParamNode; |
---|