-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathardublocklyserver_ajax.js
More file actions
345 lines (319 loc) · 12.6 KB
/
Copy pathardublocklyserver_ajax.js
File metadata and controls
345 lines (319 loc) · 12.6 KB
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
340
341
342
343
344
345
/**
* @license Licensed under the Apache License, Version 2.0 (the "License"):
* http://www.apache.org/licenses/LICENSE-2.0
*
* @fileoverview Ajax calls to the Ardublockly Server python program.
*/
'use strict';
/** Create a name space for the application. */
var ArdublocklyServer = {};
/**
* Reads JSON data from the server and forwards formatted JavaScript object.
* @param {!string} url Location for the JSON data.
* @param {!function} jsonDataCb Callback with JSON object or null for error.
*/
ArdublocklyServer.getJson = function(url, callback) {
ArdublocklyServer.sendRequest(url, 'GET', 'application/json', null, callback);
};
/**
* Sends JSON data to the ArduBlocklyServer.
* @param {!string} url Requestor URL.
* @param {!string} json JSON string.
* @param {!function} callback Request callback function.
*/
ArdublocklyServer.putJson = function(url, json, callback) {
ArdublocklyServer.sendRequest(url, 'PUT', 'application/json', json, callback);
};
/**
* Sends a request to the Ardubloockly Server that returns a JSON response.
* @param {!string} url Requestor URL.
* @param {!string} method HTTP method.
* @param {!string} contentType HTTP content type.
* @param {string} jsonObjSend JavaScript object to be parsed into JSON to send.
* @param {!function} cb Request callback function, takes a single input for a
* parsed JSON object.
*/
ArdublocklyServer.sendRequest = function(
url, method, contentType, jsonObjSend, cb) {
var request = ArdublocklyServer.createRequest();
// The data received is JSON, so it needs to be converted into the right
// format to be displayed in the page.
var onReady = function() {
if (request.readyState == 4) {
if (request.status == 200) {
var jsonObjReceived = null;
try {
jsonObjReceived = JSON.parse(request.responseText);
} catch(e) {
console.error('Incorrectly formatted JSON data from ' + url);
throw e;
}
cb(jsonObjReceived);
} else {
// alert("Check Filename.")
// window.location = "/";
// return false;
// return a null element which will be dealt with in the front end
cb(null);
}
}
};
try {
request.open(method, url, true);
request.setRequestHeader('Content-type', contentType);
request.onreadystatechange = onReady;
request.send(JSON.stringify(jsonObjSend));
} catch (e) {
// Nullify callback to indicate error
cb(null);
throw e;
}
};
/** @return {XMLHttpRequest} An XML HTTP Request multi-browser compatible. */
ArdublocklyServer.createRequest = function() {
var request = null;
try {
// Firefox, Chrome, IE7+, Opera, Safari
request = new XMLHttpRequest();
}
catch (e) {
// IE6 and earlier
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {
throw 'Your browser does not support AJAX. You will not be able to' +
'use all of Ardublockly features.';
request = null;
}
}
}
return request;
};
/**
* Creates an HTML element based on the JSON data received from the server.
* @param {!string} json_data A string containing the JSON data to be parsed.
* @return {!element} An HTML element, which type depends on the JSON 'element'
* key (currently only text input or drop down).
*/
ArdublocklyServer.jsonToIdeModal = function(jsonObj) {
if (!jsonObj) return null;
var elTitle = document.createElement('h4');
elTitle.className = (jsonObj && jsonObj.success) ? 'arduino_dialog_success' :
'arduino_dialog_failure';
var elStdOp = document.createElement('span');
elStdOp.className = 'arduino_dialog_out';
var elErrOp = document.createElement('span');
elErrOp.className = 'arduino_dialog_out_error';
// Add the Standard and Error outputs
var ideData = jsonObj.ide_data;
if (ideData && (ideData.std_output !== undefined) &&
(ideData.err_output !== undefined)) {
elStdOp.innerHTML = ideData.std_output.split('\n').join('<br />');
elErrOp.innerHTML = ideData.err_output.split('\n').join('<br />');
} else {
console.error(jsonObj);
console.error('The IDE out JSON response does not have valid "ide_data".');
}
if (jsonObj.errors) {
// Prepare error message
elTitle.innerHTML = Ardublockly.getLocalStr('arduinoOpErrorTitle');
var errStr = [];
for (var i = 0; i < jsonObj.errors.length; i++) {
var errorContext = 'Unrecognised error.';
try {
errorContext = Ardublockly.getLocalStr(
'arduinoOpErrorIdContext_' + jsonObj.errors[i].id);
} catch (e) {
// Swallow the exception, could be expanded to try to figure out issue
}
errStr.push('\nError id ' + jsonObj.errors[i].id + ': ' + errorContext);
}
elErrOp.innerHTML += '<br />' + errStr.join('<br />');
} else if (jsonObj.success && jsonObj.ide_mode) {
// Format a successful response
if (jsonObj.ide_mode == 'upload') {
elTitle.innerHTML = Ardublockly.getLocalStr('arduinoOpUploadedTitle');
} else if (jsonObj.ide_mode == 'verify') {
elTitle.innerHTML = Ardublockly.getLocalStr('arduinoOpVerifiedTitle');
} else if (jsonObj.ide_mode == 'open') {
elTitle.innerHTML = Ardublockly.getLocalStr('arduinoOpOpenedTitle');
// This is a corner case where we also add to the stand out
elStdOp.innerHTML += Ardublockly.getLocalStr('arduinoOpOpenedBody');
} else {
elTitle.innerHTML = Ardublockly.getLocalStr('arduinoOpErrorTitle');
}
} else {
console.error(jsonObj);
console.error('Unexpected response format, printed above.');
}
var element = document.createElement('div');
element.appendChild(elTitle);
element.appendChild(elStdOp);
element.appendChild(elErrOp);
return element;
};
ArdublocklyServer.jsonToHtmlTextInput = function(jsonObj) {
var element = null;
if (jsonObj) {
// Simple text input
element = document.createElement('input');
element.setAttribute('type', 'text');
element.style.cssText = '';
if (jsonObj.errors) {
element.setAttribute('value', '');
element.style.cssText = 'border-bottom: 1px solid #f75c51;' +
'box-shadow: 0 1px 0 0 #d73c30;';
} else {
element.setAttribute('value', jsonObj.selected || '');
}
}
return element;
};
ArdublocklyServer.jsonToHtmlDropdown = function(jsonObj) {
var element = null;
if (!jsonObj) {
console.error('Invalid JSON received from server.');
} else if(jsonObj.errors) {
console.error('There are errors in the JSON response from server.');
console.error(jsonObj);
} else {
// Drop down list of unknown length with a selected item
element = document.createElement('select');
element.name = jsonObj.settings_type;
for (var i = 0; i < jsonObj.options.length; i++) {
if (jsonObj.options[i].value && jsonObj.options[i].display_text) {
var option = document.createElement('option');
option.value = jsonObj.options[i].value;
option.text = jsonObj.options[i].display_text;
// Check selected option and mark it
if (jsonObj.selected) {
option.selected = jsonObj.options[i].value == jsonObj.selected;
}
element.appendChild(option);
} else {
console.error('Missing required JSON keys for Drop Down conversion.');
}
}
}
return element;
};
/**
* Gets the current Compiler location from the ArdublocklyServer settings.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.requestCompilerLocation = function(callback) {
// ArdublocklyServer.getJson('/settings/compiler', callback);
};
/**
* Sends a string to the Ardublockly Server for a the Arduino IDE executable
* path.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.setCompilerLocation = function(new_path, callback) {
ArdublocklyServer.putJson(
'/settings/compiler', {"new_value": new_path}, callback);
};
/**
* Gets the current Sketch location from the Ardublockly Server settings.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.requestSketchLocation = function(callback) {
// ArdublocklyServer.getJson('/settings/sketch', callback);
};
/**
* Sends a string to the Ardublockly Server for a the Arduino sketch folder.
* @param {!string} new_path New Sketch location path..
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.setSketchLocation = function(new_path, callback) {
ArdublocklyServer.putJson(
'/settings/sketch', {"new_value": new_path}, callback);
};
/**
* Request to the Ardublockly Server to return JSON data containing all
* available target Arduino Boards, and the selected one in the settings.
* The data is then processed into an HTML element and sent to the callback
* function as an argument.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.requestArduinoBoards = function(callback) {
// ArdublocklyServer.getJson('/settings/board', callback);
};
/**
* Sends the inputted Arduino Board type to the Ardublockly Server Settings.
* The new settings menu for the Board type is then processed into an HTML
* element and sent to the callback function as an argument.
* @param {!string} new_board Indicates which board has been selected.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.setArduinoBoard = function(new_board, callback) {
ArdublocklyServer.putJson(
'/settings/board', {"new_value": new_board}, callback);
};
/**
* Request to the Ardublockly Server to return JSON data containing all
* available serial ports in the computer, and the selected one in the
* settings. The data is then processed into an HTML element and sent to the
* callback function as an argument.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.requestSerialPorts = function(callback) {
// ArdublocklyServer.getJson('/settings/serial', callback);
};
/**
* Sends the inputted Serial Port to the Ardublockly Server Settings. The new
* settings menu for the Serial Port is then processed into an HTML element
* and sent to the callback function as an argument.
* @param {!string} new_port Indicates which port has been selected.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.setSerialPort = function(new_port, callback) {
ArdublocklyServer.putJson(
'/settings/serial', {"new_value": new_port}, callback);
};
/**
* Gets the current IDE setting from the Ardublockly Server settings. The new
* settings menu for the IDE options is then processed into an HTML element
* and sent to the callback function as an argument.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.requestIdeOptions = function(callback) {
// ArdublocklyServer.getJson('/settings/ide', callback);
};
/**
* Sends the inputted IDE option to the Ardublockly Server Settings. The new
* settings menu for the IDE options is then processed into an HTML element
* and sent to the callback function as an argument.
* @param {!string} ide_option Indicates which option has been selected.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.setIdeOptions = function(ide_option, callback) {
ArdublocklyServer.putJson(
'/settings/ide', {"new_value": ide_option}, callback);
};
/**
* Sends the Arduino code to the ArdublocklyServer to be processed as defined
* by the settings.
* @param {!string} code Arduino code in a single string format.
* @param {!function} callback Callback function for the server request, must
* have one argument to receive the JSON response.
*/
ArdublocklyServer.sendSketchToServer = function(code, callback) {
ArdublocklyServer.sendRequest(
'/code', 'POST', 'application/json', {"sketch_code": code}, callback);
};