diff options
Diffstat (limited to 'node_modules/montage/core/deserializer.js')
-rwxr-xr-x | node_modules/montage/core/deserializer.js | 783 |
1 files changed, 783 insertions, 0 deletions
diff --git a/node_modules/montage/core/deserializer.js b/node_modules/montage/core/deserializer.js new file mode 100755 index 00000000..8f1f0831 --- /dev/null +++ b/node_modules/montage/core/deserializer.js | |||
@@ -0,0 +1,783 @@ | |||
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 | /** | ||
8 | @module montage/core/deserializer | ||
9 | @requires montage/core/core | ||
10 | @requires montage/core/core/logger | ||
11 | @requires montage/core/promise | ||
12 | */ | ||
13 | |||
14 | var Montage = require("montage").Montage, | ||
15 | logger = require("core/logger").logger("deserializer"), | ||
16 | Promise = require("core/promise").Promise; | ||
17 | |||
18 | /** | ||
19 | @class module:montage/core/deserializer.Deserializer | ||
20 | @extends module:montage/core/core.Montage | ||
21 | */ | ||
22 | var Deserializer = Montage.create(Montage, /** @lends module:montage/core/deserializer.Deserializer# */ { | ||
23 | _objects: {value: null}, | ||
24 | /** | ||
25 | @private | ||
26 | */ | ||
27 | _objectStack: {value: []}, | ||
28 | _modules: {value: {}}, | ||
29 | /** | ||
30 | @private | ||
31 | */ | ||
32 | _requiredModuleIds: {value: null}, | ||
33 | _objectLabels: {value: null}, | ||
34 | /** | ||
35 | @private | ||
36 | */ | ||
37 | _serializationString: {value: null, enumerable: false}, | ||
38 | /** | ||
39 | @private | ||
40 | */ | ||
41 | _serialization: {value: null, enumerable: false}, | ||
42 | /** | ||
43 | @private | ||
44 | */ | ||
45 | _parseFunction: {value: null, enumerable: false}, | ||
46 | /** | ||
47 | @private | ||
48 | */ | ||
49 | _deserializationUnits: {value: []}, | ||
50 | // name -> function | ||
51 | /** | ||
52 | @private | ||
53 | */ | ||
54 | _indexedDeserializationUnits: {value: {}}, | ||
55 | |||
56 | __sharedDocument: { | ||
57 | value: null | ||
58 | }, | ||
59 | |||
60 | _sharedDocument: { | ||
61 | get: function() { | ||
62 | return this.__cachedDoc ? this.__cachedDoc : (this.__cachedDoc = window.document.implementation.createHTMLDocument("")); | ||
63 | } | ||
64 | }, | ||
65 | /** | ||
66 | @private | ||
67 | */ | ||
68 | _reset: {value: function() { | ||
69 | this._serializationString = null; | ||
70 | this._requiredModuleIds = null; | ||
71 | this._parseFunction = null; | ||
72 | this._serialization = null; | ||
73 | this._compiledDeserializationFunction = null; | ||
74 | this._compiledDeserializationFunctionString = null; | ||
75 | this._origin = null; | ||
76 | }}, | ||
77 | |||
78 | /** | ||
79 | Initializes the deserializer with a string of serialized objects. | ||
80 | @function | ||
81 | @param {String} string A string of serialized objects. | ||
82 | @param {String} origin The origin of the serialization, usually a filename. | ||
83 | @returns itself | ||
84 | */ | ||
85 | initWithString: {value: function(string, origin) { | ||
86 | this._reset(); | ||
87 | this._serializationString = string; | ||
88 | this._origin = origin; | ||
89 | return this; | ||
90 | }}, | ||
91 | /** | ||
92 | Initializes the deserializer object with an object representing a serialization. Since the serialization is a JSON string it is also possible to represent it in a JavaScript object. | ||
93 | @function | ||
94 | @param {object} object The serialization in JavaScript object form. | ||
95 | @returns itself | ||
96 | */ | ||
97 | initWithObject: {value: function(object) { | ||
98 | this._reset(); | ||
99 | this._serializationString = JSON.stringify(object); | ||
100 | return this; | ||
101 | }}, | ||
102 | |||
103 | /** | ||
104 | Initializes the deserializer object with a serialization string and the require object used to load the modules containing the object's prototypes. | ||
105 | @function | ||
106 | @param {string} string The serialization string. | ||
107 | @param {function} require The require function to load the modules. | ||
108 | @param {string} origin The origin of the serialization, usually a filename. | ||
109 | @returns itself | ||
110 | */ | ||
111 | initWithStringAndRequire: {value: function(string, require, origin) { | ||
112 | this._reset(); | ||
113 | this._serializationString = string; | ||
114 | this._require = require; | ||
115 | this._origin = origin; | ||
116 | return this; | ||
117 | }}, | ||
118 | |||
119 | /** | ||
120 | Defines a deserialization unit for an object. | ||
121 | @function | ||
122 | @param {string} name The unit name. | ||
123 | @param {function} funktion The delegate function that reads the serialization unit and deserializes its content into the object being deserialized. This function accepts the object being deserialized and the serialized unit as arguments. | ||
124 | */ | ||
125 | defineDeserializationUnit: {value: function(name, funktion) { | ||
126 | this._deserializationUnits.push({ | ||
127 | name: name, | ||
128 | funktion: this._indexedDeserializationUnits[name] = funktion | ||
129 | }); | ||
130 | }}, | ||
131 | |||
132 | /** | ||
133 | Returns an array with all the objects that were created or used during the call to deserializeWith* functions. | ||
134 | @function | ||
135 | @returns {Array} The array of objects. | ||
136 | */ | ||
137 | getObjectsFromLastDeserialization: {value: function() { | ||
138 | var objects = this._objects; | ||
139 | var objectsArray = []; | ||
140 | |||
141 | for (var key in objects) { | ||
142 | if (objects.hasOwnProperty(key)) { | ||
143 | objectsArray.push(objects[key]); | ||
144 | } | ||
145 | } | ||
146 | |||
147 | return objectsArray; | ||
148 | }}, | ||
149 | /** | ||
150 | This function is to be used in the context of deserializeSelf delegate used for custom object deserializations. | ||
151 | It reads an entry from the "properties" serialization unit of the object being deserialized. | ||
152 | @function | ||
153 | @param {string} name The name of the entry to be read. | ||
154 | @returns {*} The value of the entry | ||
155 | */ | ||
156 | get: {value: function(name) { | ||
157 | var stack = this._objectStack; | ||
158 | var ix = stack.length - 1; | ||
159 | |||
160 | return stack[ix][name]; | ||
161 | }}, | ||
162 | |||
163 | /** | ||
164 | This function is to be used in the context of deserializeSelf delegate used for custom object deserializations. | ||
165 | It deserializes all the named properties of a serialized object into the object given. | ||
166 | @function | ||
167 | @param {Object} object The target of the properties. | ||
168 | @param {Array} properties The property names to be deserialized. | ||
169 | */ | ||
170 | deserializePropertiesForObject: {value: function(object, properties) { | ||
171 | // TODO: ensure backward compatibility | ||
172 | if (properties && "childComponents" in properties) { | ||
173 | properties.childComponents = []; | ||
174 | console.log('Warning: "childComponents" isn\'t supported on components within the current serializaation format, this property will be reset to [].'); | ||
175 | } | ||
176 | for (var key in properties) { | ||
177 | object[key] = properties[key]; | ||
178 | } | ||
179 | }}, | ||
180 | |||
181 | /** | ||
182 | The function works on the top most object of an object stack.<br> | ||
183 | This method pushes an object to that stack making it the target of future calls. | ||
184 | @function | ||
185 | @param {object} object The object to push into the stack. | ||
186 | @private | ||
187 | */ | ||
188 | _pushContextObject: {value: function(object) { | ||
189 | this._objectStack.push(object); | ||
190 | }}, | ||
191 | |||
192 | /** | ||
193 | The function works on the top most object of an object stack.<br> | ||
194 | This method pops the top most object out of that stack making future calls to target the next object in the stack. | ||
195 | @function | ||
196 | @returns {object} The top most object in the stack. | ||
197 | @private | ||
198 | */ | ||
199 | _popContextObject: {value: function() { | ||
200 | return this._objectStack.pop(); | ||
201 | }}, | ||
202 | /** | ||
203 | @private | ||
204 | */ | ||
205 | _require: { | ||
206 | enumerable: false, | ||
207 | value: null | ||
208 | }, | ||
209 | /** | ||
210 | @private | ||
211 | */ | ||
212 | _defaultModuleLoader: { | ||
213 | enumerable: false, | ||
214 | value: function(moduleIds, callback) { | ||
215 | if (typeof require !== "function") { | ||
216 | logger.error("Deserializer: The default module loader needs the global require function to be defined."); | ||
217 | return; | ||
218 | } | ||
219 | |||
220 | var modulesLoaded = 0, | ||
221 | modules = {}, | ||
222 | _require = this._require; | ||
223 | |||
224 | moduleIds.forEach(function(moduleId) { | ||
225 | if (callback) { | ||
226 | Promise.when(_require.async(moduleId), | ||
227 | function(module) { | ||
228 | modules[moduleId] = module; | ||
229 | if (++modulesLoaded === moduleIds.length) { | ||
230 | callback(modules); | ||
231 | } |