diff options
-rw-r--r-- | js/controllers/tree-controller.js | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/js/controllers/tree-controller.js b/js/controllers/tree-controller.js new file mode 100644 index 00000000..45e4c4ba --- /dev/null +++ b/js/controllers/tree-controller.js | |||
@@ -0,0 +1,219 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | /** | ||
7 | @module montage/ui/controller/tree-controller | ||
8 | @requires montage/core/core | ||
9 | @requires montage/ui/controller/object-controller | ||
10 | @requires montage/core/event/mutable-event | ||
11 | */ | ||
12 | var Montage = require("montage").Montage, | ||
13 | ObjectController = require("montage/ui/controller/object-controller").ObjectController, | ||
14 | ArrayController = require("montage/ui/controller/array-controller").ArrayController, | ||
15 | MutableEvent = require("montage/core/event/mutable-event").MutableEvent; | ||
16 | /** | ||
17 | TODO: Write description like the array controllers: The ArrayController helps with organizing a hierarchical | ||
18 | collection of objects, and managing user selection within that collection. | ||
19 | You can assign a TreeController instance as the <code>contentProvider</code> property for a TreeView object. | ||
20 | @class module:montage/ui/controller/tree-controller.TreeController | ||
21 | @classdesc | ||
22 | @extends module:montage/ui/controller/object-controller.ObjectController | ||
23 | */ | ||
24 | var TreeController = exports.TreeController = Montage.create(ObjectController, /** @lends module:montage/ui/controller/tree-controller.TreeController# */ { | ||
25 | |||
26 | rootKey : { | ||
27 | value: null | ||
28 | }, | ||
29 | |||
30 | branchKey : { | ||
31 | value: 'children' | ||
32 | }, | ||
33 | |||
34 | _root : { | ||
35 | value : null | ||
36 | }, | ||
37 | root : { | ||
38 | get: function() { | ||
39 | return this._root; | ||
40 | }, | ||
41 | set: function(value) { | ||
42 | this._root = value; | ||
43 | |||
44 | this.initArrayControllers(); | ||
45 | } | ||
46 | }, | ||
47 | |||
48 | rootController: { | ||
49 | value: null | ||
50 | }, | ||
51 | |||
52 | initArrayControllers : { | ||
53 | value: function() { | ||
54 | var self = this; | ||
55 | |||
56 | ///// Declare function to initialize controller for each node | ||
57 | ///// that is a branch | ||
58 | |||
59 | function initController(array, depth) { | ||
60 | var controller = Montage.create(ArrayController, { | ||
61 | content : { value: array }, | ||
62 | delegate : { value: self }, | ||
63 | isSelectionEnabled : { value: true }}); | ||
64 | |||
65 | if(depth === 0) { | ||
66 | self.rootController = controller; | ||
67 | } | ||
68 | |||
69 | self.branchControllers.push({ | ||
70 | depth : depth, | ||
71 | controller : controller | ||
72 | |||
73 | }); | ||
74 | } | ||
75 | |||
76 | ///// Recursive function that finds all branch nodes and initializes | ||
77 | ///// an array controller | ||
78 | |||
79 | function walk(node, init, depth) { | ||
80 | var children = node[self.branchKey]; | ||
81 | |||
82 | if(children) { | ||
83 | //init(children, depth); | ||
84 | |||
85 | children.forEach(function(child) { | ||
86 | walk(child, init, ++depth); | ||
87 | }); | ||
88 | |||
89 | node['treeNodeType'] = 'branch'; | ||
90 | } else { | ||
91 | node['treeNodeType'] = 'leaf'; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | walk(this._root, initController, 0); | ||
96 | |||
97 | } | ||
98 | }, | ||
99 | |||
100 | /** | ||
101 | @private | ||
102 | */ | ||
103 | _selectedIndexes: { | ||
104 | value: null, | ||
105 | enumerable: false | ||
106 | }, | ||
107 | |||
108 | /** | ||
109 | Description TODO | ||
110 | @type {Function} | ||
111 | @default null | ||
112 | */ | ||
113 | selectedIndexes: { | ||
114 | get: function() { | ||
115 | |||
116 | }, | ||
117 | set: function(value) { | ||
118 | |||
119 | } | ||
120 | }, | ||
121 | |||
122 | lazyLoad : { | ||
123 | value: false | ||
124 | }, | ||
125 | |||
126 | branchControllers: { | ||
127 | value: [] | ||
128 | }, | ||
129 | |||
130 | addBranchController : { | ||
131 | value: function(controller) { | ||
132 | if(this.delegate) { | ||
133 | controller.delegate = this.delegate; | ||
134 | } | ||
135 | |||
136 | this.branchControllers.push(controller); | ||
137 | } | ||
138 | }, | ||
139 | |||
140 | /** | ||
141 | @private | ||
142 | */ | ||
143 | _content: { | ||
144 | enumerable: false, | ||
145 | value: null | ||
146 | }, | ||
147 | /** | ||
148 | The content managed by the TreeController. | ||
149 | @type {Function} | ||
150 | @default {String} null | ||
151 | */ | ||
152 | content: { | ||
153 | get: function() { | ||
154 | return this._content; | ||
155 | }, | ||
156 | set: function(value) { | ||
157 | if (this._content === value) { | ||
158 | return; | ||
159 | } | ||
160 | |||
161 | this._content = value; | ||
162 | |||
163 | //TODO for right now assume that any content change invalidates the selection completely; we'll need to address this of course | ||
164 | this.selectedObjects = null; | ||
165 | |||
166 | if (this.rootKey) { | ||
167 | if (value[this.rootKey]) { | ||
168 | this.root = value[this.rootKey]; | ||
169 | } else { | ||
170 | console.log('No root key found in content data'); | ||
171 | } | ||
172 | } else { | ||
173 | this.root = value; | ||
174 | } | ||
175 | |||
176 | |||
177 | } | ||
178 | }, | ||
179 | |||
180 | addObjects : { | ||
181 | value: function() { | ||
182 | |||
183 | var objects = Array.prototype.slice.call(arguments), | ||
184 | i, | ||
185 | objectCount = objects.length, | ||
186 | selectedContentIndexes, firstIndex; | ||
187 | |||
188 | for (i = 0; i < objectCount; i++) { | ||
189 | this.content.push(objects[i]); | ||
190 | } | ||
191 | |||
192 | if (this.selectObjectsOnAddition) { | ||
193 | selectedContentIndexes = []; | ||
194 | firstIndex = this.content.length-objectCount; | ||
195 | for (i = 0; i < objectCount; i++) { | ||
196 | selectedContentIndexes[i] = firstIndex++; | ||
197 | } | ||
198 | this.selectedContentIndexes = selectedContentIndexes; | ||
199 | this.selectedObjects = objects; | ||
200 | } | ||
201 | |||
202 | if (this.clearFilterFunctionOnAddition) { | ||
203 | this.filterFunction = null; | ||
204 | } | ||
205 | |||
206 | if (this.automaticallyOrganizeObjects) { | ||
207 | this.organizeObjects(); | ||
208 | } | ||
209 | |||
210 | } | ||
211 | }, | ||
212 | |||
213 | insertChildBefore : { value : function() {} }, | ||
214 | |||
215 | insertChildAfter : { value : function() {} } | ||
216 | |||
217 | |||
218 | |||
219 | }); | ||