diff options
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r-- | js/mediators/io-mediator.js | 219 |
1 files changed, 190 insertions, 29 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index dcafb38d..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, { |
@@ -152,17 +153,7 @@ exports.IoMediator = Montage.create(Component, { | |||
152 | // | 153 | // |
153 | switch (file.mode) { | 154 | switch (file.mode) { |
154 | case 'html': | 155 | case 'html': |
155 | //Copy webGL library if needed | 156 | |
156 | if (file.webgl && file.webgl.length > 0) { | ||
157 | for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { | ||
158 | //Checking for RDGE library to be available | ||
159 | if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') { | ||
160 | this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(file.document.root, (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name+this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase()); | ||
161 | } else { | ||
162 | //TODO: Error handle no available library to copy | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | 157 | ||
167 | //TODO: Add check for Monatage library to copy | 158 | //TODO: Add check for Monatage library to copy |
168 | 159 | ||
@@ -357,15 +348,50 @@ exports.IoMediator = Montage.create(Component, { | |||
357 | } | 348 | } |
358 | } | 349 | } |
359 | //Checking for webGL elements in document | 350 | //Checking for webGL elements in document |
360 | if (template.webgl && template.webgl.length) { | 351 | if (template.webgl && template.webgl.length > 1) {//TODO: Should be length 0, hack for a temp fix |
352 | var rdgeDirName, rdgeVersion; | ||
353 | //Copy webGL library if needed | ||
354 | for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { | ||
355 | //Checking for RDGE library to be available | ||
356 | if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') { | ||
357 | rdgeDirName = (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name+this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase(); | ||
358 | rdgeVersion = this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version; | ||
359 | this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.document.root, rdgeDirName, hackRename.bind(this)); | ||
360 | //TODO: Remove, this is copying the library into a static name | ||
361 | function hackRename (status) { | ||
362 | if (status) { | ||
363 | setTimeout(function () { | ||
364 | this.application.ninja.coreIoApi.copyDirectory({sourceUri: template.document.root+rdgeDirName, destUri: template.document.root+'assets'}); | ||
365 | }.bind(this), 3000); | ||
366 | } | ||
367 | } | ||
368 | } else { | ||
369 | //TODO: Error handle no available library to copy | ||
370 | } | ||
371 | } | ||
361 | // | 372 | // |
362 | var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script'); | 373 | var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script'), webgljstag, webgllibtag, webglrdgetag, mjstag, mjslibtag; |
363 | // | 374 | // |
364 | for (var i in scripts) { | 375 | for (var i in scripts) { |
365 | if (scripts[i].getAttribute) { | 376 | if (scripts[i].getAttribute) { |
366 | if (scripts[i].getAttribute('data-ninja-webgl') !== null) {//TODO: Use querySelectorAll | 377 | if (scripts[i].getAttribute('data-ninja-webgl') !== null) {//TODO: Use querySelectorAll |
367 | matchingtags.push(scripts[i]); | 378 | matchingtags.push(scripts[i]); |
368 | } | 379 | } |
380 | if (scripts[i].getAttribute('data-ninja-webgl-js') !== null) { | ||
381 | webgljstag = scripts[i]; // TODO: Add logic to delete unneccesary tags | ||
382 | } | ||
383 | if (scripts[i].getAttribute('data-ninja-webgl-lib') !== null) { | ||
384 | webgllibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags | ||
385 | } | ||
386 | if (scripts[i].getAttribute('data-ninja-webgl-rdge') !== null) { | ||
387 | webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags | ||
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 | } | ||
369 | } | 395 | } |
370 | } | 396 | } |
371 | // | 397 | // |
@@ -378,13 +404,47 @@ exports.IoMediator = Montage.create(Component, { | |||
378 | } | 404 | } |
379 | } | 405 | } |
380 | // | 406 | // |
407 | if (!webglrdgetag) { | ||
408 | webglrdgetag = template.document.content.document.createElement('script'); | ||
409 | webglrdgetag.setAttribute('type', 'text/javascript'); | ||
410 | webglrdgetag.setAttribute('src', rdgeDirName+'/rdge-compiled.js'); | ||
411 | webglrdgetag.setAttribute('data-ninja-webgl-rdge', 'true'); | ||
412 | template.document.content.document.head.appendChild(webglrdgetag); | ||
413 | } | ||
414 | // | ||
415 | if (!webgllibtag) { | ||
416 | webgllibtag = template.document.content.document.createElement('script'); | ||
417 | webgllibtag.setAttribute('type', 'text/javascript'); | ||
418 | webgllibtag.setAttribute('src', rdgeDirName+'/canvas-runtime.js'); | ||
419 | webgllibtag.setAttribute('data-ninja-webgl-lib', 'true'); | ||
420 | template.document.content.document.head.appendChild(webgllibtag); | ||
421 | } | ||
422 | // | ||
381 | if (!webgltag) { | 423 | if (!webgltag) { |
382 | webgltag = template.document.content.document.createElement('script'); | 424 | webgltag = template.document.content.document.createElement('script'); |
383 | webgltag.setAttribute('data-ninja-webgl', 'true'); | 425 | webgltag.setAttribute('data-ninja-webgl', 'true'); |
384 | template.document.content.document.head.appendChild(webgltag); | 426 | template.document.content.document.head.appendChild(webgltag); |
385 | } | 427 | } |
386 | //TODO: Add version and other data for RDGE | 428 | //TODO: Remove this tag and place inside JS file |
387 | json = '\n({\n\t"version": "X.X.X.X",\n\t"data": ['; | 429 | if (!webgljstag) { |
430 | webgljstag = template.document.content.document.createElement('script'); | ||
431 | webgljstag.setAttribute('type', 'text/javascript'); | ||
432 | webgljstag.setAttribute('data-ninja-webgl-js', 'true'); | ||
433 | template.document.content.document.head.appendChild(webgljstag); | ||
434 | } | ||
435 | //TODO: Decide if this should be over-writter or only written on creation | ||
436 | var rootElement = 'document.body'; //TODO: Set actual root element | ||
437 | webgljstag.innerHTML = "\ | ||
438 | //Loading webGL/canvas data on window load\n\ | ||
439 | window.addEventListener('load', loadWebGL, false);\n\ | ||
440 | function loadWebGL (e) {\n\ | ||
441 | window.removeEventListener('load', loadWebGL, false);\n\ | ||
442 | //Calling method to initialize all webGL/canvas(es)\n\ | ||
443 | initWebGl("+rootElement+", '"+rdgeDirName+"/');\n\ | ||
444 | }\ | ||
445 | "; | ||
446 | //TODO: This data should be saved to a JSON file eventually | ||
447 | json = '\n({\n\t"version": "'+rdgeVersion+'",\n\t"directory": "'+rdgeDirName+'/",\n\t"data": ['; | ||
388 | //Looping through data to create escaped array | 448 | //Looping through data to create escaped array |
389 | for (var j=0; template.webgl[j]; j++) { | 449 | for (var j=0; template.webgl[j]; j++) { |
390 | if (j === 0) { | 450 | if (j === 0) { |
@@ -398,18 +458,71 @@ exports.IoMediator = Montage.create(Component, { | |||
398 | //Setting string in tag | 458 | //Setting string in tag |
399 | webgltag.innerHTML = json; | 459 | webgltag.innerHTML = json; |
400 | } | 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 | |||
516 | //Cleaning URLs from HTML | ||
401 | 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.oute |