aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tools/template/template-creator.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/tools/template/template-creator.js')
-rwxr-xr-xnode_modules/tools/template/template-creator.js123
1 files changed, 123 insertions, 0 deletions
diff --git a/node_modules/tools/template/template-creator.js b/node_modules/tools/template/template-creator.js
new file mode 100755
index 00000000..c5d3cffd
--- /dev/null
+++ b/node_modules/tools/template/template-creator.js
@@ -0,0 +1,123 @@
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/tools/template/template-creator
8 @requires montage/ui/template
9*/
10
11exports = typeof exports !== "undefined" ? exports : {};
12
13var Montage = require("montage/core/core").Montage;
14var Template = require("montage/ui/template").Template;
15
16/**
17 @class module:montage/tools/template/template-creator.TemplateCreator
18 @extends module:montage/ui/template.Template
19*/
20var TemplateCreator = exports.TemplateCreator = Montage.create(Template, /** @lends module:montage/tools/template/template-creator.TemplateCreator# */ {
21 initWithDocument: {
22 value: function(doc, montageJsPath) {
23 return this.initWithHeadAndBodyElements(doc.head, doc.body, montageJsPath);
24 }
25 },
26
27 initWithBodyElement: {
28 value: function(body, montageJsPath) {
29 return this.initWithHeadAndBodyElements(null, body, montageJsPath);
30 }
31 },
32
33 initWithHeadAndBodyElements: {
34 value: function(head, body, montageJsPath) {
35 var serializer = this.serializer,
36 objects = {},
37 components = {},
38 componentsChildComponents = {},
39 doc,
40 script,
41 self = this;
42
43 this._componentNamesIndex = {};
44 this._objectNamesIndex = {};
45 doc = this._document = document.implementation.createHTMLDocument("");
46
47 function copyNode(sourceNode, targetNode, isRootNode) {
48 var childNodes = sourceNode.childNodes,
49 childNode,
50 targetChildNode,
51 label,
52 script,
53 component = isRootNode ? null : sourceNode.controller;
54
55 if (component) {
56 label = self._generateLabelForComponent(component, Object.keys(components));
57 component._element = targetNode;
58 components[label] = component;
59 componentsChildComponents[label] = component.childComponents;
60 delete component.childComponents;
61 } else {
62 for (var i = 0; (childNode = childNodes[i]); i++) {
63 targetChildNode = targetNode.appendChild(childNode.cloneNode(false));
64 copyNode(childNode, targetChildNode);
65 }
66 }
67 }
68
69 if (head) {
70 doc.head.innerHTML = head.innerHTML;
71 }
72 if (montageJsPath) {
73 script = doc.createElement("script");
74 script.setAttribute("src", montageJsPath);
75 doc.head.appendChild(script);
76 doc.head.insertBefore(doc.createTextNode("\n "), script);
77 }
78
79 // try to make things look nice...
80 var html = doc.documentElement;
81 html.insertBefore(doc.createTextNode("\n"), doc.head);
82 html.insertBefore(doc.createTextNode("\n"), doc.body);
83 html.appendChild(doc.createTextNode("\n"));
84 if (!head) {
85 // the first child is the title
86 doc.head.insertBefore(doc.createTextNode("\n "), doc.head.firstChild);
87 }
88
89 copyNode(body, this._document.body, true);
90 this._ownerSerialization = serializer.serialize(components);
91 for (var label in components) {
92 components[label].childComponents = componentsChildComponents[label];
93 }
94 components = componentsChildComponents = null;
95 this._externalObjects = serializer.getExternalObjects();
96
97 return this;
98 }
99 },
100
101 _componentNamesIndex: {
102 value: null
103 },
104
105 _generateLabelForComponent: {value: function(component, labels) {
106 var componentInfo = Montage.getInfoForObject(component),
107 componentLabel = componentInfo.label,
108 componentName,
109 index;
110
111 if (componentLabel) {
112 return componentLabel;
113 } else {
114 componentName = componentInfo.objectName.toLowerCase();
115 do {
116 index = this._componentNamesIndex[componentName] || 1;
117 this._componentNamesIndex[componentName] = index + 1;
118 } while (labels.indexOf(componentName+index) >= 0);
119
120 return componentName + index;
121 }
122 }},
123}); \ No newline at end of file