From f02b6c98b193cc4cb7755a098b548efddf15c1bb Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Sun, 3 Jun 2012 10:05:21 -0700 Subject: Adding blocking logic to copy libs Need to implement logic in parsing method, might require a minor restructuring. --- js/document/mediators/template.js | 7 ++++--- js/io/system/ninjalibrary.js | 42 ++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 17 deletions(-) (limited to 'js') diff --git a/js/document/mediators/template.js b/js/document/mediators/template.js index aa23c00f..1aac7e15 100755 --- a/js/document/mediators/template.js +++ b/js/document/mediators/template.js @@ -331,7 +331,8 @@ exports.TemplateDocumentMediator = Montage.create(Component, { //TODO: Make proper webGL/Canvas method // - var matchingtags = [], scripts = template.file.content.document.getElementsByTagName('script'), webgltag, webgllibtag, webglrdgetag, mjstag, mjslibtag; + var webgltag, webgllibtag, webglrdgetag, mjstag, mjslibtag, matchingtags = [], + scripts = template.file.content.document.getElementsByTagName('script'); // for (var i in scripts) { if (scripts[i].getAttribute) { @@ -375,7 +376,7 @@ exports.TemplateDocumentMediator = Montage.create(Component, { if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') { rdgeDirName = (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name + this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase(); rdgeVersion = this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version; - this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.file.root, rdgeDirName); + this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.file.root, rdgeDirName, function(result) {console.log(result)}); } else { //TODO: Error handle no available library to copy } @@ -501,7 +502,7 @@ exports.TemplateDocumentMediator = Montage.create(Component, { if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'Montage') { mjsDirName = (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name + this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase(); mjsVersion = this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version; - this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.file.root, mjsDirName); + this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.file.root, mjsDirName, function(result) {console.log(result)}); diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js index 12af5988..0470a166 100644 --- a/js/io/system/ninjalibrary.js +++ b/js/io/system/ninjalibrary.js @@ -57,35 +57,38 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { // copyLibToCloud: { value: function (path, libName, callback) { + var libCheck = this.coreApi.directoryExists({uri: path+libName}); //Checking for library to exists - if(this.coreApi.directoryExists({uri: path+libName}).status === 404) { + if(libCheck.status === 404) { //Getting contents to begin copying this.chromeApi.directoryContents(this.chromeApi.fileSystem.root, function (contents) { for (var i in contents) { if (libName === contents[i].name) { //Getting contents of library to be copied this.chromeApi.directoryContents(contents[i], function (lib) { - //Copying to cloud, adding blocking if no callback specified - if (!callback) { - //TODO: Add blocking logic here - this.copyDirectoryToCloud(path, contents[i], path); - } else { - this.copyDirectoryToCloud(path, contents[i], path, callback); - } + //Copying to cloud + if (callback) { + this.copyDirectoryToCloud(path, contents[i], path, {total: 0, copied: 0, callback: callback.bind(this)}); + } else { + this.copyDirectoryToCloud(path, contents[i], path, {total: 0, copied: 0}); + } }.bind(this)); break; } } }.bind(this)); + } else if (libCheck.status === 204){ + //Already present, so sending success + if (callback) callback(true); } else { - //TODO: Add error handling + if (callback) callback(false); } } }, //////////////////////////////////////////////////////////////////// // copyDirectoryToCloud: { - value: function(root, folder, fileRoot, callback) { + value: function(root, folder, fileRoot, tracking) { //Setting up directory name if (folder.name) { var dir; @@ -109,19 +112,30 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { //Checking for file or directory if (contents[i].isDirectory) { //Recursive call if directory - this.copyDirectoryToCloud(dir, contents[i], fileRoot); + this.copyDirectoryToCloud(dir, contents[i], fileRoot, tracking); } else if (contents[i].isFile){ + // + tracking.total++; //Copying file this.chromeApi.fileContent(contents[i].fullPath, function (result) { //Using binary when copying files to allow images and such to work - this.coreApi.createFile({uri: (fileRoot+result.file.fullPath).replace(/\/\//gi, '/'), contents: result.content}); + var file = this.coreApi.createFile({uri: (fileRoot+result.file.fullPath).replace(/\/\//gi, '/'), contents: result.content}); + //Checking for file copy success + if (file.status === 201) { + tracking.copied++; + } else { + //Error + tracking.callback(false); + } + //Checking for all files to be copied to make callback + if (tracking.copied === tracking.total && tracking.callback) { + tracking.callback(true); + } }.bind(this)); } } }.bind(this)); } - //TODO Add logic for proper callback status(es) - if (callback) callback(true); } }, //////////////////////////////////////////////////////////////////// -- cgit v1.2.3