diff options
Diffstat (limited to 'js/io/system/coreioapi.js')
-rwxr-xr-x | js/io/system/coreioapi.js | 906 |
1 files changed, 906 insertions, 0 deletions
diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js new file mode 100755 index 00000000..1585fc33 --- /dev/null +++ b/js/io/system/coreioapi.js | |||
@@ -0,0 +1,906 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /* ///////////////////////////////////////////////////////////////////// | ||
8 | //////////////////////////////////////////////////////////////////////// | ||
9 | NOTES: | ||
10 | These methods should only be access through the file and project IO classes. | ||
11 | //////////////////////////////////////////////////////////////////////// | ||
12 | ///////////////////////////////////////////////////////////////////// */ | ||
13 | var Montage = require("montage/core/core").Montage, | ||
14 | Component = require("montage/ui/component").Component; | ||
15 | //////////////////////////////////////////////////////////////////////// | ||
16 | //Exporting as Project I/O | ||
17 | exports.CoreIoApi = Montage.create(Component, { | ||
18 | //////////////////////////////////////////////////////////////////// | ||
19 | // | ||
20 | deserializedFromTemplate: { | ||
21 | enumerable: false, | ||
22 | value: function () { | ||
23 | //////////////////////////////////////////////////////////// | ||
24 | |||
25 | //TODO: Add logic for getting rooUrl from local storage | ||
26 | |||
27 | //////////////////////////////////////////////////////////// | ||
28 | |||
29 | |||
30 | |||
31 | //Checking for status of I/O API | ||
32 | this.ioDetected = this.isActive(); | ||
33 | //TODO: Add welcome screen logic, probably externally | ||
34 | } | ||
35 | }, | ||
36 | //////////////////////////////////////////////////////////////////// | ||
37 | //Method to check status of I/O API, will return false if not active | ||
38 | isActive: { | ||
39 | enumerable: false, | ||
40 | value: function () { | ||
41 | //Doing a directory root check, a 200 status means running | ||
42 | if (this.getDirectoryContents({uri:'/'}).status === 200) { | ||
43 | return true; | ||
44 | } else { | ||
45 | return false; | ||
46 | } | ||
47 | } | ||
48 | }, | ||
49 | //////////////////////////////////////////////////////////////////// | ||
50 | //Root API URL | ||
51 | _ioDetected: { | ||
52 | enumerable: false, | ||
53 | value: false | ||
54 | }, | ||
55 | //////////////////////////////////////////////////////////////////// | ||
56 | // | ||
57 | ioDetected: { | ||
58 | enumerable: false, | ||
59 | get: function() { | ||
60 | return this._ioDetected; | ||
61 | }, | ||
62 | set: function(value) { | ||
63 | this._ioDetected = value; | ||
64 | } | ||
65 | }, | ||
66 | //////////////////////////////////////////////////////////////////// | ||
67 | //Root API URL | ||
68 | _rootUrl: { | ||
69 | enumerable: false, | ||
70 | value: 'http://localhost:16380' | ||
71 | }, | ||
72 | //////////////////////////////////////////////////////////////////// | ||
73 | // | ||
74 | rootUrl: { | ||
75 | enumerable: false, | ||
76 | get: function() { | ||
77 | return this._rootUrl; | ||
78 | }, | ||
79 | set: function(value) { | ||
80 | this._rootUrl = value; | ||
81 | } | ||
82 | }, | ||
83 | //////////////////////////////////////////////////////////////////// | ||
84 | //File service API URL | ||
85 | _fileServiceURL: { | ||
86 | enumerable: false, | ||
87 | value: '/file' | ||
88 | }, | ||
89 | //////////////////////////////////////////////////////////////////// | ||
90 | // | ||
91 | fileServiceURL: { | ||
92 | enumerable: false, | ||
93 | get: function() { | ||
94 | return this.rootUrl+this._fileServiceURL; | ||
95 | }, | ||
96 | set: function(value) { | ||
97 | this._fileServiceURL = value; | ||
98 | } | ||
99 | }, | ||
100 | //////////////////////////////////////////////////////////////////// | ||
101 | //Directory service API URL | ||
102 | _directoryServiceURL: { | ||
103 | enumerable: false, | ||
104 | value: '/directory' | ||
105 | }, | ||
106 | //////////////////////////////////////////////////////////////////// | ||
107 | // | ||
108 | directoryServiceURL: { | ||
109 | enumerable: false, | ||
110 | get: function() { | ||
111 | return this.rootUrl+this._directoryServiceURL; | ||
112 | }, | ||
113 | set: function(value) { | ||
114 | this._directoryServiceURL = value; | ||
115 | } | ||
116 | }, | ||
117 | //////////////////////////////////////////////////////////////////// | ||
118 | // private helper to parse URIs and append them to the service URL | ||
119 | _prepareServiceURL: { | ||
120 | enumerable: false, | ||
121 | value: function(serviceURL, path) { | ||
122 | var urlOut = path.replace(/\\/g,"/"); | ||
123 | urlOut = urlOut.replace(/:/g,""); | ||
124 | urlOut = encodeURI(urlOut); | ||
125 | //add leading / if not already there | ||
126 | if((urlOut.length > 0) && (urlOut.charAt(0) !== "/")){ | ||
127 | urlOut = "/" + urlOut; | ||
128 | } | ||
129 | //remove extra / at the end | ||
130 | if((urlOut.length > 1) && (urlOut.charAt(urlOut.length - 1) === "/")){ | ||
131 | urlOut = urlOut.substring(0, (urlOut.length - 1)); | ||
132 | } | ||
133 | |||
134 | return serviceURL + urlOut; | ||
135 | } | ||
136 | }, | ||
137 | //////////////////////////////////////////////////////////////////// | ||
138 | // Checks for the existence of a file | ||
139 | // Parameters: | ||
140 | // the file parameter must contain the following properties | ||
141 | // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" | ||
142 | // | ||
143 | // Return values: | ||
144 | // returns an object with two properties | ||
145 | // success: boolean indicating if the call succeeded or failed | ||
146 | // status: int indicating the request HTTP status code | ||
147 | // 204 - the file exists | ||
148 | // 404 - the file does not exist | ||
149 | // 500 - unknown server error occurred | ||
150 | fileExists: { | ||
151 | enumerable: false, | ||
152 | value: function(file) { | ||
153 | // | ||
154 | var retValue = { success:null, status:null }; | ||
155 | // | ||
156 | if(file && file.uri && file.uri.length) { | ||
157 | try { | ||
158 | var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), | ||
159 | xhr = new XMLHttpRequest(); | ||
160 | // | ||
161 | xhr.open("GET", serviceURL, false); | ||
162 | xhr.setRequestHeader("check-existence-only", "true"); | ||
163 | xhr.send(); | ||
164 | // | ||
165 | if (xhr.readyState === 4) { | ||
166 | retValue.status = xhr.status; | ||
167 | retValue.success = true; | ||
168 | } | ||
169 | } | ||
170 | catch(error) { | ||
171 | xhr = null; | ||
172 | retValue.success = false; | ||
173 | } | ||
174 | } | ||
175 | // | ||
176 | return retValue; | ||
177 | } | ||
178 | }, | ||
179 | //////////////////////////////////////////////////////////////////// | ||
180 | // Creates a new file at the specified path | ||
181 | // Parameters: | ||
182 | // the file parameter must contain the following properties | ||
183 | // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" | ||
184 | // it can optionally contain the following properties | ||
185 | // contentType: string with the content type i.e. "text/plain". "text/plain" is assumed if this property is not specified | ||
186 | // contents: string containing the file contents. These contents will be saved to the new file. | ||
187 | // | ||
188 | // Return values: | ||
189 | // returns an object with two properties | ||
190 | // success: boolean indicating if the call succeeded or failed | ||
191 | // status: int indicating the request HTTP status code | ||
192 | // 201 - the file was created and contents were saved if they were passed | ||
193 | // 400 - the file already exists and could not be created | ||
194 | // 500 - unknown server error occurred | ||
195 | createFile: { | ||
196 | enumerable: false, | ||
197 | value: function(file) { | ||
198 | var retValue = { success:null, status:null }; | ||
199 | if(file && file.uri && file.uri.length) { | ||
200 | try { | ||
201 | var serviceURL = this._prepareServiceURL(this.fileServiceURL, file.uri), | ||
202 | xhr = new XMLHttpRequest(); | ||
203 | // | ||
204 | xhr.open("POST", serviceURL, false); | ||
205 | if(file.contentType && file.contentType.length) | ||
206 | xhr.setRequestHeader("Content-Type", file.contentType); | ||
207 | else | ||
208 | xhr.setRequestHeader("Content-Type", "text/plain"); | ||
209 | |||
210 | if(file.contents && file.contents.length) | ||
211 | xhr.send(file.contents); | ||
212 | else | ||
213 | xhr.send(); | ||
214 | |||
215 | if (xhr.readyState === 4) { | ||
216 | retValue.status = xhr.status; | ||
217 | retValue.success = true; | ||
218 | } | ||
219 | } | ||
220 | catch(error) { | ||
221 | xhr = null; | ||
222 | retValue.success = false; | ||
223 | } | ||
224 | } | ||
225 | |||
226 | return retValue; | ||
227 | } | ||
228 | }, | ||
229 | //////////////////////////////////////////////////////////////////// | ||
230 | // Save contents into an existing file at the specified path | ||
231 | // Parameters: | ||
232 | // the file parameter must contain the following properties | ||
233 | // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" | ||
234 | // it can optionally contain the following properties | ||
235 | // contentType: string with the content type i.e. "text/plain". "text/plain" is assumed if this property is not specified | ||
236 | // contents: string containing the file contents. These contents will be saved to the new file. | ||
237 | // | ||
238 | // Return values: | ||