-
Notifications
You must be signed in to change notification settings - Fork 3
/
saveToCloud.js
339 lines (319 loc) · 11.1 KB
/
saveToCloud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/** Cloud saver is a helper class that has all the built in functions to
save projects to the api. Use like cloud = new CloudSaver();
You can optionally specify the URLs to use.
@param {String} optionalProjAPIURL - URL of project list / create api
@param {String} optionalFileAPIURL - URL of file api
@param {String} optionalLoginUrl - URL of login api
@param {String} optionalLoadProjURL - URL of project load api
@param {String} optionalUserAPIURL - URL of user api
@param {String} optionalGISDSURL - URL of gis list datasets api
@param {String} optionalGISPolyURL - URL of the api that returns GIS polys
@param {String} optionalGISPointURL - URL of the api that returns GIS points
@param {String} optionalLogoutAPIURL - URL of the api that logs you out
*/
function CloudSaver(optionalProjAPIURL,
optionalFileAPIURL,
optionalLoginUrl,
optionalLoadProjURL,
optionalUserAPIURL,
optionalGISDSURL,
optionalGISPolyURL,
optionalGISPointURL,
optionalLogoutAPIURL
) {
if (optionalProjAPIURL) this.ProjAPIURL = optionalProjAPIURL;
else this.projAPIURL = '/api/projects/';
if (optionalFileAPIURL) this.fileAPIURL = optionalFileAPIURL;
else this.fileAPIURL = '/api/files/';
if (optionalLoginUrl) this.loginUrl = optionalLoginUrl;
else this.loginUrl = '/accounts/login/';
if (optionalLoadProjURL) this.loadProjURL = optionalLoginUrl;
else this.loadProjURL = '/projects/';
if (optionalUserAPIURL) this.userAPIURL = optionalUserAPIURL;
else this.userAPIURL = '/api/user';
if (optionalUserAPIURL) this.gisDSURL = optionalGISDSURL;
else this.gisDSURL = '/api-gis/api-ds/';
if (optionalUserAPIURL) this.gisPolyURL = optionalGISPolyURL;
else this.gisPolyURL = '/api-gis/api-poly/';
if (optionalUserAPIURL) this.gisPointURL = optionalGISPointURL;
else this.gisPointURL = '/api-gis/api-mp/';
if (optionalLogoutAPIURL) this.logoutAPIURL = optionalLogoutAPIURL;
else this.logoutAPIURL = '/accounts/logout/';
this.getCSRFToken();
};
/** Log in does what it sounds like, makes a post to the API to log you in,
follow up with get CSRF or Get user data.
@param {String} username - Username to log in with
@param {String} password - Password to log in with
@param {function} callBack - The returned function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.login = function(username,
password,
callBack,
errorCallBack) {
$.post(this.loginUrl, {'login': username, 'password': password},
callBack).fail(errorCallBack);
};
/** Use this to allow other API calls besides login */
CloudSaver.prototype.getCSRFToken = function() {
/** gets a cookie of a specific type from the page
@param {String} name - should pretty much always be csrftoken
@return {String} - returns the cookie
*/
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie != '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(
name.length + 1));
break;
}
}
}
return cookieValue;
}
const csrftoken = getCookie('csrftoken');
/** tests if this is csrf safe
@param {String} method - stests the given method
@return {Boolean} - is safe
*/
function csrfSafeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
/** test that a given url is a same-origin URL
@param {String} url - the URL to test
@return {Boolean} - is same origin
*/
function sameOrigin(url) {
const host = document.location.host; // host + port
const protocol = document.location.protocol;
const srOrigin = '//' + host;
const origin = protocol + srOrigin;
return (url == origin ||
url.slice(0, origin.length + 1) == origin + '/') ||
(url == srOrigin ||
url.slice(0, srOrigin.length + 1) == srOrigin + '/') ||
!(/^(\/\/|http:|https:).*/.test(url));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader('X-CSRFToken', csrftoken);
}
},
});
};
/** Saves a file to the server, save the ID for use with create / update project
@param {String} file - The data to be uploaded
@param {function} callBack - The returned function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.saveFile = function(file, callBack, errorCallBack) {
$.ajax({
type: 'PUT',
url: this.fileAPIURL,
data: file,
processData: false,
contentType: false,
success: callBack,
}).fail(errorCallBack);
};
/** Make a project to be able to find your saved file again, returns the details
of the project created, including ID for updating
@param {String} projectName - Name of your project
@param {int} applicationID - The number of the application you're using
@param {String} dataID - The file location from save file call back
@param {String} imgID - The image file location important for viewing projects
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.createProject = function(projectName,
applicationID,
dataID,
imgID,
callBack,
errorCallBack) {
$.post(this.projAPIURL, {
name: projectName,
description: '',
classroom: '',
application: applicationID,
project: dataID,
screenshot: imgID,
}, callBack, 'json').fail(errorCallBack);
};
/** Update a project instead of making a new one
@param {int} projectID - ID of the number to be updated
@param {String} projectName - Name of your project
@param {int} applicationID - The number of the application you're using
@param {String} dataID - The file location from save file call back
@param {String} imgID - The image file location important for viewing projects
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.updateProject = function(projectID,
projectName,
applicationID,
dataID,
imgID,
callBack,
errorCallBack) {
$.ajax({
type: 'PUT',
url: this.projAPIURL+projectID+'/',
data: {
name: projectName,
description: '',
classroom: null,
application: applicationID,
project: dataID,
screenshot: imgID,
},
success: callBack,
dataType: 'json',
}).fail(errorCallBack);
};
/** Already got a project, no problem, just load it with this function
@param {int} projectID - ID of the number to be updated
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.loadProject = function(projectID,
callBack,
errorCallBack) {
$.get(this.projAPIURL + projectID + '/', null, function(data) {
$.get(data.project_url, null,
callBack).fail(errorCallBack);
}).fail(errorCallBack);
};
/** Get the list of projects for the current user, must be signed in
@param {int} userID - ID of the number of user
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.listProject = function(userID, callBack, errorCallBack) {
$.get(this.projAPIURL+'?owner='+userID, null,
callBack, 'json').fail(errorCallBack);
};
/** Signed in, but don't know which user you are, call this
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.getUser = function(callBack, errorCallBack) {
$.ajax({
dataType: 'json',
url: this.userAPIURL,
success: callBack,
}).fail(errorCallBack);
};
/** Reports the list of GIS datasets available
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.getGISDatasets = function(callBack, errorCallBack) {
$.ajax({
dataType: 'json',
url: this.gisDSURL,
success: callBack,
}).fail(errorCallBack);
};
/** Reports the list of GIS datasets available
@param {int} dataset - The name of the dataset to query
@param {float} minLat - Minimum latitude to fetch
@param {float} maxLat - Maximum latitude to fetch
@param {float} minLong - Minimum longitude to fetch
@param {float} maxLong - Maximum longitude to fetch
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
@param {string} optionalTags - CSV list of tags you want to filter by
*/
CloudSaver.prototype.getGISPolys = function(dataset,
minLat,
maxLat,
minLong,
maxLong,
callBack,
errorCallBack,
optionalTags) {
let query = this.gisPolyURL +
'?dataset=' + dataset +
'&min_lat=' + minLat +
'&max_lat=' + maxLat +
'&min_lon=' + minLong +
'&max_lon=' + maxLong;
if (optionalTags) {
query += '?tags=' + optionalTags;
}
$.ajax({
dataType: 'json',
url: query,
success: callBack,
}).fail(errorCallBack);
};
/** Reports the list of GIS datasets available
@param {int} dataset - The name of the dataset to query
@param {float} minLat - Minimum latitude to fetch
@param {float} maxLat - Maximum latitude to fetch
@param {float} minLong - Minimum longitude to fetch
@param {float} maxLong - Maximum longitude to fetch
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
@param {string} optionalTags - CSV list of tags you want to filter by
*/
CloudSaver.prototype.getGISPoints = function(dataset,
minLat,
maxLat,
minLong,
maxLong,
callBack,
errorCallBack,
optionalTags) {
let query = this.gisPointURL +
'?dataset=' + dataset +
'&min_lat=' + minLat +
'&max_lat=' + maxLat +
'&min_lon=' + minLong +
'&max_lon=' + maxLong;
if (optionalTags) {
query += '?tags=' + optionalTags;
}
$.ajax({
dataType: 'json',
url: query,
success: callBack,
}).fail(errorCallBack);
};
/** Don't want to bother writing your own login? Here is one that returns user
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.loginPopup = function(callBack, errorCallBack) {
const username = prompt('Enter your username', '');
if (!username) {
alert('No username entered, signin aborted');
return;
}
const password = prompt('Hello ' + username + ', enter your password', '');
if (!password) {
alert('No password entered, signin aborted');
return;
}
const myself = this;
this.login(username, password, function(data) {
myself.getUser(callBack, errorCallBack);
},
errorCallBack
);
};
/** Want to logout, no worries, you're not trapped anymore
@param {function} callBack - The return function
@param {function} errorCallBack - If there is an error
*/
CloudSaver.prototype.logout = function(callBack, errorCallBack) {
$.post(this.logoutAPIURL, {}, callBack, 'json').fail(errorCallBack);
};