diff options
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r-- | js/mediators/io-mediator.js | 130 |
1 files changed, 120 insertions, 10 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index d81f4543..1efbb8bb 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js | |||
@@ -6,10 +6,11 @@ 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 | 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 | //////////////////////////////////////////////////////////////////////// | 14 | //////////////////////////////////////////////////////////////////////// |
14 | // | 15 | // |
15 | exports.IoMediator = Montage.create(Component, { | 16 | exports.IoMediator = Montage.create(Component, { |
@@ -347,7 +348,7 @@ exports.IoMediator = Montage.create(Component, { | |||
347 | } | 348 | } |
348 | } | 349 | } |
349 | //Checking for webGL elements in document | 350 | //Checking for webGL elements in document |
350 | if (template.webgl && template.webgl.length > 0) { | 351 | if (template.webgl && template.webgl.length > 1) {//TODO: Should be length 0, hack for a temp fix |
351 | var rdgeDirName, rdgeVersion; | 352 | var rdgeDirName, rdgeVersion; |
352 | //Copy webGL library if needed | 353 | //Copy webGL library if needed |
353 | for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { | 354 | for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { |
@@ -369,7 +370,7 @@ exports.IoMediator = Montage.create(Component, { | |||
369 | } | 370 | } |
370 | } | 371 | } |
371 | // | 372 | // |
372 | var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script'), webgljstag, webgllibtag, webglrdgetag; | 373 | var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script'), webgljstag, webgllibtag, webglrdgetag, mjstag, mjslibtag; |
373 | // | 374 | // |
374 | for (var i in scripts) { | 375 | for (var i in scripts) { |
375 | if (scripts[i].getAttribute) { | 376 | if (scripts[i].getAttribute) { |
@@ -385,6 +386,12 @@ exports.IoMediator = Montage.create(Component, { | |||
385 | if (scripts[i].getAttribute('data-ninja-webgl-rdge') !== null) { | 386 | if (scripts[i].getAttribute('data-ninja-webgl-rdge') !== null) { |
386 | webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags | 387 | webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags |
387 | } | 388 | } |
389 | if (scripts[i].getAttribute('type') !== 'text/montage-serialization') { | ||
390 | mjstag = scripts[i]; // TODO: Add logic to delete unneccesary tags | ||
391 | } | ||
392 | if (scripts[i].getAttribute('data-mjs-lib') !== null) { | ||
393 | mjslibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags | ||
394 | } | ||
388 | } | 395 | } |
389 | } | 396 | } |
390 | // | 397 | // |
@@ -451,6 +458,61 @@ function loadWebGL (e) {\n\ | |||
451 | //Setting string in tag | 458 | //Setting string in tag |
452 | webgltag.innerHTML = json; | 459 | webgltag.innerHTML = json; |
453 | } | 460 | } |
461 | |||
462 | |||
463 | |||
464 | |||
465 | |||
466 | |||
467 | |||
468 | // | ||
469 | var mjsCounter = 0, mjsComponents = [], temp = TemplateCreator.create(); | ||
470 | // | ||
471 | for (var m in template.mjs) { | ||
472 | mjsComponents.push(template.mjs[m]); | ||
473 | mjsCounter++; | ||
474 | } | ||
475 | // | ||
476 | if (template.mjs && mjsCounter > 0) { | ||
477 | var mjsDirName, mjsVersion, | ||
478 | mjscode = temp.initWithHeadAndBodyElements(template.document.content.document.documentElement.head, template.document.content.document.documentElement.body, mjsComponents)._ownerSerialization; | ||
479 | //Copy Montage library if needed | ||
480 | for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { | ||
481 | //Checking for Montage library to be available | ||
482 | if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'Montage') { | ||
483 | mjsDirName = (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name+this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase(); | ||
484 | mjsVersion = this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version; | ||
485 | this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.document.root, mjsDirName); | ||
486 | //TODO: Fix to allow no overwrite and nested locations | ||
487 | var packjson = this.application.ninja.coreIoApi.createFile({uri: template.document.root+'package.json', contents: '{"mappings": {"montage": "'+mjsDirName+'/"}}'}); | ||
488 | } else { | ||
489 | //TODO: Error handle no available library to copy | ||
490 | } | ||
491 | } | ||
492 | // | ||
493 | if (!mjslibtag) { | ||
494 | mjslibtag = template.document.content.document.createElement('script'); | ||
495 | mjslibtag.setAttribute('type', 'text/javascript'); | ||
496 | mjslibtag.setAttribute('src', mjsDirName+'/montage.js'); | ||
497 | mjslibtag.setAttribute('data-mjs-lib', 'true'); | ||
498 | template.document.content.document.head.appendChild(mjslibtag); | ||
499 | } | ||
500 | // | ||
501 | if (!mjstag) { | ||
502 | mjstag = template.document.content.document.createElement('script'); | ||
503 | mjstag.setAttribute('type', 'text/montage-serialization'); | ||
504 | template.document.content.document.head.appendChild(mjstag); | ||
505 | } | ||
506 | // | ||
507 | mjstag.innerHTML = mjscode; | ||
508 | } | ||
509 | |||
510 | |||
511 | |||
512 | |||
513 | |||
514 | |||
515 | |||
454 | //Cleaning URLs from HTML | 516 | //Cleaning URLs from HTML |
455 | var cleanHTML = template.document.content.document.documentElement.outerHTML.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaRootUrl.bind(this)); | 517 | var cleanHTML = template.document.content.document.documentElement.outerHTML.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaRootUrl.bind(this)); |
456 | // | 518 | // |
@@ -470,8 +532,6 @@ function loadWebGL (e) {\n\ | |||
470 | getUrlfromNinjaUrl: { | 532 | getUrlfromNinjaUrl: { |
471 | enumerable: false, | 533 | enumerable: false, |
472 | value: function (url, fileRootUrl, fileUrl) { | 534 | value: function (url, fileRootUrl, fileUrl) { |
473 | //console.log("Params: ", url, fileRootUrl, fileUrl); | ||
474 | //console.log("Getting: " + url); | ||
475 | // | 535 | // |
476 | if (url.indexOf(fileRootUrl) !== -1) { | 536 | if (url.indexOf(fileRootUrl) !== -1) { |
477 | url = url.replace(new RegExp(fileRootUrl.replace(/\//gi, '\\\/'), 'gi'), ''); | 537 | url = url.replace(new RegExp(fileRootUrl.replace(/\//gi, '\\\/'), 'gi'), ''); |
@@ -499,13 +559,63 @@ function loadWebGL (e) {\n\ | |||
499 | // | 559 | // |
500 | url = (path+newURL).replace(/\/\//gi, '/'); | 560 | url = (path+newURL).replace(/\/\//gi, '/'); |
501 | } | 561 | } |
502 | //console.log("Returning: " + url); | ||
503 | //console.log("-----"); | ||
504 | // | 562 | // |
505 | return url; | 563 | return url; |
506 | } | 564 | } |
507 | }, | 565 | }, |
508 | //////////////////////////////////////////////////////////////////// | 566 | //////////////////////////////////////////////////////////////////// |
567 | // | ||
568 | getDocRootUrl: { | ||
569 | value: function () { | ||
570 | return this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]).replace(/\/\//gi, '/')); | ||
571 | } | ||
572 | }, | ||
573 | //////////////////////////////////////////////////////////////////// | ||
574 | // | ||
575 | getNinjaPropUrlRedirect: { | ||
576 | enumerable: false, | ||
577 | value: function (prop/* , root */) { | ||
578 | //Checking for property value to not contain a full direct URL | ||
579 | if (!prop.match(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi)) { | ||
580 | //Checking for attributes and type of source | ||
581 | if (prop.indexOf('href') !== -1 || prop.indexOf('src') !== -1) { | ||
582 | //From HTML attribute | ||
583 | //if (root) { | ||
584 | //prop = (root+prop).replace(/"([^"]*)"/gi, this.getNinjaUrlPrepend.bind(this)); | ||
585 | //} else { | ||
586 | prop = prop.replace(/"([^"]*)"/gi, this.getNinjaUrlPrepend.bind(this)); | ||
587 | //} | ||
588 | } else if (prop.indexOf('url') !== -1) { | ||
589 | //From CSS property | ||
590 | //if (root) { | ||
591 | //prop = (root+prop).replace(/[^()\\""\\'']+/g, cssUrlToNinjaUrl.bind(this)); | ||
592 | //} else { | ||
593 | prop = prop.replace(/[^()\\""\\'']+/g, cssUrlToNinjaUrl.bind(this)); | ||
594 | //} | ||
595 | function cssUrlToNinjaUrl (s) { | ||
596 | if (s !== 'url') { | ||
597 | s = this.getDocRootUrl() + s; | ||
598 | } | ||
599 | return s; | ||
600 | } | ||
601 | } | ||
602 | } | ||
603 | return prop; | ||
604 | } | ||
605 | }, | ||
606 | //////////////////////////////////////////////////////////////////// | ||
607 | // | ||
608 | getNinjaUrlPrepend: { | ||
609 | enumerable: false, | ||
610 | value: function (url) { | ||
611 | if (url.indexOf('data:') !== -1) { | ||
612 | return url; | ||
613 | } else { | ||
614 | return '"'+this.getDocRootUrl()+url.replace(/\"/gi, '')+'"'; | ||
615 | } | ||
616 | } | ||
617 | }, | ||
618 | //////////////////////////////////////////////////////////////////// | ||
509 | //Method to return a string from CSS rules (to be saved to a file) | 619 | //Method to return a string from CSS rules (to be saved to a file) |
510 | getCssFromRules: { | 620 | getCssFromRules: { |
511 | enumerable: false, | 621 | enumerable: false, |