From ad85ff92ba44fb3c8ccf24c2f1b9296804dfa8ca Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Mon, 6 Feb 2012 22:33:42 -0800 Subject: Single file library syncing Added the ability to store locally in chrome single file libraries used by Ninja. Working on adding multi-file libraries. --- js/io/system/chromeapi.js | 51 +++++++++++++++++++---- js/io/system/coreioapi.js | 34 ++------------- js/io/system/ninjalibrary.js | 93 +++++++++++++++++++++++++++++++++++++++++- js/io/system/ninjalibrary.json | 2 +- 4 files changed, 138 insertions(+), 42 deletions(-) (limited to 'js/io/system') diff --git a/js/io/system/chromeapi.js b/js/io/system/chromeapi.js index fb141687..6df41fd3 100644 --- a/js/io/system/chromeapi.js +++ b/js/io/system/chromeapi.js @@ -9,6 +9,9 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot NOTES: The init function starts up the file system API, and a size must be set, no unlimited available as of now. + + Core API reference in NINJA: this.application.ninja.coreIoApi + //////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// */ // @@ -61,24 +64,51 @@ exports.ChromeApi = Montage.create(Object.prototype, { }, //////////////////////////////////////////////////////////////////// // + fileNew: { + enumerable: true, + value: function(filePath, content, mime, callback) { + // + this.fileSystem.root.getFile(filePath, {create: true}, function(f) { + // + f.createWriter(function(writer) { + // + var b = new window.WebKitBlobBuilder; + b.append(content); + writer.write(b.getBlob(mime)); + // + if (callback) callback(true); + }, function (e) {if (callback) callback(false)}); + }, function (e) {if (callback) callback(false)}); + } + }, + //////////////////////////////////////////////////////////////////// + //Creating directory from path, callback optional directoryNew: { enumerable: true, - value: function() { + value: function(directoryPath, callback) { + //Checking for directory not to already exist + this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { + if (callback) callback(false); + return; //Directory already exists + }); + //Creating new directory + this.fileSystem.root.getDirectory(directoryPath, {create: true}, function(dir) { + if (callback) callback(true); + }, function (e) {if (callback) callback(false)}); } }, - //////////////////////////////////////////////////////////////////// // directoryDelete: { enumerable: true, value: function(directoryPath, callback) { // - this.fileSystem.getDirectory(directoryPath, {}, function(dirEntry) { + this.fileSystem.root.getDirectory(directoryPath, {}, function(dir) { // - dirEntry.removeRecursively(function() { - callback(true); + dir.removeRecursively(function() { + if (callback) callback(true); }); - }, function (e) {callback(false)}); + }, function (e) {if (callback) callback(false)}); } }, //////////////////////////////////////////////////////////////////// @@ -125,9 +155,12 @@ exports.ChromeApi = Montage.create(Object.prototype, { // var lib = []; // - - - + for(var i=0; contents[i]; i++) { + // + if (contents[i].isDirectory) { + lib.push(contents[i].name); + } + } //Dispatching action ready event var libraryEvent = document.createEvent("CustomEvent"); libraryEvent.initEvent('library', true, true); diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js index b821936f..9836f33f 100755 --- a/js/io/system/coreioapi.js +++ b/js/io/system/coreioapi.js @@ -17,6 +17,7 @@ var Montage = require("montage/core/core").Montage, Popup = require("js/components/popup.reel").Popup, CloudPopup = require("js/io/ui/cloudpopup.reel").CloudPopup, ChromeApi = require("js/io/system/chromeapi").ChromeApi; + NinjaLibrary = require("js/io/system/ninjalibrary").NinjaLibrary; //////////////////////////////////////////////////////////////////////// //Exporting as Project I/O exports.CoreIoApi = Montage.create(Component, { @@ -37,6 +38,8 @@ exports.CoreIoApi = Montage.create(Component, { this.ioServiceDetected = false; } //////////////////////////////////////////////////////////// + //Instance of ninja library + this.ninjaLibrary = NinjaLibrary; //Getting reference of chrome file system API this.chromeFileSystem = ChromeApi; //Sending size in MBs for file system storage @@ -69,36 +72,7 @@ exports.CoreIoApi = Montage.create(Component, { //Removing events this.chromeFileSystem.removeEventListener('library', this, false); // - var xhr = new XMLHttpRequest(), libs, tocopylibs = []; - //Getting known json list of libraries to copy to chrome - xhr.open("GET", '/ninja-internal/js/io/system/ninjalibrary.json', false); - xhr.send(); - //Checkng for correct reponse - if (xhr.readyState === 4) { - //Parsing json libraries - libs = JSON.parse(xhr.response); - // - if (e._event.ninjaChromeLibrary.length > 0) { - //Compare - } else { - //No library is present, must copy all - for (var i in libs.libraries) { - if (libs.libraries[i].singular) { - tocopylibs.push({name: String(libs.libraries[i].name+libs.libraries[i].version).toLowerCase(), path: libs.libraries[i].path, singular: true}); - } else { - tocopylibs.push({name: String(libs.libraries[i].name+libs.libraries[i].version).toLowerCase(), path: libs.libraries[i].path, singular: false}); - } - } - } - // - if (tocopylibs.length > 0) { - //Copy libraries - } else { - //No libraries to copy - } - } else { - //Error - } + this.ninjaLibrary.synchronize(e._event.ninjaChromeLibrary, this.chromeFileSystem); } }, //////////////////////////////////////////////////////////////////// diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js index 3f8585ed..e3d855f1 100644 --- a/js/io/system/ninjalibrary.js +++ b/js/io/system/ninjalibrary.js @@ -7,6 +7,9 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot /* ///////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// NOTES: + + Core API reference in NINJA: this.application.ninja.coreIoApi + //////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// */ // @@ -16,12 +19,98 @@ var Montage = require("montage/core/core").Montage; exports.NinjaLibrary = Montage.create(Object.prototype, { //////////////////////////////////////////////////////////////////// // - init: { + _chromeApi: { + enumerable: false, + value: null + }, + //////////////////////////////////////////////////////////////////// + // + chromeApi: { + enumerable: false, + get: function() { + return this._chromeApi; + }, + set: function(value) { + this._chromeApi = value; + } + }, + //////////////////////////////////////////////////////////////////// + // + synchronize: { + enumerable: true, + value: function(chromeLibs, chrome) { + // + this.chromeApi = chrome; + // + var i, libs, xhr = new XMLHttpRequest(), tocopylibs = []; + //Getting known json list of libraries to copy to chrome + xhr.open("GET", '/ninja-internal/js/io/system/ninjalibrary.json', false); + xhr.send(); + //Checkng for correct reponse + if (xhr.readyState === 4) { + //Parsing json libraries + libs = JSON.parse(xhr.response); + // + if (chromeLibs.length > 0) { + //Compare (always deleting for testing) + for (i=0; chromeLibs[i]; i++) { + this.chromeApi.directoryDelete(chromeLibs[i]); + } + } else { + //No library is present, must copy all + for (var j in libs.libraries) { + //name: used to folder container contents + //path: url of descriptor json or single file to load (descriptor has list of files) + //singular: indicates the path is the file to be loaded into folder + if (libs.libraries[j].file) { + tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path, file: libs.libraries[j].file}); + } else { + tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path}); + } + } + } + // + if (tocopylibs.length > 0) { + for (i=0; tocopylibs[i]; i++) { + //Checking for library to be single file + if (tocopylibs[i].file) { + //Creating root folder + this.chromeApi.directoryNew('/'+tocopylibs[i].name); + //Getting file contents + xhr = new XMLHttpRequest(); + xhr.open("GET", tocopylibs[i].path, false); + xhr.send(); + //Checking for status + if (xhr.readyState === 4) { //TODO: add check for mime type + //Creating new file from loaded content + this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response, 'text/plain'); + //this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response, 'text/plain', function (v){console.log(v)}); + } else { + //Error + } + } else { + // + } + } + } else { + //No libraries to copy + } + } else { + //Error + } + } + }/* +, + //////////////////////////////////////////////////////////////////// + // + createFolder: { enumerable: true, - value: function() { + value: function(name) { // + this.chromeApi.directoryNew(name); } } +*/ //////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// }); diff --git a/js/io/system/ninjalibrary.json b/js/io/system/ninjalibrary.json index affb72a0..87dff7f6 100644 --- a/js/io/system/ninjalibrary.json +++ b/js/io/system/ninjalibrary.json @@ -1,6 +1,6 @@ { "libraries": [ {"name": "Montage", "path": "/ninja-internal/node_modules/descriptor.json", "version": "0.6.0"}, - {"name": "RDGE", "path": "/ninja-internal/js/helper-classes/RDGE/rdge-compiled.js", "version": "0.6.0", "singular": "true"} + {"name": "RDGE", "path": "/ninja-internal/js/helper-classes/RDGE/rdge-compiled.js", "version": "0.6.0", "file": "rdge-compiled.js"} ] } \ No newline at end of file -- cgit v1.2.3