ardublocklyserver_ajax.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. *
  5. * @fileoverview Ajax calls to the Ardublockly Server python program.
  6. */
  7. 'use strict';
  8. /** Create a name space for the application. */
  9. var ArdublocklyServer = {};
  10. /**
  11. * Sends Form data to the ArduBlocklyServer using Ajax.
  12. * @param {!string} url Requestor URL.
  13. * @param {!string} params Form parameters in the 'var=x&var2=y' format.
  14. * @param {!function} callback Request callback function.
  15. */
  16. ArdublocklyServer.ajaxPostForm = function(url, params, callback) {
  17. var request = ArdublocklyServer.createAjaxRequest();
  18. try {
  19. request.open('POST', url, true);
  20. request.setRequestHeader(
  21. 'Content-type', 'application/x-www-form-urlencoded');
  22. } catch (e) {
  23. // The request will fail if opening the html directly on a browser, so
  24. // let's just send the callback nullified and the front end will deal.
  25. callback(null);
  26. }
  27. // The data received is JSON, so it needs to be converted into the right
  28. // format to be displayed in the page.
  29. request.onreadystatechange = function() {
  30. if (request.readyState == 4) {
  31. if (request.status == 200) {
  32. callback(request.responseText);
  33. } else if (request.status == 405) {
  34. // return a null element which will be dealt with in the front end
  35. callback(null);
  36. }
  37. }
  38. };
  39. // Send the data
  40. try {
  41. request.send(params);
  42. } catch (e) {
  43. // Nullify callback to indicate error
  44. callback(null);
  45. }
  46. };
  47. /**
  48. * Sends plain data to the ArduBlocklyServer using Ajax.
  49. * @param {!string} url Requester URL.
  50. * @param {!string} data Plain text currently used to send Arduino code only.
  51. * @param {!function} callback Request callback function.
  52. */
  53. ArdublocklyServer.ajaxPostPlain = function(url, data, callback) {
  54. var request = ArdublocklyServer.createAjaxRequest();
  55. request.open('POST', url, true);
  56. request.setRequestHeader('Content-type', 'text/plain');
  57. // The data received is JSON, so it needs to be converted into the right
  58. // format to be displayed in the page.
  59. request.onreadystatechange = function() {
  60. if (request.readyState == 4) {
  61. if (request.status == 200) {
  62. callback(request.responseText);
  63. } else if (request.status == 405) {
  64. // return a null element which will be dealt with in the front end
  65. callback(null);
  66. }
  67. }
  68. };
  69. // Send the data
  70. try {
  71. request.send(data);
  72. } catch (e) {
  73. // The request will fail if opening the html directly on a browser, so
  74. // let's just send the callback nullified and the front end will deal.
  75. callback(null);
  76. }
  77. };
  78. /**
  79. * Reads JSON data from the server and forwards formatted JavaScript object.
  80. * @param {!string} fileLocation Location for the JSON data.
  81. * @param {!function} jsonDataCb Callback with JSON object or null for error.
  82. */
  83. Ardublockly.getJsonData = function(fileLocation, jsonDataCb) {
  84. var request = ArdublocklyServer.createAjaxRequest();
  85. request.overrideMimeType("application/json");
  86. var requestCb = function() {
  87. if (request.readyState == 4) {
  88. if (request.status == 200) {
  89. var jsonObj = null;
  90. try {
  91. jsonObj = JSON.parse(request.responseText);
  92. } catch(e) {
  93. console.error('Incorrectly formatted JSON data from ' + fileLocation);
  94. throw e;
  95. }
  96. jsonDataCb(jsonObj);
  97. } else {
  98. jsonDataCb(null);
  99. }
  100. }
  101. };
  102. try {
  103. request.open('GET', fileLocation, true);
  104. request.onreadystatechange = requestCb;
  105. request.send(null);
  106. } catch (e) {
  107. jsonDataCb(null);
  108. }
  109. };
  110. /** @return {XMLHttpRequest} An XML HTTP Request multi-browser compatible. */
  111. ArdublocklyServer.createAjaxRequest = function() {
  112. var request = false;
  113. try {
  114. // Firefox, Chrome, IE7+, Opera, Safari
  115. request = new XMLHttpRequest();
  116. }
  117. catch (e) {
  118. // IE6 and earlier
  119. try {
  120. request = new ActiveXObject('Msxml2.XMLHTTP');
  121. }
  122. catch (e) {
  123. try {
  124. request = new ActiveXObject('Microsoft.XMLHTTP');
  125. }
  126. catch (e) {
  127. throw 'Your browser does not support AJAX. You will not be able to' +
  128. 'use all of Ardublockly features.';
  129. request = null;
  130. }
  131. }
  132. }
  133. return request;
  134. };
  135. /**
  136. * Creates an HTML element based on the JSON data received from the server.
  137. * @param {!string} json_data A string containing the JSON data to be parsed.
  138. * @return {!element} An HTML element, which type depends on the JSON 'element'
  139. * key (currently only text input or drop down).
  140. */
  141. ArdublocklyServer.createElementFromJson = function(json_data) {
  142. var parsed_json = JSON.parse(json_data);
  143. var element = null;
  144. if (parsed_json.element == 'text_input') {
  145. // Simple text input
  146. element = document.createElement('input');
  147. element.setAttribute('type', 'text');
  148. element.setAttribute('value', parsed_json.display_text);
  149. } else if (parsed_json.element == 'dropdown') {
  150. // Drop down list of unknown length with a selected item
  151. element = document.createElement('select');
  152. element.name = parsed_json.response_type;
  153. for (var i = 0; i < parsed_json.options.length; i++) {
  154. var option = document.createElement('option');
  155. option.value = parsed_json.options[i].value;
  156. option.text = parsed_json.options[i].display_text;
  157. // Check selected option and mark it
  158. if (parsed_json.options[i].value == parsed_json.selected) {
  159. option.selected = true;
  160. }
  161. element.appendChild(option);
  162. }
  163. } else if (parsed_json.element == 'div_ide_output') {
  164. // Formatted text for the Arduino IDE CLI output
  165. var el_title = document.createElement('h4');
  166. el_title.innerHTML = Ardublockly.getLocalStr(parsed_json.conclusion);
  167. if (parsed_json.success == true) {
  168. el_title.className = 'arduino_dialog_success';
  169. } else {
  170. el_title.className = 'arduino_dialog_failure';
  171. }
  172. var el_out = document.createElement('span');
  173. el_out.className = 'arduino_dialog_out';
  174. // If larger than 50 characters then don't bother looking for language key
  175. if (parsed_json.output.length < 50) {
  176. el_out.innerHTML = Ardublockly.getLocalStr(parsed_json.output) ||
  177. parsed_json.output.split('\n').join('<br />');
  178. } else {
  179. el_out.innerHTML = parsed_json.output.split('\n').join('<br />');
  180. }
  181. element = document.createElement('div');
  182. element.appendChild(el_title);
  183. element.appendChild(el_out);
  184. // Only ouput error message if it was not successful
  185. if (parsed_json.success == false) {
  186. var el_err = document.createElement('span');
  187. el_err.className = 'arduino_dialog_out_error';
  188. // If larger than 50 characters then don't bother looking for language key
  189. if (parsed_json.output.length < 50) {
  190. el_err.innerHTML = Ardublockly.getLocalStr(parsed_json.error_output) ||
  191. parsed_json.error_output.split('\n').join('<br />');
  192. } else {
  193. el_err.innerHTML = parsed_json.error_output.split('\n').join('<br />');
  194. }
  195. element.appendChild(el_err);
  196. }
  197. } else {
  198. //TODO: Not recognised, alert the user/developer somehow
  199. }
  200. return element;
  201. };