diff options
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r-- | js/mediators/io-mediator.js | 224 |
1 files changed, 151 insertions, 73 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index 057a849d..b1916446 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js | |||
@@ -10,7 +10,7 @@ var Montage = require("montage/core/core").Montage, | |||
10 | Component = require("montage/ui/component").Component, | 10 | Component = require("montage/ui/component").Component, |
11 | FileIo = require("js/io/system/fileio").FileIo, | 11 | FileIo = require("js/io/system/fileio").FileIo, |
12 | ProjectIo = require("js/io/system/projectio").ProjectIo, | 12 | ProjectIo = require("js/io/system/projectio").ProjectIo, |
13 | TemplateCreator = require("node_modules/tools/template-creator").TemplateCreator; | 13 | TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator; |
14 | //////////////////////////////////////////////////////////////////////// | 14 | //////////////////////////////////////////////////////////////////////// |
15 | // | 15 | // |
16 | exports.IoMediator = Montage.create(Component, { | 16 | exports.IoMediator = Montage.create(Component, { |
@@ -53,14 +53,14 @@ exports.IoMediator = Montage.create(Component, { | |||
53 | // | 53 | // |
54 | fileNew: { | 54 | fileNew: { |
55 | enumerable: false, | 55 | enumerable: false, |
56 | value: function (file, template, callback) { | 56 | value: function (file, url, callback, template) { |
57 | //Loading template from template URL | 57 | //Loading template from template URL |
58 | var xhr = new XMLHttpRequest(), result; | 58 | var xhr = new XMLHttpRequest(), result; |
59 | xhr.open("GET", template, false); | 59 | xhr.open("GET", url, false); |
60 | xhr.send(); | 60 | xhr.send(); |
61 | if (xhr.readyState === 4) { | 61 | if (xhr.readyState === 4) { |
62 | //Making call to create file, checking for return code | 62 | //Making call to create file, checking for return code |
63 | switch (this.fio.newFile({ uri: file, contents: xhr.response })) { | 63 | switch (this.fio.newFile({ uri: file, contents: parseTemplate(xhr.response, template) })) { |
64 | case 201: | 64 | case 201: |
65 | result = { status: 201, success: true, uri: file }; | 65 | result = { status: 201, success: true, uri: file }; |
66 | break; | 66 | break; |
@@ -74,6 +74,21 @@ exports.IoMediator = Montage.create(Component, { | |||
74 | result = { status: 500, success: false, uri: file }; | 74 | result = { status: 500, success: false, uri: file }; |
75 | break; | 75 | break; |
76 | } | 76 | } |
77 | //TODO: Improve template data injection | ||
78 | function parseTemplate (content, template) { | ||
79 | // | ||
80 | if (template.name.toLowerCase() === 'banner' || template.name.toLowerCase() === 'animation') { | ||
81 | //Getting dimensions of banner | ||
82 | var dimensions = template.id.split('x'); | ||
83 | dimensions = {width: String(dimensions[0])+'px', height: String(dimensions[1])+'px'}; | ||
84 | // | ||
85 | content = content.replace(/Dimensions@@@/gi, "Dimensions@@@"+template.id); | ||
86 | content = content.replace(/ninja-banner {}/gi, "ninja-banner {overflow: visible; width: "+dimensions.width+"; height: "+dimensions.height+"}"); | ||
87 | content = content.replace(/ninja-content-wrapper {}/gi, "ninja-content-wrapper {overflow: hidden; width: "+dimensions.width+"; height: "+dimensions.height+"}"); | ||
88 | } | ||
89 | // | ||
90 | return content; | ||
91 | } | ||
77 | } else { | 92 | } else { |
78 | result = { status: 500, success: false, uri: file }; | 93 | result = { status: 500, success: false, uri: file }; |
79 | } | 94 | } |
@@ -147,25 +162,25 @@ exports.IoMediator = Montage.create(Component, { | |||
147 | // | 162 | // |
148 | fileSave: { | 163 | fileSave: { |
149 | enumerable: false, | 164 | enumerable: false, |
150 | value: function (file, callback) { | 165 | value: function (doc, callback) { |
151 | // | 166 | // |
152 | var contents, save; | 167 | var contents, save; |
153 | // | 168 | // |
154 | switch (file.mode) { | 169 | switch (doc.mode) { |
155 | case 'html': | 170 | case 'html': |
156 | |||
157 | |||
158 | //TODO: Add check for Monatage library to copy | ||
159 | |||
160 | //Getting content from function to properly handle saving assets (as in external if flagged) | 171 | //Getting content from function to properly handle saving assets (as in external if flagged) |
161 | contents = this.parseNinjaTemplateToHtml(file); | 172 | if (doc.template && (doc.template.type === 'banner' || doc.template.type === 'animation')) { |
173 | contents = this.parseNinjaTemplateToHtml(doc, true); | ||
174 | } else { | ||
175 | contents = this.parseNinjaTemplateToHtml(doc); | ||
176 | } | ||
162 | break; | 177 | break; |
163 | default: | 178 | default: |
164 | contents = file.content; | 179 | contents = doc.content; |
165 | break; | 180 | break; |
166 | } | 181 | } |
167 | //Making call to save file | 182 | //Making call to save file |
168 | save = this.fio.saveFile({ uri: file.document.uri, contents: contents }); | 183 | save = this.fio.saveFile({ uri: doc.file.uri, contents: contents }); |
169 | //Checking for callback | 184 | //Checking for callback |
170 | if (callback) callback(save); | 185 | if (callback) callback(save); |
171 | } | 186 | } |
@@ -196,23 +211,65 @@ exports.IoMediator = Montage.create(Component, { | |||
196 | //Setting content to temp | 211 | //Setting content to temp |
197 | doc.getElementsByTagName('html')[0].innerHTML = html; | 212 | doc.getElementsByTagName('html')[0].innerHTML = html; |
198 | //Creating return object | 213 | //Creating return object |
199 | return { head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc }; | 214 | return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc}; |
200 | } | 215 | } |
201 | }, | 216 | }, |
202 | //////////////////////////////////////////////////////////////////// | 217 | //////////////////////////////////////////////////////////////////// |
203 | //TODO: Expand to allow more templates, clean up variables | 218 | //TODO: Expand to allow more templates, clean up variables |
204 | parseNinjaTemplateToHtml: { | 219 | parseNinjaTemplateToHtml: { |
205 | enumerable: false, | 220 | enumerable: false, |
206 | value: function (template) { | 221 | value: function (template, ninjaWrapper) { |
207 | var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])); | 222 | //TODO: Improve reference for rootUrl |
223 | var regexRootUrl, | ||
224 | rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])), | ||
225 | mjsCreator = template.mjsTemplateCreator.create(), | ||
226 | mJsSerialization, | ||
227 | montageTemplate; | ||
228 | // | ||
229 | montageTemplate = mjsCreator.initWithDocument(template.document); | ||
230 | //Setting up expression for parsing URLs | ||
208 | regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); | 231 | regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); |
209 | //Injecting head and body into old document | 232 | //Injecting head and body into old document |
210 | template.document.content.document.head.innerHTML = template.head.replace(regexRootUrl, ''); | 233 | if (montageTemplate._ownerSerialization.length > 0) { |
211 | template.document.content.document.body.innerHTML = template.body.replace(regexRootUrl, ''); | 234 | template.file.content.document.head.innerHTML = montageTemplate._document.head.innerHTML.replace(regexRootUrl, ''); |
235 | template.file.content.document.body.innerHTML = montageTemplate._document.body.innerHTML.replace(regexRootUrl, ''); | ||
236 | // | ||
237 | mJsSerialization = montageTemplate._ownerSerialization; | ||
238 | } else { | ||
239 | template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, ''); | ||
240 | template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, ''); | ||
241 | } | ||
242 | |||
243 | //Copying attributes to maintain same properties as the <body> | ||
244 | for (var n in template.body.attributes) { | ||
245 | if (template.body.attributes[n].value) { | ||
246 | // | ||
247 | template.file.content.document.body.setAttribute(template.body.attributes[n].name, template.body.attributes[n].value); | ||
248 | } | ||
249 | } | ||
250 | //TODO: Add attribute copying for <HEAD> and <HTML> | ||
251 | |||
252 | |||
253 | |||
254 | |||
212 | //Getting all CSS (style or link) tags | 255 | //Getting all CSS (style or link) tags |
213 | var styletags = template.document.content.document.getElementsByTagName('style'), | 256 | var styletags = template.file.content.document.getElementsByTagName('style'), |
214 | linktags = template.document.content.document.getElementsByTagName('link'), | 257 | linktags = template.file.content.document.getElementsByTagName('link'), |
215 | toremovetags = []; | 258 | toremovetags = [], |
259 | njtemplatetags = template.file.content.document.querySelectorAll('[data-ninja-template]'); | ||
260 | |||
261 | ////////////////////////////////////////////////// | ||
262 | //TODO: Remove, temp hack, this is to be fixed by Montage | ||
263 | var basetags = template.file.content.document.getElementsByTagName('base'); | ||
264 | for (var g in basetags) { | ||
265 | if (basetags[g].getAttribute) toremovetags.push(basetags[g]); | ||
266 | } | ||
267 | ////////////////////////////////////////////////// | ||
268 | |||
269 | // | ||
270 | for (var f in njtemplatetags) { | ||
271 | if (njtemplatetags[f].getAttribute) toremovetags.push(njtemplatetags[f]); | ||
272 | } | ||
216 | //Getting styles tags to be removed from document | 273 | //Getting styles tags to be removed from document |
217 | if (styletags.length) { | 274 | if (styletags.length) { |
218 | for (var j = 0; j < styletags.length; j++) { | 275 | for (var j = 0; j < styletags.length; j++) { |
@@ -229,15 +286,16 @@ exports.IoMediator = Montage.create(Component, { | |||
229 | for (var h = 0; toremovetags[h]; h++) { | 286 | for (var h = 0; toremovetags[h]; h++) { |
230 | try { | 287 | try { |
231 | //Checking head first | 288 | //Checking head first |
232 | template.document.content.document.head.removeChild(toremovetags[h]); | 289 | template.file.content.document.head.removeChild(toremovetags[h]); |
233 | } catch (e) { | 290 | } catch (e) { |
234 | try { | 291 | |
292 | } | ||
293 | try { | ||
235 | //Checking body if not in head | 294 | //Checking body if not in head |
236 | template.document.content.document.body.removeChild(toremovetags[h]); | 295 | template.file.content.document.body.removeChild(toremovetags[h]); |
237 | } catch (e) { | 296 | } catch (e) { |
238 | //Error, not found! | 297 | //Error, not found! |
239 | } | 298 | } |
240 | } | ||
241 | } | 299 | } |
242 | //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) | 300 | //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) |
243 | for (var l in linktags) { | 301 | for (var l in linktags) { |
@@ -255,13 +313,13 @@ exports.IoMediator = Montage.create(Component, { | |||
255 | if (template.styles) { | 313 | if (template.styles) { |
256 | //Getting all style tags | 314 | //Getting all style tags |
257 | var styleCounter = 0, | 315 | var styleCounter = 0, |
258 | docStyles = template.document.content.document.getElementsByTagName('style'); | 316 | docStyles = template.file.content.document.getElementsByTagName('style'); |
259 | //Looping through all style tags | 317 | //Looping through all style tags |
260 | for (var i in template.styles) { | 318 | for (var i in template.styles) { |
261 | if (template.styles[i].ownerNode) { | 319 | if (template.styles[i].ownerNode) { |
262 | if (template.styles[i].ownerNode.getAttribute) { | 320 | if (template.styles[i].ownerNode.getAttribute) { |
263 | //Checking for node not to be loaded from file | 321 | //Checking for node not to be loaded from file |
264 | if (template.styles[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.styles[i].ownerNode.getAttribute('data-ninja-t |