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/mediators/io-mediator.js | 85 +++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 29 deletions(-) (limited to 'js/mediators') 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