index.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>ArduPilot Custom Firmware Builder</title>
  6. <meta name="description"
  7. content="ArduPilot Custom Firmware Builder. It allows to build custom ArduPilot firmware by selecting the wanted features.">
  8. <meta name="author" content="ArduPilot Team">
  9. <meta name="viewport" content="width=device-width, initial-scale=1">
  10. <!-- OG Meta Tags to improve the way the post looks when you share the page on LinkedIn, Facebook, Google+ -->
  11. <meta property="og:site_name" content="ArduPilot"/>
  12. <meta property="og:site" content=""/>
  13. <meta property="og:title" content="ArduPilot Custom Firmware Builder"/>
  14. <meta property="og:description"
  15. content="ArduPilot Custom Firmware Builder. It allows to build custom ArduPilot firmware by selecting the wanted features."/>
  16. <!-- description shown in the actual shared post -->
  17. <meta property="og:type" content="website">
  18. <meta property="og:url" content="https://custom.ardupilot.org/">
  19. <meta property="og:image" content="https://ardupilot.org/application/files/6315/7552/1962/ArduPilot-Motto.png">
  20. <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='styles/main.css') }}">
  21. <script type="text/javascript" src="{{ url_for('static', filename='js/CollapsibleLists.js')}}"></script>
  22. </head>
  23. <body onload="javascript: init()">
  24. <div id="main">
  25. <a href="https://custom.ardupilot.org/">
  26. <div id="logo">
  27. </div>
  28. </a>
  29. <div id="menu">
  30. <h2>ArduPilot Custom Firmware Builder</h2>
  31. <form action="/generate" method="post">
  32. <div id="vehicle_list">
  33. <label for="vehicle">Choose a vehicle:
  34. <select name="vehicle" id="vehicle" onchange="onVehicleChange(this.value);">
  35. {% for vehicle in get_vehicle_names() %}
  36. <option value="{{vehicle}}" {% if vehicle == get_default_vehicle_name() %} selected {% endif %}>{{vehicle}}</option>
  37. {% endfor %}
  38. </select>
  39. </label>
  40. </div>
  41. <p></p>
  42. <div id="branch_list"></div>
  43. <p></p>
  44. <div id="board_list"></div>
  45. <p></p>
  46. <div id="build_options"></div>
  47. <br>
  48. <div id="message" style="color:red"></div>
  49. <br>
  50. <input type="submit" value="Generate" id="submit">
  51. <input type="button" value="Reset option defaults" id="reset_def" onclick="Features.applyDefaults();">
  52. </form>
  53. </div>
  54. <hr>
  55. <p>Exisiting builds (click on the status of a build to view it):</p>
  56. <div id="build_status"></div>
  57. <br/>
  58. <script>
  59. const Features = (() => {
  60. let features = {};
  61. let features_dictionary = {};
  62. function resetDictionary() {
  63. features_dictionary = {};
  64. features.forEach((category, cat_idx) => {
  65. category['options'].forEach((option, opt_idx) => {
  66. features_dictionary[option.define] = {
  67. 'category_index' : cat_idx,
  68. 'option_index' : opt_idx,
  69. };
  70. });
  71. });
  72. }
  73. function reset(new_features) {
  74. features = new_features;
  75. resetDictionary();
  76. }
  77. function getByDefine(define) {
  78. let dict_value = features_dictionary[define];
  79. if (dict_value == undefined) {
  80. return null;
  81. }
  82. return features[dict_value['category_index']]['options'][dict_value['option_index']];
  83. }
  84. function updateDefaults(defines_array) {
  85. // updates default on the basis of define array passed
  86. // the define array consists define in format, EXAMPLE_DEFINE or !EXAMPLE_DEFINE
  87. // we update the defaults in features object by processing those defines
  88. for (let i=0; i<defines_array.length; i++) {
  89. let select_opt = (defines_array[i][0] != '!');
  90. let sanitised_define = (select_opt ? defines_array[i] : defines_array[i].substring(1)); // this removes the leading '!' from define if it contatins
  91. if (getByDefine(sanitised_define)) {
  92. let cat_idx = features_dictionary[sanitised_define]['category_index'];
  93. let opt_idx = features_dictionary[sanitised_define]['option_index'];
  94. getByDefine(sanitised_define).default = select_opt ? 1 : 0;
  95. }
  96. }
  97. }
  98. function applyDefaults() {
  99. features.forEach(category => {
  100. category['options'].forEach(option => {
  101. element = document.getElementById(option['label']);
  102. if (element != undefined) {
  103. element.checked = (option['default'] == 1);
  104. }
  105. });
  106. });
  107. }
  108. return {reset, getByDefine, updateDefaults, applyDefaults};
  109. })();
  110. var pending_update_calls = 0; // to keep track of unresolved Promises
  111. function init() {
  112. refresh_builds();
  113. onVehicleChange(document.getElementById("vehicle").value);
  114. }
  115. function setMessage (messageText) {
  116. document.getElementById("message").innerHTML = messageText;
  117. }
  118. // enables or disables the elements with ids passed as an array
  119. // if enable is true, the elements are enabled and vice-versa
  120. function enableDisableElementsById(ids, enable) {
  121. for (let i=0; i<ids.length; i++) {
  122. let element = document.getElementById(ids[i]);
  123. if (element) {
  124. element.disabled = (!enable);
  125. }
  126. }
  127. }
  128. function onVehicleChange(new_vehicle) {
  129. // following elemets will be blocked (disabled) when we make the request
  130. let elements_to_block = ['vehicle', 'submit', 'reset_def'];
  131. enableDisableElementsById(elements_to_block, false);
  132. let request_url = '/get_allowed_branches/'+new_vehicle;
  133. setMessage("Fetching the list of available branches for "+new_vehicle);
  134. pending_update_calls += 1;
  135. sendAjaxRequestForJsonResponse(request_url)
  136. .then((json_response) => {
  137. let new_branch = json_response.default_branch;
  138. let all_branches = json_response.branches;
  139. updateBranches(all_branches, new_branch);
  140. })
  141. .catch((message) => {
  142. console.log("Branch update failed. "+message);
  143. })
  144. .finally(() => {
  145. setMessage("");
  146. enableDisableElementsById(elements_to_block, true);
  147. pending_update_calls -= 1;
  148. fetchAndUpdateDefaults();
  149. });
  150. }
  151. function updateBranches(all_branches, new_branch) {
  152. let branch_element = document.getElementById('branch');
  153. let old_branch = branch_element ? branch_element.value : '';
  154. fillBranches(all_branches, new_branch);
  155. if (old_branch != new_branch) {
  156. onBranchChange(new_branch);
  157. }
  158. }
  159. function onBranchChange(new_branch) {
  160. // following elemets will be blocked (disabled) when we make the request
  161. let elements_to_block = ['branch', 'submit', 'reset_def'];
  162. enableDisableElementsById(elements_to_block, false);
  163. let request_url = '/boards_and_features/'+new_branch;
  164. setMessage("Fetching the list of boards and features for "+new_branch);
  165. pending_update_calls += 1;
  166. sendAjaxRequestForJsonResponse(request_url)
  167. .then((json_response) => {
  168. let boards = json_response.boards;
  169. let new_board = json_response.default_board;
  170. let new_features = json_response.features;
  171. updateBoards(boards, new_board);
  172. Features.reset(new_features);
  173. fillBuildOptions(new_features);
  174. })
  175. .catch((message) => {
  176. console.log("Boards and features update failed. "+message);
  177. })
  178. .finally(() => {
  179. setMessage("");
  180. enableDisableElementsById(elements_to_block, true);
  181. pending_update_calls -= 1;
  182. fetchAndUpdateDefaults();
  183. });
  184. }
  185. function updateBoards(all_boards, new_board) {
  186. let board_element = document.getElementById('board');
  187. let old_board = board_element ? board.value : '';
  188. fillBoards(all_boards, new_board);
  189. if (old_board != new_board) {
  190. onBoardChange(new_board);
  191. }
  192. }
  193. function onBoardChange(new_board) {
  194. fetchAndUpdateDefaults();
  195. }
  196. function fetchAndUpdateDefaults() {
  197. // return early if there is an unresolved promise (i.e., there is an ongoing ajax call)
  198. if (pending_update_calls > 0) {
  199. return;
  200. }
  201. elements_to_block = ['reset_def']
  202. enableDisableElementsById(elements_to_block, false);
  203. let branch = document.getElementById('branch').value;
  204. let vehicle = document.getElementById('vehicle').value;
  205. let board = document.getElementById('board').value;
  206. let request_url = '/get_defaults/'+vehicle+'/'+branch+'/'+board;
  207. setMessage("Fetching defaults for "+vehicle+" and "+board+" on "+branch);
  208. sendAjaxRequestForJsonResponse(request_url)
  209. .then((json_response) => {
  210. Features.updateDefaults(json_response);
  211. Features.applyDefaults();
  212. })
  213. .catch((message) => {
  214. console.log("Default reset failed. "+message);
  215. })
  216. .finally(() => {
  217. setMessage("");
  218. enableDisableElementsById(elements_to_block, true);
  219. });
  220. }
  221. function refresh_builds() {
  222. var output = document.getElementById('build_status');
  223. var xhr = new XMLHttpRequest();
  224. xhr.open('GET', "/builds/status.html");
  225. // disable cache, thanks to: https://stackoverflow.com/questions/22356025/force-cache-control-no-cache-in-chrome-via-xmlhttprequest-on-f5-reload
  226. xhr.setRequestHeader("Cache-Control", "no-cache, no-store, max-age=0");
  227. xhr.setRequestHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
  228. xhr.setRequestHeader("Pragma", "no-cache");
  229. xhr.onload = function () {
  230. if (xhr.status === 200) {
  231. output.innerHTML = xhr.responseText;
  232. }
  233. setTimeout(refresh_builds, 5000)
  234. }
  235. xhr.send();
  236. }
  237. function fillBoards(boards, default_board) {
  238. let output = document.getElementById('board_list');
  239. output.innerHTML = "<p>Please select the required options for the custom firmware build, then hit 'Generate'.</p>"+
  240. "<label for='board'>Choose a board: "+
  241. "<select name='board' id='board' onchange='onBoardChange(this.value)'>"+
  242. "</select>"+
  243. "</label>";
  244. let boardList = document.getElementById("board")
  245. boards.forEach(board => {
  246. let opt = document.createElement('option');
  247. opt.value = board;
  248. opt.innerHTML = board;
  249. opt.selected = (board === default_board);
  250. boardList.appendChild(opt);
  251. });
  252. }
  253. function fillBuildOptions(buildOptions) {
  254. let output = document.getElementById('build_options');
  255. output.innerHTML = "<label for='features'>Select Features: "+
  256. "<ul class='collapsibleList' id='outer_list'></ul>"+
  257. "</label>";
  258. let outerList = document.getElementById("outer_list");
  259. buildOptions.forEach(category => {
  260. let outerListItem = document.createElement('li');
  261. outerListItem.innerHTML = category['name'];
  262. let innerList = document.createElement('ul');
  263. category['options'].forEach(option => {
  264. let innerListItem = document.createElement('li');
  265. let checkBox = document.createElement('input');
  266. checkBox.type = "checkbox";
  267. checkBox.name = option['label'];
  268. checkBox.id = option['label'];
  269. checkBox.value = "1";
  270. checkBox.checked = (option['default'] == 1);
  271. checkBox.addEventListener('click', function handleClick(event) {
  272. dependencies(option['label'], option['dependency']);
  273. });
  274. innerListItem.appendChild(checkBox);
  275. innerListItem.appendChild(document.createTextNode(option['description']));
  276. innerList.appendChild(innerListItem);
  277. });
  278. outerListItem.appendChild(innerList);
  279. outerList.appendChild(outerListItem);
  280. });
  281. CollapsibleLists.apply();
  282. }
  283. function dependencies(f_label, f_dependency1) {
  284. cb = document.getElementById(f_label);
  285. switch (cb.name) {
  286. case f_label:
  287. const f_dependency = f_dependency1.split(",")
  288. var arrayLength = f_dependency.length;
  289. for (let i = 0; i < arrayLength; i++) {
  290. if (document.getElementById(f_dependency[i]).checked == false) {
  291. document.getElementById(f_dependency[i]).checked = cb.checked;
  292. }
  293. }
  294. break;
  295. }
  296. }
  297. // returns a Promise
  298. // the promise is resolved when we recieve status code 200 from the AJAX request
  299. // the JSON response for the request is returned in such case
  300. // the promise is rejected when the status code is not 200
  301. // the status code is returned in such case
  302. function sendAjaxRequestForJsonResponse(url) {
  303. return new Promise((resolve, reject) => {
  304. var xhr = new XMLHttpRequest();
  305. xhr.open('GET', url);
  306. xhr.onload = function () {
  307. if (xhr.status == 200) {
  308. resolve(JSON.parse(xhr.response));
  309. } else {
  310. reject("Got response:"+xhr.response+" (Status Code: "+xhr.status+")");
  311. }
  312. }
  313. xhr.send();
  314. });
  315. }
  316. function fillBranches(branches, branch_to_select) {
  317. var output = document.getElementById('branch_list');
  318. output.innerHTML = "<label for='branch'>Choose a branch: "+
  319. "<select name='branch' id='branch' onchange='onBranchChange(this.value);'>"+
  320. "</select>"+
  321. "</label>";
  322. branchList = document.getElementById("branch");
  323. branches.forEach(branch => {
  324. opt = document.createElement('option');
  325. opt.value = branch['full_name'];
  326. opt.innerHTML = branch['label'];
  327. opt.selected = (branch['full_name'] === branch_to_select);
  328. branchList.appendChild(opt);
  329. });
  330. }
  331. </script>
  332. </div>
  333. </body>
  334. <hr>
  335. <footer>Created by Will Piper, <a href=https://github.com/ArduPilot/CustomBuild>Source Code</a>.</footer>
  336. </html>