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 = LayerParamLoader |
---|
13 | * base_link = `Ext.util.Observable <http://dev.sencha.com/deploy/dev/docs/?class=Ext.util.Observable>`_ |
---|
14 | */ |
---|
15 | |
---|
16 | /** api: constructor |
---|
17 | * .. class:: LayerParamLoader |
---|
18 | * |
---|
19 | * A loader that creates children from its node's layer |
---|
20 | * (``OpenLayers.Layer.HTTPRequest``) by items in one of the values in |
---|
21 | * the layer's params object. |
---|
22 | */ |
---|
23 | GeoExt.tree.LayerParamLoader = function(config) { |
---|
24 | Ext.apply(this, config); |
---|
25 | this.addEvents( |
---|
26 | |
---|
27 | /** api: event[beforeload] |
---|
28 | * Triggered before loading children. Return false to avoid |
---|
29 | * loading children. |
---|
30 | * |
---|
31 | * Listener arguments: |
---|
32 | * |
---|
33 | * * loader - :class:`GeoExt.tree.LayerLoader` this loader |
---|
34 | * * node - ``Ex.tree.TreeNode`` the node that this loader is |
---|
35 | * configured with |
---|
36 | */ |
---|
37 | "beforeload", |
---|
38 | |
---|
39 | /** api: event[load] |
---|
40 | * Triggered after children were loaded. |
---|
41 | * |
---|
42 | * Listener arguments: |
---|
43 | * |
---|
44 | * * loader - :class:`GeoExt.tree.LayerLoader` this loader |
---|
45 | * * node - ``Ex.tree.TreeNode`` the node that this loader is |
---|
46 | * configured with |
---|
47 | */ |
---|
48 | "load" |
---|
49 | ); |
---|
50 | |
---|
51 | GeoExt.tree.LayerParamLoader.superclass.constructor.call(this); |
---|
52 | }; |
---|
53 | |
---|
54 | Ext.extend(GeoExt.tree.LayerParamLoader, Ext.util.Observable, { |
---|
55 | |
---|
56 | /** api: config[param] |
---|
57 | * ``String`` Key for a param (key-value pair in the params object of the |
---|
58 | * layer) that this loader uses to create childnodes from its items. The |
---|
59 | * value can either be an ``Array`` or a ``String``, delimited by the |
---|
60 | * character (or string) provided as ``delimiter`` config option. |
---|
61 | */ |
---|
62 | |
---|
63 | /** private: property[param] |
---|
64 | * ``String`` |
---|
65 | */ |
---|
66 | param: null, |
---|
67 | |
---|
68 | /** api: config[delimiter] |
---|
69 | * ``String`` Delimiter of the ``param``'s value's items. Default is |
---|
70 | * ``,`` (comma). If the ``param``'s value is an array, this property has |
---|
71 | * no effect. |
---|
72 | */ |
---|
73 | |
---|
74 | /** private: property[delimiter] |
---|
75 | * ``String`` |
---|
76 | */ |
---|
77 | delimiter: ",", |
---|
78 | |
---|
79 | /** private: method[load] |
---|
80 | * :param node: ``Ext.tree.TreeNode`` The node to add children to. |
---|
81 | * :param callback: ``Function`` |
---|
82 | */ |
---|
83 | load: function(node, callback) { |
---|
84 | if(this.fireEvent("beforeload", this, node)) { |
---|
85 | while (node.firstChild) { |
---|
86 | node.removeChild(node.firstChild); |
---|
87 | } |
---|
88 | |
---|
89 | var paramValue = |
---|
90 | (node.layer instanceof OpenLayers.Layer.HTTPRequest) && |
---|
91 | node.layer.params[this.param]; |
---|
92 | if(paramValue) { |
---|
93 | var items = (paramValue instanceof Array) ? |
---|
94 | paramValue.slice() : |
---|
95 | paramValue.split(this.delimiter); |
---|
96 | |
---|
97 | Ext.each(items, function(item, index, allItems) { |
---|
98 | this.addParamNode(item, allItems, node); |
---|
99 | }, this); |
---|
100 | } |
---|
101 | |
---|
102 | if(typeof callback == "function"){ |
---|
103 | callback(); |
---|
104 | } |
---|
105 | |
---|
106 | this.fireEvent("load", this, node); |
---|
107 | } |
---|
108 | }, |
---|
109 | |
---|
110 | /** private: method[addParamNode] |
---|
111 | * :param paramItem: ``String`` The param item that the child node will |
---|
112 | * represent. |
---|
113 | * :param allParamItems: ``Array`` The full list of param items. |
---|
114 | * :param node: :class:`GeoExt.tree.LayerNode`` The node that the param |
---|
115 | * node will be added to as child. |
---|
116 | * |
---|
117 | * Adds a child node representing a param value of the layer |
---|
118 | */ |
---|
119 | addParamNode: function(paramItem, allParamItems, node) { |
---|
120 | var child = this.createNode({ |
---|
121 | layer: node.layer, |
---|
122 | param: this.param, |
---|
123 | item: paramItem, |
---|
124 | allItems: allParamItems, |
---|
125 | delimiter: this.delimiter |
---|
126 | }); |
---|
127 | var sibling = node.item(0); |
---|
128 | if(sibling) { |
---|
129 | node.insertBefore(child, sibling); |
---|
130 | } else { |
---|
131 | node.appendChild(child); |
---|
132 | } |
---|
133 | }, |
---|
134 | |
---|
135 | /** api: method[createNode] |
---|
136 | * :param attr: ``Object`` attributes for the new node |
---|
137 | * |
---|
138 | * Override this function for custom TreeNode node implementation, or to |
---|
139 | * modify the attributes at creation time. |
---|
140 | */ |
---|
141 | createNode: function(attr){ |
---|
142 | if(this.baseAttrs){ |
---|
143 | Ext.apply(attr, this.baseAttrs); |
---|
144 | } |
---|
145 | if(typeof attr.uiProvider == 'string'){ |
---|
146 | attr.uiProvider = this.uiProviders[attr.uiProvider] || eval(attr.uiProvider); |
---|
147 | } |
---|
148 | attr.nodeType = attr.nodeType || "gx_layerparam"; |
---|
149 | |
---|
150 | return new Ext.tree.TreePanel.nodeTypes[attr.nodeType](attr); |
---|
151 | } |
---|
152 | }); |
---|