automatically add else block, close if block mutator when user clicks outside of it

This commit is contained in:
Silver Kuusik 2018-01-15 00:15:07 +01:00
parent 2e053cc911
commit 44b008d8fe
1 changed files with 31 additions and 20 deletions

View File

@ -220,6 +220,7 @@ window.onload = function() {
/* change the if block to be more cheerful */
Blockly.Blocks.logic.HUE = '#44CC00';
/* inject blockly */
var blocklyArea = document.getElementById('blocklyArea');
var blocklyDiv = document.getElementById('blocklyDiv');
workspace = Blockly.inject(blocklyDiv, {
@ -279,6 +280,36 @@ window.onload = function() {
/* on blockly code change */
function onCodeChanged(event) {
/* the block */
var block = workspace.getBlockById(event.blockId);
/* if the if condition block was created */
if (event.type == Blockly.Events.CREATE && block.type == "controls_if") {
/* automatically add the else statement input */
block.elseCount_ = 1;
block.updateShape_();
/* disable the if condition block */
$("block[type=controls_if]").replaceWith("<block type='controls_if' disabled='true'></block>");
workspace.updateToolbox(document.getElementById("toolbox"));
/* set a click listener on the document */
$(document).click(function(e) {
var target = e.target;
/* when the user clicks anywhere outside the mutator and not on the mutator icon */
if (!$(target).is('.blocklyBubbleCanvas') && !$(target).parents().is('.blocklyBubbleCanvas')) {
if (!$(target).is('.blocklyIconGroup') && !$(target).parents().is('.blocklyIconGroup')) {
/* hide the mutator */
block.mutator.setVisible(false);
}
}
});
/* if the if condition block was removed */
} else if (event.type == Blockly.Events.DELETE && event.oldXml.getAttributeNode("type").nodeValue == "controls_if") {
/* enable the if condition block */
$("block[type=controls_if]").replaceWith("<block type='controls_if'></block>");
workspace.updateToolbox(document.getElementById("toolbox"));
}
/* only process change and move commands */
if (event.type != Blockly.Events.CHANGE && event.type != Blockly.Events.MOVE) return;
/* generate code from the used blocks */
@ -290,17 +321,6 @@ window.onload = function() {
/* save the code to the local storage */
var xml = Blockly.Xml.workspaceToDom(workspace);
localStorage.setItem("sumorobot.currentProgram", Blockly.Xml.domToText(xml));
/* if the if condition block is used */
if (sumocode.indexOf("if") != -1) {
/* disable the if condition block */
$("block[type=controls_if]").replaceWith("<block type='controls_if' disabled='true'></block>");
workspace.updateToolbox(document.getElementById("toolbox"));
} else {
/* enable the if condition block */
$("block[type=controls_if]").replaceWith("<block type='controls_if'></block>");
workspace.updateToolbox(document.getElementById("toolbox"));
}
}
/* add a change listener to Blockly */
@ -473,13 +493,4 @@ window.onload = function() {
/* load the Mixer stream */
$("#stream").html('<iframe width="100%" height="100%" allowfullscreen="true" src="https://mixer.com/embed/player/14551694"></iframe>');
/* try to close if block bubble canvas */
/*$(document).click(function(e) {
var target = e.target;
if (!$(target).is('.blocklyBubbleCanvas') && !$(target).parents().is('.blocklyBubbleCanvas')) {
if (!$(target).is('.blocklyIconGroup') && !$(target).parents().is('.blocklyIconGroup'))
$('.blocklyBubbleCanvas').empty();
}
});*/
}