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