aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
authorJose Antonio Marquez2012-03-05 16:16:34 -0800
committerJose Antonio Marquez2012-03-05 16:16:34 -0800
commit0913cd53a13083119a6e6bfc2ee939e6becd40e0 (patch)
treeede8140fc9d0cd7cd8810d0f36a9dfb13e2aed86 /js/mediators/io-mediator.js
parent45052033e018ecc0cba582f84c0431d3bd4020fe (diff)
parent56efed8b1ed9974aade615fce2d96bc214d21540 (diff)
downloadninja-0913cd53a13083119a6e6bfc2ee939e6becd40e0.tar.gz
Merge branch 'refs/heads/FileIO' into FileIO-Build-Candidate
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js71
1 files changed, 52 insertions, 19 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index 0a851a4d..c02710c2 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -213,9 +213,11 @@ exports.IoMediator = Montage.create(Component, {
213 parseNinjaTemplateToHtml: { 213 parseNinjaTemplateToHtml: {
214 enumerable: false, 214 enumerable: false,
215 value: function (template) { 215 value: function (template) {
216 var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]));
217 regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi');
216 //Injecting head and body into old document 218 //Injecting head and body into old document
217 template.document.content.document.head.innerHTML = template.head; 219 template.document.content.document.head.innerHTML = template.head.replace(regexRootUrl, '');
218 template.document.content.document.body.innerHTML = template.body; 220 template.document.content.document.body.innerHTML = template.body.replace(regexRootUrl, '');
219 //Getting all CSS (style or link) tags 221 //Getting all CSS (style or link) tags
220 var styletags = template.document.content.document.getElementsByTagName('style'), 222 var styletags = template.document.content.document.getElementsByTagName('style'),
221 linktags = template.document.content.document.getElementsByTagName('link'), 223 linktags = template.document.content.document.getElementsByTagName('link'),
@@ -248,7 +250,7 @@ exports.IoMediator = Montage.create(Component, {
248 for (var l in linktags) { 250 for (var l in linktags) {
249 if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll 251 if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll
250 for (var p=0; toremovetags[p]; p++) { 252 for (var p=0; toremovetags[p]; p++) {
251 if (toremovetags[p].getAttribute('data-ninja-file-url') === ('/'+linktags[l].getAttribute('href'))) { 253 if (toremovetags[p].getAttribute('href') === linktags[l].getAttribute('href')) {
252 if (!toremovetags[p].getAttribute('data-ninja-disabled')) { 254 if (!toremovetags[p].getAttribute('data-ninja-disabled')) {
253 linktags[l].removeAttribute('disabled'); 255 linktags[l].removeAttribute('disabled');
254 } 256 }
@@ -330,29 +332,60 @@ exports.IoMediator = Montage.create(Component, {
330 } 332 }
331 } 333 }
332 } 334 }
333 var local, regex, fileCouldDirUrl, adjCss = this.getCssFromRules(template.css[i].cssRules), cssUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'); 335
334 //TODO: Assure logic for local directory 336 ///////////////////////////////////////////////////////////////////////////////////////////
335 local = cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-2])[0] || cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0] || cssUrl.split(cssUrl.split('/')[0])[0]; 337 ///////////////////////////////////////////////////////////////////////////////////////////
336 // 338
337 fileCouldDirUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+local).replace(/\/\//gi, '/')); 339
338 // 340 var cleanedCss,
339 regex = new RegExp(fileCouldDirUrl.replace(/\//gi, '\\\/'), 'gi'); 341 dirtyCss = this.getCssFromRules(template.css[i].cssRules),
340 // 342 fileUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'),
341 if (local.split('/').length > 2) { 343 fileRootUrl = this.application.ninja.coreIoApi.rootUrl+fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0],
342 adjCss = adjCss.replace(regex, '../'); 344 cleanedCss = dirtyCss.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaUrl.bind(this));
343 } else { 345
344 adjCss = adjCss.replace(regex, ''); 346
347 function parseNinjaUrl (url) {
348 //console.log("Getting: " + url);
349 //
350 if (url.indexOf(fileRootUrl) !== -1) {
351 url = url.replace(new RegExp(fileRootUrl.replace(/\//gi, '\\\/'), 'gi'), '');
352 } else {
353 var assetsDirs = (url.replace(new RegExp((this.application.ninja.coreIoApi.rootUrl).replace(/\//gi, '\\\/'), 'gi'), '')).split('/');
354 var fileDirs = (fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0]).split('/');
355 var counter = 0;
356 var path = '';
357 var newURL = '';
358 //
359 for (var p=0; p < fileDirs.length-1; p++) {
360 if (fileDirs[p] === assetsDirs[p]) {
361 counter++;
362 }
363 }
364 //
365 for (var p=0; p < (fileDirs.length-counter)-1; p++) {
366 path += '../';
367 }
368 //
369 for (var p=counter; p < assetsDirs.length; p++) {
370 newURL += '/'+assetsDirs[p];
371 }
372 //
373 url = (path+newURL).replace(/\/\//gi, '/');
374 }
375 //console.log("Returning: " + url);
376 //console.log("-----");
377 return url;
345 } 378 }
346 379
380 ///////////////////////////////////////////////////////////////////////////////////////////
381 ///////////////////////////////////////////////////////////////////////////////////////////
347 382
348 //console.log(adjCss);
349 //console.log(fileCouldDirUrl);
350 //return;
351 383
352 384
353 385
354 //Saving data from rules array converted to string into <link> file 386 //Saving data from rules array converted to string into <link> file
355 var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: adjCss}); 387 var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: cleanedCss});
388 //TODO: Add error handling for saving files
356 } 389 }
357 } 390 }
358 } 391 }