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