2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.wire.TreeAdapter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.wire.TreeAdapter"] = true;
|
|
|
3 |
dojo.provide("dojox.wire.TreeAdapter");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.wire.CompositeWire");
|
|
|
6 |
|
|
|
7 |
dojo.declare("dojox.wire.TreeAdapter", dojox.wire.CompositeWire, {
|
|
|
8 |
// summary:
|
|
|
9 |
// A composite Wire for tree nodes
|
|
|
10 |
// description:
|
|
|
11 |
// This class has multiple child Wires for tree nodes, their title and
|
|
|
12 |
// child nodes.
|
|
|
13 |
// The root object for this class must be an array.
|
|
|
14 |
// 'node' Wires in 'nodes' property is used to identify an object
|
|
|
15 |
// representing a node.
|
|
|
16 |
// 'title' Wires in 'nodes' property is used to get the title string
|
|
|
17 |
// of a node.
|
|
|
18 |
// 'children' Wires in 'nodes' property is used to iterate over child
|
|
|
19 |
// node objects.
|
|
|
20 |
// The node values are returned in an array as follows:
|
|
|
21 |
// [
|
|
|
22 |
// {title: title1,
|
|
|
23 |
// children: [
|
|
|
24 |
// {title: title2,
|
|
|
25 |
// child: ...},
|
|
|
26 |
// {title: title3,
|
|
|
27 |
// child: ...},
|
|
|
28 |
// ...
|
|
|
29 |
// ]},
|
|
|
30 |
// ...
|
|
|
31 |
// ]
|
|
|
32 |
// This class only supports getValue(), but not setValue().
|
|
|
33 |
|
|
|
34 |
_wireClass: "dojox.wire.TreeAdapter",
|
|
|
35 |
|
|
|
36 |
constructor: function(/*Object*/args){
|
|
|
37 |
// summary:
|
|
|
38 |
// Initialize properties
|
|
|
39 |
// description:
|
|
|
40 |
// If object properties ('node', 'title' and 'children') of array
|
|
|
41 |
// elements specified in 'nodes' property are not Wires, Wires are
|
|
|
42 |
// created from them as arguments, with 'parent' property set to
|
|
|
43 |
// this Wire instance.
|
|
|
44 |
// args:
|
|
|
45 |
// Arguments to initialize properties
|
|
|
46 |
// nodes:
|
|
|
47 |
// An array containing objects for child Wires for node values
|
|
|
48 |
this._initializeChildren(this.nodes);
|
|
|
49 |
},
|
|
|
50 |
_getValue: function(/*Array*/object){
|
|
|
51 |
// summary:
|
|
|
52 |
// Return an array of tree node values
|
|
|
53 |
// description:
|
|
|
54 |
// This method iterates over an array specified to 'object'
|
|
|
55 |
// argument and calls getValue() method of 'node' Wires with each
|
|
|
56 |
// element of the array to get object(s) that represetns nodes.
|
|
|
57 |
// (If 'node' Wires are omitted, the array element is used for
|
|
|
58 |
// further processing.)
|
|
|
59 |
// Then, getValue() method of 'title' Wires are called to get
|
|
|
60 |
// title strings for nodes.
|
|
|
61 |
// (If 'title' Wires are omitted, the objects representing nodes
|
|
|
62 |
// are used as title strings.)
|
|
|
63 |
// And if an array of objects with 'node' and 'title' Wires is
|
|
|
64 |
// specified to 'children', it is used to gather child nodes and
|
|
|
65 |
// their title strings in the same way recursively.
|
|
|
66 |
// Finally, an array of the top-level node objects are retuned.
|
|
|
67 |
// object:
|
|
|
68 |
// A root array
|
|
|
69 |
// returns:
|
|
|
70 |
// An array of tree node values
|
|
|
71 |
if(!object || !this.nodes){
|
|
|
72 |
return object; //Array
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
var array = object;
|
|
|
76 |
if(!dojo.isArray(array)){
|
|
|
77 |
array = [array];
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
var nodes = [];
|
|
|
81 |
for(var i in array){
|
|
|
82 |
for(var i2 in this.nodes){
|
|
|
83 |
nodes = nodes.concat(this._getNodes(array[i], this.nodes[i2]));
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
return nodes; //Array
|
|
|
87 |
},
|
|
|
88 |
|
|
|
89 |
_setValue: function(/*Array*/object, /*Array*/value){
|
|
|
90 |
// summary:
|
|
|
91 |
// Not supported
|
|
|
92 |
throw new Error("Unsupported API: " + this._wireClass + "._setValue");
|
|
|
93 |
},
|
|
|
94 |
|
|
|
95 |
_initializeChildren: function(/*Array*/children){
|
|
|
96 |
// summary:
|
|
|
97 |
// Initialize child Wires
|
|
|
98 |
// description:
|
|
|
99 |
// If 'node' or 'title' properties of array elements specified in
|
|
|
100 |
// 'children' argument are not Wires, Wires are created from them
|
|
|
101 |
// as arguments, with 'parent' property set to this Wire instance.
|
|
|
102 |
// If an array element has 'children' property, this method is
|
|
|
103 |
// called recursively with it.
|
|
|
104 |
// children:
|
|
|
105 |
// An array of objects containing child Wires
|
|
|
106 |
if(!children){
|
|
|
107 |
return; //undefined
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
for(var i in children){
|
|
|
111 |
var child = children[i];
|
|
|
112 |
if(child.node){
|
|
|
113 |
child.node.parent = this;
|
|
|
114 |
if(!dojox.wire.isWire(child.node)){
|
|
|
115 |
child.node = dojox.wire.create(child.node);
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
if(child.title){
|
|
|
119 |
child.title.parent = this;
|
|
|
120 |
if(!dojox.wire.isWire(child.title)){
|
|
|
121 |
child.title = dojox.wire.create(child.title);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
if(child.children){
|
|
|
125 |
this._initializeChildren(child.children);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
},
|
|
|
129 |
|
|
|
130 |
_getNodes: function(/*Object*/object, /*Object*/child){
|
|
|
131 |
// summary:
|
|
|
132 |
// Return an array of tree node values
|
|
|
133 |
// description:
|
|
|
134 |
// This method calls getValue() method of 'node' Wires with
|
|
|
135 |
// 'object' argument to get object(s) that represents nodes.
|
|
|
136 |
// (If 'node' Wires are omitted, 'object' is used for further
|
|
|
137 |
// processing.)
|
|
|
138 |
// Then, getValue() method of 'title' Wires are called to get
|
|
|
139 |
// title strings for nodes.
|
|
|
140 |
// (If 'title' Wires are omitted, the objects representing nodes
|
|
|
141 |
// are used as title strings.)
|
|
|
142 |
// And if an array of objects with 'node' and 'title' Wires is
|
|
|
143 |
// specified to 'children', it is used to gather child nodes and
|
|
|
144 |
// their title strings in the same way recursively.
|
|
|
145 |
// Finally, an array of node objects are returned.
|
|
|
146 |
// object:
|
|
|
147 |
// An object
|
|
|
148 |
// child:
|
|
|
149 |
// An object with child Wires
|
|
|
150 |
// returns:
|
|
|
151 |
var array = null;
|
|
|
152 |
if(child.node){
|
|
|
153 |
array = child.node.getValue(object);
|
|
|
154 |
if(!array){
|
|
|
155 |
return [];
|
|
|
156 |
}
|
|
|
157 |
if(!dojo.isArray(array)){
|
|
|
158 |
array = [array];
|
|
|
159 |
}
|
|
|
160 |
}else{
|
|
|
161 |
array = [object];
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
var nodes = [];
|
|
|
165 |
for(var i in array){
|
|
|
166 |
object = array[i];
|
|
|
167 |
var node = {};
|
|
|
168 |
if(child.title){
|
|
|
169 |
node.title = child.title.getValue(object);
|
|
|
170 |
}else{
|
|
|
171 |
node.title = object;
|
|
|
172 |
}
|
|
|
173 |
if(child.children){
|
|
|
174 |
var children = [];
|
|
|
175 |
for(var i2 in child.children){
|
|
|
176 |
children = children.concat(this._getNodes(object, child.children[i2]));
|
|
|
177 |
}
|
|
|
178 |
if(children.length > 0){
|
|
|
179 |
node.children = children;
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
nodes.push(node);
|
|
|
183 |
}
|
|
|
184 |
return nodes; //Array
|
|
|
185 |
}
|
|
|
186 |
});
|
|
|
187 |
|
|
|
188 |
}
|