aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tools/template/template-creator.js
diff options
context:
space:
mode:
authorEric Guzman2012-05-17 17:43:05 -0700
committerEric Guzman2012-05-17 17:43:05 -0700
commit458a4598ed64e3d8c6010a216fb7fb4b3c3c87e7 (patch)
treeee329e409fe28ae54c894b9e27e82e1f85aefc85 /node_modules/tools/template/template-creator.js
parent82954f400f7f8609aef0d2bc1f44c9d960907be6 (diff)
parent52394cdd71bd62c8c109fd135fa146b7183fbd1f (diff)
downloadninja-458a4598ed64e3d8c6010a216fb7fb4b3c3c87e7.tar.gz
Merge branch 'dom-architecture' of github.com:Motorola-Mobility/ninja-internal into CSSPanelUpdates
Conflicts: js/document/templates/montage-html/default_html.css js/lib/NJUtils.js
Diffstat (limited to 'node_modules/tools/template/template-creator.js')
-rwxr-xr-xnode_modules/tools/template/template-creator.js113
1 files changed, 113 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..374353f0
--- /dev/null
+++ b/node_modules/tools/template/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._componentNamesIndex = {};
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