aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--node_modules/tools/template-creator.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/node_modules/tools/template-creator.js b/node_modules/tools/template-creator.js
new file mode 100644
index 00000000..6b50cc1e
--- /dev/null
+++ b/node_modules/tools/template-creator.js
@@ -0,0 +1,113 @@
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 doc,
39 script,
40 self = this;
41
42 this._objectNamesIndex = {};
43 doc = this._document = document.implementation.createHTMLDocument("");
44
45 function copyNode(sourceNode, targetNode, isRootNode) {
46 var childNodes = sourceNode.childNodes,
47 childNode,
48 targetChildNode,
49 label,
50 script,
51 component = isRootNode ? null : sourceNode.controller;
52
53 if (component) {
54 label = self._generateLabelForComponent(component);
55 targetNode.setAttribute("id", label);
56 component._element = targetNode;
57 components[label] = component;
58 } else {
59 for (var i = 0; (childNode = childNodes[i]); i++) {
60 targetChildNode = targetNode.appendChild(childNode.cloneNode(false));
61 copyNode(childNode, targetChildNode);
62 }
63 }
64 }
65
66 if (head) {
67 doc.head.innerHTML = head.innerHTML;
68 }
69 if (montageJsPath) {
70 script = doc.createElement("script");
71 script.setAttribute("src", montageJsPath);
72 doc.head.appendChild(script);
73 doc.head.insertBefore(doc.createTextNode("\n "), script);
74 }
75
76 // try to make things look nice...
77 var html = doc.documentElement;
78 html.insertBefore(doc.createTextNode("\n"), doc.head);
79 html.insertBefore(doc.createTextNode("\n"), doc.body);
80 html.appendChild(doc.createTextNode("\n"));
81 if (!head) {
82 // the first child is the title
83 doc.head.insertBefore(doc.createTextNode("\n "), doc.head.firstChild);
84 }
85
86 copyNode(body, this._document.body, true);
87 this._ownerSerialization = serializer.serialize(components);
88 this._externalObjects = serializer.getExternalObjects();
89
90 return this;
91 }
92 },
93
94 _componentNamesIndex: {
95 value: null
96 },
97
98 _generateLabelForComponent: {value: function(component) {
99 var componentInfo = Montage.getInfoForObject(component),
100 componentLabel = componentInfo.label,
101 componentName,
102 index;
103
104 if (componentLabel) {
105 return componentLabel;
106 } else {
107 componentName = componentInfo.objectName.toLowerCase();
108 index = this._componentNamesIndex[componentName] || 1;
109 this._componentNamesIndex[componentName] = index + 1;
110 return componentName + index;
111 }
112 }},
113}); \ No newline at end of file