diff options
Diffstat (limited to 'js/document/mediators/template.js')
-rwxr-xr-x | js/document/mediators/template.js | 796 |
1 files changed, 793 insertions, 3 deletions
diff --git a/js/document/mediators/template.js b/js/document/mediators/template.js index c5b45ba1..4c72314a 100755 --- a/js/document/mediators/template.js +++ b/js/document/mediators/template.js | |||
@@ -6,16 +6,806 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
6 | 6 | ||
7 | //////////////////////////////////////////////////////////////////////// | 7 | //////////////////////////////////////////////////////////////////////// |
8 | // | 8 | // |
9 | var Montage = require("montage/core/core").Montage, | 9 | var Montage = require("montage/core/core").Montage, |
10 | Component = require("montage/ui/component").Component; | 10 | Component = require("montage/ui/component").Component, |
11 | TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator, | ||
12 | ClassUuid = require("js/components/core/class-uuid").ClassUuid; | ||
11 | //////////////////////////////////////////////////////////////////////// | 13 | //////////////////////////////////////////////////////////////////////// |
12 | // | 14 | // |
13 | exports.TemplateDocumentMediator = Montage.create(Component, { | 15 | exports.TemplateDocumentMediator = Montage.create(Component, { |
14 | //////////////////////////////////////////////////////////////////// | 16 | //////////////////////////////////////////////////////////////////// |
15 | // | 17 | // |
16 | hasTemplate: { | 18 | hasTemplate: { |
17 | enumerable: false, | ||
18 | value: false | 19 | value: false |
20 | }, | ||
21 | //////////////////////////////////////////////////////////////////// | ||
22 | // | ||
23 | getAppTemplatesUrlRegEx: { | ||
24 | value: function () { | ||
25 | var regex = new RegExp(chrome.extension.getURL(this.application.ninja.currentDocument.model.views.design.iframe.src.split(chrome.extension.getURL('/'))[1]).replace(/\//gi, '\\\/'), 'gi'); | ||
26 | return regex; | ||
27 | } | ||
28 | }, | ||
29 | //////////////////////////////////////////////////////////////////// | ||
30 | // | ||
31 | getDataDirectory: { | ||
32 | value: function (path) { | ||
33 | //TODO: Implement user overwrite | ||
34 | return this._getUserDirectory(path+'data/'); | ||
35 | } | ||
36 | }, | ||
37 | //////////////////////////////////////////////////////////////////// | ||
38 | // | ||
39 | getNinjaDirectory: { | ||
40 | value: function (path) { | ||
41 | //TODO: Implement user overwrite | ||
42 | return this._getUserDirectory(this.getDataDirectory(path)+'ninja/'); | ||
43 | } | ||
44 | }, | ||
45 | //////////////////////////////////////////////////////////////////// | ||
46 | // | ||
47 | getCanvasDirectory: { | ||
48 | value: function (path) { | ||
49 | //TODO: Implement user overwrite | ||
50 | return this._getUserDirectory(this.getNinjaDirectory(path)+'canvas/'); | ||
51 | } | ||
52 | }, | ||
53 | //////////////////////////////////////////////////////////////////// | ||
54 | // | ||
55 | _getUserDirectory: { | ||
56 | value: function (path) { | ||
57 | //Checking for data directory | ||
58 | var check = this.application.ninja.coreIoApi.fileExists({uri: path}), directory; | ||
59 | //Creating directory if doesn't exists | ||
60 | switch (check.status) { | ||
61 | case 204: //Exists | ||
62 | directory = path; | ||
63 | break; | ||
64 | case 404: //Doesn't exists | ||
65 | directory = this.application.ninja.coreIoApi.createDirectory({uri: path}); | ||
66 | //Checking for success | ||
67 | if (directory.status === 201) { | ||
68 | directory = path; | ||
69 | } else { | ||
70 | //Error | ||
71 | directory = null; | ||
72 | } | ||
73 | break; | ||
74 | default: //Error | ||
75 | directory = null; | ||
76 | break; | ||
77 | } | ||
78 | //Returning the path to the directory on disk (null for any error) | ||
79 | return directory; | ||
80 | } | ||
81 | }, | ||
82 | //////////////////////////////////////////////////////////////////// | ||
83 | // | ||
84 | parseHtmlToNinjaTemplate: { | ||
85 | value: function (html) { | ||
86 | //Creating temp object to mimic HTML | ||
87 | var doc = window.document.implementation.createHTMLDocument(), template, docHtmlTag, | ||
88 | hackHtml = document.createElement('html'), hackTag; | ||
89 | //Setting content to temp | ||
90 | doc.getElementsByTagName('html')[0].innerHTML = html; | ||
91 | //TODO: Improve this, very bad way of copying attributes (in a pinch to get it working) | ||
92 | hackHtml.innerHTML = html.replace(/html/gi, 'ninjahtmlhack'); | ||
93 | hackTag = hackHtml.getElementsByTagName('ninjahtmlhack')[0]; | ||
94 | docHtmlTag = doc.getElementsByTagName('html')[0]; | ||
95 | //Looping through the attributes to copy them | ||
96 | if (hackTag) { | ||
97 | for (var m in hackTag.attributes) { | ||
98 | if (hackTag.attributes[m].value) { | ||
99 | docHtmlTag.setAttribute(hackTag.attributes[m].name.replace(/ninjahtmlhack/gi, 'html'), hackTag.attributes[m].value.replace(/ninjahtmlhack/gi, 'html')); | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | //Garbage collection | ||
104 | hackHtml = hackTag = null; | ||
105 | //Creating return object | ||
106 | return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc}; | ||
107 | } | ||
108 | }, | ||
109 | //////////////////////////////////////////////////////////////////// | ||
110 | //TODO: Expand to allow more templates, clean up variables | ||
111 | parseNinjaTemplateToHtml: { | ||
112 | value: function (saveExternalData, template, ninjaWrapper, libCopyCallback) { | ||
113 | //TODO: Improve reference for rootUrl | ||
114 | var regexRootUrl, | ||
115 | rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])), | ||
116 | mjsCreator = template.mjsTemplateCreator.create(), | ||
117 | mJsSerialization, | ||
118 | toremovetags = [], | ||
119 | presentNodes, | ||
120 | montageTemplate; | ||
121 | //Creating instance of template creator | ||
122 | montageTemplate = mjsCreator.initWithDocument(template.document); | ||
123 | //Setting up expression for parsing URLs | ||
124 | regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); | ||
125 | //Injecting head and body into old document | ||
126 | if (montageTemplate._ownerSerialization.length > 0) { | ||
127 | template.file.content.document.head.innerHTML = montageTemplate._document.head.innerHTML.replace(regexRootUrl, ''); | ||
128 | template.file.content.document.body.innerHTML = montageTemplate._document.body.innerHTML.replace(regexRootUrl, ''); | ||
129 | // | ||
130 | mJsSerialization = montageTemplate._ownerSerialization; | ||
131 | } else { | ||
132 | template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, ''); | ||
133 | template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, ''); | ||
134 | } | ||
135 | //Removes all attributes from node | ||
136 | function wipeAttributes (node) { | ||
137 | for (var f in node.attributes) { | ||
138 | node.removeAttribute(node.attributes[f].name); | ||
139 | } | ||
140 | } | ||
141 | //Copying attributes to maintain same properties as the <body> | ||
142 | wipeAttributes(template.file.content.document.body); | ||
143 | for (var n in template.body.attributes) { | ||
144 | if (template.body.attributes[n].value) { | ||
145 | template.file.content.document.body.setAttribute(template.body.attributes[n].name, template.body.attributes[n].value); | ||
146 | } | ||
147 | } | ||
148 | wipeAttributes(template.file.content.document.head); | ||
149 | //Copying attributes to maintain same properties as the <head> | ||
150 | for (var m in template.document.head.attributes) { | ||
151 | if (template.document.head.attributes[m].value) { | ||
152 | template.file.content.document.head.setAttribute(template.document.head.attributes[m].name, template.document.head.attributes[m].value); | ||
153 | } | ||
154 | } | ||
155 | //Copying attributes to maintain same properties as the <html> | ||
156 | var htmlTagMem = template.document.getElementsByTagName('html')[0], htmlTagDoc = template.file.content.document.getElementsByTagName('html')[0]; | ||
157 | wipeAttributes(htmlTagDoc); | ||
158 | for (var m in htmlTagMem.attributes) { | ||
159 | if (htmlTagMem.attributes[m].value) { | ||
160 | if (htmlTagMem.attributes[m].value.replace(/montage-app-bootstrapping/gi, '').length>0) { | ||
161 | htmlTagDoc.setAttribute(htmlTagMem.attributes[m].name, htmlTagMem.attributes[m].value.replace(/ montage-app-bootstrapping/gi, '')); | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | //Getting list of current nodes (Ninja DOM) | ||
166 | presentNodes = template.file.content.document.getElementsByTagName('*'); | ||
167 | //Looping through nodes to determine origin and removing if not inserted by Ninja | ||
168 | for (var n in presentNodes) { | ||
169 | if (presentNodes[n].getAttribute && presentNodes[n].getAttribute('data-ninja-node') === null) { | ||
170 | toremovetags.push(presentNodes[n]); | ||
171 | } else if (presentNodes[n].getAttribute && presentNodes[n].getAttribute('data-ninja-node') !== null) { | ||
172 | //Removing attribute | ||
173 | presentNodes[n].removeAttribute('data-ninja-node'); | ||
174 | } | ||
175 | } | ||
176 | //Getting all CSS (style or link) tags | ||
177 | var styletags = template.file.content.document.getElementsByTagName('style'), | ||
178 | linktags = template.file.content.document.getElementsByTagName('link'), | ||
179 | njtemplatetags = template.file.content.document.querySelectorAll('[data-ninja-template]'); | ||
180 | |||
181 | ////////////////////////////////////////////////// | ||
182 | //TODO: Remove, temp hack, this is to be fixed by Montage | ||
183 | var basetags = template.file.content.document.getElementsByTagName('base'); | ||
184 | for (var g in basetags) { | ||
185 | if (basetags[g].getAttribute && basetags[g].href && basetags[g].href.indexOf('chrome-extension://') !== -1) toremovetags.push(basetags[g]); | ||
186 | } | ||
187 | ////////////////////////////////////////////////// | ||
188 | |||
189 | //Adding to tags to be removed form template | ||
190 | for (var f in njtemplatetags) { | ||
191 | if (njtemplatetags[f].getAttribute) toremovetags.push(njtemplatetags[f]); | ||
192 | } | ||
193 | //Getting styles tags to be removed from document | ||
194 | if (styletags.length) { | ||
195 | for (var j = 0; j < styletags.length; j++) { | ||
196 | if (styletags[j].getAttribute) { | ||
197 | if (styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) { | ||
198 | toremovetags.push(styletags[j]); | ||
199 | } else if (styletags[j].getAttribute('data-ninja-external-url')) { | ||
200 | toremovetags.push(styletags[j]); | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | } | ||
205 | //Removing styles tags from document | ||
206 | for (var h = 0; toremovetags[h]; h++) { | ||
207 | try { | ||
208 | //Checking head first | ||
209 | template.file.content.document.head.removeChild(toremovetags[h]); | ||
210 | } catch (e) { | ||
211 | |||
212 | } | ||
213 | try { | ||
214 | //Checking body if not in head | ||
215 | template.file.content.document.body.removeChild(toremovetags[h]); | ||