aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/wire/TreeAdapter.js
blob: b9b16bf43bdfd558f128fa50fe0683bf79205edf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
if(!dojo._hasResource["dojox.wire.TreeAdapter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.wire.TreeAdapter"] = true;
dojo.provide("dojox.wire.TreeAdapter");

dojo.require("dojox.wire.CompositeWire");

dojo.declare("dojox.wire.TreeAdapter", dojox.wire.CompositeWire, {
	//	summary:
	//		A composite Wire for tree nodes
	//	description:
	//		This class has multiple child Wires for tree nodes, their title and
	//		child nodes.
	//		The root object for this class must be an array.
	//		'node' Wires in 'nodes' property is used to identify an object
	//		representing a node.
	//		'title' Wires in 'nodes' property is used to get the title string
	//		of a node.
	//		'children' Wires in 'nodes' property is used to iterate over child
	//		node objects.
	//		The node values are returned in an array as follows:
	//			[
	//				{title: title1,
	//		  	 	children: [
	//					{title: title2,
	//					 child: ...},
	//					{title: title3,
	//					 child: ...},
	//					...
	//				]},
	//				...
	//			]
	//		This class only supports getValue(), but not setValue().
	
	_wireClass: "dojox.wire.TreeAdapter",
	
	constructor: function(/*Object*/args){
		//	summary:
		//		Initialize properties
		//	description:
		//		If object properties ('node', 'title' and 'children') of array
		//		elements specified in 'nodes' property are not Wires, Wires are
		//		created from them as arguments, with 'parent' property set to
		//		this Wire instance.
		//	args:
		//		Arguments to initialize properties
		//		nodes:
		//			An array containing objects for child Wires for node values
		this._initializeChildren(this.nodes);
	},
	_getValue: function(/*Array*/object){
		//	summary:
		//		Return an array of tree node values
		//	description:
		//		This method iterates over an array specified to 'object'
		//		argument and calls getValue() method of 'node' Wires with each
		//		element of the array to get object(s) that represetns nodes.
		//		(If 'node' Wires are omitted, the array element is used for
		//		further processing.)
		//		Then, getValue() method of 'title' Wires are called to get
		//		title strings for nodes.
		//		(If 'title' Wires are omitted, the objects representing nodes
		//		are used as title strings.)
		//		And if an array of objects with 'node' and 'title' Wires is
		//		specified to 'children', it is used to gather child nodes and
		//		their title strings in the same way recursively.
		//		Finally, an array of the top-level node objects are retuned.
		//	object:
		//		A root array
		//	returns:
		//		An array of tree node values
		if(!object || !this.nodes){
			return object; //Array
		}

		var array = object;
		if(!dojo.isArray(array)){
			array = [array];
		}

		var nodes = [];
		for(var i in array){
			for(var i2 in this.nodes){
				nodes = nodes.concat(this._getNodes(array[i], this.nodes[i2]));
			}
		}
		return nodes; //Array
	},

	_setValue: function(/*Array*/object, /*Array*/value){
		//	summary:
		//		Not supported
		throw new Error("Unsupported API: " + this._wireClass + "._setValue");
	},

	_initializeChildren: function(/*Array*/children){
		//	summary:
		//		Initialize child Wires
		//	description:
		//		If 'node' or 'title' properties of array elements specified in
		//		'children' argument are not Wires, Wires are created from them
		//		as arguments, with 'parent' property set to this Wire instance.
		//		If an array element has 'children' property, this method is
		//		called recursively with it.
		//	children:
		//		An array of objects containing child Wires
		if(!children){
			return; //undefined
		}

		for(var i in children){
			var child = children[i];
			if(child.node){
				child.node.parent = this;
				if(!dojox.wire.isWire(child.node)){
					child.node = dojox.wire.create(child.node);
				}
			}
			if(child.title){
				child.title.parent = this;
				if(!dojox.wire.isWire(child.title)){
					child.title = dojox.wire.create(child.title);
				}
			}
			if(child.children){
				this._initializeChildren(child.children);
			}
		}
	},

	_getNodes: function(/*Object*/object, /*Object*/child){
		//	summary:
		//		Return an array of tree node values
		//	description:
		//		This method calls getValue() method of 'node' Wires with
		//		'object' argument to get object(s) that represents nodes.
		//		(If 'node' Wires are omitted, 'object' is used for further
		//		processing.)
		//		Then, getValue() method of 'title' Wires are called to get
		//		title strings for nodes.
		//		(If 'title' Wires are omitted, the objects representing nodes
		//		are used as title strings.)
		//		And if an array of objects with 'node' and 'title' Wires is
		//		specified to 'children', it is used to gather child nodes and
		//		their title strings in the same way recursively.
		//		Finally, an array of node objects are returned.
		//	object:
		//		An object
		//	child:
		//		An object with child Wires
		//	returns:
		var array = null;
		if(child.node){
			array = child.node.getValue(object);
			if(!array){
				return [];
			}
			if(!dojo.isArray(array)){
				array = [array];
			}
		}else{
			array = [object];
		}

		var nodes = [];
		for(var i in array){
			object = array[i];
			var node = {};
			if(child.title){
				node.title = child.title.getValue(object);
			}else{
				node.title = object;
			}
			if(child.children){
				var children = [];
				for(var i2 in child.children){
					children = children.concat(this._getNodes(object, child.children[i2]));
				}
				if(children.length > 0){
					node.children = children;
				}
			}
			nodes.push(node);
		}
		return nodes; //Array
	}
});

}