aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJose Antonio Marquez2012-05-21 18:28:38 -0700
committerJose Antonio Marquez2012-05-21 18:28:38 -0700
commit11a5be59ff80e0672e9ecf36679be28effb7696c (patch)
tree276b4d9b2cf7fa6c2e96f982fdd71fd6cc406358
parent2994a18010dec4023d904ddc28ca19101f206d6b (diff)
downloadninja-11a5be59ff80e0672e9ecf36679be28effb7696c.tar.gz
Modifying Canvas Data I/O
Changing methods to save all data to external files, this will be optional for the user. Currently save will only save to external files, but UI will allow user to save in file. Need to implement loading data on file open for external file, only works for in file currently. Modified the Runtime file to load all data itself to not bulk up the user document.
-rw-r--r--assets/canvas-runtime.js32
-rw-r--r--js/mediators/io-mediator.js147
2 files changed, 141 insertions, 38 deletions
diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js
index eeafaab6..9bf22313 100644
--- a/assets/canvas-runtime.js
+++ b/assets/canvas-runtime.js
@@ -8,9 +8,37 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
8var NinjaCvsRt = NinjaCvsRt || {}; 8var NinjaCvsRt = NinjaCvsRt || {};
9 9
10/////////////////////////////////////////////////////////////////////// 10///////////////////////////////////////////////////////////////////////
11//Loading webGL/canvas data on window load
12window.addEventListener('load', loadCanvasData, false);
13//Load data function (on document loaded)
14function loadCanvasData (e) {
15 //Cleaning up events
16 window.removeEventListener('load', loadCanvasData, false);
17 //Getting tag with data, MUST contain attribute
18 var xhr, tag = document.querySelectorAll(['script[data-ninja-canvas-lib]'])[0];
19 //Checking for data to be external file
20 if (tag.getAttribute('data-ninja-canvas-json') !== null) {
21 //Loading JSON data
22 xhr = new XMLHttpRequest();
23 xhr.open("GET", tag.getAttribute('data-ninja-canvas-json'), false);
24 xhr.send();
25 //Checking for data
26 if (xhr.readyState === 4) {
27 //Calling method to initialize all webGL/canvas(es)
28 NinjaCvsRt.initWebGl(document.body, tag.getAttribute('data-ninja-canvas-libpath'), xhr.response);
29 } else {
30 //TODO: Add error for users
31 }
32 } else {//Data in document itself
33 //Calling method to initialize all webGL/canvas(es)
34 NinjaCvsRt.initWebGl(document.body, tag.getAttribute('data-ninja-canvas-libpath'), document.querySelectorAll(['script[data-ninja-canvas]'])[0].innerHTML);
35 }
36}
37
38///////////////////////////////////////////////////////////////////////
11//Loading webGL/canvas data 39//Loading webGL/canvas data
12NinjaCvsRt.initWebGl = function (rootElement, directory) { 40NinjaCvsRt.initWebGl = function (rootElement, directory, data) {
13 var cvsDataMngr, ninjaWebGlData = JSON.parse((document.querySelectorAll(['script[data-ninja-webgl]'])[0].innerHTML.replace('(', '')).replace(')', '')); 41 var cvsDataMngr, ninjaWebGlData = JSON.parse((data.replace('(', '')).replace(')', ''));
14 if (ninjaWebGlData && ninjaWebGlData.data) { 42 if (ninjaWebGlData && ninjaWebGlData.data) {
15 for (var n=0; ninjaWebGlData.data[n]; n++) { 43 for (var n=0; ninjaWebGlData.data[n]; n++) {
16 ninjaWebGlData.data[n] = unescape(ninjaWebGlData.data[n]); 44 ninjaWebGlData.data[n] = unescape(ninjaWebGlData.data[n]);
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index 8fe88ee6..bbb78a8b 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -203,6 +203,59 @@ exports.IoMediator = Montage.create(Component, {
203 }, 203 },
204 //////////////////////////////////////////////////////////////////// 204 ////////////////////////////////////////////////////////////////////
205 // 205 //
206 getDataDirectory: {
207 value: function (path) {
208 //TODO: Implement user overwrite
209 return this._getUserDirectory(path+'data/');
210 }
211 },
212 ////////////////////////////////////////////////////////////////////
213 //
214 getNinjaDirectory: {
215 value: function (path) {
216 //TODO: Implement user overwrite
217 return this._getUserDirectory(this.getDataDirectory(path)+'ninja/');
218 }
219 },
220 ////////////////////////////////////////////////////////////////////
221 //
222 getCanvasDirectory: {
223 value: function (path) {
224 //TODO: Implement user overwrite
225 return this._getUserDirectory(this.getNinjaDirectory(path)+'canvas/');
226 }
227 },
228 ////////////////////////////////////////////////////////////////////
229 //
230 _getUserDirectory: {
231 value: function (path) {
232 //Checking for data directory
233 var check = this.application.ninja.coreIoApi.fileExists({uri: path}), directory;
234 //Creating directory if doesn't exists
235 switch (check.status) {
236 case 204: //Exists
237 directory = path;
238 break;
239 case 404: //Doesn't exists
240 directory = this.application.ninja.coreIoApi.createDirectory({uri: path});
241 //Checking for success
242 if (directory.status === 201) {
243 directory = path;
244 } else {
245 //Error
246 directory = null;
247 }
248 break;
249 default: //Error
250 directory = null;
251 break;
252 }
253 //Returning the path to the directory on disk (null for any error)
254 return directory;
255 }
256 },
257 ////////////////////////////////////////////////////////////////////
258 //
206 parseHtmlToNinjaTemplate: { 259 parseHtmlToNinjaTemplate: {
207 enumerable: false, 260 enumerable: false,
208 value: function (html) { 261 value: function (html) {
@@ -406,20 +459,19 @@ exports.IoMediator = Montage.create(Component, {
406 } 459 }
407 } 460 }
408 // 461 //
409 var matchingtags = [], scripts = template.file.content.document.getElementsByTagName('script'), webgltag, webgljstag, webgllibtag, webglrdgetag, mjstag, mjslibtag; 462 var matchingtags = [], scripts = template.file.content.document.getElementsByTagName('script'), webgltag, webgllibtag, webglrdgetag, mjstag, mjslibtag;
463 //this.getDataDirectory(template.file.root)
464 //this.getNinjaDirectory(template.file.root)
410 // 465 //
411 for (var i in scripts) { 466 for (var i in scripts) {
412 if (scripts[i].getAttribute) { 467 if (scripts[i].getAttribute) {
413 if (scripts[i].getAttribute('data-ninja-webgl') !== null) {//TODO: Use querySelectorAll 468 if (scripts[i].getAttribute('data-ninja-canvas') !== null) {//TODO: Use querySelectorAll
414 matchingtags.push(scripts[i]); 469 matchingtags.push(scripts[i]);
415 } 470 }
416 if (scripts[i].getAttribute('data-ninja-webgl-js') !== null) { 471 if (scripts[i].getAttribute('data-ninja-canvas-lib') !== null) {
417 webgljstag = scripts[i]; // TODO: Add logic to delete unneccesary tags
418 }
419 if (scripts[i].getAttribute('data-ninja-webgl-lib') !== null) {
420 webgllibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags 472 webgllibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags
421 } 473 }
422 if (scripts[i].getAttribute('data-ninja-webgl-rdge') !== null) { 474 if (scripts[i].getAttribute('data-ninja-canvas-rdge') !== null) {
423 webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags 475 webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags
424 } 476 }
425 if (scripts[i].getAttribute('type') === 'text/montage-serialization') { 477 if (scripts[i].getAttribute('type') === 'text/montage-serialization') {
@@ -432,7 +484,19 @@ exports.IoMediator = Montage.create(Component, {
432 } 484 }
433 //Checking for webGL elements in document 485 //Checking for webGL elements in document
434 if (template.webgl && template.webgl.length > 1) {//TODO: Should be length 0, hack for a temp fix 486 if (template.webgl && template.webgl.length > 1) {//TODO: Should be length 0, hack for a temp fix
435 var rdgeDirName, rdgeVersion; 487 var rdgeDirName, rdgeVersion, cvsDataDir = this.getCanvasDirectory(template.file.root), fileCvsDir, fileCvsDirAppend, cvsDirCounter = 1;
488 //
489 if (cvsDataDir) {
490 fileCvsDir = cvsDataDir+template.file.name.split('.'+template.file.extension)[0];
491 if (!this._getUserDirectory(fileCvsDir)) {
492 fileCvsDirAppend = fileCvsDir+cvsDirCounter;
493 while (!this._getUserDirectory(fileCvsDirAppend)) {
494 fileCvsDirAppend = fileCvsDir+(cvsDirCounter++);
495 }
496 }
497 //TODO: Allow user overwrite
498 fileCvsDir += '/';
499 }
436 //Copy webGL library if needed 500 //Copy webGL library if needed
437 for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { 501 for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) {
438 //Checking for RDGE library to be available 502 //Checking for RDGE library to be available
@@ -453,12 +517,12 @@ exports.IoMediator = Montage.create(Component, {
453 webgltag = matchingtags[matchingtags.length - 1]; //Saving all data to last one... 517 webgltag = matchingtags[matchingtags.length - 1]; //Saving all data to last one...
454 } 518 }
455 } 519 }
456 // 520 //TODO: Add check for file needed
457 if (!webglrdgetag) { 521 if (!webglrdgetag) {
458 webglrdgetag = template.file.content.document.createElement('script'); 522 webglrdgetag = template.file.content.document.createElement('script');
459 webglrdgetag.setAttribute('type', 'text/javascript'); 523 webglrdgetag.setAttribute('type', 'text/javascript');
460 webglrdgetag.setAttribute('src', rdgeDirName + '/rdge-compiled.js'); 524 webglrdgetag.setAttribute('src', rdgeDirName + '/rdge-compiled.js');
461 webglrdgetag.setAttribute('data-ninja-webgl-rdge', 'true'); 525 webglrdgetag.setAttribute('data-ninja-canvas-rdge', 'true');
462 if (ninjaWrapper) { 526 if (ninjaWrapper) {
463 template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webglrdgetag); 527 template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webglrdgetag);
464 } else { 528 } else {
@@ -470,7 +534,7 @@ exports.IoMediator = Montage.create(Component, {
470 webgllibtag = template.file.content.document.createElement('script'); 534 webgllibtag = template.file.content.document.createElement('script');
471 webgllibtag.setAttribute('type', 'text/javascript'); 535 webgllibtag.setAttribute('type', 'text/javascript');
472 webgllibtag.setAttribute('src', rdgeDirName + '/canvas-runtime.js'); 536 webgllibtag.setAttribute('src', rdgeDirName + '/canvas-runtime.js');
473 webgllibtag.setAttribute('data-ninja-webgl-lib', 'true'); 537 webgllibtag.setAttribute('data-ninja-canvas-lib', 'true');
474 if (ninjaWrapper) { 538 if (ninjaWrapper) {
475 template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgllibtag); 539 template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgllibtag);
476 } else { 540 } else {
@@ -478,51 +542,62 @@ exports.IoMediator = Montage.create(Component, {
478 } 542 }
479 } 543 }
480 // 544 //
481 if (!webgltag) { 545 if (!webgltag && !fileCvsDir) {
482 webgltag = template.file.content.document.createElement('script'); 546 webgltag = template.file.content.document.createElement('script');
483 webgltag.setAttribute('data-ninja-webgl', 'true'); 547 webgltag.setAttribute('data-ninja-canvas', 'true');
484 if (ninjaWrapper) { 548 if (ninjaWrapper) {
485 template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgltag); 549 template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgltag);
486 } else { 550 } else {
487 template.file.content.document.head.appendChild(webgltag); 551 template.file.content.document.head.appendChild(webgltag);
488 } 552 }
489 } 553 }
490 //TODO: Remove this tag and place inside JS file 554
491 if (!webgljstag) {
492 webgljstag = template.file.content.document.createElement('script');
493 webgljstag.setAttribute('type', 'text/javascript');
494 webgljstag.setAttribute('data-ninja-webgl-js', 'true');