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