aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--js/mediators/io-mediator.js66
1 files changed, 51 insertions, 15 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index 0a851a4d..5917edba 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -330,29 +330,65 @@ exports.IoMediator = Montage.create(Component, {
330 } 330 }
331 } 331 }
332 } 332 }
333 var local, regex, fileCouldDirUrl, adjCss = this.getCssFromRules(template.css[i].cssRules), cssUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'); 333
334 //TODO: Assure logic for local directory 334 ///////////////////////////////////////////////////////////////////////////////////////////
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]; 335 ///////////////////////////////////////////////////////////////////////////////////////////
336 // 336
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, '/')); 337
338 // 338 var cleanedCss, fileCouldDirUrl, pathDepth, pathToDocRoot = '../';
339 regex = new RegExp(fileCouldDirUrl.replace(/\//gi, '\\\/'), 'gi'); 339 dirtyCss = this.getCssFromRules(template.css[i].cssRules),
340 // 340 fileUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'),
341 if (local.split('/').length > 2) { 341 fileRootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0]).replace(/\/\//gi, '/')),
342 adjCss = adjCss.replace(regex, '../'); 342 localPath = fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-2])[0] || fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0] || fileUrl.split(fileUrl.split('/')[0])[0],
343 } else { 343 documentRootURL = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]));
344 adjCss = adjCss.replace(regex, ''); 344
345 pathDepth = Math.floor(localPath.split('/').length/2);
346
347 for (var p=0; p < pathDepth; p++) {
348 pathToDocRoot += '../';
349 }
350
351 fileCouldDirUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+localPath).replace(/\/\//gi, '/'));
352
353 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));
354
355 function parseNinjaUrl (url) {
356 //
357 if (url.indexOf(fileCouldDirUrl) !== -1 || url.indexOf(fileRootUrl) !== -1) {
358 //
359 if (pathDepth < 1 || url.indexOf(fileRootUrl) > -1) {
360 //
361 url = url.replace(new RegExp(fileRootUrl.replace(/\//gi, '\\\/'), 'gi'), '');
362 } else {
363 //
364 url = url.replace(new RegExp(fileCouldDirUrl.replace(/\//gi, '\\\/'), 'gi'), '../');
365 }
366 } else {
367 //
368 if (url.indexOf(documentRootURL) !== 1) {
369 url = url.replace(new RegExp(documentRootURL.replace(/\//gi, '\\\/'), 'gi'), pathToDocRoot);
370 } else if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== 1) {
371 //TODO: Add logic for files above root document folder
372 }
373 }
374 //console.log("Returning: " + url);
375 //console.log("-----");
376 return url;
345 } 377 }
346 378
379 //console.log(cleanedCss);
380
381 ///////////////////////////////////////////////////////////////////////////////////////////
382 ///////////////////////////////////////////////////////////////////////////////////////////
383
347 384
348 //console.log(adjCss);
349 //console.log(fileCouldDirUrl);
350 //return; 385 //return;
351 386
352 387
353 388
354 //Saving data from rules array converted to string into <link> file 389 //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}); 390 var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: cleanedCss});
391 //TODO: Add error handling for saving files
356 } 392 }
357 } 393 }
358 } 394 }