diff options
Diffstat (limited to 'node_modules/montage/ui/template.js')
-rwxr-xr-x | node_modules/montage/ui/template.js | 313 |
1 files changed, 251 insertions, 62 deletions
diff --git a/node_modules/montage/ui/template.js b/node_modules/montage/ui/template.js index 210d8cb7..ee2cc1be 100755 --- a/node_modules/montage/ui/template.js +++ b/node_modules/montage/ui/template.js | |||
@@ -37,7 +37,16 @@ var Template = exports.Template = Montage.create(Montage, /** @lends module:mont | |||
37 | /** | 37 | /** |
38 | @private | 38 | @private |
39 | */ | 39 | */ |
40 | _document: {value: null}, | 40 | _document: { |
41 | enumerable: false, | ||
42 | value: null | ||
43 | }, | ||
44 | |||
45 | document: { | ||
46 | get: function() { | ||
47 | return this._document; | ||
48 | } | ||
49 | }, | ||
41 | /** | 50 | /** |
42 | @private | 51 | @private |
43 | */ | 52 | */ |
@@ -134,6 +143,17 @@ var Template = exports.Template = Montage.create(Montage, /** @lends module:mont | |||
134 | } | 143 | } |
135 | }, | 144 | }, |
136 | 145 | ||
146 | initWithHtmlString: { | ||
147 | value: function(htmlString) { | ||
148 | var doc = this.createHtmlDocumentFromString(htmlString); | ||
149 | |||
150 | this._isLoaded = true; | ||
151 | this.initWithDocument(doc); | ||
152 | |||
153 | return this; | ||
154 | } | ||
155 | }, | ||
156 | |||
137 | /** | 157 | /** |
138 | Initializes the Template object with a specific module id that represents an HTML page. | 158 | Initializes the Template object with a specific module id that represents an HTML page. |
139 | @function | 159 | @function |
@@ -234,63 +254,62 @@ var Template = exports.Template = Montage.create(Montage, /** @lends module:mont | |||
234 | } | 254 | } |
235 | }, | 255 | }, |
236 | 256 | ||
237 | /** | 257 | _deserialize: { |
238 | Instantiates the Template by specifying an object as the owner and a document where the elements referenced in the serialization should be found. | 258 | value: function(instances, targetDocument, callback) { |
239 | @function | ||
240 | @param {Object} rootObject The owner object of the template. | ||
241 | @param {HTMLDocument} document The HTML document to be used to find elements referenced from the serialization. | ||
242 | @param {Function} callback The callback function to invoke when the template is instantiated. | ||
243 | */ | ||
244 | instantiateWithOwnerAndDocument: { | ||
245 | value: function(owner, document, callback) { | ||
246 | var self = this, | 259 | var self = this, |
247 | defaultApplication = applicationExports.application; | 260 | defaultApplication = applicationExports.application; |
248 | this.getDeserializer(function(deserializer) { | ||
249 | function invokeTemplateDidLoad(objects) { | ||
250 | owner = objects.owner; | ||
251 | self._invokeTemplateDidLoadWithOwner(deserializer, owner); | ||
252 | self.waitForStyles(function() { | ||
253 | callback(owner); | ||
254 | }); | ||
255 | } | ||
256 | 261 | ||
257 | var externalObjects, | 262 | this.getDeserializer(function(deserializer) { |
258 | targetDocument, | 263 | var externalObjects; |
259 | instances; | ||
260 | 264 | ||
261 | if (deserializer) { | 265 | if (deserializer) { |
262 | externalObjects = self._externalObjects; | 266 | externalObjects = self._externalObjects; |
263 | if (externalObjects) { | 267 | if (externalObjects) { |
264 | instances = Object.create(externalObjects); | 268 | for (var label in externalObjects) { |
265 | instances.owner = owner; | 269 | if (!(label in instances)) { |
266 | instances.application = defaultApplication; | 270 | instances[label] = externalObjects[label]; |
267 | } else { | 271 | } |
268 | instances = {owner: owner, application: defaultApplication}; | 272 | } |
269 | } | 273 | } |
270 | 274 | ||
271 | if (owner && owner._element) { | 275 | instances.application = defaultApplication; |
272 | targetDocument = owner._element.ownerDocument; | 276 | instances.template = self; |
273 | } | ||
274 | 277 | ||
275 | if (self._document === window.document) { | 278 | if (self._document === window.document) { |
276 | deserializer.deserializeWithInstancesAndDocument(instances, self._document, invokeTemplateDidLoad); | 279 | deserializer.deserializeWithInstancesAndDocument(instances, self._document, callback); |
277 | } else { | 280 | } else { |
278 | self.setupDocument(window.document); | 281 | deserializer.deserializeWithInstancesAndElementForDocument(instances, self._document.body, targetDocument, callback); |
279 | deserializer.deserializeWithInstancesAndElementForDocument(instances, self._document.body, targetDocument, invokeTemplateDidLoad); | ||
280 | } | 282 | } |
281 | } else { | 283 | } else { |
282 | if (self._document !== window.document) { | 284 | callback(); |
283 | self.setupDocument(window.document); | ||
284 | } | ||
285 | self.waitForStyles(function() { | ||
286 | callback(); | ||
287 | }); | ||
288 | } | 285 | } |
289 | }); | 286 | }); |
290 | } | 287 | } |
291 | }, | 288 | }, |
292 | 289 | ||
293 | /** | 290 | /** |
291 | Instantiates the Template by specifying an object as the owner and a document where the elements referenced in the serialization should be found. | ||
292 | @function | ||
293 | @param {Object} rootObject The owner object of the template. | ||
294 | @param {HTMLDocument} document The HTML document to be used to find elements referenced from the serialization. | ||
295 | @param {Function} callback The callback function to invoke when the template is instantiated. | ||
296 | */ | ||
297 | instantiateWithOwnerAndDocument: { | ||
298 | value: function(owner, targetDocument, callback) { | ||
299 | var self = this; | ||
300 | |||
301 | this._partiallyInstantiateWithInstancesForDocument({owner: owner}, targetDocument, function(objects) { | ||
302 | if (objects) { | ||
303 | self._invokeTemplateDidLoad(objects); | ||
304 | } | ||
305 | self.waitForStyles(function() { | ||
306 | callback(objects ? objects.owner : null); | ||
307 | }); | ||
308 | }); | ||
309 | } | ||
310 | }, | ||
311 | |||
312 | /** | ||
294 | Instantiates the Template by using a component as the owner. | 313 | Instantiates the Template by using a component as the owner. |
295 | All elements refereced in the serialization will be found on the document the component is attached to. | 314 | All elements refereced in the serialization will be found on the document the component is attached to. |
296 | @function | 315 | @function |
@@ -303,32 +322,86 @@ var Template = exports.Template = Montage.create(Montage, /** @lends module:mont | |||
303 | this.instantiateWithOwnerAndDocument(component, document, callback); | 322 | this.instantiateWithOwnerAndDocument(component, document, callback); |
304 | }}, | 323 | }}, |
305 | 324 | ||
325 | instantiateWithDocument: { | ||
326 | value: function(document, callback) { | ||
327 | return this.instantiateWithOwnerAndDocument(null, document, callback); | ||
328 | } | ||
329 | }, | ||
330 | |||
331 | _partiallyInstantiateWithInstancesForDocument: { | ||
332 | value: function(instances, targetDocument, callback) { | ||
333 | var self = this, | ||
334 | owner = instances.owner; | ||
335 | |||
336 | if (!targetDocument && owner && owner._element) { | ||
337 | targetDocument = owner._element.ownerDocument; | ||
338 | } | ||
339 | |||
340 | function importHeaders(objects) { | ||
341 | if (self._document !== targetDocument) { | ||
342 | self.exportHeaders(targetDocument); | ||
343 | } | ||
344 | callback(objects); | ||
345 | } | ||
346 | |||
347 | this._deserialize(instances, targetDocument, function(objects, element) { | ||
348 | if (self._extends && !self._isExpanded) { | ||
349 | var _extends = self._extends, | ||
350 | element = _extends.element, | ||
351 | instances = _extends.instances, | ||
352 | instancesMapping = _extends.instancesMapping, | ||
353 | elementId = _extends.elementId; | ||
354 | |||
355 | if (!element && elementId) { | ||
356 | element = element.querySelector("*[data-montage-id='" + elementId + "']"); | ||
357 | } | ||
358 | |||
359 | if (!instances) { | ||
360 | if (instancesMapping) { | ||
361 | instances = {}; | ||
362 | for (var label in instancesMapping) { | ||
363 | instances[label] = objects[instancesMapping[label]]; | ||
364 | } | ||
365 | instances.owner = objects.owner; | ||
366 | } else { | ||
367 | instances = {owner: objects.owner}; | ||
368 | } | ||
369 | } | ||
370 | self._extendsTemplateWithInstances(_extends.templateModuleId, element, instances, function(extendsObjects) { | ||
371 | var labels = Object.keys(extendsObjects); | ||
372 | |||
373 | for (var i =0, label; (label = labels[i]); i++) { | ||
374 | objects[label] = extendsObjects[label]; | ||
375 | } | ||
376 | importHeaders(objects); | ||
377 | }); | ||
378 | } else { | ||
379 | importHeaders(objects); | ||
380 | } | ||
381 | }); | ||
382 | } | ||
383 | }, | ||
384 | |||
306 | /** | 385 | /** |
307 | Instantiates the Template with no elements references. | 386 | Instantiates the Template with no elements references. |
308 | @function | 387 | @function |
309 | */ | 388 | */ |
310 | instantiate: {value: function() { | 389 | instantiate: { |
311 | var self = this; | 390 | value: function(callback) { |
312 | var deserializer = Deserializer.create(); | 391 | return this.instantiateWithOwnerAndDocument(null, null, callback); |
313 | |||
314 | function invokeTemplateDidLoad(o |