aboutsummaryrefslogtreecommitdiff
path: root/js/components/controllers/tree-controller.js
blob: cb95ca1d1cee1442643a95949e63209a0523d0c7 (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
189
190
191
192
193
194
195
196
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
/**
 @module montage/ui/controller/tree-controller
 @requires montage/core/core
 @requires montage/ui/controller/object-controller
 @requires montage/core/event/mutable-event
 */
var Montage = require("montage").Montage,
    ObjectController = require("montage/ui/controller/object-controller").ObjectController,
    ArrayController = require("montage/ui/controller/array-controller").ArrayController,
    MutableEvent = require("montage/core/event/mutable-event").MutableEvent;
/**
    TODO: Write description like the array controllers: The ArrayController helps with organizing a hierarchical
    collection of objects, and managing user selection within that collection.
    You can assign a TreeController instance as the <code>contentProvider</code> property for a TreeView object.
    @class module:montage/ui/controller/tree-controller.TreeController
    @classdesc
    @extends module:montage/ui/controller/object-controller.ObjectController
*/
var TreeController = exports.TreeController = Montage.create(ObjectController, /** @lends module:montage/ui/controller/tree-controller.TreeController# */ {

    _delegate : { value: null },
    delegate : {
        get: function() {
            return this._delegate;
        },
        set: function(value) {
            this._delegate = value;
        }
    },

    rootKey : {
        value: null
    },

    branchKey : {
        value: null
    },

    _root : {
        value : null
    },
    root : {
        get: function() {
            return this._root;
        },
        set: function(value) {
            this._root = value;

            this.initArrayControllers();
        }
    },

    rootController: {
        value: null
    },

    initArrayControllers : {
        value: function() {
            var self = this;

            ///// Recursive function that finds all branch nodes and initializes
            ///// sets the tree node type to "branch" or "leaf"

            function walk(node, init, depth) {
                var branch = node[self.branchKey];

                if(branch) {
                    branch.forEach(function(node) {
                        walk(node, init, ++depth);
                    });

                    node['treeNodeType'] = 'branch';
                } else {
                    node['treeNodeType'] = 'leaf';
                }
            }

            walk(this._root, 0);

        }
    },

    /**
        @private
     */
    _selectedIndexes: {
        value: null,
        enumerable: false
    },

   /**
        Description TODO
        @type {Function}
        @default null
    */
   selectedIndexes: {
       get: function() {
           return this._selectedIndexes;
       },
       set: function(value) {
           this._selectedIndexes = value;
       }
   },

    branchControllers: {
        value: [],
        distinct: true
    },

    addBranchController : {
        value: function(controller) {
            if(this.delegate) {
                controller.delegate = this.delegate;
            }

            this.branchControllers.push(controller);
        }
    },

    /**
     @private
     */
    _content: {
        enumerable: false,
        value: null
    },
    /**
        The content managed by the TreeController.
        @type {Function}
        @default {String} null
    */
    content: {
        get: function() {
            return this._content;
        },
        set: function(value) {
            if (this._content === value) {
                return;
            }

            this._content = value;

            this.selectedObjects = null;

            if (this.rootKey) {
                if (value[this.rootKey]) {
                    this.root = value[this.rootKey];
                } else {
                    console.log('No root key found in content data');
                }
            } else {
                this.root = value;
            }

        }
    },

    addObjects : {
        value: function() {

            var objects = Array.prototype.slice.call(arguments),
                i,
                objectCount = objects.length,
                selectedContentIndexes, firstIndex;

            for (i = 0; i < objectCount; i++) {
                this.content.push(objects[i]);
            }

            if (this.selectObjectsOnAddition) {
                selectedContentIndexes = [];
                firstIndex = this.content.length-objectCount;
                for (i = 0; i < objectCount; i++) {
                    selectedContentIndexes[i] = firstIndex++;
                }
                this.selectedContentIndexes = selectedContentIndexes;
                this.selectedObjects = objects;
            }

            if (this.clearFilterFunctionOnAddition) {
                this.filterFunction = null;
            }

            if (this.automaticallyOrganizeObjects) {
                this.organizeObjects();
            }

        }
    }

});