1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 | 1×
1×
109×
1×
108×
108×
108×
141×
141×
141×
2×
2×
1×
1×
1×
2×
1×
141×
141×
141×
141×
141×
51×
51×
145×
141×
141×
141×
263×
582×
582×
308×
274×
273×
272×
20×
272×
1×
141×
141×
273×
272×
18×
140×
125×
15×
8×
528×
528×
120×
408×
51×
51×
51×
133×
133×
133×
133×
2×
131×
13×
54×
40×
40×
85×
133×
1×
1×
| var Montage = require("../../core").Montage,
MontageReviver = require("./montage-reviver").MontageReviver,
Promise = require("../../promise").Promise;
var MontageInterpreter = Montage.specialize({
_require: {value: null},
_reviver: {value: null},
init: {
value: function (_require, objectRequires) {
if (typeof _require !== "function") {
throw new Error("Function 'require' missing.");
}
this._reviver = new MontageReviver()
.init(_require, objectRequires);
this._require = _require;
return this;
}
},
instantiate: {
value: function (serialization, objects, element) {
var context;
context = new MontageContext()
.init(serialization, this._reviver, objects, element, this._require);
return context.getObjects();
}
},
preloadModules: {
value: function (serialization) {
var reviver = this._reviver,
moduleLoader = reviver.moduleLoader,
object,
locationId,
locationDesc,
module,
promises = [];
for (var label in serialization) {
object = serialization[label];
locationId = object.prototype || object.object;
Iif (locationId) {
locationDesc = MontageReviver.parseObjectLocationId(locationId);
module = moduleLoader.getModule(
locationDesc.moduleId, label);
if (Promise.is(module)) {
promises.push(module);
}
}
}
Iif (promises.length > 0) {
return Promise.all(promises);
}
}
}
});
var MontageContext = Montage.specialize({
_ELEMENT_ID_ATTRIBUTE: {value: "data-montage-id"},
_unitsToDeserialize: {value: null},
_element: {value: null},
_require: {value: null},
_objects: {value: null},
_userObjects: {value: null},
_serialization: {value: null},
_reviver: {value: null},
constructor: {
value: function () {
this._unitsToDeserialize = [];
}
},
init: {
value: function (serialization, reviver, objects, element, _require) {
this._reviver = reviver;
this._serialization = serialization;
this._objects = Object.create(null);
if (objects) {
this._userObjects = Object.create(null);
for (var label in objects) {
this._userObjects[label] = objects[label];
}
}
this._element = element;
this._require = _require;
return this;
}
},
setObjectLabel: {
value: function(object, label) {
this._objects[label] = object;
}
},
getObject: {
value: function(label) {
var serialization = this._serialization,
reviver = this._reviver,
objects = this._objects,
object;
if (label in objects) {
return objects[label];
} else if (label in serialization) {
object = reviver.reviveRootObject(serialization[label], this, label);
// If no object has been set by the reviver we safe its
// return, it could be a value or a promise, we need to
// make sure the object won't be revived twice.
if (!(label in objects)) {
objects[label] = object;
}
return object;
} else {
return Promise.reject(
new Error("Object with label '" + label + "' was not found.")
);
}
}
},
getObjects: {
value: function() {
var self = this,
serialization = this._serialization,
promises = [],
result;
for (var label in serialization) {
result = this.getObject(label);
if (Promise.is(result)) {
promises.push(result);
}
}
if (promises.length === 0) {
return Promise.resolve(this._invokeDidReviveObjects());
} else {
return Promise.all(promises).then(function() {
return self._invokeDidReviveObjects();
});
}
}
},
hasUserObject: {
value: function(label) {
var userObjects = this._userObjects;
if (userObjects) {
return label in userObjects;
} else {
return false;
}
}
},
getUserObject: {
value: function(label) {
var userObjects = this._userObjects;
Eif (userObjects) {
return userObjects[label];
}
}
},
_invokeDidReviveObjects: {
value: function() {
var self = this,
reviver = this._reviver,
result;
Eif (typeof reviver.didReviveObjects === "function") {
result = reviver.didReviveObjects(this._objects, this);
if (Promise.is(result)) {
return result.then(function() {
return self._objects;
});
}
}
return this._objects;
}
},
hasObject: {
value: function (label) {
return label in this._serialization;
}
},
getRequire: {
value: function () {
return this._require;
}
},
getElement: {
value: function () {
return this._element;
}
},
getElementById: {
value: function (id) {
var selector = '*[' + this._ELEMENT_ID_ATTRIBUTE + '="' + id + '"]';
return this._element.querySelector(selector);
}
},
setUnitsToDeserialize: {
value: function (object, objectDesc, unitNames) {
this._unitsToDeserialize.push({
object: object,
objectDesc: objectDesc,
unitNames: unitNames
});
}
},
getUnitsToDeserialize: {
value: function () {
return this._unitsToDeserialize;
}
}
});
exports.MontageInterpreter = MontageInterpreter;
exports.MontageContext = MontageContext;
|