diff --git a/www/css/blocklino.css b/www/css/blocklino.css index 04c9878e..74be6d13 100644 --- a/www/css/blocklino.css +++ b/www/css/blocklino.css @@ -1,271 +1,281 @@ -@CHARSET "UTF-8"; - -::-webkit-scrollbar { width: 0px; } - -html,body { - height: 100% -} -body { - background-color: #2d2d64; - font-family: 'Poppins', sans-serif; - margin: 0 -} - -#div_var { - position: absolute; - text-align: center; - top: 45px; - margin: 10px -} -#divfenetre { - background-color: #fff; - position: absolute; - top: 45px; - bottom: 0px; - width: 100% -} -#divBody { - position: absolute; - top: 78px; - bottom: 0px; - width: 100% -} -#title-bar { - -webkit-app-region: drag; - height: 80px; - text-align: center; - line-height: 40px; - vertical-align: middle; - background-color: #2d2d64; - padding: none; - margin: 0px -} -#title-bar-repl { - -webkit-app-region: drag; - height: 40px; - text-align: center; - line-height: 40px; - vertical-align: middle; - background-color: #b8b8b8; - padding: none; - margin: 0px -} -#toptitle { - -webkit-app-region: drag; - height: 30px; - text-align: center; - line-height: 20px; - vertical-align: middle; - background-color: #2d2d64; - padding: none; - margin: 0px -} -#title { - position: fixed; - font-weight: bold; - top: 0px; - left: 6px; - color:white -} -#title-project-name { - margin-left: 0px; - border-radius: 5px; - color: #2d2d64; - font-weight: bold; - height: 42px; -} -#title-bar-btns { - -webkit-app-region: no-drag; - position: fixed; - right: 0px - -} -#fenetre_term{ - width: 630px; - height: 415px; - border: 1px solid #ccc; - padding:0px 5px 5px 5px; - margin-bottom: 5px; - margin-left: 5px; - border-radius: 5px; - overflow: auto -} -#fenetre_repl{ - width: 630px; - height: 415px; - color:white; - background-color: #b8b8b8; - border: 1px solid #ccc; - padding:0px 5px 5px 5px; - margin-bottom: 5px; - margin-left: 5px; - border-radius: 5px; - overflow: auto -} -#header { - position: absolute; - top: 25px; - background: #2d2d64; - height: 40px; - width: 100% -} -.arduino_card_mini_picture_div { - position:absolute; - top:20px; - right:45px; - width:250px; - height:600px -} -.div_close_modal { - padding-right: 10px; - float: right; - bottom: 10px; - position: absolute; - text-align: center -} -.modal-body-config-global_left { - position: relative -} -#content_factory{ - height: 100%; - width: 100%; - padding-right: 5px -} -#content_blocks{ - height: 100%; - width: 100% -} -#blockly_l{ - background-color: #fff; - float:left; - height:100%; - width:55% -} -#blockly_r{ - float:right; - height:100%; - width:45% -} -#btn_all { - position: absolute; - left: 5px; - top: 2px; - z-index: 10 - -} -#btn_group { - position: absolute; - right: 5px; - top: 2px; - z-index: 10 - -} -#toggle { - width: 25%; - position: absolute; - z-index: 1; - right: 15px; - top: 0px; - bottom: 20px; - opacity: 0.9 -} -#content_code { - font-size: 18px; - position: absolute; - border: 1px solid #fff; - top: 0px; - right: 5px; - bottom: 5px; - left: 5px -} -#toggle pre { - height: 100% -} -#serial-checboxes { - margin-left: 20px -} -#modal-body-config { - -webkit-column-count: 3; - -moz-column-count: 3; - column-count: 3 -} -#select_all { - margin-left: 15px -} -#span_select_all { - font-weight: bold -} -.blocklySvg { - border-top: none !important; - background-color: #A9B9C9; -} -.blocklyScrollbarHandle { - fill: #FF5700 -} -.blocklyToolboxDiv { - background-color: #f2f2f2 !important; - font-weight: bold; - margin-left: 0px; - width: 220px; - opacity: 1 -} - -.blocklyTreeLabel { - cursor: default; -font-family: 'Poppins', sans-serif; -font-size: 18px; -font-weight: bold; -padding: 0 3px; -vertical-align: middle; -} - -.blocklyFlyoutBackground { - fill: #e6e6e6 !important -} -.blocklyTreeRoot { - padding: 0 !important -} -.blocklyToolboxDiv .blocklyTreeRoot [role="treeitem"] { - position: relative -} -.blocklyToolboxDiv .blocklyTreeRoot [role="treeitem"].blocklyTreeRow { - line-height: normal !important; - padding: 6px 50px 6px 12px !important -} -.blocklyTreeRow { - height: 34px !important; - line-height: 30px !important -} -.blocklyTreeSeparator { - background: none repeat scroll 0 0 #e9f9f9 !important; - border-bottom: none !important; - height: 10px !important -} -.colors_category { - height: 100%; - left: 0; - position: absolute; - top: 0; - width: 8px -} -.loading { - position: fixed; - left: 0px; - top: 0px; - width: 100%; - height: 100%; - z-index: 9999; - background: url(../media/ajax-loading.gif) center no-repeat #2d2d64 -} -.vignette { - max-width: 80px; - max-height: 80px -} -.blocklyScrollbarKnob { - fill: #2d2d64 !important; - fill-opacity: 0.7 !important -} -.blocklyScrollbarBackground { - stroke-width: 0px !important; - fill: none !important -} +@CHARSET "UTF-8"; + +::-webkit-scrollbar { + -webkit-appearance: none; + width: 8px; + height: 8px; +} + +::-webkit-scrollbar-thumb { + border-radius: 4px; + background-color: rgba(45, 45, 100, .5); + box-shadow: 0 0 1px rgba(255, 255, 255, .5); +} + +html,body { + height: 100% +} +body { + background-color: #2d2d64; + font-family: 'Poppins', sans-serif; + margin: 0 +} + +#div_var { + position: absolute; + text-align: center; + top: 45px; + margin: 10px +} +#divfenetre { + background-color: #fff; + position: absolute; + top: 45px; + bottom: 0px; + width: 100% +} +#divBody { + position: absolute; + top: 78px; + bottom: 0px; + width: 100% +} +#title-bar { + -webkit-app-region: drag; + height: 80px; + text-align: center; + line-height: 40px; + vertical-align: middle; + background-color: #2d2d64; + padding: none; + margin: 0px +} +#title-bar-repl { + -webkit-app-region: drag; + height: 40px; + text-align: center; + line-height: 40px; + vertical-align: middle; + background-color: #b8b8b8; + padding: none; + margin: 0px +} +#toptitle { + -webkit-app-region: drag; + height: 30px; + text-align: center; + line-height: 20px; + vertical-align: middle; + background-color: #2d2d64; + padding: none; + margin: 0px +} +#title { + position: fixed; + font-weight: bold; + top: 0px; + left: 6px; + color:white +} +#title-project-name { + margin-left: 0px; + border-radius: 5px; + color: #2d2d64; + font-weight: bold; + height: 42px; +} +#title-bar-btns { + -webkit-app-region: no-drag; + position: fixed; + right: 0px + +} +#fenetre_term{ + width: 630px; + height: 415px; + border: 1px solid #ccc; + padding:0px 5px 5px 5px; + margin-bottom: 5px; + margin-left: 5px; + border-radius: 5px; + overflow: auto +} +#fenetre_repl{ + width: 630px; + height: 415px; + color:white; + background-color: #b8b8b8; + border: 1px solid #ccc; + padding:0px 5px 5px 5px; + margin-bottom: 5px; + margin-left: 5px; + border-radius: 5px; + overflow: auto +} +#header { + position: absolute; + top: 25px; + background: #2d2d64; + height: 40px; + width: 100% +} +.arduino_card_mini_picture_div { + position:absolute; + top:20px; + right:45px; + width:250px; + height:600px +} +.div_close_modal { + padding-right: 10px; + float: right; + bottom: 10px; + position: absolute; + text-align: center +} +.modal-body-config-global_left { + position: relative +} +#content_factory{ + height: 100%; + width: 100%; + padding-right: 5px +} +#content_blocks{ + height: 100%; + width: 100% +} +#blockly_l{ + background-color: #fff; + float:left; + height:100%; + width:55% +} +#blockly_r{ + float:right; + height:100%; + width:45% +} +#btn_all { + position: absolute; + left: 5px; + top: 2px; + z-index: 10 + +} +#btn_group { + position: absolute; + right: 5px; + top: 2px; + z-index: 10 + +} +#toggle { + width: 25%; + position: absolute; + z-index: 1; + right: 15px; + top: 0px; + bottom: 20px; + opacity: 0.9 +} +#content_code { + font-size: 18px; + position: absolute; + border: 1px solid #fff; + top: 0px; + right: 5px; + bottom: 5px; + left: 5px +} +#toggle pre { + height: 100% +} +#serial-checboxes { + margin-left: 20px +} +#modal-body-config { + -webkit-column-count: 3; + -moz-column-count: 3; + column-count: 3 +} +#select_all { + margin-left: 15px +} +#span_select_all { + font-weight: bold +} +.blocklySvg { + border-top: none !important; + background-color: #A9B9C9; +} +.blocklyScrollbarHandle { + fill: #FF5700 +} +.blocklyToolboxDiv { + background-color: #f2f2f2 !important; + font-weight: bold; + margin-left: 0px; + width: 220px; + opacity: 1 +} + +.blocklyTreeLabel { + cursor: default; +font-family: 'Poppins', sans-serif; +font-size: 18px; +font-weight: bold; +padding: 0 3px; +vertical-align: middle; +} + +.blocklyFlyoutBackground { + fill: #e6e6e6 !important +} +.blocklyTreeRoot { + padding: 0 !important +} +.blocklyToolboxDiv .blocklyTreeRoot [role="treeitem"] { + position: relative +} +.blocklyToolboxDiv .blocklyTreeRoot [role="treeitem"].blocklyTreeRow { + line-height: normal !important; + padding: 6px 50px 6px 12px !important +} +.blocklyTreeRow { + height: 34px !important; + line-height: 30px !important +} +.blocklyTreeSeparator { + background: none repeat scroll 0 0 #e9f9f9 !important; + border-bottom: none !important; + height: 10px !important +} +.colors_category { + height: 100%; + left: 0; + position: absolute; + top: 0; + width: 8px +} +.loading { + position: fixed; + left: 0px; + top: 0px; + width: 100%; + height: 100%; + z-index: 9999; + background: url(../media/ajax-loading.gif) center no-repeat #2d2d64 +} +.vignette { + max-width: 80px; + max-height: 80px +} +.blocklyScrollbarKnob { + fill: #2d2d64 !important; + fill-opacity: 0.7 !important +} +.blocklyScrollbarBackground { + stroke-width: 0px !important; + fill: none !important +} \ No newline at end of file diff --git a/www/index.html b/www/index.html index b81ef7ea..bbd3a1bc 100644 --- a/www/index.html +++ b/www/index.html @@ -1,413 +1,413 @@ - - - - - - Otto Blockly - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
-
Logo
- - -
-
- - - -
-
- -
-
- -
-
#enter your code here
-
- - - - - - - - - - - - - - - - - - - - - - + + + + + + Otto Blockly + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
Logo
+ + +
+
+ + + +
+
+ +
+
+ +
+
#enter your code here
+
+ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/www/js/blocklino.js b/www/js/blocklino.js index 984acafc..95bc2cae 100644 --- a/www/js/blocklino.js +++ b/www/js/blocklino.js @@ -1,861 +1,862 @@ -'use strict'; - -var BlocklyDuino = {}; -BlocklyDuino.selectedToolbox = "toolbox_arduino_all"; -BlocklyDuino.selectedCard = "nano"; -BlocklyDuino.content = "on"; -BlocklyDuino.workspace = null; - -BlocklyDuino.init = function() { - Code.initLanguage(); - BlocklyDuino.loadConfig(); - BlocklyDuino.workspace = Blockly.inject('content_blocks',{grid:{snap:true},sounds:true,media:'media/',toolbox:BlocklyDuino.buildToolbox(),zoom:{controls:true,wheel:true}}); - BlocklyDuino.bindFunctions(); - BlocklyDuino.workspace.render(); - BlocklyDuino.workspace.addChangeListener(BlocklyDuino.renderArduinoCodePreview); - BlocklyDuino.loadFile(); - window.addEventListener('unload', BlocklyDuino.backupBlocks, false); -}; -BlocklyDuino.loadFile = function() { - var urlFile = BlocklyDuino.getStringParamFromUrl('url', ''); - if (urlFile.endsWith(".py")) { - $.get(urlFile, function(data) { - $('#codeORblock').bootstrapToggle("off"); - $('a[href="#content_code"]').tab('show'); - $('#btn_print').addClass("hidden"); - $('#btn_preview').addClass("hidden"); - $('#btn_verify').removeClass("hidden"); - $('#btn_search').removeClass("hidden"); - window.localStorage.content="off"; - editor.session.setMode("ace/mode/python"); - editor.setOptions({ - enableBasicAutocompletion: true, - enableSnippets: true, - enableLiveAutocompletion: true - }); - editor.setValue(data,1) - }, 'text') - } - if (urlFile.endsWith(".ino")) { - $.get(urlFile, function(data) { - $('#codeORblock').bootstrapToggle("off"); - $('a[href="#content_code"]').tab('show'); - $('#btn_print').addClass("hidden"); - $('#btn_preview').addClass("hidden"); - $('#btn_verify').removeClass("hidden"); - $('#btn_search').removeClass("hidden"); - window.localStorage.content="off"; - editor.session.setMode("ace/mode/c_cpp"); - editor.setOptions({ - enableBasicAutocompletion: true, - enableSnippets: true, - enableLiveAutocompletion: true - }); - editor.setValue(data,1) - }, 'text') - } - var loadOnce = null; - try {loadOnce = window.localStorage.loadOnceBlocks} catch (e) {} - if (urlFile) { - $.get( urlFile, function(data){BlocklyDuino.loadBlocks(data)}, 'text') - } else { - BlocklyDuino.loadBlocks() - } -}; -BlocklyDuino.save_com = function() { - $("#portserie").blur(); - var com=$("#portserie").val(); - window.localStorage.com = com; -}; -BlocklyDuino.renderArduinoCodePreview = function() { - var prog = window.localStorage.prog; - if (prog != "python") { - $('#pre_previewArduino').text(Blockly.Arduino.workspaceToCode(BlocklyDuino.workspace)); - $('#pre_previewArduino').html(prettyPrintOne($('#pre_previewArduino').html(), 'cpp')); - } else { - $('#pre_previewArduino').text(Blockly.Python.workspaceToCode(BlocklyDuino.workspace)); - $('#pre_previewArduino').html(prettyPrintOne($('#pre_previewArduino').html(), 'py')); - } -}; -BlocklyDuino.getStringParamFromUrl = function(name, defaultValue) { - var val = location.search.match(new RegExp('[?&]' + name + '=([^&]+)')); - return val ? decodeURIComponent(val[1].replace(/\+/g, '%20')) : defaultValue; -}; -BlocklyDuino.addReplaceParamToUrl = function(url, param, value) { - var re = new RegExp("([?&])" + param + "=.*?(&|$)", "i"); - var separator = url.indexOf('?') !== -1 ? "&" : "?"; - if (url.match(re)) { - return url.replace(re, '$1' + param + "=" + value + '$2'); - } - else { - return url + separator + param + "=" + value; - } -}; -BlocklyDuino.loadBlocks = function(defaultXml) { - if (defaultXml) { - var xml = Blockly.Xml.textToDom(defaultXml); - Blockly.Xml.domToWorkspace(xml,BlocklyDuino.workspace); - } else { - var loadOnce = null; - try { - loadOnce = window.localStorage.loadOnceBlocks; - } catch (e) {} - if (loadOnce != null) { - delete window.localStorage.loadOnceBlocks; - var xml = Blockly.Xml.textToDom(loadOnce); - Blockly.Xml.domToWorkspace(xml,BlocklyDuino.workspace); - } - } -}; -BlocklyDuino.load = function(event) { - var files = event.target.files; - if (files.length != 1) { - return; - } - var reader = new FileReader(); - reader.onloadend = function(event) { - var target = event.target; - if (target.readyState == 2) { - if (files[0].name.endsWith("ino")) { - $('#codeORblock').bootstrapToggle("off"); - $('a[href="#content_code"]').tab('show'); - $('#btn_print').addClass("hidden"); - $('#btn_preview').addClass("hidden"); - $('#btn_verify').removeClass("hidden"); - $('#btn_search').removeClass("hidden"); - window.localStorage.content="off"; - editor.session.setMode("ace/mode/c_cpp"); - editor.setOptions({ - enableBasicAutocompletion: true, - enableSnippets: true, - enableLiveAutocompletion: true - }); - editor.setValue(target.result,1) - } - if (files[0].name.endsWith("py")) { - $('#codeORblock').bootstrapToggle("off"); - $('a[href="#content_code"]').tab('show'); - $('#btn_print').addClass("hidden"); - $('#btn_preview').addClass("hidden"); - $('#btn_verify').removeClass("hidden"); - $('#btn_search').removeClass("hidden"); - window.localStorage.content="off"; - editor.session.setMode("ace/mode/python"); - editor.setOptions({ - enableBasicAutocompletion: true, - enableSnippets: true, - enableLiveAutocompletion: true - }); - editor.setValue(target.result,1) - } - try { - var xml = Blockly.Xml.textToDom(target.result); - - } catch (e) { - alert(MSG['xmlError']+'\n' + e); - return - } - BlocklyDuino.workspace.clear(); - Blockly.Xml.domToWorkspace(xml,BlocklyDuino.workspace); - BlocklyDuino.workspace.render(); - } - }; - reader.readAsText(files[0]) -}; -BlocklyDuino.backupBlocks = function() { - if (typeof Blockly != 'undefined' && window.localStorage) { - var xml = Blockly.Xml.workspaceToDom(BlocklyDuino.workspace); - var text = Blockly.Xml.domToText(xml); - window.localStorage.loadOnceBlocks = text; - } -}; -BlocklyDuino.loadConfig = function() { - var card = window.localStorage.card; - var content=window.localStorage.content; - var prog = window.localStorage.prog; - if (card===undefined) { - window.localStorage.card = BlocklyDuino.selectedCard; - window.localStorage.prog = profile[BlocklyDuino.selectedCard].prog; - window.localStorage.toolbox = BlocklyDuino.selectedToolbox; - $("#boards").val(BlocklyDuino.selectedCard); - $('#arduino_card_mini_picture').attr("src", profile[BlocklyDuino.selectedCard]['picture']); - if(BlocklyDuino.selectedCard=="nanooptiboot"||BlocklyDuino.selectedCard=="nano"||BlocklyDuino.selectedCard=="nona4809"){ - $("#warning").show(); - }else{ - $("#warning").hide(); - } - $("#toolboxes").val(BlocklyDuino.selectedToolbox); - BlocklyDuino.loadToolboxDefinition(BlocklyDuino.selectedToolbox) - } else { - var toolbox = window.localStorage.toolbox; - BlocklyDuino.selectedToolbox = toolbox; - $("#boards").val(card); - $('#arduino_card_mini_picture').attr("src", profile[card]['picture']); - if(card=="nanooptiboot"||card=="nano"||card=="nona4809"){ - $("#warning").show(); - }else{ - $("#warning").hide(); - } - $("#toolboxes").val(toolbox); - BlocklyDuino.loadToolboxDefinition(toolbox) - } - if (content===undefined) { - window.localStorage.content = BlocklyDuino.content; - $('#codeORblock').bootstrapToggle(BlocklyDuino.content); - $('#btn_verify').addClass("hidden"); - $('#btn_search').addClass("hidden") - } else { - $('#codeORblock').bootstrapToggle(content); - if (content=="off") { - $('a[href="#content_code"]').tab('show'); - $('#btn_verify').removeClass("hidden"); - $('#btn_search').removeClass("hidden") - } - $('#btn_verify').addClass("hidden"); - $('#btn_search').addClass("hidden") - } - if (prog == "python") { - $('#btn_bin').addClass("hidden") - } -}; -BlocklyDuino.change_card = function() { - BlocklyDuino.backupBlocks(); - var card = window.localStorage.card; - var toolbox = window.localStorage.toolbox; - $("#boards").blur(); - var new_card = $("#boards").val(); - var new_prog = window.profile[new_card].prog; - if (window.profile[new_card].cpu != window.profile[card].cpu) { - if (window.confirm(MSG['arduino_card'] + window.profile[new_card].description + ' ?')){ - $('#arduino_card_mini_picture').attr("src", profile[new_card]['picture']); - if (new_prog != "python") { - $('#btn_preview').attr('title', MSG['btn_preview_ino']); - $('#btn_saveino').attr('title', MSG['btn_save_ino']); - $('#btn_bin').removeClass("hidden"); - - window.localStorage.prog = new_prog; - var new_toolbox = "toolbox_arduino_all"; //by default - var mystartfile; - mystartfile= ''; - mystartfile += ''; - mystartfile += ''; - - if (window.profile[new_card].cpu == "esp8266") - new_toolbox = "toolbox_arduino_all-esp8266" - else if (window.profile[new_card].cpu == "esp32") - new_toolbox = "toolbox_arduino_all-esp32" - else if (window.profile[new_card].cpu == "atmega328p-x") - new_toolbox = "toolbox_arduino_all-mrtx" - else if (window.profile[new_card].cpu == "mrtnode-esp32") - new_toolbox = "toolbox_arduino_all-mrtnode" - - - window.localStorage.toolbox = new_toolbox; - BlocklyDuino.workspace.clear(); - - // If CPU has changed and any of the 2 CPUs is an esp (32 or 8266) we need to load blocks for the new board - if ((window.profile[new_card].cpu != window.profile[card].cpu) /*&& ((window.profile[new_card].cpu.startsWith("esp")) || (window.profile[card].cpu.startsWith("esp")) ) */ ) - BlocklyDuino.loadBlocks(mystartfile); - else - BlocklyDuino.loadBlocks(); - - BlocklyDuino.loadToolboxDefinition(new_toolbox); - Blockly.getMainWorkspace().updateToolbox(BlocklyDuino.buildToolbox()); - if (window.localStorage.level==1) - $("#btn_level1").trigger("click"); - else - if ( window.localStorage.level==2) - $("#btn_level2").trigger("click"); - else - $("#btn_level3").trigger("click"); - - - BlocklyDuino.workspace.render() - } else { - $('#btn_preview').attr('title', MSG['btn_preview_py']); - $('#btn_saveino').attr('title', MSG['btn_save_py']); - $('#btn_bin').addClass("hidden"); - if ( window.profile[new_card].cpu == "cortexM0" ) { - var new_toolbox = "toolbox_microbit"; - } else { - var new_toolbox = "toolbox_lycee"; - } - window.localStorage.prog = new_prog; - window.localStorage.toolbox = new_toolbox; - BlocklyDuino.workspace.clear(); - BlocklyDuino.loadToolboxDefinition(new_toolbox); - Blockly.getMainWorkspace().updateToolbox(BlocklyDuino.buildToolbox()); - BlocklyDuino.workspace.render() - } - } else { - $("#boards").val(card); - return - } - } - window.localStorage.card = new_card -}; -BlocklyDuino.discard = function() { - var count = BlocklyDuino.workspace.getAllBlocks().length; - if (count < 4 || window.confirm(MSG['discard'])) { - BlocklyDuino.workspace.clear(); - BlocklyDuino.workspace.render(); - } -}; -BlocklyDuino.Undo = function() { - if (localStorage.getItem("content") == "on") { - Blockly.mainWorkspace.undo(0) - } else { - editor.undo() - } -}; -BlocklyDuino.Redo = function() { - if (localStorage.getItem("content") == "on") { - Blockly.mainWorkspace.undo(1) - } else { - editor.redo() - } -}; -BlocklyDuino.search = function() { - editor.execCommand("find") -}; -BlocklyDuino.bindFunctions = function() { - $('.modal-child').on('show.bs.modal', function () { - var modalParent = $(this).attr('data-modal-parent'); - $(modalParent).css('opacity', 0) - }); - $('.modal-child').on('hidden.bs.modal', function () { - var modalParent = $(this).attr('data-modal-parent'); - $(modalParent).css('opacity', 1) - }); - $('#btn_new').on("click", BlocklyDuino.discard); - $('#btn_undo').on("click", BlocklyDuino.Undo); - $('#btn_redo').on("click", BlocklyDuino.Redo); - $('#btn_print').on("click", BlocklyDuino.workspace_capture); - $('#btn_search').on("click", BlocklyDuino.search); - $('#boards').on("focus", function() { - BlocklyDuino.selectedCard = $(this).val() - }); - $('#btn_preview').on("click", function() { - $("#toggle").toggle("slide") - }); - $('#codeORblock').on("change", function() { - if (window.localStorage.prog!="python") { - editor.session.setMode("ace/mode/c_cpp"); - editor.setOptions({ - enableBasicAutocompletion: true, - enableSnippets: true, - enableLiveAutocompletion: true - }); - } else { - editor.session.setMode("ace/mode/python"); - editor.setOptions({ - enableBasicAutocompletion: true, - enableSnippets: true, - enableLiveAutocompletion: true - }); - } - if (window.localStorage.content=="on") { - editor.setValue($('#pre_previewArduino').text(),1); - $('a[href="#content_code"]').tab('show'); - $('#btn_print').addClass("hidden"); - $('#btn_preview').addClass("hidden"); - $('#btn_verify').removeClass("hidden"); - $('#btn_search').removeClass("hidden"); - window.localStorage.content="off" - } else { - $('a[href="#content_blocks"]').tab('show'); - $('#btn_print').removeClass("hidden"); - $('#btn_preview').removeClass("hidden"); - $('#btn_verify').addClass("hidden"); - $('#btn_search').addClass("hidden"); - window.localStorage.content="on" - } - }); -/* Keep code preview window visible when clicked to allow selection - $('#pre_previewArduino').on("click", function() { - $("#toggle").toggle("slide"); - }); */ - $('#btn_verify').mouseover(function() { - document.getElementById("survol").textContent = "Check code"; - }).mouseout(function() { - document.getElementById("survol").textContent = ""; - }); - $('#btn_flash').mouseover(function() { - document.getElementById("survol").textContent = "Upload code"; - }).mouseout(function() { - document.getElementById("survol").textContent = ""; - }); - $('#btn_bin').mouseover(function() { - document.getElementById("survol").textContent = "Export as Binary .hex"; - }).mouseout(function() { - document.getElementById("survol").textContent = ""; - }); - $('#toolboxes').on("focus", function() { - BlocklyDuino.selectedToolbox = $(this).val(); - }); - $('#toolboxes').on("change", BlocklyDuino.changeToolboxDefinition); - $('#configModal').on('hidden.bs.modal', function(e) { - BlocklyDuino.loadToolboxDefinition(BlocklyDuino.selectedToolbox); - }); - $('#load').on("change", BlocklyDuino.load); - $('#btn_fakeload').on("click", function() { - $('#load').click() - }); - $('#btn_config').on("click", BlocklyDuino.openConfigToolbox); - $('#btn_level1').on("click", BlocklyDuino.buildToolboxLevel1); - $('#btn_level2').on("click", BlocklyDuino.buildToolboxLevel2); - $('#btn_level3').on("click", BlocklyDuino.buildToolboxLevel3); - - $('#select_all').on("click", BlocklyDuino.checkAll); - $('#btn_valid_config').on("click", BlocklyDuino.changeToolbox); - $('#btn_example').on("click", BlocklyDuino.buildExamples); - if(typeof process === 'undefined') { - $('#btn_factory').on("click", function() { - window.open("factory.html","_blank", null); - }); - $('#btn_saveXML').on("click", BlocklyDuino.saveXmlFile); - $('#btn_saveino').on("click", function () { - if (window.localStorage.prog == "arduino") { - BlocklyDuino.saveino() - } else { - BlocklyDuino.savepy() - } - }); - } -}; -BlocklyDuino.checkAll = function() { - if(this.checked) { - $('#modal-body-config input:checkbox[id^=checkbox_]').each(function() { - this.checked = true; - }); - } - else { - $('#modal-body-config input:checkbox[id^=checkbox_]').each(function() { - this.checked = false; - }); - } -}; -BlocklyDuino.openConfigToolbox = function() { - var modalbody = $("#modal-body-config"); - var loadIds = window.localStorage.toolboxids; - if (loadIds === undefined || loadIds === "") { - if ($('#defaultCategories1').length) { - loadIds = $('#defaultCategories1').html(); - } else { - loadIds = ''; - } - } - modalbody.empty(); - var i=0, n; - var ligne = ""; - $("#toolbox").children("category").each(function() { - n = loadIds.search($(this).attr("id")); - if (n >= 0) { - ligne = ' ' +Blockly.Msg[$(this).attr("id")]+ '
'; - } else { - ligne = ' ' +Blockly.Msg[$(this).attr("id")]+ '
'; - } - i++; - modalbody.append(ligne); - }); -}; -BlocklyDuino.changeToolbox = function() { - BlocklyDuino.backupBlocks(); - var toolboxIds = []; - window.localStorage.lang = $('#languageMenu').val(); - $('#modal-body-config input:checkbox[id^=checkbox_]').each(function() { - if (this.checked == true) { - var xmlid = this.id; - toolboxIds.push(xmlid.replace("checkbox_", "")) - } - }); - window.localStorage.toolboxids = toolboxIds; - Blockly.getMainWorkspace().updateToolbox(BlocklyDuino.buildToolbox()); - BlocklyDuino.workspace.render(); - $('#configModal').modal('hide') - window.location.reload(); -}; - -BlocklyDuino.changelanguage = function() { - window.localStorage.lang = $('#languageMenu').val(); - window.location.reload(); -}; - - - - -BlocklyDuino.buildToolbox = function() { - var loadIds = window.localStorage.toolboxids; - if (loadIds === undefined || loadIds === "") { - if ($('#defaultCategories1').length) { - loadIds = $('#defaultCategories1').html(); - } else { - loadIds = ''; - } - } - var xmlValue = ''; - var xmlids = loadIds.split(","); - for (var i = 0; i < xmlids.length; i++) { - if ($('#'+xmlids[i]).length) { - xmlValue += $('#'+xmlids[i])[0].outerHTML; - } - } - xmlValue += ''; - return xmlValue; -}; - - -BlocklyDuino.buildToolboxLevel1 = function() { - var loadIds = []; - - if ($('#defaultCategories1').length) { - loadIds = $('#defaultCategories1').html(); - } - window.localStorage.toolboxids=loadIds; - window.localStorage.level=1; - - var xmlValue = ''; - var xmlids = loadIds.split(","); - for (var i = 0; i < xmlids.length; i++) { - if ($('#'+xmlids[i]).length) { - xmlValue += $('#'+xmlids[i])[0].outerHTML; - } - } - xmlValue += ''; - Blockly.getMainWorkspace().updateToolbox(xmlValue); - -}; - -BlocklyDuino.buildToolboxLevel2 = function() { - var loadIds = []; - - if ($('#defaultCategories2').length) { - loadIds = $('#defaultCategories2').html(); - } - window.localStorage.toolboxids=loadIds; - window.localStorage.level=2; - - - var xmlValue = ''; - var xmlids = loadIds.split(","); - for (var i = 0; i < xmlids.length; i++) { - if ($('#'+xmlids[i]).length) { - xmlValue += $('#'+xmlids[i])[0].outerHTML; - } - } - xmlValue += ''; - Blockly.getMainWorkspace().updateToolbox(xmlValue); - -}; - -BlocklyDuino.buildToolboxLevel3 = function() { - var loadIds = []; - - if ($('#defaultCategories3').length) { - loadIds = $('#defaultCategories3').html(); - } - - window.localStorage.toolboxids=loadIds; - window.localStorage.level=3; - - var xmlValue = ''; - var xmlids = loadIds.split(","); - for (var i = 0; i < xmlids.length; i++) { - if ($('#'+xmlids[i]).length) { - xmlValue += $('#'+xmlids[i])[0].outerHTML; - } - } - xmlValue += ''; - Blockly.getMainWorkspace().updateToolbox(xmlValue); - - -}; - - - -BlocklyDuino.loadToolboxDefinition = function(toolboxFile) { - $.ajax({ - type: "GET", - url: "./toolbox/" + toolboxFile + ".xml", - dataType: "xml", - async : false - }).done(function(data){ - var toolboxXml = ''; - $("#toolbox").remove(); - $('body').append(toolboxXml); - $("xml").find("category").each(function() { - if (!$(this).attr('id')) { - $(this).attr('id', $(this).attr('name')); - $(this).attr('name', Blockly.Msg[$(this).attr('name')]) - } - }) - }).fail(function(data) { - $("#toolbox").remove() - }) -}; -BlocklyDuino.changeToolboxDefinition = function() { - BlocklyDuino.loadToolboxDefinition($("#toolboxes").val()); - BlocklyDuino.openConfigToolbox(); -}; -BlocklyDuino.buildExamples = function() { - $.ajax({ - cache: false, - url: "./examples/examples.json", - dataType: "json", - success : function(data) { - $("#includedContent").empty(); - $.each(data, function(i, example){ - if (example.visible) { - var line = "" - + "" + example.source_text + "" - // + "" - // + "" - // + "" - + ""; - $("#includedContent").append(line); - } - }); - } - }); -}; -Blockly.Variables.flyoutCategory = function(workspace) { - var variableList = workspace.variableList; - variableList.sort(goog.string.caseInsensitiveCompare); - var xmlList = []; - var button = goog.dom.createDom('button'); - button.setAttribute('text', Blockly.Msg.NEW_VARIABLE); - button.setAttribute('callbackKey', 'CREATE_VARIABLE'); - Blockly.registerButtonCallback('CREATE_VARIABLE', function(button) { - Blockly.Variables.createVariable(button.getTargetWorkspace()); - }); - xmlList.push(button); - if (variableList.length > 0) { - if (window.localStorage.prog!="python") { - if (Blockly.Blocks['variables_set_init']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'variables_set_init'); - block.setAttribute('gap', 8); - var field = goog.dom.createDom('field', null, variableList[0]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - if (Blockly.Blocks['variables_set']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'variables_set'); - block.setAttribute('gap', 8); - var field = goog.dom.createDom('field', null, variableList[0]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - if (Blockly.Blocks['math_change']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'math_change'); - block.setAttribute('gap', 8); - var field = goog.dom.createDom('field', null, variableList[0]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - if (Blockly.Blocks['variables_const']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'variables_const'); - block.setAttribute('gap', 8); - var field = goog.dom.createDom('field', null, variableList[0]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - if (Blockly.Blocks['base_define_const']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'base_define_const'); - if (Blockly.Blocks['variables_get']) { - block.setAttribute('gap', 16); - } - var field = goog.dom.createDom('field', null, variableList[0]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - for (var i = 0; i < variableList.length; i++) { - if (Blockly.Blocks['variables_get']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'variables_get'); - if (Blockly.Blocks['variables_set']) { - block.setAttribute('gap', 8); - } - var field = goog.dom.createDom('field', null, variableList[i]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - } - } else { - if (Blockly.Blocks['variables_set']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'variables_set'); - block.setAttribute('gap', 8); - var field = goog.dom.createDom('field', null, variableList[0]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - if (Blockly.Blocks['math_change']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'math_change'); - block.setAttribute('gap', 8); - var field = goog.dom.createDom('field', null, variableList[0]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - for (var i = 0; i < variableList.length; i++) { - if (Blockly.Blocks['variables_get']) { - var block = goog.dom.createDom('block'); - block.setAttribute('type', 'variables_get'); - if (Blockly.Blocks['variables_set']) { - block.setAttribute('gap', 8); - } - var field = goog.dom.createDom('field', null, variableList[i]); - field.setAttribute('name', 'VAR'); - block.appendChild(field); - xmlList.push(block); - } - } - } - } - return xmlList; -}; -BlocklyDuino.workspace_capture = function() { - var ws = BlocklyDuino.workspace.svgBlockCanvas_.cloneNode(true); - ws.removeAttribute("width"); - ws.removeAttribute("height"); - ws.removeAttribute("transform"); - var styleElem = document.createElementNS("http://www.w3.org/2000/svg", "style"); - styleElem.textContent = Blockly.Css.CONTENT.join('') ; - ws.insertBefore(styleElem, ws.firstChild); - var bbox = BlocklyDuino.workspace.svgBlockCanvas_.getBBox(); - var canvas = document.createElement( "canvas" ); - canvas.width = Math.ceil(bbox.width+10); - canvas.height = Math.ceil(bbox.height+10); - var ctx = canvas.getContext( "2d" ); - var xml = new XMLSerializer().serializeToString(ws); - xml = ''+xml+''; - var img = new Image(); - img.setAttribute( "src", 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(xml)))); - img.onload = function() { - ctx.drawImage( img, 5, 5 ); - var canvasdata = canvas.toDataURL("image/png",1); - var datenow = Date.now(); - var a = document.createElement("a"); - a.download = "capture"+datenow+".png"; - a.href = canvasdata; - document.body.appendChild(a); - a.click(); - } -}; -BlocklyDuino.cardPicture_change = function() { -// if($("#pinout").val()=="nanooptiboot"||$("#pinout").val()=="nano"||$("#pinout").val()=="nona4809"){ -// $("#warning").show(); -// }else{ -// $("#warning").hide(); -// } - if ($("#pinout").val()) { - $('#arduino_card_mini_picture').attr("src", profile[$("#pinout").val()]['picture']) - } else { - $('#arduino_card_mini_picture').attr("src", "") - } - - if($("#pinout").val()=="nano") - document.getElementById('infoboard').innerHTML=MSG[$("#pinout").val()]; - else - document.getElementById('infoboard').innerHTML=""; - -}; -BlocklyDuino.saveino = function() { - var code = $('#pre_previewArduino').text(); - var datenow = Date.now(); - var projectname = document.getElementById("title-project-name").value; - var projectname = document.getElementById("title-project-name").value; - var filename = projectname+".ino"; - var element = document.createElement('a'); - element.setAttribute('href', 'data:text/ino;charset=utf-8,' + encodeURIComponent(code)); - element.setAttribute('download', filename); - element.style.display = 'none'; - document.body.appendChild(element); - element.click(); - document.body.removeChild(element); -}; -BlocklyDuino.savepy = function() { - var code = $('#pre_previewArduino').text(); - var datenow = Date.now(); - var projectname = document.getElementById("title-project-name").value; - var projectname = document.getElementById("title-project-name").value; - var filename = projectname+".py"; - var element = document.createElement('a'); - element.setAttribute('href', 'data:text/ino;charset=utf-8,' + encodeURIComponent(code)); - element.setAttribute('download', filename); - element.style.display = 'none'; - document.body.appendChild(element); - element.click(); - document.body.removeChild(element); -}; -BlocklyDuino.saveXmlFile = function () { - if (window.localStorage.content=="on") { - var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace); - var toolbox = window.localStorage.toolbox; - if (!toolbox) { - toolbox = $("#toolboxes").val(); - } - if (toolbox) { - var newel = document.createElement("toolbox"); - newel.appendChild(document.createTextNode(toolbox)); - xml.insertBefore(newel, xml.childNodes[0]); - } - var toolboxids = window.localStorage.toolboxids; - if (toolboxids === undefined || toolboxids === "") { - if ($('#defaultCategories1').length) { - toolboxids = $('#defaultCategories1').html(); - } - } - var data = Blockly.Xml.domToPrettyText(xml); - var datenow = Date.now(); - document.getElementById("title-project-name").value === '' ? document.getElementById("title-project-name").value = "Ottocode" : null; - var projectname = document.getElementById("title-project-name").value; - var filename = projectname+".bloc"; - var element = document.createElement('a'); - element.setAttribute('href', 'data:text/bloc;charset=utf-8,' + encodeURIComponent(data)); - element.setAttribute('download', filename); - element.style.display = 'none'; - document.body.appendChild(element); - element.click(); - document.body.removeChild(element) - } else if (window.localStorage.prog=="arduino"){ - var code = editor.getValue(); - var datenow = Date.now(); - document.getElementById("title-project-name").value === '' ? document.getElementById("title-project-name").value = "Ottocode" : null; - var projectname = document.getElementById("title-project-name").value; - var filename = projectname+".ino"; - var element = document.createElement('a'); - element.setAttribute('href', 'data:text/ino;charset=utf-8,' + encodeURIComponent(code)); - element.setAttribute('download', filename); - element.style.display = 'none'; - document.body.appendChild(element); - element.click(); - document.body.removeChild(element); - } else { - var code = editor.getValue(); - var datenow = Date.now(); - document.getElementById("title-project-name").value === '' ? document.getElementById("title-project-name").value = "Ottocode" : null; - var projectname = document.getElementById("title-project-name").value; - var filename = projectname+".py"; - var element = document.createElement('a'); - element.setAttribute('href', 'data:py/ino;charset=utf-8,' + encodeURIComponent(code)); - element.setAttribute('download', filename); - element.style.display = 'none'; - document.body.appendChild(element); - element.click(); - document.body.removeChild(element); - } -}; +'use strict'; + +var BlocklyDuino = {}; +BlocklyDuino.selectedToolbox = "toolbox_arduino_all"; +BlocklyDuino.selectedCard = "nano"; +BlocklyDuino.content = "on"; +BlocklyDuino.workspace = null; + +BlocklyDuino.init = function() { + Code.initLanguage(); + BlocklyDuino.loadConfig(); + BlocklyDuino.workspace = Blockly.inject('content_blocks',{grid:{snap:true},sounds:true,media:'media/',toolbox:BlocklyDuino.buildToolbox(),zoom:{controls:true,wheel:true}}); + BlocklyDuino.bindFunctions(); + BlocklyDuino.workspace.render(); + BlocklyDuino.workspace.addChangeListener(BlocklyDuino.renderArduinoCodePreview); + BlocklyDuino.loadFile(); + window.addEventListener('unload', BlocklyDuino.backupBlocks, false); +}; +BlocklyDuino.loadFile = function() { + var urlFile = BlocklyDuino.getStringParamFromUrl('url', ''); + if (urlFile.endsWith(".py")) { + $.get(urlFile, function(data) { + $('#codeORblock').bootstrapToggle("off"); + $('a[href="#content_code"]').tab('show'); + $('#btn_print').addClass("hidden"); + $('#btn_preview').addClass("hidden"); + $('#btn_verify').removeClass("hidden"); + $('#btn_search').removeClass("hidden"); + window.localStorage.content="off"; + editor.session.setMode("ace/mode/python"); + editor.setOptions({ + enableBasicAutocompletion: true, + enableSnippets: true, + enableLiveAutocompletion: true + }); + editor.setValue(data,1) + }, 'text') + } + if (urlFile.endsWith(".ino")) { + $.get(urlFile, function(data) { + $('#codeORblock').bootstrapToggle("off"); + $('a[href="#content_code"]').tab('show'); + $('#btn_print').addClass("hidden"); + $('#btn_preview').addClass("hidden"); + $('#btn_verify').removeClass("hidden"); + $('#btn_search').removeClass("hidden"); + window.localStorage.content="off"; + editor.session.setMode("ace/mode/c_cpp"); + editor.setOptions({ + enableBasicAutocompletion: true, + enableSnippets: true, + enableLiveAutocompletion: true + }); + editor.setValue(data,1) + }, 'text') + } + var loadOnce = null; + try {loadOnce = window.localStorage.loadOnceBlocks} catch (e) {} + if (urlFile) { + $.get( urlFile, function(data){BlocklyDuino.loadBlocks(data)}, 'text') + } else { + BlocklyDuino.loadBlocks() + } +}; +BlocklyDuino.save_com = function() { + $("#portserie").blur(); + var com=$("#portserie").val(); + window.localStorage.com = com; +}; +BlocklyDuino.renderArduinoCodePreview = function() { + var prog = window.localStorage.prog; + if (prog != "python") { + $('#pre_previewArduino').text(Blockly.Arduino.workspaceToCode(BlocklyDuino.workspace)); + $('#pre_previewArduino').html(prettyPrintOne($('#pre_previewArduino').html(), 'cpp')); + } else { + $('#pre_previewArduino').text(Blockly.Python.workspaceToCode(BlocklyDuino.workspace)); + $('#pre_previewArduino').html(prettyPrintOne($('#pre_previewArduino').html(), 'py')); + } +}; +BlocklyDuino.getStringParamFromUrl = function(name, defaultValue) { + var val = location.search.match(new RegExp('[?&]' + name + '=([^&]+)')); + return val ? decodeURIComponent(val[1].replace(/\+/g, '%20')) : defaultValue; +}; +BlocklyDuino.addReplaceParamToUrl = function(url, param, value) { + var re = new RegExp("([?&])" + param + "=.*?(&|$)", "i"); + var separator = url.indexOf('?') !== -1 ? "&" : "?"; + if (url.match(re)) { + return url.replace(re, '$1' + param + "=" + value + '$2'); + } + else { + return url + separator + param + "=" + value; + } +}; +BlocklyDuino.loadBlocks = function(defaultXml) { + if (defaultXml) { + var xml = Blockly.Xml.textToDom(defaultXml); + Blockly.Xml.domToWorkspace(xml,BlocklyDuino.workspace); + } else { + var loadOnce = null; + try { + loadOnce = window.localStorage.loadOnceBlocks; + } catch (e) {} + if (loadOnce != null) { + delete window.localStorage.loadOnceBlocks; + var xml = Blockly.Xml.textToDom(loadOnce); + Blockly.Xml.domToWorkspace(xml,BlocklyDuino.workspace); + } + } +}; +BlocklyDuino.load = function(event) { + var files = event.target.files; + if (files.length != 1) { + return; + } + var reader = new FileReader(); + reader.onloadend = function(event) { + var target = event.target; + document.getElementById('title-project-name').value = files[0].name.substring(0, files[0].name.indexOf('.')); + if (target.readyState == 2) { + if (files[0].name.endsWith("ino")) { + $('#codeORblock').bootstrapToggle("off"); + $('a[href="#content_code"]').tab('show'); + $('#btn_print').addClass("hidden"); + $('#btn_preview').addClass("hidden"); + $('#btn_verify').removeClass("hidden"); + $('#btn_search').removeClass("hidden"); + window.localStorage.content="off"; + editor.session.setMode("ace/mode/c_cpp"); + editor.setOptions({ + enableBasicAutocompletion: true, + enableSnippets: true, + enableLiveAutocompletion: true + }); + editor.setValue(target.result,1) + } + if (files[0].name.endsWith("py")) { + $('#codeORblock').bootstrapToggle("off"); + $('a[href="#content_code"]').tab('show'); + $('#btn_print').addClass("hidden"); + $('#btn_preview').addClass("hidden"); + $('#btn_verify').removeClass("hidden"); + $('#btn_search').removeClass("hidden"); + window.localStorage.content="off"; + editor.session.setMode("ace/mode/python"); + editor.setOptions({ + enableBasicAutocompletion: true, + enableSnippets: true, + enableLiveAutocompletion: true + }); + editor.setValue(target.result,1) + } + try { + var xml = Blockly.Xml.textToDom(target.result); + + } catch (e) { + alert(MSG['xmlError']+'\n' + e); + return + } + BlocklyDuino.workspace.clear(); + Blockly.Xml.domToWorkspace(xml,BlocklyDuino.workspace); + BlocklyDuino.workspace.render(); + } + }; + reader.readAsText(files[0]) +}; +BlocklyDuino.backupBlocks = function() { + if (typeof Blockly != 'undefined' && window.localStorage) { + var xml = Blockly.Xml.workspaceToDom(BlocklyDuino.workspace); + var text = Blockly.Xml.domToText(xml); + window.localStorage.loadOnceBlocks = text; + } +}; +BlocklyDuino.loadConfig = function() { + var card = window.localStorage.card; + var content=window.localStorage.content; + var prog = window.localStorage.prog; + if (card===undefined) { + window.localStorage.card = BlocklyDuino.selectedCard; + window.localStorage.prog = profile[BlocklyDuino.selectedCard].prog; + window.localStorage.toolbox = BlocklyDuino.selectedToolbox; + $("#boards").val(BlocklyDuino.selectedCard); + $('#arduino_card_mini_picture').attr("src", profile[BlocklyDuino.selectedCard]['picture']); + if(BlocklyDuino.selectedCard=="nanooptiboot"||BlocklyDuino.selectedCard=="nano"||BlocklyDuino.selectedCard=="nona4809"){ + $("#warning").show(); + }else{ + $("#warning").hide(); + } + $("#toolboxes").val(BlocklyDuino.selectedToolbox); + BlocklyDuino.loadToolboxDefinition(BlocklyDuino.selectedToolbox) + } else { + var toolbox = window.localStorage.toolbox; + BlocklyDuino.selectedToolbox = toolbox; + $("#boards").val(card); + $('#arduino_card_mini_picture').attr("src", profile[card]['picture']); + if(card=="nanooptiboot"||card=="nano"||card=="nona4809"){ + $("#warning").show(); + }else{ + $("#warning").hide(); + } + $("#toolboxes").val(toolbox); + BlocklyDuino.loadToolboxDefinition(toolbox) + } + if (content===undefined) { + window.localStorage.content = BlocklyDuino.content; + $('#codeORblock').bootstrapToggle(BlocklyDuino.content); + $('#btn_verify').addClass("hidden"); + $('#btn_search').addClass("hidden") + } else { + $('#codeORblock').bootstrapToggle(content); + if (content=="off") { + $('a[href="#content_code"]').tab('show'); + $('#btn_verify').removeClass("hidden"); + $('#btn_search').removeClass("hidden") + } + $('#btn_verify').addClass("hidden"); + $('#btn_search').addClass("hidden") + } + if (prog == "python") { + $('#btn_bin').addClass("hidden") + } +}; +BlocklyDuino.change_card = function() { + BlocklyDuino.backupBlocks(); + var card = window.localStorage.card; + var toolbox = window.localStorage.toolbox; + $("#boards").blur(); + var new_card = $("#boards").val(); + var new_prog = window.profile[new_card].prog; + if (window.profile[new_card].cpu != window.profile[card].cpu) { + if (window.confirm(MSG['arduino_card'] + window.profile[new_card].description + ' ?')){ + $('#arduino_card_mini_picture').attr("src", profile[new_card]['picture']); + if (new_prog != "python") { + $('#btn_preview').attr('title', MSG['btn_preview_ino']); + $('#btn_saveino').attr('title', MSG['btn_save_ino']); + $('#btn_bin').removeClass("hidden"); + + window.localStorage.prog = new_prog; + var new_toolbox = "toolbox_arduino_all"; //by default + var mystartfile; + mystartfile= ''; + mystartfile += ''; + mystartfile += ''; + + if (window.profile[new_card].cpu == "esp8266") + new_toolbox = "toolbox_arduino_all-esp8266" + else if (window.profile[new_card].cpu == "esp32") + new_toolbox = "toolbox_arduino_all-esp32" + else if (window.profile[new_card].cpu == "atmega328p-x") + new_toolbox = "toolbox_arduino_all-mrtx" + else if (window.profile[new_card].cpu == "mrtnode-esp32") + new_toolbox = "toolbox_arduino_all-mrtnode" + + + window.localStorage.toolbox = new_toolbox; + BlocklyDuino.workspace.clear(); + + // If CPU has changed and any of the 2 CPUs is an esp (32 or 8266) we need to load blocks for the new board + if ((window.profile[new_card].cpu != window.profile[card].cpu) /*&& ((window.profile[new_card].cpu.startsWith("esp")) || (window.profile[card].cpu.startsWith("esp")) ) */ ) + BlocklyDuino.loadBlocks(mystartfile); + else + BlocklyDuino.loadBlocks(); + + BlocklyDuino.loadToolboxDefinition(new_toolbox); + Blockly.getMainWorkspace().updateToolbox(BlocklyDuino.buildToolbox()); + if (window.localStorage.level==1) + $("#btn_level1").trigger("click"); + else + if ( window.localStorage.level==2) + $("#btn_level2").trigger("click"); + else + $("#btn_level3").trigger("click"); + + + BlocklyDuino.workspace.render() + } else { + $('#btn_preview').attr('title', MSG['btn_preview_py']); + $('#btn_saveino').attr('title', MSG['btn_save_py']); + $('#btn_bin').addClass("hidden"); + if ( window.profile[new_card].cpu == "cortexM0" ) { + var new_toolbox = "toolbox_microbit"; + } else { + var new_toolbox = "toolbox_lycee"; + } + window.localStorage.prog = new_prog; + window.localStorage.toolbox = new_toolbox; + BlocklyDuino.workspace.clear(); + BlocklyDuino.loadToolboxDefinition(new_toolbox); + Blockly.getMainWorkspace().updateToolbox(BlocklyDuino.buildToolbox()); + BlocklyDuino.workspace.render() + } + } else { + $("#boards").val(card); + return + } + } + window.localStorage.card = new_card +}; +BlocklyDuino.discard = function() { + var count = BlocklyDuino.workspace.getAllBlocks().length; + if (count < 4 || window.confirm(MSG['discard'])) { + BlocklyDuino.workspace.clear(); + BlocklyDuino.workspace.render(); + } +}; +BlocklyDuino.Undo = function() { + if (localStorage.getItem("content") == "on") { + Blockly.mainWorkspace.undo(0) + } else { + editor.undo() + } +}; +BlocklyDuino.Redo = function() { + if (localStorage.getItem("content") == "on") { + Blockly.mainWorkspace.undo(1) + } else { + editor.redo() + } +}; +BlocklyDuino.search = function() { + editor.execCommand("find") +}; +BlocklyDuino.bindFunctions = function() { + $('.modal-child').on('show.bs.modal', function () { + var modalParent = $(this).attr('data-modal-parent'); + $(modalParent).css('opacity', 0) + }); + $('.modal-child').on('hidden.bs.modal', function () { + var modalParent = $(this).attr('data-modal-parent'); + $(modalParent).css('opacity', 1) + }); + $('#btn_new').on("click", BlocklyDuino.discard); + $('#btn_undo').on("click", BlocklyDuino.Undo); + $('#btn_redo').on("click", BlocklyDuino.Redo); + $('#btn_print').on("click", BlocklyDuino.workspace_capture); + $('#btn_search').on("click", BlocklyDuino.search); + $('#boards').on("focus", function() { + BlocklyDuino.selectedCard = $(this).val() + }); + $('#btn_preview').on("click", function() { + $("#toggle").toggle("slide") + }); + $('#codeORblock').on("change", function() { + if (window.localStorage.prog!="python") { + editor.session.setMode("ace/mode/c_cpp"); + editor.setOptions({ + enableBasicAutocompletion: true, + enableSnippets: true, + enableLiveAutocompletion: true + }); + } else { + editor.session.setMode("ace/mode/python"); + editor.setOptions({ + enableBasicAutocompletion: true, + enableSnippets: true, + enableLiveAutocompletion: true + }); + } + if (window.localStorage.content=="on") { + editor.setValue($('#pre_previewArduino').text(),1); + $('a[href="#content_code"]').tab('show'); + $('#btn_print').addClass("hidden"); + $('#btn_preview').addClass("hidden"); + $('#btn_verify').removeClass("hidden"); + $('#btn_search').removeClass("hidden"); + window.localStorage.content="off" + } else { + $('a[href="#content_blocks"]').tab('show'); + $('#btn_print').removeClass("hidden"); + $('#btn_preview').removeClass("hidden"); + $('#btn_verify').addClass("hidden"); + $('#btn_search').addClass("hidden"); + window.localStorage.content="on" + } + }); +/* Keep code preview window visible when clicked to allow selection + $('#pre_previewArduino').on("click", function() { + $("#toggle").toggle("slide"); + }); */ + $('#btn_verify').mouseover(function() { + document.getElementById("survol").textContent = "Check code"; + }).mouseout(function() { + document.getElementById("survol").textContent = ""; + }); + $('#btn_flash').mouseover(function() { + document.getElementById("survol").textContent = "Upload code"; + }).mouseout(function() { + document.getElementById("survol").textContent = ""; + }); + $('#btn_bin').mouseover(function() { + document.getElementById("survol").textContent = "Export as Binary .hex"; + }).mouseout(function() { + document.getElementById("survol").textContent = ""; + }); + $('#toolboxes').on("focus", function() { + BlocklyDuino.selectedToolbox = $(this).val(); + }); + $('#toolboxes').on("change", BlocklyDuino.changeToolboxDefinition); + $('#configModal').on('hidden.bs.modal', function(e) { + BlocklyDuino.loadToolboxDefinition(BlocklyDuino.selectedToolbox); + }); + $('#load').on("change", BlocklyDuino.load); + $('#btn_fakeload').on("click", function() { + $('#load').click() + }); + $('#btn_config').on("click", BlocklyDuino.openConfigToolbox); + $('#btn_level1').on("click", BlocklyDuino.buildToolboxLevel1); + $('#btn_level2').on("click", BlocklyDuino.buildToolboxLevel2); + $('#btn_level3').on("click", BlocklyDuino.buildToolboxLevel3); + + $('#select_all').on("click", BlocklyDuino.checkAll); + $('#btn_valid_config').on("click", BlocklyDuino.changeToolbox); + $('#btn_example').on("click", BlocklyDuino.buildExamples); + if(typeof process === 'undefined') { + $('#btn_factory').on("click", function() { + window.open("factory.html","_blank", null); + }); + $('#btn_saveXML').on("click", BlocklyDuino.saveXmlFile); + $('#btn_saveino').on("click", function () { + if (window.localStorage.prog == "arduino") { + BlocklyDuino.saveino() + } else { + BlocklyDuino.savepy() + } + }); + } +}; +BlocklyDuino.checkAll = function() { + if(this.checked) { + $('#modal-body-config input:checkbox[id^=checkbox_]').each(function() { + this.checked = true; + }); + } + else { + $('#modal-body-config input:checkbox[id^=checkbox_]').each(function() { + this.checked = false; + }); + } +}; +BlocklyDuino.openConfigToolbox = function() { + var modalbody = $("#modal-body-config"); + var loadIds = window.localStorage.toolboxids; + if (loadIds === undefined || loadIds === "") { + if ($('#defaultCategories1').length) { + loadIds = $('#defaultCategories1').html(); + } else { + loadIds = ''; + } + } + modalbody.empty(); + var i=0, n; + var ligne = ""; + $("#toolbox").children("category").each(function() { + n = loadIds.search($(this).attr("id")); + if (n >= 0) { + ligne = ' ' +Blockly.Msg[$(this).attr("id")]+ '
'; + } else { + ligne = ' ' +Blockly.Msg[$(this).attr("id")]+ '
'; + } + i++; + modalbody.append(ligne); + }); +}; +BlocklyDuino.changeToolbox = function() { + BlocklyDuino.backupBlocks(); + var toolboxIds = []; + window.localStorage.lang = $('#languageMenu').val(); + $('#modal-body-config input:checkbox[id^=checkbox_]').each(function() { + if (this.checked == true) { + var xmlid = this.id; + toolboxIds.push(xmlid.replace("checkbox_", "")) + } + }); + window.localStorage.toolboxids = toolboxIds; + Blockly.getMainWorkspace().updateToolbox(BlocklyDuino.buildToolbox()); + BlocklyDuino.workspace.render(); + $('#configModal').modal('hide') + window.location.reload(); +}; + +BlocklyDuino.changelanguage = function() { + window.localStorage.lang = $('#languageMenu').val(); + window.location.reload(); +}; + + + + +BlocklyDuino.buildToolbox = function() { + var loadIds = window.localStorage.toolboxids; + if (loadIds === undefined || loadIds === "") { + if ($('#defaultCategories1').length) { + loadIds = $('#defaultCategories1').html(); + } else { + loadIds = ''; + } + } + var xmlValue = ''; + var xmlids = loadIds.split(","); + for (var i = 0; i < xmlids.length; i++) { + if ($('#'+xmlids[i]).length) { + xmlValue += $('#'+xmlids[i])[0].outerHTML; + } + } + xmlValue += ''; + return xmlValue; +}; + + +BlocklyDuino.buildToolboxLevel1 = function() { + var loadIds = []; + + if ($('#defaultCategories1').length) { + loadIds = $('#defaultCategories1').html(); + } + window.localStorage.toolboxids=loadIds; + window.localStorage.level=1; + + var xmlValue = ''; + var xmlids = loadIds.split(","); + for (var i = 0; i < xmlids.length; i++) { + if ($('#'+xmlids[i]).length) { + xmlValue += $('#'+xmlids[i])[0].outerHTML; + } + } + xmlValue += ''; + Blockly.getMainWorkspace().updateToolbox(xmlValue); + +}; + +BlocklyDuino.buildToolboxLevel2 = function() { + var loadIds = []; + + if ($('#defaultCategories2').length) { + loadIds = $('#defaultCategories2').html(); + } + window.localStorage.toolboxids=loadIds; + window.localStorage.level=2; + + + var xmlValue = ''; + var xmlids = loadIds.split(","); + for (var i = 0; i < xmlids.length; i++) { + if ($('#'+xmlids[i]).length) { + xmlValue += $('#'+xmlids[i])[0].outerHTML; + } + } + xmlValue += ''; + Blockly.getMainWorkspace().updateToolbox(xmlValue); + +}; + +BlocklyDuino.buildToolboxLevel3 = function() { + var loadIds = []; + + if ($('#defaultCategories3').length) { + loadIds = $('#defaultCategories3').html(); + } + + window.localStorage.toolboxids=loadIds; + window.localStorage.level=3; + + var xmlValue = ''; + var xmlids = loadIds.split(","); + for (var i = 0; i < xmlids.length; i++) { + if ($('#'+xmlids[i]).length) { + xmlValue += $('#'+xmlids[i])[0].outerHTML; + } + } + xmlValue += ''; + Blockly.getMainWorkspace().updateToolbox(xmlValue); + + +}; + + + +BlocklyDuino.loadToolboxDefinition = function(toolboxFile) { + $.ajax({ + type: "GET", + url: "./toolbox/" + toolboxFile + ".xml", + dataType: "xml", + async : false + }).done(function(data){ + var toolboxXml = ''; + $("#toolbox").remove(); + $('body').append(toolboxXml); + $("xml").find("category").each(function() { + if (!$(this).attr('id')) { + $(this).attr('id', $(this).attr('name')); + $(this).attr('name', Blockly.Msg[$(this).attr('name')]) + } + }) + }).fail(function(data) { + $("#toolbox").remove() + }) +}; +BlocklyDuino.changeToolboxDefinition = function() { + BlocklyDuino.loadToolboxDefinition($("#toolboxes").val()); + BlocklyDuino.openConfigToolbox(); +}; +BlocklyDuino.buildExamples = function() { + $.ajax({ + cache: false, + url: "./examples/examples.json", + dataType: "json", + success : function(data) { + $("#includedContent").empty(); + $.each(data, function(i, example){ + if (example.visible) { + var line = "" + + "" + example.source_text + "" + // + "" + // + "" + // + "" + + ""; + $("#includedContent").append(line); + } + }); + } + }); +}; +Blockly.Variables.flyoutCategory = function(workspace) { + var variableList = workspace.variableList; + variableList.sort(goog.string.caseInsensitiveCompare); + var xmlList = []; + var button = goog.dom.createDom('button'); + button.setAttribute('text', Blockly.Msg.NEW_VARIABLE); + button.setAttribute('callbackKey', 'CREATE_VARIABLE'); + Blockly.registerButtonCallback('CREATE_VARIABLE', function(button) { + Blockly.Variables.createVariable(button.getTargetWorkspace()); + }); + xmlList.push(button); + if (variableList.length > 0) { + if (window.localStorage.prog!="python") { + if (Blockly.Blocks['variables_set_init']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'variables_set_init'); + block.setAttribute('gap', 8); + var field = goog.dom.createDom('field', null, variableList[0]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + if (Blockly.Blocks['variables_set']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'variables_set'); + block.setAttribute('gap', 8); + var field = goog.dom.createDom('field', null, variableList[0]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + if (Blockly.Blocks['math_change']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'math_change'); + block.setAttribute('gap', 8); + var field = goog.dom.createDom('field', null, variableList[0]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + if (Blockly.Blocks['variables_const']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'variables_const'); + block.setAttribute('gap', 8); + var field = goog.dom.createDom('field', null, variableList[0]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + if (Blockly.Blocks['base_define_const']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'base_define_const'); + if (Blockly.Blocks['variables_get']) { + block.setAttribute('gap', 16); + } + var field = goog.dom.createDom('field', null, variableList[0]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + for (var i = 0; i < variableList.length; i++) { + if (Blockly.Blocks['variables_get']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'variables_get'); + if (Blockly.Blocks['variables_set']) { + block.setAttribute('gap', 8); + } + var field = goog.dom.createDom('field', null, variableList[i]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + } + } else { + if (Blockly.Blocks['variables_set']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'variables_set'); + block.setAttribute('gap', 8); + var field = goog.dom.createDom('field', null, variableList[0]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + if (Blockly.Blocks['math_change']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'math_change'); + block.setAttribute('gap', 8); + var field = goog.dom.createDom('field', null, variableList[0]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + for (var i = 0; i < variableList.length; i++) { + if (Blockly.Blocks['variables_get']) { + var block = goog.dom.createDom('block'); + block.setAttribute('type', 'variables_get'); + if (Blockly.Blocks['variables_set']) { + block.setAttribute('gap', 8); + } + var field = goog.dom.createDom('field', null, variableList[i]); + field.setAttribute('name', 'VAR'); + block.appendChild(field); + xmlList.push(block); + } + } + } + } + return xmlList; +}; +BlocklyDuino.workspace_capture = function() { + var ws = BlocklyDuino.workspace.svgBlockCanvas_.cloneNode(true); + ws.removeAttribute("width"); + ws.removeAttribute("height"); + ws.removeAttribute("transform"); + var styleElem = document.createElementNS("http://www.w3.org/2000/svg", "style"); + styleElem.textContent = Blockly.Css.CONTENT.join('') ; + ws.insertBefore(styleElem, ws.firstChild); + var bbox = BlocklyDuino.workspace.svgBlockCanvas_.getBBox(); + var canvas = document.createElement( "canvas" ); + canvas.width = Math.ceil(bbox.width+10); + canvas.height = Math.ceil(bbox.height+10); + var ctx = canvas.getContext( "2d" ); + var xml = new XMLSerializer().serializeToString(ws); + xml = ''+xml+''; + var img = new Image(); + img.setAttribute( "src", 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(xml)))); + img.onload = function() { + ctx.drawImage( img, 5, 5 ); + var canvasdata = canvas.toDataURL("image/png",1); + var datenow = Date.now(); + var a = document.createElement("a"); + a.download = "capture"+datenow+".png"; + a.href = canvasdata; + document.body.appendChild(a); + a.click(); + } +}; +BlocklyDuino.cardPicture_change = function() { +// if($("#pinout").val()=="nanooptiboot"||$("#pinout").val()=="nano"||$("#pinout").val()=="nona4809"){ +// $("#warning").show(); +// }else{ +// $("#warning").hide(); +// } + if ($("#pinout").val()) { + $('#arduino_card_mini_picture').attr("src", profile[$("#pinout").val()]['picture']) + } else { + $('#arduino_card_mini_picture').attr("src", "") + } + + if($("#pinout").val()=="nano") + document.getElementById('infoboard').innerHTML=MSG[$("#pinout").val()]; + else + document.getElementById('infoboard').innerHTML=""; + +}; +BlocklyDuino.saveino = function() { + var code = $('#pre_previewArduino').text(); + var datenow = Date.now(); + var projectname = document.getElementById("title-project-name").value; + var projectname = document.getElementById("title-project-name").value; + var filename = projectname+".ino"; + var element = document.createElement('a'); + element.setAttribute('href', 'data:text/ino;charset=utf-8,' + encodeURIComponent(code)); + element.setAttribute('download', filename); + element.style.display = 'none'; + document.body.appendChild(element); + element.click(); + document.body.removeChild(element); +}; +BlocklyDuino.savepy = function() { + var code = $('#pre_previewArduino').text(); + var datenow = Date.now(); + var projectname = document.getElementById("title-project-name").value; + var projectname = document.getElementById("title-project-name").value; + var filename = projectname+".py"; + var element = document.createElement('a'); + element.setAttribute('href', 'data:text/ino;charset=utf-8,' + encodeURIComponent(code)); + element.setAttribute('download', filename); + element.style.display = 'none'; + document.body.appendChild(element); + element.click(); + document.body.removeChild(element); +}; +BlocklyDuino.saveXmlFile = function () { + if (window.localStorage.content=="on") { + var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace); + var toolbox = window.localStorage.toolbox; + if (!toolbox) { + toolbox = $("#toolboxes").val(); + } + if (toolbox) { + var newel = document.createElement("toolbox"); + newel.appendChild(document.createTextNode(toolbox)); + xml.insertBefore(newel, xml.childNodes[0]); + } + var toolboxids = window.localStorage.toolboxids; + if (toolboxids === undefined || toolboxids === "") { + if ($('#defaultCategories1').length) { + toolboxids = $('#defaultCategories1').html(); + } + } + var data = Blockly.Xml.domToPrettyText(xml); + var datenow = Date.now(); + document.getElementById("title-project-name").value === '' ? document.getElementById("title-project-name").value = "Ottocode" : null; + var projectname = document.getElementById("title-project-name").value; + var filename = projectname+".bloc"; + var element = document.createElement('a'); + element.setAttribute('href', 'data:text/bloc;charset=utf-8,' + encodeURIComponent(data)); + element.setAttribute('download', filename); + element.style.display = 'none'; + document.body.appendChild(element); + element.click(); + document.body.removeChild(element) + } else if (window.localStorage.prog=="arduino"){ + var code = editor.getValue(); + var datenow = Date.now(); + document.getElementById("title-project-name").value === '' ? document.getElementById("title-project-name").value = "Ottocode" : null; + var projectname = document.getElementById("title-project-name").value; + var filename = projectname+".ino"; + var element = document.createElement('a'); + element.setAttribute('href', 'data:text/ino;charset=utf-8,' + encodeURIComponent(code)); + element.setAttribute('download', filename); + element.style.display = 'none'; + document.body.appendChild(element); + element.click(); + document.body.removeChild(element); + } else { + var code = editor.getValue(); + var datenow = Date.now(); + document.getElementById("title-project-name").value === '' ? document.getElementById("title-project-name").value = "Ottocode" : null; + var projectname = document.getElementById("title-project-name").value; + var filename = projectname+".py"; + var element = document.createElement('a'); + element.setAttribute('href', 'data:py/ino;charset=utf-8,' + encodeURIComponent(code)); + element.setAttribute('download', filename); + element.style.display = 'none'; + document.body.appendChild(element); + element.click(); + document.body.removeChild(element); + } +}; \ No newline at end of file