From 74efc1238f51e9c0ffdeffe2a349f66011ef9f17 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Thu, 9 Feb 2012 15:02:16 -0800 Subject: File Open Adding file open functionality. --- js/io/system/coreioapi.js | 10 ++++-- js/io/system/fileio.js | 68 ++++++++++++++++++++++++++++++++++-- js/mediators/io-mediator.js | 85 +++++++++++++++++++++++++++++---------------- 3 files changed, 129 insertions(+), 34 deletions(-) diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js index d9505cd6..f62133ac 100755 --- a/js/io/system/coreioapi.js +++ b/js/io/system/coreioapi.js @@ -92,9 +92,15 @@ exports.CoreIoApi = Montage.create(Component, { //TODO: Remove test //this.ninjaLibrary.copyLibToCloud('Users/kgq387/Desktop/Ninja Cloud/Disk', 'montage0.6.0'); //this.ninjaLibrary.deleteLibraries(); - window.hack = function (name, type) { + /* +window.hack = function (name, type) { this.application.ninja.ioMediator.fileNew('Users/kgq387/Desktop/Ninja Cloud/Disk/'+name+'.'+type, '/js/io/templates/files/'+type+'.txt', function (status) {console.log(status)}); }.bind(this); +*/ + window.hack = function (path) { + // + this.application.ninja.ioMediator.fileOpen('Users/kgq387/Desktop/Ninja Cloud/Disk/'+path, function (result) {console.log(result)}); + }.bind(this); } }, //////////////////////////////////////////////////////////////////// @@ -621,7 +627,7 @@ exports.CoreIoApi = Montage.create(Component, { // 200 - the file was read and its contents were returned // 404 - the file does not exist // 500 - unknown server error occurred - openFile: { + readFile: { enumerable: false, value: function(file) { // diff --git a/js/io/system/fileio.js b/js/io/system/fileio.js index 7bf4d41f..045fa2fd 100755 --- a/js/io/system/fileio.js +++ b/js/io/system/fileio.js @@ -64,13 +64,40 @@ exports.FileIo = Montage.create(Component, { // readFile: { enumerable: true, - value: function() { + value: function(file) { //Checking for API to be available if (!this.application.ninja.coreIoApi.cloudAvailable()) { //API not available, no IO action taken return null; } - // + //Peforming check for file to exist + var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create, result; + //Upon successful check, handling results + if (check.success) { + //Handling status of check + switch (check.status) { + case 204: + //File exists + result = {}; + result.content = this.application.ninja.coreIoApi.readFile(file).content; + result.details = this.infoFile(file); + status = check.status; + break; + case 404: + //File does not exists, ready to be created + status = check.status; + break; + default: + //Unknown Error + status = 500; + break; + } + } else { + //Unknown Error + status = 500; + } + //Returning resulting code + return {status: status, file: result}; } }, //////////////////////////////////////////////////////////////////// @@ -116,14 +143,49 @@ exports.FileIo = Montage.create(Component, { // infoFile: { enumerable: true, - value: function() { + value: function(file) { //Checking for API to be available if (!this.application.ninja.coreIoApi.cloudAvailable()) { //API not available, no IO action taken return null; } // + var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), details; + // + if (check.success) { + //Handling status of check + switch (check.status) { + case 204: + //File exists + details = JSON.parse(this.application.ninja.coreIoApi.isFileWritable(file).content); + details.uri = file.uri; + details.name = this.getFileNameFromPath(file.uri); + details.extension = details.name.split('.')[details.name.split('.').length-1]; + break; + case 404: + //File does not exists, ready to be created + + break; + default: + //Unknown Error + + break; + } + } else { + //Unknown Error + + } + return details; } + }, + //////////////////////////////////////////////////////////////////// + // + getFileNameFromPath : { + value: function(path) { + path = path.replace(/[/\\]$/g,""); + path = path.replace(/\\/g,"/"); + return path.substr(path.lastIndexOf('/') + 1); + } } //////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index 66fea8b3..f2f2f642 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js @@ -32,18 +32,24 @@ exports.IoMediator = Montage.create(Component, { fio: { enumerable: false, value: FileIo + }, + //////////////////////////////////////////////////////////////////// + // + pio: { + enumerable: false, + value: ProjectIo }, //////////////////////////////////////////////////////////////////// // fileNew: { enumerable: false, value: function (file, template, callback) { - // + //Loading template from template URL var xhr = new XMLHttpRequest(), result; xhr.open("GET", template, false); xhr.send(); if (xhr.readyState === 4) { - // + //Making call to create file, checking for return code switch (this.fio.newFile({uri: file, contents: xhr.response})) { case 201: result = {status: 201, success: true, uri: file}; @@ -61,8 +67,11 @@ exports.IoMediator = Montage.create(Component, { } else { result = {status: 500, success: false, uri: file}; } - // + //Sending result to callback if requested for handling if (callback) callback(result); + //Codes + // 204: File exists | 400: File exists + // 201: File succesfully created | 500: Unknown (Probably cloud API not running) } }, //////////////////////////////////////////////////////////////////// @@ -70,23 +79,50 @@ exports.IoMediator = Montage.create(Component, { fileOpen: { enumerable: false, value: function (file, callback) { - var response = "", fileContent="", filename="", fileType="js", returnObj=null; - - response = this.application.ninja.coreIoApi.openFile({"uri":file.uri}); - if((response.success === true) && ((response.status === 200) || (response.status === 304))){ - fileContent = response.content; - } - - - //TODO : format html content to render in design view - - - filename = this.getFileNameFromPath(file.uri); - if(file.uri.indexOf('.') != -1){ - fileType = file.uri.substr(file.uri.lastIndexOf('.') + 1); - } - returnObj = {"type": ""+fileType, "name": ""+filename, "source": fileContent, "uri": file.uri}; - callback(returnObj); + //Reading file (Ninja doesn't really open a file, all in browser memory) + var read = this.fio.readFile({uri: file}), result; + //Checking for status + switch(read.status) { + case 204: + //Creating and formatting result object for callbak + result = read.file.details; + //TODO: Add handling for converting HTML to Ninja format + result.content = read.file.content; + result.status = read.status; + // + if (callback) callback(result); + break; + case 404: + //File does not exists + if (callback) callback({status: read.status}); + break; + default: + //Unknown + if (callback) callback({status: 500}); + break; + } + /* + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + //Return Object Description + Object.status (Always presents for handling) + 204: File exists (Success) + 404: File does not exists (Failure) + 500: Unknown (Probably cloud API not running) + + (Below only present if succesfull 204) + + Object.content + Object.extension + Object.name + Object.uri + Object.creationDate + Object.modifiedDate + Object.readOnly + Object.size + //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// + */ } }, //////////////////////////////////////////////////////////////////// @@ -104,15 +140,6 @@ exports.IoMediator = Montage.create(Component, { value: function (file, copy, callback) { // } - }, - //////////////////////////////////////////////////////////////////// - ///// Return the last part of a path (e.g. filename) - getFileNameFromPath : { - value: function(path) { - path = path.replace(/[/\\]$/g,""); - path = path.replace(/\\/g,"/"); - return path.substr(path.lastIndexOf('/') + 1); - } } //////////////////////////////////////////////////////////////////// }); -- cgit v1.2.3