diff options
author | Jose Antonio Marquez | 2012-02-09 15:02:16 -0800 |
---|---|---|
committer | Jose Antonio Marquez | 2012-02-09 15:02:16 -0800 |
commit | 74efc1238f51e9c0ffdeffe2a349f66011ef9f17 (patch) | |
tree | e50598ebfe139b7cda4a3ff9702d7d3243cbd6b2 /js | |
parent | 44c65bc6bbd5b754f9073f93a3afca8dd9acc864 (diff) | |
download | ninja-74efc1238f51e9c0ffdeffe2a349f66011ef9f17.tar.gz |
File Open
Adding file open functionality.
Diffstat (limited to 'js')
-rwxr-xr-x | js/io/system/coreioapi.js | 10 | ||||
-rwxr-xr-x | js/io/system/fileio.js | 68 | ||||
-rw-r--r-- | 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, { | |||
92 | //TODO: Remove test | 92 | //TODO: Remove test |
93 | //this.ninjaLibrary.copyLibToCloud('Users/kgq387/Desktop/Ninja Cloud/Disk', 'montage0.6.0'); | 93 | //this.ninjaLibrary.copyLibToCloud('Users/kgq387/Desktop/Ninja Cloud/Disk', 'montage0.6.0'); |
94 | //this.ninjaLibrary.deleteLibraries(); | 94 | //this.ninjaLibrary.deleteLibraries(); |
95 | window.hack = function (name, type) { | 95 | /* |
96 | window.hack = function (name, type) { | ||
96 | this.application.ninja.ioMediator.fileNew('Users/kgq387/Desktop/Ninja Cloud/Disk/'+name+'.'+type, '/js/io/templates/files/'+type+'.txt', function (status) {console.log(status)}); | 97 | this.application.ninja.ioMediator.fileNew('Users/kgq387/Desktop/Ninja Cloud/Disk/'+name+'.'+type, '/js/io/templates/files/'+type+'.txt', function (status) {console.log(status)}); |
97 | }.bind(this); | 98 | }.bind(this); |
99 | */ | ||
100 | window.hack = function (path) { | ||
101 | // | ||
102 | this.application.ninja.ioMediator.fileOpen('Users/kgq387/Desktop/Ninja Cloud/Disk/'+path, function (result) {console.log(result)}); | ||
103 | }.bind(this); | ||
98 | } | 104 | } |
99 | }, | 105 | }, |
100 | //////////////////////////////////////////////////////////////////// | 106 | //////////////////////////////////////////////////////////////////// |
@@ -621,7 +627,7 @@ exports.CoreIoApi = Montage.create(Component, { | |||
621 | // 200 - the file was read and its contents were returned | 627 | // 200 - the file was read and its contents were returned |
622 | // 404 - the file does not exist | 628 | // 404 - the file does not exist |
623 | // 500 - unknown server error occurred | 629 | // 500 - unknown server error occurred |
624 | openFile: { | 630 | readFile: { |
625 | enumerable: false, | 631 | enumerable: false, |
626 | value: function(file) { | 632 | value: function(file) { |
627 | // | 633 | // |
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, { | |||
64 | // | 64 | // |
65 | readFile: { | 65 | readFile: { |
66 | enumerable: true, | 66 | enumerable: true, |
67 | value: function() { | 67 | value: function(file) { |
68 | //Checking for API to be available | 68 | //Checking for API to be available |
69 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { | 69 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { |
70 | //API not available, no IO action taken | 70 | //API not available, no IO action taken |
71 | return null; | 71 | return null; |
72 | } | 72 | } |
73 | // | 73 | //Peforming check for file to exist |
74 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create, result; | ||
75 | //Upon successful check, handling results | ||
76 | if (check.success) { | ||
77 | //Handling status of check | ||
78 | switch (check.status) { | ||
79 | case 204: | ||
80 | //File exists | ||
81 | result = {}; | ||
82 | result.content = this.application.ninja.coreIoApi.readFile(file).content; | ||
83 | result.details = this.infoFile(file); | ||
84 | status = check.status; | ||
85 | break; | ||
86 | case 404: | ||
87 | //File does not exists, ready to be created | ||
88 | status = check.status; | ||
89 | break; | ||
90 | default: | ||
91 | //Unknown Error | ||
92 | status = 500; | ||
93 | break; | ||
94 | } | ||
95 | } else { | ||
96 | //Unknown Error | ||
97 | status = 500; | ||
98 | } | ||
99 | //Returning resulting code | ||
100 | return {status: status, file: result}; | ||
74 | } | 101 | } |
75 | }, | 102 | }, |
76 | //////////////////////////////////////////////////////////////////// | 103 | //////////////////////////////////////////////////////////////////// |
@@ -116,14 +143,49 @@ exports.FileIo = Montage.create(Component, { | |||
116 | // | 143 | // |
117 | infoFile: { | 144 | infoFile: { |
118 | enumerable: true, | 145 | enumerable: true, |
119 | value: function() { | 146 | value: function(file) { |
120 | //Checking for API to be available | 147 | //Checking for API to be available |
121 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { | 148 | if (!this.application.ninja.coreIoApi.cloudAvailable()) { |
122 | //API not available, no IO action taken | 149 | //API not available, no IO action taken |
123 | return null; | 150 | return null; |
124 | } | 151 | } |
125 | // | 152 | // |
153 | var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), details; | ||
154 | // | ||
155 | if (check.success) { | ||
156 | //Handling status of check | ||
157 | switch (check.status) { | ||
158 | case 204: | ||
159 | //File exists | ||
160 | details = JSON.parse(this.application.ninja.coreIoApi.isFileWritable(file).content); | ||
161 | details.uri = file.uri; | ||
162 | details.name = this.getFileNameFromPath(file.uri); | ||
163 | details.extension = details.name.split('.')[details.name.split('.').length-1]; | ||
164 | break; | ||
165 | case 404: | ||
166 | //File does not exists, ready to be created | ||
167 | |||
168 | break; | ||
169 | default: | ||
170 | //Unknown Error | ||
171 | |||
172 | break; | ||
173 | } | ||
174 | } else { | ||
175 | //Unknown Error | ||
176 | |||
177 | } | ||
178 | return details; | ||
126 | } | 179 | } |
180 | }, | ||
181 | //////////////////////////////////////////////////////////////////// | ||
182 | // | ||
183 | getFileNameFromPath : { | ||
184 | value: function(path) { | ||
185 | path = path.replace(/[/\\]$/g,""); | ||
186 | path = path.replace(/\\/g,"/"); | ||
187 | return path.substr(path.lastIndexOf('/') + 1); | ||
188 | } | ||
127 | } | 189 | } |
128 | //////////////////////////////////////////////////////////////////// | 190 | //////////////////////////////////////////////////////////////////// |
129 | //////////////////////////////////////////////////////////////////// | 191 | //////////////////////////////////////////////////////////////////// |
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 | |||
@@ -33,17 +33,23 @@ exports.IoMediator = Montage.create(Component, { | |||
33 | enumerable: false, | 33 | enumerable: false, |
34 | value: FileIo | 34 | value: FileIo |
35 | }, | 35 | }, |
36 | //////////////////////////////////////////////////////////////////// | ||
37 | // | ||
38 | pio: { | ||
39 | enumerable: false, | ||
40 | value: ProjectIo | ||
41 | }, | ||
36 | //////////////////////////////////////////////////////////////////// | 42 | //////////////////////////////////////////////////////////////////// |
37 | // | 43 | // |
38 | fileNew: { | 44 | fileNew: { |
39 | enumerable: false, | 45 | enumerable: false, |
40 | value: function (file, template, callback) { | 46 | value: function (file, template, callback) { |
41 | // | 47 | //Loading template from template URL |
42 | var xhr = new XMLHttpRequest(), result; | 48 | var xhr = new XMLHttpRequest(), result; |
43 | xhr.open("GET", template, false); | 49 | xhr.open("GET", template, false); |
44 | xhr.send(); | 50 | xhr.send(); |
45 | if (xhr.readyState === 4) { | 51 | if (xhr.readyState === 4) { |
46 | // | 52 | //Making call to create file, checking for return code |
47 | switch (this.fio.newFile({uri: file, contents: xhr.response})) { | 53 | switch (this.fio.newFile({uri: file, contents: xhr.response})) { |
48 | case 201: | 54 | case 201: |
49 | result = {status: 201, success: true, uri: file}; | 55 | result = {status: 201, success: true, uri: file}; |
@@ -61,8 +67,11 @@ exports.IoMediator = Montage.create(Component, { | |||
61 | } else { | 67 | } else { |
62 | result = {status: 500, success: false, uri: file}; | 68 | result = {status: 500, success: false, uri: file}; |
63 | } | 69 | } |
64 | // | 70 | //Sending result to callback if requested for handling |
65 | if (callback) callback(result); | 71 | if (callback) callback(result); |
72 | //Codes | ||
73 | // 204: File exists | 400: File exists | ||
74 | // 201: File succesfully created | 500: Unknown (Probably cloud API not running) | ||
66 | } | 75 | } |
67 | }, | 76 | }, |
68 | //////////////////////////////////////////////////////////////////// | 77 | //////////////////////////////////////////////////////////////////// |
@@ -70,23 +79,50 @@ exports.IoMediator = Montage.create(Component, { | |||
70 | fileOpen: { | 79 | fileOpen: { |
71 | enumerable: false, | 80 | enumerable: false, |
72 | value: function (file, callback) { | 81 | value: function (file, callback) { |
73 | var response = "", fileContent="", filename="", fileType="js", returnObj=null; | 82 | //Reading file (Ninja doesn't really open a file, all in browser memory) |
74 | 83 | var read = this.fio.readFile({uri: file}), result; | |
75 | response = this.application.ninja.coreIoApi.openFile({"uri":file.uri}); | 84 | //Checking for status |
76 | if((response.success === true) && ((response.status === 200) || (response.status === 304))){ | 85 | switch(read.status) { |
77 | fileContent = response.content; | 86 | case 204: |
78 | } | 87 | //Creating and formatting result object for callbak |
79 | 88 | result = read.file.details; | |
80 | 89 | //TODO: Add handling for converting HTML to Ninja format | |
81 | //TODO : format html content to render in design view | 90 | result.content = read.file.content; |
82 | 91 | result.status = read.status; | |
83 | 92 | // | |
84 | filename = this.getFileNameFromPath(file.uri); | 93 | if (callback) callback(result); |
85 | if(file.uri.indexOf('.') != -1){ | 94 | break; |
86 | fileType = file.uri.substr(file.uri.lastIndexOf('.') + 1); | 95 | case 404: |
87 | } | 96 | //File does not exists |
88 | returnObj = {"type": ""+fileType, "name": ""+filename, "source": fileContent, "uri": file.uri}; | 97 | if (callback) callback({status: read.status}); |
89 | callback(returnObj); | 98 | break; |
99 | default: | ||
100 | //Unknown | ||
101 | if (callback) callback({status: 500}); | ||
102 | break; | ||
103 | } | ||