ardublockly_design.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 JavaScript to configure front end design for Ardublockly app.
  6. */
  7. 'use strict';
  8. /** Create a namespace for the application. */
  9. var Ardublockly = Ardublockly || {};
  10. /** Initialises all the design related JavaScript. */
  11. Ardublockly.designJsInit = function () {
  12. Ardublockly.materializeJsInit();
  13. Ardublockly.sketchNameSizeEffect();
  14. Ardublockly.sketchNameSet();
  15. // Ardublockly.cloudSketchNameSizeEffect();
  16. // Ardublockly.cloudSketchNameSet();
  17. };
  18. /**
  19. * Initialises all required components from materialize framework.
  20. * The be executed on document ready.
  21. */
  22. Ardublockly.materializeJsInit = function () {
  23. // Navigation bar
  24. $('.button-collapse').sideNav({
  25. menuWidth: 240,
  26. activationWidth: 70,
  27. edge: 'left'
  28. });
  29. // // Serial bar
  30. // $('.button-collapse-right').sideNav({
  31. // menuWidth: 550,
  32. // activationWidth: 70,
  33. // edge: 'right',
  34. // onCloseStart: function(){
  35. // }
  36. // });
  37. $('.modal').modal({
  38. dismissible: true,
  39. opacity: .5,
  40. in_duration: 200,
  41. out_duration: 250
  42. });
  43. // Drop down menus
  44. $('.dropdown-button').dropdown({ hover: false });
  45. // Overlay content panels using modals (android dialogs)
  46. $("ul.tabs").tabs({
  47. swipeable: true,
  48. responsiveThreshold: 220
  49. })
  50. $("#app_center_modal").modal({
  51. ready: () => {
  52. $(".tab:nth-child(1)>a").click()
  53. }
  54. })
  55. $('#setting_teacher').modal({
  56. ready: () => {
  57. },
  58. })
  59. //Teachable Machine modal
  60. $('#Teachable_Machine').modal({
  61. ready: () => {
  62. $('#setting_teacher').modal("close")
  63. if (platform.name === "Chrome" || platform.name === "Firefox") {
  64. console.log('chrome or Firefox')
  65. } else {
  66. Ardublockly.alertMessage(
  67. Ardublockly.getLocalStr('unsupportWebcamTitle'),
  68. Ardublockly.getLocalStr('unsupportWebcamBody'),
  69. false);
  70. $('#Teachable_Machine').modal('close')
  71. return;
  72. }
  73. if (window.location.href.split("://")[0] === "http") {
  74. Ardublockly.customAlertMessage(
  75. Ardublockly.getLocalStr('requestWebcamTitle'),
  76. Ardublockly.getLocalStr('requestWebcamBody'),
  77. Ardublockly.getLocalStr('goHttps'),
  78. () => {
  79. window.location.href = "https://" + window.location.href.split("://")[1]
  80. });
  81. $('#Teachable_Machine').modal('close')
  82. return;
  83. }
  84. if (!CCB.userState) {
  85. document.getElementById('teachable_modal').style.display = 'none'
  86. }
  87. CCB.UserInfo = CCB.UserInfo || {}
  88. let lang = 'en';
  89. document.location.search.substring(1).split('&').forEach((item) => {
  90. if (item.split('=')[0] == 'lang') {
  91. lang = item.split('=')[1];
  92. }
  93. });
  94. CCB.UserInfo.url = lang;
  95. CCB.UserInfo.boards = CCB.boards[0];
  96. $("#TM_id").css("height", $("#Teachable_Machine")[0].offsetHeight)
  97. // document.getElementById("TM_id").contentWindow.postMessage("startCam", "*")
  98. appendTMIframe("TM_iframe", function () { document.getElementById("TM_id").contentWindow.postMessage(CCB.UserInfo, "*"); }, "//ai-demos.cocorobo.cn/teachable-machine-2/public/");
  99. let loading = document.getElementById('TM_id');
  100. loading.onload = () => {
  101. document.getElementById("TM_id").contentWindow.postMessage(CCB.UserInfo, "*")
  102. // window.addEventListener('message', function (e) {
  103. // if (e.data === 'none') {
  104. // }
  105. // })
  106. setTimeout(() => {
  107. document.getElementById('teachable_modal').style.display = 'none'
  108. }, 1000)
  109. }
  110. // CCB.socket && CCB.socket.emit('command', {
  111. // cmd: 'open',
  112. // option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  113. // });
  114. window.addEventListener('message', function (e) {
  115. let data = '';
  116. if (document.getElementById('TM_id_posenet') && CCB.isOpen && e.data !== "close") {
  117. for (const key in e.data) {
  118. if (e.data[key] === "yes") {
  119. data = key
  120. }
  121. }
  122. data = data.slice(6) === "A" ? "green" : data.slice(6) === "B" ? "purple" : "orange"
  123. // socket 发送数据
  124. CCB.socket && CCB.socket.emit('command', {
  125. cmd: 'send',
  126. option: { msg: `SOF|${data}|`, postfix: "blc" }
  127. })
  128. }
  129. }, false);
  130. },
  131. // 点击 遮罩层 关闭弹框
  132. complete: () => {
  133. document.getElementById("TM_id").contentWindow.postMessage("stopCam", "*")
  134. let obj = document.getElementById('TM_id');
  135. if (obj) {
  136. obj.parentNode.removeChild(obj)
  137. document.getElementById('teachable_modal').style.display = 'block'
  138. // socket 关闭
  139. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  140. }
  141. }
  142. })
  143. $('#setting_teacher_ponsenet').modal({
  144. ready: () => {
  145. },
  146. })
  147. //Teachable Machine modal
  148. $('#Teachable_Machine_ponsenet').modal({
  149. ready: () => {
  150. $('#setting_teacher_ponsenet').modal("close")
  151. if (platform.name === "Chrome" || platform.name === "Firefox") {
  152. console.log('chrome or Firefox')
  153. } else {
  154. Ardublockly.alertMessage(
  155. Ardublockly.getLocalStr('unsupportWebcamTitle'),
  156. Ardublockly.getLocalStr('unsupportWebcamBody'),
  157. false);
  158. $('#Teachable_Machine_ponsenet').modal('close')
  159. return;
  160. }
  161. if (window.location.href.split("://")[0] === "http") {
  162. Ardublockly.customAlertMessage(
  163. Ardublockly.getLocalStr('requestWebcamTitle'),
  164. Ardublockly.getLocalStr('requestWebcamBody'),
  165. Ardublockly.getLocalStr('goHttps'),
  166. () => {
  167. window.location.href = "https://" + window.location.href.split("://")[1]
  168. });
  169. $('#Teachable_Machine_ponsenet').modal('close')
  170. return;
  171. }
  172. if (!CCB.userState) {
  173. document.getElementById('teachable_posenet_modal').style.display = 'none'
  174. }
  175. CCB.UserInfo = CCB.UserInfo || {}
  176. let lang = 'en';
  177. document.location.search.substring(1).split('&').forEach((item) => {
  178. if (item.split('=')[0] == 'lang') {
  179. lang = item.split('=')[1];
  180. }
  181. });
  182. CCB.UserInfo.url = lang;
  183. CCB.UserInfo.boards = CCB.boards[0];
  184. $("#TM_posenet_id").css("height", $("#Teachable_Machine_ponsenet")[0].offsetHeight)
  185. // document.getElementById("TM_id").contentWindow.postMessage("startCam", "*")
  186. let url = "//ai-demos.cocorobo.cn/teachable-machine-pose-recognition-2/public/"
  187. appendTMPosenetIframe("TM_posenet_iframe", function () { document.getElementById("TM_posenet_id").contentWindow.postMessage(CCB.UserInfo, "*"); }, url);
  188. let loading = document.getElementById('TM_posenet_id');
  189. loading.onload = () => {
  190. document.getElementById("TM_posenet_id").contentWindow.postMessage(CCB.UserInfo, "*")
  191. // window.addEventListener('message', function (e) {
  192. // if (e.data === 'none') {
  193. // }
  194. // })
  195. setTimeout(() => {
  196. document.getElementById('teachable_posenet_modal').style.display = 'none'
  197. }, 1000)
  198. }
  199. window.addEventListener('message', function (e) {
  200. if (document.getElementById('TM_posenet_id') && CCB.isOpen && e.data !== "close") {
  201. let data = '';
  202. for (const key in e.data.data) {
  203. if (e.data.data[key] !== "no") {
  204. data = e.data.data[key]
  205. }
  206. }
  207. // data = data.slice(6) === "A" ? "green" : data.slice(6) === "B" ? "purple" : "orange"
  208. // socket 发送数据
  209. CCB.socket && CCB.socket.emit('command', {
  210. cmd: 'send',
  211. option: { msg: `SOF|${data}|${e.data.posenet ? e.data.posenet : ""}|`, postfix: "blc" }
  212. })
  213. }
  214. }, false);
  215. },
  216. // 点击 遮罩层 关闭弹框
  217. complete: () => {
  218. let obj = document.getElementById('TM_posenet_id');
  219. if (obj) {
  220. document.getElementById("TM_posenet_id").contentWindow.postMessage("stopCam", "*")
  221. obj.parentNode.removeChild(obj)
  222. document.getElementById('teachable_posenet_modal').style.display = 'block'
  223. // socket 关闭
  224. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  225. }
  226. }
  227. })
  228. $("#setting_webcam_capture_modal").modal({
  229. ready: () => {
  230. }
  231. })
  232. $('#webcam_capture_modal').modal({
  233. ready: () => {
  234. $('#setting_webcam_capture_modal').modal('close');
  235. if (window.location.href.split("://")[0] === "http") {
  236. Ardublockly.customAlertMessage(
  237. Ardublockly.getLocalStr('requestWebcamTitle'),
  238. Ardublockly.getLocalStr('requestWebcamBody'),
  239. Ardublockly.getLocalStr('goHttps'),
  240. () => {
  241. window.location.href = "https://" + window.location.href.split("://")[1]
  242. });
  243. $('#webcam_capture_modal').modal('close');
  244. return;
  245. }
  246. window.send = 'cloud';
  247. window.isSend = false;
  248. $('.translatable_disconnectBtn').css('display', 'none');
  249. $('.translatable_connectBtn').css({ "float": "right", 'margin-right': '5px', 'display': 'block' });
  250. $('.translatable_send_cloud').addClass('select_send_way_color');
  251. $('.translatable_send_module').removeClass('select_send_way_color');
  252. $('.send_cloud').css({ 'display': 'block', 'position': 'relative' });
  253. $('.send_module').css({ 'display': 'none', 'position': 'relative' });
  254. $('.port').text(CCB.boards[0])
  255. if (CCB.boards.length > 0 && CCB.userState) {
  256. $('.translatable_connectBtn').removeClass('disabled');
  257. // CCB.socket && CCB.socket.emit('command', {
  258. // cmd: 'open',
  259. // option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  260. // });
  261. }
  262. },
  263. complete: () => {
  264. document.getElementById('camera_switch').checked = false;
  265. Webcam.reset();
  266. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  267. }
  268. })
  269. $("#setting_gesture_recog_modal").modal({
  270. ready: () => {
  271. }
  272. })
  273. $('#gesture_recog_modal').modal({
  274. ready: () => {
  275. $('#setting_gesture_recog_modal').modal('close');
  276. if (window.location.href.split("://")[0] === "http") {
  277. Ardublockly.customAlertMessage(
  278. Ardublockly.getLocalStr('requestWebcamTitle'),
  279. Ardublockly.getLocalStr('requestWebcamBody'),
  280. Ardublockly.getLocalStr('goHttps'),
  281. () => {
  282. window.location.href = "https://" + window.location.href.split("://")[1]
  283. });
  284. $('#gesture_recog_modal').modal('close');
  285. return;
  286. }
  287. window.send = 'cloud';
  288. window.isSend = false;
  289. $('.translatable_disconnectBtn').css('display', 'none');
  290. $('.translatable_connectBtn').css({ "float": "right", 'margin-right': '5px', 'display': 'block' });
  291. $('.translatable_send_cloud').addClass('select_send_way_color');
  292. $('.translatable_send_module').removeClass('select_send_way_color');
  293. $('.send_cloud').css({ 'display': 'block', 'position': 'relative' });
  294. $('.send_module').css({ 'display': 'none', 'position': 'relative' });
  295. $('.port').text(CCB.boards[0])
  296. if (CCB.boards.length > 0 && CCB.userState) {
  297. $('.translatable_connectBtn').removeClass('disabled');
  298. // CCB.socket && CCB.socket.emit('command', {
  299. // cmd: 'open',
  300. // option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  301. // });
  302. }
  303. },
  304. complete: () => {
  305. document.getElementById('gestureAnalyzeButton').innerHTML = CCB.str_group.gesture_recognition_button_analyze;
  306. document.getElementById('cloudRequestStatuss').innerHTML = '';
  307. document.getElementById('gestureResultAreaContent').innerHTML = CCB.str_group.gesture_recognition_button_result;
  308. document.getElementById('camera_switch_gesture').checked = false;
  309. Webcam.reset();
  310. $("#gestureAnalyzeButton").addClass("disabled");
  311. $('#webCameraGesture').removeClass("webCameraGestureCapturedStyling");
  312. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  313. }
  314. })
  315. $("#setting_voice_input_modal").modal({
  316. ready: () => {
  317. }
  318. })
  319. $('#voice_input_modal').modal({
  320. ready: () => {
  321. $('#setting_voice_input_modal').modal('close');
  322. if (window.location.href.split("://")[0] === "http") {
  323. Ardublockly.customAlertMessage(
  324. Ardublockly.getLocalStr('requestVoiceRecognitionTitle'),
  325. Ardublockly.getLocalStr('requestVoiceRecognitionBody'),
  326. Ardublockly.getLocalStr('goHttps'),
  327. () => {
  328. window.location.href = "https://" + window.location.href.split("://")[1]
  329. });
  330. $('#voice_input_modal').modal('close');
  331. return;
  332. }
  333. window.send = 'cloud';
  334. window.isSend = false;
  335. $('.translatable_disconnectBtn').css('display', 'none');
  336. $('.translatable_connectBtn').css({ "float": "right", 'margin-right': '5px', 'display': 'block' });
  337. $('.translatable_send_cloud').addClass('select_send_way_color');
  338. $('.translatable_send_module').removeClass('select_send_way_color');
  339. $('.send_cloud').css({ 'display': 'block', 'position': 'relative' });
  340. $('.send_module').css({ 'display': 'none', 'position': 'relative' });
  341. $('.port').text(CCB.boards[0])
  342. if (CCB.boards.length > 0 && CCB.userState) {
  343. $('.translatable_connectBtn').removeClass('disabled');
  344. // CCB.socket && CCB.socket.emit('command', {
  345. // cmd: 'open',
  346. // option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  347. // });
  348. }
  349. },
  350. complete: () => {
  351. $('#speech_button').html(CCB.str_group.start_speech);
  352. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  353. }
  354. })
  355. $('#real_time_voice_input_modal').modal({
  356. ready: () => {
  357. if (window.location.href.split("://")[0] === "http") {
  358. Ardublockly.customAlertMessage(
  359. Ardublockly.getLocalStr('requestVoiceRecognitionTitle'),
  360. Ardublockly.getLocalStr('requestVoiceRecognitionBody'),
  361. Ardublockly.getLocalStr('goHttps'),
  362. () => {
  363. window.location.href = "https://" + window.location.href.split("://")[1]
  364. });
  365. $('#real_time_voice_input_modal').modal('close');
  366. return;
  367. }
  368. },
  369. })
  370. $('#setting_objectRecog').modal({
  371. ready: () => {
  372. },
  373. })
  374. // 日志
  375. $('#change_log').modal({
  376. ready: () => {
  377. if (!document.getElementById('changLog').style.opacity == '1') {
  378. document.getElementById('changLog').contentWindow.location.reload();
  379. }
  380. setTimeout(() => {
  381. document.getElementById('changLog').style.opacity = '1'
  382. document.getElementById('changLog').contentWindow.document.getElementsByTagName('body')[0].setAttribute('class', 'ready close')
  383. document.getElementById('changLog').contentWindow.document.getElementsByClassName('sidebar-toggle')[0].style.visibility = "hidden";
  384. document.getElementById('changLog').contentWindow.document.getElementsByClassName('body-nav')[0].style.visibility = "hidden"
  385. }, 1000)
  386. }
  387. })
  388. $('#Object_recog_modal').modal({
  389. // 打开弹框
  390. ready: () => {
  391. $('#setting_objectRecog').modal("close")
  392. if (platform.name === "Chrome" || platform.name === "Firefox") {
  393. console.log('chrome or Firefox')
  394. } else {
  395. Ardublockly.alertMessage(
  396. Ardublockly.getLocalStr('unsupportWebcamTitle'),
  397. Ardublockly.getLocalStr('unsupportWebcamBody'),
  398. false);
  399. $('#Object_recog_modal').modal('close')
  400. return;
  401. }
  402. if (window.location.href.split("://")[0] === "http") {
  403. Ardublockly.customAlertMessage(
  404. Ardublockly.getLocalStr('requestWebcamTitle'),
  405. Ardublockly.getLocalStr('requestWebcamBody'),
  406. Ardublockly.getLocalStr('goHttps'),
  407. () => {
  408. window.location.href = "https://" + window.location.href.split("://")[1]
  409. });
  410. $('#Object_recog_modal').modal('close');
  411. return;
  412. }
  413. if (!CCB.userState) {
  414. document.getElementById('object_modal').style.display = 'none'
  415. }
  416. CCB.UserInfo = CCB.UserInfo || {}
  417. let lang = 'en';
  418. document.location.search.substring(1).split('&').forEach((item) => {
  419. if (item.split('=')[0] == 'lang') {
  420. lang = item.split('=')[1];
  421. }
  422. });
  423. CCB.UserInfo.boards = CCB.boards[0];
  424. $("#TM_id_object").css("height", $("#Object_recog_modal")[0].offsetHeight)
  425. CCB.UserInfo.state = "startCam"
  426. CCB.UserInfo.url = lang
  427. appendTMIframes("TM_iframe_object", function () { document.getElementById("TM_id_object").contentWindow.postMessage(CCB.UserInfo, "*"); }, "//ai-demos.cocorobo.cn/object-detection-2/");
  428. let loading = document.getElementById('TM_id_object');
  429. loading.onload = () => {
  430. document.getElementById("TM_id_object").contentWindow.postMessage(CCB.UserInfo, "*")
  431. setTimeout(() => {
  432. document.getElementById('object_modal').style.display = 'none'
  433. }, 1000)
  434. }
  435. // CCB.socket && CCB.socket.emit('command', {
  436. // cmd: 'open',
  437. // option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  438. // });
  439. window.addEventListener('message', function (e) {
  440. console.log('data', e.data)
  441. if (document.getElementById('TM_id_posenet') && CCB.isOpen && e.data !== "close") {
  442. let data = ''
  443. e.data && e.data.slice(0, 8).map(x => {
  444. data = data + x.class + '|'
  445. })
  446. // socket 发送数据
  447. CCB.socket && CCB.socket.emit('command', {
  448. cmd: 'send',
  449. option: { msg: `SOF|${data}`, postfix: "blc" }
  450. })
  451. }
  452. }, false);
  453. },
  454. // 关闭弹框
  455. complete: () => {
  456. let obj = document.getElementById('TM_id_object');
  457. if (obj) {
  458. obj.parentNode.removeChild(obj)
  459. document.getElementById('object_modal').style.display = 'block'
  460. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  461. }
  462. }
  463. })
  464. // 姿态识别
  465. $('#setting_posenetRecog').modal({
  466. ready: () => {
  467. },
  468. })
  469. $('#Posent_recog_modal').modal({
  470. // 打开弹框
  471. ready: () => {
  472. $('#setting_posenetRecog').modal("close")
  473. if (platform.name === "Chrome" || platform.name === "Firefox") {
  474. console.log('chrome or Firefox')
  475. } else {
  476. Ardublockly.alertMessage(
  477. Ardublockly.getLocalStr('unsupportWebcamTitle'),
  478. Ardublockly.getLocalStr('unsupportWebcamBody'),
  479. false);
  480. $('#Posent_recog_modal').modal('close')
  481. return;
  482. }
  483. if (window.location.href.split("://")[0] === "http") {
  484. Ardublockly.customAlertMessage(
  485. Ardublockly.getLocalStr('requestWebcamTitle'),
  486. Ardublockly.getLocalStr('requestWebcamBody'),
  487. Ardublockly.getLocalStr('goHttps'),
  488. () => {
  489. window.location.href = "https://" + window.location.href.split("://")[1]
  490. });
  491. $('#Posent_recog_modal').modal('close');
  492. return;
  493. }
  494. if (!CCB.userState) {
  495. document.getElementById('posenet_modal').style.display = 'none'
  496. }
  497. CCB.UserInfo = CCB.UserInfo || {}
  498. let lang = 'en';
  499. document.location.search.substring(1).split('&').forEach((item) => {
  500. if (item.split('=')[0] == 'lang') {
  501. lang = item.split('=')[1];
  502. }
  503. });
  504. $("#TM_id_posenet").css("height", $("#Posent_recog_modal")[0].offsetHeight)
  505. CCB.UserInfo.state = "startCam";
  506. CCB.UserInfo.url = lang;
  507. CCB.UserInfo.boards = CCB.boards[0];
  508. appendTMIframePosenet("TM_iframe_posenet", function () { document.getElementById("TM_id_posenet").contentWindow.postMessage(CCB.UserInfo, "*"); }, "//ai-demos.cocorobo.cn/posenet-2/demos/dist/");
  509. let loading = document.getElementById('TM_id_posenet');
  510. loading.onload = () => {
  511. document.getElementById("TM_id_posenet").contentWindow.postMessage(CCB.UserInfo, "*")
  512. setTimeout(() => {
  513. document.getElementById('posenet_modal').style.display = 'none'
  514. }, 1000)
  515. }
  516. // socket 连接
  517. CCB.socket && CCB.socket.emit('command', {
  518. cmd: 'open',
  519. option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  520. });
  521. window.addEventListener('message', function (e) {
  522. console.log("iframe data", e.data)
  523. // socket 发送数据
  524. if (document.getElementById('TM_id_posenet') && CCB.isOpen && e.data !== "close") {
  525. CCB.socket && CCB.socket.emit('command', {
  526. cmd: 'send',
  527. option: { msg: `SOF|${e.data.posture}|`, postfix: "blc" }
  528. })
  529. }
  530. }, false);
  531. },
  532. // 关闭弹框
  533. complete: () => {
  534. let obj = document.getElementById('TM_id_posenet');
  535. if (obj) {
  536. obj.parentNode.removeChild(obj)
  537. document.getElementById('posenet_modal').style.display = 'block'
  538. // socket 关闭
  539. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  540. }
  541. }
  542. })
  543. // 机器人控制
  544. $('#controller').modal({
  545. ready: () => {
  546. // socket 连接
  547. if (CCB.boards.length > 0) {
  548. CCB.issend = true;
  549. CCB.socket && CCB.socket.emit('command', {
  550. cmd: 'open',
  551. option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  552. });
  553. }
  554. },
  555. complete: () => {
  556. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  557. CCB.issend = false;
  558. }
  559. })
  560. // 机器人控制
  561. $('#controllers').modal({
  562. ready: () => {
  563. // socket 连接
  564. // if (CCB.boards.length > 0) {
  565. // CCB.issend = true;
  566. // CCB.socket && CCB.socket.emit('command', {
  567. // cmd: 'open',
  568. // option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  569. // });
  570. // }
  571. },
  572. complete: () => {
  573. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  574. CCB.issend = false;
  575. }
  576. })
  577. window.addEventListener('message', (e) => {
  578. if (e.data === 'open') {
  579. // socket 连接
  580. CCB.socket && CCB.socket.emit('command', {
  581. cmd: 'open',
  582. option: { port: $('#ports').val(), baud: $('#serial_baud').val() }
  583. });
  584. } else if (e.data === 'close') {
  585. // socket 关闭
  586. CCB.socket && CCB.socket.emit('command', { cmd: 'close' });
  587. }
  588. })
  589. $(".modale").modal({ dismissible: false })
  590. // Pop-up tool tips
  591. $('.tooltipped').tooltip({ 'delay': 50 });
  592. // Select menus
  593. // $('select').material_select();
  594. //scrollspy
  595. $('.scrollspy').scrollSpy();
  596. $('.modal_close').on('click', function () {
  597. $('.modal_closes').modal('close')
  598. })
  599. // 更新文件
  600. $("#updatePy").modal({
  601. ready: () => {
  602. // 获取 sd 卡的路径
  603. // pythoncmdselect('/sd/',function(data){return data})
  604. console.log('获取 sd 卡', pythoncmdselect('/sd/', function (data) {
  605. console.log(data)
  606. }))
  607. }
  608. })
  609. $('.confim').click(()=>{
  610. console.log('确定')
  611. $("#updatePy").modal('close')
  612. })
  613. $('.cancle').click(()=>{
  614. console.log('取消')
  615. $("#updatePy").modal('close')
  616. })
  617. };
  618. /** Binds the event listeners relevant to the page design. */
  619. Ardublockly.bindDesignEventListeners = function () {
  620. // Resize blockly workspace on window resize
  621. window.addEventListener(
  622. 'resize', Ardublockly.resizeBlocklyWorkspace, false);
  623. };
  624. /**
  625. * Sets the toolbox HTML element to be display or not and change the visibility
  626. * button to reflect the new state.
  627. * When the toolbox is visible it should display the "visibility-off" icon with
  628. * no background, and the opposite when toolbox is hidden.
  629. * @param {!boolean} show Indicates if the toolbox should be set visible.
  630. */
  631. Ardublockly.displayToolbox = function (show) {
  632. var toolbox = $('.blocklyToolboxDiv');
  633. var toolboxTree = $('.blocklyTreeRoot');
  634. if (show) {
  635. toolbox.show();
  636. toolbox.animate({ height: document.getElementById('content_blocks').style.height }, 300,
  637. function () {
  638. toolboxTree.css('overflow-y', 'auto');
  639. window.dispatchEvent(new Event('resize'));
  640. });
  641. } else {
  642. toolboxTree.css('overflow-y', 'hidden');
  643. toolbox.animate({ height: 38 }, 300, function () {
  644. toolbox.fadeOut(350, 'linear', function () {
  645. window.dispatchEvent(new Event('resize'));
  646. setTimeout(function () { toolbox.height(38); }, 100);
  647. });
  648. });
  649. }
  650. };
  651. /** Resizes the container for the Blockly workspace. */
  652. Ardublockly.resizeBlocklyWorkspace = function () {
  653. var contentBlocks = document.getElementById('content_blocks');
  654. var wrapperPanelSize =
  655. Ardublockly.getBBox_(document.getElementById('blocks_panel'));
  656. if (document.documentElement.clientWidth < 993) {
  657. let footer = document.getElementById('footer_');
  658. if (document.getElementById('Serial-monitor').style.display == 'none') {
  659. let Ardunio = Ardublockly.getBBox_(document.getElementById('codeArea'));
  660. let Ardunio1 = Ardublockly.getBBox_(document.getElementById('uploader'));
  661. footer.style.marginTop = Ardunio.height + Ardunio1.height + 'px';
  662. } else {
  663. let height = Ardublockly.getBBox_(document.getElementById('Serial-monitor'))
  664. footer.style.marginTop = height.height + 'px'
  665. }
  666. } else {
  667. document.getElementById('footer_').style.marginTop = 0
  668. }
  669. contentBlocks.style.top = wrapperPanelSize.y + 'px';
  670. contentBlocks.style.left = wrapperPanelSize.x + 'px';
  671. // Height and width need to be set, read back, then set again to
  672. // compensate for scrollbars.
  673. contentBlocks.style.height = wrapperPanelSize.height + 'px';
  674. contentBlocks.style.height =
  675. (2 * wrapperPanelSize.height - contentBlocks.offsetHeight) + 'px';
  676. contentBlocks.style.width = wrapperPanelSize.width + 'px';
  677. contentBlocks.style.width =
  678. (2 * wrapperPanelSize.width - contentBlocks.offsetWidth) + 'px';
  679. };
  680. /**
  681. * Sets the text for a "Materialize Modal" (like an android Dialog) to have
  682. * alert-like HTML messages.
  683. * @param {!string} title HTML to include in title.
  684. * @param {!element} body HTML to include in body.
  685. * @param {boolean=} confirm Indicates if the user is shown and option to just
  686. * 'Ok' or 'Ok and cancel'.
  687. * @param {string=|function=} callback If confirm option is selected this would
  688. * be the function called when clicked 'OK'.
  689. */
  690. Ardublockly.materialAlert = function (title, body, confirm, callback) {
  691. $('#gen_alert_title').text(title);
  692. $('#gen_alert_body').text('');
  693. $('#gen_alert_body').append(body);
  694. if (confirm == true) {
  695. $('#gen_alert_cancel_link').css({ 'display': 'inline-block' });
  696. if (callback) {
  697. $('#gen_alert_ok_link').bind('click', callback);
  698. }
  699. } else {
  700. $('#gen_alert_cancel_link').css({ 'display': 'none' });
  701. $('#gen_alert_ok_link').unbind('click');
  702. }
  703. $('#gen_alert').modal('open');
  704. window.location.hash = '';
  705. };
  706. Ardublockly.exampleAlert = function (title, body, confirm, callback) {
  707. $('#example_alert_body').text('');
  708. $('#example_alert_body').append(body);
  709. $('#example_alert').modal('open');
  710. $('#loading').css({'display':'block'});
  711. window.location.hash = '';
  712. };
  713. Ardublockly.customAlert = function (title, body, customText, callback) {
  714. $('#cus_alert_title').text(title);
  715. $('#cus_alert_body').text('');
  716. $('#cus_alert_body').append(body);
  717. if (customText) {
  718. $('#cus_alert_button_content').text(customText);
  719. if (callback) {
  720. $('#cus_alert_button').bind('click', callback);
  721. }
  722. } else {
  723. $('#cus_alert_button').unbind('click');
  724. }
  725. $('#cus_alert').modal('open');
  726. window.location.hash = '';
  727. };
  728. /**
  729. * Opens the modal that allows selection on additional toolbox categories.
  730. * @param {!element} htmlContent HTML to include in modal body.
  731. */
  732. Ardublockly.openAdditionalBlocksModal = function (htmlContent) {
  733. $('#blocks_menu_body').text('');
  734. $('#blocks_menu_body').append(htmlContent);
  735. $('#blocks_menu').openModal({
  736. dismissible: true,
  737. opacity: .5,
  738. in_duration: 200,
  739. out_duration: 250
  740. });
  741. };
  742. /**
  743. * Creates an HTML node with the blocks category information from arguments.
  744. * @param {!string} title Text to include as category title.
  745. * @param {!string} description TExt to include in as description.
  746. * @param {!function} clickBind Function to bind to the tick box click.
  747. * @return {!element} HTML element to display the category content.
  748. */
  749. Ardublockly.createExtraBlocksCatHtml = function (title, description, clickBind) {
  750. var tickId = title.replace(/\s+/g, '');
  751. var tickHtml = document.createElement('input');
  752. tickHtml.type = 'checkbox';
  753. tickHtml.id = tickId;
  754. tickHtml.addEventListener('click', function () {
  755. clickBind(document.getElementById(tickId).checked);
  756. });
  757. var tickLabelHtml = document.createElement('label');
  758. tickLabelHtml.htmlFor = tickId;
  759. tickLabelHtml.className = 'modal_label_title';
  760. tickLabelHtml.appendChild(document.createTextNode(title));
  761. var separatorHtml = document.createElement('div');
  762. separatorHtml.className = 'divider';
  763. var descriptionHthml = document.createElement('div');
  764. descriptionHthml.appendChild(document.createTextNode(description));
  765. var htmlContent = document.createElement('div');
  766. htmlContent.className = 'modal_section';
  767. htmlContent.appendChild(tickHtml);
  768. htmlContent.appendChild(tickLabelHtml);
  769. htmlContent.appendChild(separatorHtml);
  770. htmlContent.appendChild(descriptionHthml);
  771. return htmlContent;
  772. };
  773. /**
  774. * Displays a short message for 4 seconds in the form of a Materialize toast.
  775. * @param {!string} message Text to be temporarily displayed.
  776. */
  777. Ardublockly.MaterialToast = function (message) {
  778. Materialize.toast(message, 4000, 'shareUrl');
  779. };
  780. /**
  781. * Initialises the sketch name input text JavaScript to dynamically adjust its
  782. * width to the width of its contents.
  783. */
  784. Ardublockly.sketchNameSizeEffect = function () {
  785. var resizeInput = function () {
  786. $(this).attr('size', $(this).val().length);
  787. };
  788. var correctInput = function () {
  789. // If nothing in the input, add default name
  790. if ($(this).val() == '') {
  791. // $(this).val(Ardublockly.LOCALISED_TEXT.sketch_name);
  792. $(this).attr('size', 8);
  793. }
  794. // Replace all spaces with underscores
  795. $(this).val($(this).val().replace(/ /g, '_'));
  796. };
  797. var sketchNameInput = $('#sketch_name');
  798. sketchNameInput.keydown(resizeInput).each(resizeInput);
  799. sketchNameInput.blur(correctInput);
  800. };
  801. /**
  802. * Sets a string to the SketchName input field and triggers the events set from
  803. * Ardublockly.sketchNameSizeEffect().
  804. * @param {string?} newName Optional string to place in the sketch_name input.
  805. */
  806. Ardublockly.sketchNameSet = function (newName) {
  807. var sketchNewName = newName || '';
  808. var sketchNameInput = $('#sketch_name');
  809. sketchNameInput.val(sketchNewName);
  810. sketchNameInput.attr('size', sketchNewName.length);
  811. sketchNameInput.keydown();
  812. sketchNameInput.blur();
  813. };
  814. /**
  815. * Initialises the sketch name input text JavaScript of the cloud
  816. * to dynamically adjust its width to the width of its contents.
  817. */
  818. Ardublockly.cloudSketchNameSizeEffect = function () {
  819. var resizeInput = function () {
  820. $(this).attr('size', $(this).val().length);
  821. };
  822. var correctInput = function () {
  823. // If nothing in the input, add default name
  824. if ($(this).val() == '') {
  825. $(this).val('Untitled.xml');
  826. $(this).attr('size', 10);
  827. }
  828. // Replace all spaces with underscores
  829. $(this).val($(this).val().replace(/ /g, '_'));
  830. };
  831. var cloudSketchNameInput = $('#cloud_sketchName');
  832. cloudSketchNameInput.keydown(resizeInput).each(resizeInput);
  833. cloudSketchNameInput.blur(correctInput);
  834. };
  835. /**
  836. * Sets a string to the SketchName input field of the cloud and triggers the events set from
  837. * Ardublockly.sketchNameSizeEffect().
  838. * @param {string?} newName Optional string to place in the sketch_name input.
  839. */
  840. Ardublockly.cloudSketchNameSet = function (newName) {
  841. var cloudSketchNewName = newName || '';
  842. var cloudSketchNameInput = $('#cloud_sketchName');
  843. cloudSketchNameInput.val(cloudSketchNewName);
  844. cloudSketchNameInput.attr('size', cloudSketchNewName.length);
  845. cloudSketchNameInput.keydown();
  846. cloudSketchNameInput.blur();
  847. };
  848. /**
  849. * Compute the absolute coordinates and dimensions of an HTML element.
  850. * @param {!Element} element Element to match.
  851. * @return {!Object} Contains height, width, x, and y properties.
  852. * @private
  853. */
  854. Ardublockly.getBBox_ = function (element) {
  855. var height = element.offsetHeight;
  856. var width = element.offsetWidth;
  857. var x = 0;
  858. var y = 0;
  859. do {
  860. x += element.offsetLeft;
  861. y += element.offsetTop;
  862. element = element.offsetParent;
  863. } while (element);
  864. return {
  865. height: height,
  866. width: width,
  867. x: x,
  868. y: y
  869. };
  870. };