From 11a5be59ff80e0672e9ecf36679be28effb7696c Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Mon, 21 May 2012 18:28:38 -0700 Subject: 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. --- assets/canvas-runtime.js | 32 +++++++++- js/mediators/io-mediator.js | 147 +++++++++++++++++++++++++++++++++----------- 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 @@ -7,10 +7,38 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot // namespace for the Ninja Canvas Runtime var NinjaCvsRt = NinjaCvsRt || {}; +/////////////////////////////////////////////////////////////////////// +//Loading webGL/canvas data on window load +window.addEventListener('load', loadCanvasData, false); +//Load data function (on document loaded) +function loadCanvasData (e) { + //Cleaning up events + window.removeEventListener('load', loadCanvasData, false); + //Getting tag with data, MUST contain attribute + var xhr, tag = document.querySelectorAll(['script[data-ninja-canvas-lib]'])[0]; + //Checking for data to be external file + if (tag.getAttribute('data-ninja-canvas-json') !== null) { + //Loading JSON data + xhr = new XMLHttpRequest(); + xhr.open("GET", tag.getAttribute('data-ninja-canvas-json'), false); + xhr.send(); + //Checking for data + if (xhr.readyState === 4) { + //Calling method to initialize all webGL/canvas(es) + NinjaCvsRt.initWebGl(document.body, tag.getAttribute('data-ninja-canvas-libpath'), xhr.response); + } else { + //TODO: Add error for users + } + } else {//Data in document itself + //Calling method to initialize all webGL/canvas(es) + NinjaCvsRt.initWebGl(document.body, tag.getAttribute('data-ninja-canvas-libpath'), document.querySelectorAll(['script[data-ninja-canvas]'])[0].innerHTML); + } +} + /////////////////////////////////////////////////////////////////////// //Loading webGL/canvas data -NinjaCvsRt.initWebGl = function (rootElement, directory) { - var cvsDataMngr, ninjaWebGlData = JSON.parse((document.querySelectorAll(['script[data-ninja-webgl]'])[0].innerHTML.replace('(', '')).replace(')', '')); +NinjaCvsRt.initWebGl = function (rootElement, directory, data) { + var cvsDataMngr, ninjaWebGlData = JSON.parse((data.replace('(', '')).replace(')', '')); if (ninjaWebGlData && ninjaWebGlData.data) { for (var n=0; ninjaWebGlData.data[n]; n++) { 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, { }, //////////////////////////////////////////////////////////////////// // + getDataDirectory: { + value: function (path) { + //TODO: Implement user overwrite + return this._getUserDirectory(path+'data/'); + } + }, + //////////////////////////////////////////////////////////////////// + // + getNinjaDirectory: { + value: function (path) { + //TODO: Implement user overwrite + return this._getUserDirectory(this.getDataDirectory(path)+'ninja/'); + } + }, + //////////////////////////////////////////////////////////////////// + // + getCanvasDirectory: { + value: function (path) { + //TODO: Implement user overwrite + return this._getUserDirectory(this.getNinjaDirectory(path)+'canvas/'); + } + }, + //////////////////////////////////////////////////////////////////// + // + _getUserDirectory: { + value: function (path) { + //Checking for data directory + var check = this.application.ninja.coreIoApi.fileExists({uri: path}), directory; + //Creating directory if doesn't exists + switch (check.status) { + case 204: //Exists + directory = path; + break; + case 404: //Doesn't exists + directory = this.application.ninja.coreIoApi.createDirectory({uri: path}); + //Checking for success + if (directory.status === 201) { + directory = path; + } else { + //Error + directory = null; + } + break; + default: //Error + directory = null; + break; + } + //Returning the path to the directory on disk (null for any error) + return directory; + } + }, + //////////////////////////////////////////////////////////////////// + // parseHtmlToNinjaTemplate: { enumerable: false, value: function (html) { @@ -406,20 +459,19 @@ exports.IoMediator = Montage.create(Component, { } } // - var matchingtags = [], scripts = template.file.content.document.getElementsByTagName('script'), webgltag, webgljstag, webgllibtag, webglrdgetag, mjstag, mjslibtag; + var matchingtags = [], scripts = template.file.content.document.getElementsByTagName('script'), webgltag, webgllibtag, webglrdgetag, mjstag, mjslibtag; + //this.getDataDirectory(template.file.root) + //this.getNinjaDirectory(template.file.root) // for (var i in scripts) { if (scripts[i].getAttribute) { - if (scripts[i].getAttribute('data-ninja-webgl') !== null) {//TODO: Use querySelectorAll + if (scripts[i].getAttribute('data-ninja-canvas') !== null) {//TODO: Use querySelectorAll matchingtags.push(scripts[i]); } - if (scripts[i].getAttribute('data-ninja-webgl-js') !== null) { - webgljstag = scripts[i]; // TODO: Add logic to delete unneccesary tags - } - if (scripts[i].getAttribute('data-ninja-webgl-lib') !== null) { + if (scripts[i].getAttribute('data-ninja-canvas-lib') !== null) { webgllibtag = scripts[i]; // TODO: Add logic to delete unneccesary tags } - if (scripts[i].getAttribute('data-ninja-webgl-rdge') !== null) { + if (scripts[i].getAttribute('data-ninja-canvas-rdge') !== null) { webglrdgetag = scripts[i]; // TODO: Add logic to delete unneccesary tags } if (scripts[i].getAttribute('type') === 'text/montage-serialization') { @@ -432,7 +484,19 @@ exports.IoMediator = Montage.create(Component, { } //Checking for webGL elements in document if (template.webgl && template.webgl.length > 1) {//TODO: Should be length 0, hack for a temp fix - var rdgeDirName, rdgeVersion; + var rdgeDirName, rdgeVersion, cvsDataDir = this.getCanvasDirectory(template.file.root), fileCvsDir, fileCvsDirAppend, cvsDirCounter = 1; + // + if (cvsDataDir) { + fileCvsDir = cvsDataDir+template.file.name.split('.'+template.file.extension)[0]; + if (!this._getUserDirectory(fileCvsDir)) { + fileCvsDirAppend = fileCvsDir+cvsDirCounter; + while (!this._getUserDirectory(fileCvsDirAppend)) { + fileCvsDirAppend = fileCvsDir+(cvsDirCounter++); + } + } + //TODO: Allow user overwrite + fileCvsDir += '/'; + } //Copy webGL library if needed for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { //Checking for RDGE library to be available @@ -453,12 +517,12 @@ exports.IoMediator = Montage.create(Component, { webgltag = matchingtags[matchingtags.length - 1]; //Saving all data to last one... } } - // + //TODO: Add check for file needed if (!webglrdgetag) { webglrdgetag = template.file.content.document.createElement('script'); webglrdgetag.setAttribute('type', 'text/javascript'); webglrdgetag.setAttribute('src', rdgeDirName + '/rdge-compiled.js'); - webglrdgetag.setAttribute('data-ninja-webgl-rdge', 'true'); + webglrdgetag.setAttribute('data-ninja-canvas-rdge', 'true'); if (ninjaWrapper) { template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webglrdgetag); } else { @@ -470,7 +534,7 @@ exports.IoMediator = Montage.create(Component, { webgllibtag = template.file.content.document.createElement('script'); webgllibtag.setAttribute('type', 'text/javascript'); webgllibtag.setAttribute('src', rdgeDirName + '/canvas-runtime.js'); - webgllibtag.setAttribute('data-ninja-webgl-lib', 'true'); + webgllibtag.setAttribute('data-ninja-canvas-lib', 'true'); if (ninjaWrapper) { template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgllibtag); } else { @@ -478,51 +542,62 @@ exports.IoMediator = Montage.create(Component, { } } // - if (!webgltag) { + if (!webgltag && !fileCvsDir) { webgltag = template.file.content.document.createElement('script'); - webgltag.setAttribute('data-ninja-webgl', 'true'); + webgltag.setAttribute('data-ninja-canvas', 'true'); if (ninjaWrapper) { template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgltag); } else { template.file.content.document.head.appendChild(webgltag); } } - //TODO: Remove this tag and place inside JS file - if (!webgljstag) { - webgljstag = template.file.content.document.createElement('script'); - webgljstag.setAttribute('type', 'text/javascript'); - webgljstag.setAttribute('data-ninja-webgl-js', 'true'); - if (ninjaWrapper) { - template.file.content.document.body.getElementsByTagName('ninja-content')[0].appendChild(webgljstag); - } else { - template.file.content.document.head.appendChild(webgljstag); - } - } + //TODO: Decide if this should be over-writter or only written on creation var rootElement = 'document.body'; //TODO: Set actual root element - webgljstag.innerHTML = "\ -//Loading webGL/canvas data on window load\n\ -window.addEventListener('load', loadWebGL, false);\n\ -function loadWebGL (e) {\n\ - window.removeEventListener('load', loadWebGL, false);\n\ - //Calling method to initialize all webGL/canvas(es)\n\ - NinjaCvsRt.initWebGl(" + rootElement + ", '" + rdgeDirName + "/');\n\ -}\ - "; + //TODO: This data should be saved to a JSON file eventually var json = '\n({\n\t"version": "' + rdgeVersion + '",\n\t"directory": "' + rdgeDirName + '/",\n\t"data": ['; //Looping through data to create escaped array for (var j = 0; template.webgl[j]; j++) { if (j === 0) { - json += '\n\t\t\t"' + escape(template.webgl[j]) + '"'; + //if (fileCvsDir) { + // json += '\n\t\t\t"' + template.webgl[j] + '"'; + //} else { + json += '\n\t\t\t"' + escape(template.webgl[j]) + '"'; + //} } else { - json += ',\n\t\t\t"' + escape(template.webgl[j]) + '"'; + //if (fileCvsDir) { + // json += ',\n\t\t\t"' + template.webgl[j] + '"'; + //} else { + json += ',\n\t\t\t"' + escape(template.webgl[j]) + '"'; + //} } } //Closing array (make-shift JSON string to validate data in