aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/objects-controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/controllers/objects-controller.js')
-rw-r--r--js/controllers/objects-controller.js231
1 files changed, 231 insertions, 0 deletions
diff --git a/js/controllers/objects-controller.js b/js/controllers/objects-controller.js
new file mode 100644
index 00000000..877f7f9f
--- /dev/null
+++ b/js/controllers/objects-controller.js
@@ -0,0 +1,231 @@
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
7var Montage = require("montage/core/core").Montage,
8 Component = require("montage/ui/component").Component;
9
10var CATEGORIES = {
11
12};
13
14var objectsController = exports.ObjectsController = Montage.create(Component, {
15
16 _currentDocument : {
17 value : null,
18 enumerable : false
19 },
20 currentDocument : {
21 get : function() {
22 return this._currentDocument;
23 },
24 set : function(doc) {
25 if(!doc) { return false; }
26
27 // TODO: remove setTimeout when timing of montage initialization is done
28 setTimeout(function() {
29 this.bindToModelObjects();
30 }.bind(this), 1000);
31
32 this._currentDocument = doc;
33 },
34 enumerable : false
35 },
36
37 objects : {
38 value: []
39 },
40
41 _isBoundToModelObjects : {
42 value: null
43 },
44 bindToModelObjects : {
45 value: function() {
46 //// Remove any previous bindings if previously bound
47 if(!this._isBoundToModelObjects) {
48 Object.deleteBinding(this, 'objects');
49 this._isBoundToModelObjects = true;
50 }
51
52// Object.defineBinding(this, 'objects', {
53// boundObject: this.currentDocument.model,
54// boundObjectPropertyPath: 'mObjects',
55// oneway: false
56// });
57 }
58 },
59
60 /* --------------------------
61 Binding Methods
62 ----------------------------- */
63
64 addBinding : {
65 value: function(bindingArgs) {
66 if(!bindingArgs.sourceObject || !bindingArgs.sourceObjectPropertyPath || !bindingArgs) { return; }
67
68 Object.defineBinding(bindingArgs.sourceObject, bindingArgs.sourceObjectPropertyPath, bindingArgs);
69 this.currentObjectBindings = this.getObjectBindings(bindingArgs.sourceObject);
70 }
71 },
72
73 removeBinding : {
74 value: function(bindingArgs) {
75 if(!bindingArgs) { return; }
76
77
78
79 Object.deleteBinding(bindingArgs.sourceObject, bindingArgs.sourceObjectPropertyPath);
80 this.currentObjectBindings = this.getObjectBindings(bindingArgs.sourceObject);
81 }
82 },
83
84 editBinding : {
85 value: function(bindingArgs, newProperties) {
86 var property;
87
88 this.removeBinding(bindingArgs);
89
90 if(newProperties) {
91 for(property in newProperties) {
92 bindingArgs[property] = newProperties[property];
93 }
94 }
95
96 this.addBinding(bindingArgs);
97
98 }
99 },
100
101 getObjectBindings : {
102 value: function(object) {
103 var descriptors = object._bindingDescriptors,
104 bindingsArray = [],
105 property, descriptor, bindingArgsObject;
106
107 if(descriptors) {
108 for(property in descriptors) {
109 if(descriptors.hasOwnProperty(property)) {
110 descriptor = descriptors[property];
111
112 bindingArgsObject = {
113 sourceObject : object,
114 sourceObjectPropertyPath : property,
115 boundObject : descriptor.boundObject,
116 boundObjectPropertyPath : descriptor.boundObjectPropertyPath,
117 oneway : descriptor.oneway
118 };
119
120 bindingsArray.push(bindingArgsObject);
121 }
122 }
123 }
124
125 return bindingsArray;
126 }
127 },
128
129 /* ---- Get Bindable Properties ---- */
130
131 getPropertyList : {
132 value: function(object, excludeUnderscoreProperties) {
133 return this.getPrototypes(object).map(function(proto) {
134
135 var metadata = proto._montage_metadata,
136 objectName = (metadata) ? metadata.objectName : "Object";
137
138 return {
139 category : objectName,
140 properties : this.getPropertiesFromObject(proto)
141 };
142 }, this);
143
144 }
145 },
146
147 getPropertiesFromObject : {
148 value: function (object, excludeUnderscoreProperties) {
149 var properties = [];
150
151 for(var key in object) {
152 if(object.hasOwnProperty(key)) {
153 properties.push(key);
154 }
155 }
156
157 if(excludeUnderscoreProperties) {
158 properties = properties.filter(function(property) {
159 return property[0] !== '_';
160 }, this);
161 }
162
163 return properties.sort();
164 }
165 },
166
167 getPrototypes : {
168 value: function(object) {
169 var object_i = object,
170 prototypes = [object_i];
171
172 ///// Collect prototypes
173 while(Object.getPrototypeOf(object_i)) {
174 object_i = Object.getPrototypeOf(object_i);
175 prototypes.push(object_i);
176 }
177
178 return prototypes;
179 }
180 },
181
182 /* ----- Category properties ----- */
183
184 getObjectCategory : {
185 value: function(object) {
186 if(this._hasPrototype(object, 'Component')) {
187 return 'Component';
188 }
189
190 return null;
191 }
192 },
193
194 _hasPrototype : {
195 value: function(object, prototypeName) {
196 var prototypes = this.getPrototypes(object).map(function(proto) {
197 var metadata = proto._montage_metadata;
198 return (metadata) ? metadata.objectName : "Object";
199 });
200
201 return prototypes.indexOf(prototypeName) !== -1;
202 }
203 },
204
205 /* ---- Bindable controller properties ---- */
206
207 currentObjectBindings : {
208 value: null
209 },
210 _currentObject : {
211 value: null
212 },
213 currentObject : {
214 get: function() {
215 return this._currentObject;
216 },
217 set: function(value) {
218 //if(value === this._currentObject) { return; }
219
220 if(value) {
221 this.currentObjectBindings = this.getObjectBindings(value);
222 console.log("Property list", this.getPropertyList(value, true));
223 } else {
224 this.currentObjectBindings = [];
225 }
226
227 this._currentObject = value;
228 }
229 }
230
231}); \ No newline at end of file