diff options
author | Valerio Virgillito | 2012-02-01 00:09:27 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-01 00:09:27 -0800 |
commit | 8592cfb89db05f0e52d8c1b8c7046e6f49e3522d (patch) | |
tree | a6f9b5adfe16fd6232708f8a2345cd79056caca9 /node_modules/montage-user/core/serializer.js | |
parent | c6d1a1da9e7cdf477daf32410d80691eb1e828d5 (diff) | |
download | ninja-8592cfb89db05f0e52d8c1b8c7046e6f49e3522d.tar.gz |
Adding a montage framework copy for the user document
Adding a new montage copy containing the latest version of montage to use the new native widgets
Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'node_modules/montage-user/core/serializer.js')
-rwxr-xr-x | node_modules/montage-user/core/serializer.js | 465 |
1 files changed, 465 insertions, 0 deletions
diff --git a/node_modules/montage-user/core/serializer.js b/node_modules/montage-user/core/serializer.js new file mode 100755 index 00000000..449ebf7d --- /dev/null +++ b/node_modules/montage-user/core/serializer.js | |||
@@ -0,0 +1,465 @@ | |||
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 | @module montage/core/serializer | ||
8 | @requires montage | ||
9 | @requires montage/core/uuid | ||
10 | @requires montage/core/deserializer | ||
11 | */ | ||
12 | var Montage = require("montage").Montage; | ||
13 | var Uuid = require("core/uuid").Uuid; | ||
14 | var Deserializer = require("core/deserializer").Deserializer; | ||
15 | var logger = require("core/logger").logger("serializer"); | ||
16 | var Element; | ||
17 | |||
18 | // Shadowing the global with a local allows us to feature-test without typeof | ||
19 | // Element does not exist on the server-side | ||
20 | if (typeof window !== "undefined") { | ||
21 | Element = window.Element; | ||
22 | } | ||
23 | |||
24 | /** | ||
25 | @class module:montage/core/serializer.Serializer | ||
26 | @classdesc Serialized objects are indexed by uuid. | ||
27 | @extends module:montage/core/core.Montage | ||
28 | */ | ||
29 | var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Serializer# */ { | ||
30 | _MONTAGE_ID_ATTRIBUTE: {value: "data-montage-id"}, | ||
31 | _serializedObjects: {value: {}}, // uuid -> string | ||
32 | _serializedReferences: {value: {}}, // uuid -> string | ||
33 | _externalObjects: {value: null}, // label -> object | ||
34 | _externalElements: {value: null}, | ||
35 | _objectStack: {value: null}, | ||
36 | _objectReferences: {value: null}, // uuid -> object with properies | ||
37 | // last used index of an objectName to create a label | ||
38 | _objectNamesIndex: {value: null}, | ||
39 | _objectLabels: {value: null}, // uuid -> label | ||
40 | _serializationUnits: {value: []}, | ||
41 | |||
42 | serializeNullValues: {value: false}, | ||
43 | |||
44 | /** | ||
45 | Defines a serialization unit for an object. | ||
46 | @function | ||
47 | @param {string} name The unit name. | ||
48 | @param {function} funktion The delegate function that creates the serialization unit. This function accepts the object being serialized as an argument and should return an object to be be JSON'd. | ||
49 | */ | ||
50 | defineSerializationUnit: {value: function(name, funktion) { | ||
51 | this._serializationUnits.push({ | ||
52 | name: name, | ||
53 | funktion: funktion | ||
54 | }); | ||
55 | }}, | ||
56 | |||
57 | /** | ||
58 | Defines the require function to be used. | ||
59 | @function | ||
60 | @param {function} require The require function to be used to identify module ids of the objects being serialized. | ||
61 | */ | ||
62 | initWithRequire: { | ||
63 | value: function(require) { | ||
64 | this._require = require; | ||
65 | return this; | ||
66 | } | ||
67 | }, | ||
68 | |||
69 | /** | ||
70 | Serializes a single object. | ||
71 | @function | ||
72 | @param {object} object The object to be serialized. | ||
73 | @returns {string} The serialized object. | ||
74 | */ | ||
75 | serializeObject: { | ||
76 | value: function(object) { | ||
77 | return this.serialize({root: object}); | ||
78 | } | ||
79 | }, | ||
80 | |||
81 | /** | ||
82 | Serialize several objects under specific labels. | ||
83 | @function | ||
84 | @param {object} objects A label->object mapping of the objects to be serialized. | ||
85 | @returns {string} The serialized objects. | ||
86 | */ | ||
87 | serialize: { | ||
88 | value: function(objects) { | ||
89 | var serialization, | ||
90 | valueSerialization, | ||
91 | label; | ||
92 | |||
93 | this._serializedObjects = {}; | ||
94 | this._serializedReferences = {}; | ||
95 | this._externalObjects = {}; | ||
96 | this._externalElements = []; | ||
97 | this._objectNamesIndex = {}; | ||
98 | this._objectLabels = {}; | ||
99 | this._objectReferences = {}; | ||
100 | |||
101 | for (label in objects) { | ||
102 | this._objectLabels[objects[label].uuid] = label; | ||
103 | } | ||
104 | |||
105 | for (label in objects) { | ||
106 | valueSerialization = this._serializeValue(objects[label], null, 2); | ||
107 | // objects are automatically inserted as top level objects after calling _serializeValue, but native objects have to be manually inserted them. | ||
108 | if (!(label in this._serializedObjects)) { | ||
109 | this._serializedObjects[label] = {value: valueSerialization}; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | serialization = this._getSerialization(this._serializedObjects); | ||
114 | //console.log(serialization); | ||
115 | // save the require used for this serialization | ||
116 | this._require = require; | ||
117 | this._serialization = serialization; | ||
118 | return serialization; | ||
119 | } | ||
120 | }, | ||
121 | |||
122 | /** | ||
123 | This function is to be used in the context of serializeSelf delegate used for custom object serializations. | ||
124 | It adds an entry to the "properties" serialization unit of the object being serialized. | ||
125 | @function | ||
126 | @param {string} name The name of the entry to be added. | ||
127 | @param {string} value The value to be serialized. | ||
128 | */ | ||
129 | set: {value: function(name, value) { | ||
130 | var stack = this._objectStack; | ||
131 | |||
132 | return (stack[stack.length - 1][name] = value); | ||
133 | }}, | ||
134 | |||
135 | /** | ||
136 | This function is to be used in the context of serializeSelf delegate used for custom object serializations. | ||
137 | It adds an entry to the "properties" serialization unit of the object being serialized. The value for this entry will be stored as a reference only and not the value itself. | ||
138 | @function | ||
139 | @param {string} name The name of the entry to be added. | ||
140 | @param {string} value The value to be referenced. | ||
141 | */ | ||
142 | setReference: {value: function(name, value) { | ||
143 | var stack = this._objectStack, | ||
144 | stackElement = stack[stack.length - 1], | ||
145 | objectReferences = this._objectReferences, | ||
146 | uuid = stackElement.uuid; | ||
147 | |||
148 | if (!(uuid in objectReferences)) { | ||
149 | objectReferences[uuid] = {}; | ||
150 | objectReferences[uuid][name] = true; | ||
151 | } | ||
152 | |||
153 | return (stackElement[name] = value); | ||
154 | }}, | ||
155 | |||
156 | /** | ||
157 | This function is to be used in the context of serializeSelf delegate used for custom object serializations. | ||
158 | It serializes all properties specified as part of the "properties" serialization unit. | ||
159 | @function | ||
160 | @param {array} propertyNames The array with the property names to be serialized. | ||
161 | */ | ||
162 | setProperties: {value: function(propertyNames) { | ||
163 | var ix = this._objectStack.length - 2, | ||
164 | object = this._objectStack[ix]; | ||
165 | |||
166 | for (var i = 0, l = propertyNames.length; i < l; i++) { | ||
167 | var propertyName = propertyNames[i]; | ||
168 | if (Montage.getPropertyAttribute(object, propertyName, "serializable") === "reference") { | ||
169 | this.setReference(propertyName, object[propertyName]); | ||
170 | } else { | ||
171 | this.set(propertyName, object[propertyName]); | ||
172 | } | ||
173 | } | ||
174 | }}, | ||
175 | |||
176 | /** | ||
177 | This function is to be used in the context of serializeSelf delegate used for custom object serializations. | ||
178 | It adds an object to be serialized into the current serialization. | ||
179 | @function | ||
180 | @param {object} object The object to be serialized. | ||
181 | */ | ||
182 | addObject: {value: function(object) { | ||
183 | var valueSerialization = this._serializeValue(object, null, 2); | ||
184 | var label = this._getObjectLabel(object); | ||
185 | // objects are automatically inserted as top level objects after calling _serializeValue, but native objects have to be manually inserted them. | ||
186 | if (!(label in this._serializedObjects)) { | ||
187 | this._serializedObjects[label] = {value: valueSerialization}; | ||
188 | } | ||
189 | }}, | ||
190 | |||
191 | /** | ||
192 | @private | ||
193 | */ | ||
194 | _pushContextObject: {value: function(object) { | ||
195 | if (this._objectStack === null) { | ||
196 | this._objectStack = [object]; | ||
197 | } else { | ||
198 | this._objectStack.push(object); | ||
199 | } | ||
200 | }}, | ||
201 | |||
202 | /** | ||
203 | @private | ||
204 | */ | ||
205 | _popContextObject: {value: function() { | ||
206 | return this._objectStack.pop(); | ||
207 | }}, | ||
208 | |||
209 | /** | ||
210 | Returns a dictionary of the external objects that were referenced in the last serialization. | ||
211 | @function | ||
212 | @returns {object} The dictionary of external objects {label: object} | ||
213 | */ | ||
214 | getExternalObjects: {value: function() { | ||
215 | var externalObjects = this._externalObjects; | ||
216 | |||
217 | for (var label in externalObjects) { | ||
218 |