source: Dev/trunk/src/client/dojox/wire/TreeAdapter.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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