fix closing control_if mutator

This commit is contained in:
Silver Kuusik 2018-01-15 01:25:07 +01:00
parent 44b008d8fe
commit 6a4982c939

View File

@ -17,10 +17,12 @@ var sumostart = false;
/* disable / enable Python code */ /* disable / enable Python code */
var pythonEnabled = false; var pythonEnabled = false;
/* control_if block id */
var controlBlockId = "";
/* last hightlighted block id */ /* last hightlighted block id */
var highlighted = ""; var lastHighlighted = "";
/* block highlight WebSocket */ /* block highlight WebSocket */
var block_highlight = null; var blockHighlight = null;
var Sumorobot = function(wsUri) { var Sumorobot = function(wsUri) {
/* assign the WebSocket URI */ /* assign the WebSocket URI */
@ -280,31 +282,21 @@ window.onload = function() {
/* on blockly code change */ /* on blockly code change */
function onCodeChanged(event) { function onCodeChanged(event) {
/* the block */
var block = workspace.getBlockById(event.blockId);
/* if the if condition block was created */ /* if the if condition block was created */
if (event.type == Blockly.Events.CREATE && block.type == "controls_if") { if (event.type == Blockly.Events.CREATE && event.xml.getAttributeNode("type").nodeValue == "controls_if") {
/* remember the control_if block id */
controlBlockId = event.blockId;
/* automatically add the else statement input */ /* automatically add the else statement input */
var block = workspace.getBlockById(event.blockId);
block.elseCount_ = 1; block.elseCount_ = 1;
block.updateShape_(); block.updateShape_();
/* disable the if condition block */ /* disable the if condition block */
$("block[type=controls_if]").replaceWith("<block type='controls_if' disabled='true'></block>"); $("block[type=controls_if]").replaceWith("<block type='controls_if' disabled='true'></block>");
workspace.updateToolbox(document.getElementById("toolbox")); 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 */ /* if the if condition block was removed */
} else if (event.type == Blockly.Events.DELETE && event.oldXml.getAttributeNode("type").nodeValue == "controls_if") { } else if (event.type == Blockly.Events.DELETE && event.oldXml.getAttributeNode("type").nodeValue == "controls_if") {
/* remove the control_if block id */
controlBlockId = "";
/* enable the if condition block */ /* enable the if condition block */
$("block[type=controls_if]").replaceWith("<block type='controls_if'></block>"); $("block[type=controls_if]").replaceWith("<block type='controls_if'></block>");
workspace.updateToolbox(document.getElementById("toolbox")); workspace.updateToolbox(document.getElementById("toolbox"));
@ -446,7 +438,7 @@ window.onload = function() {
$(".btn-stop").click(function() { $(".btn-stop").click(function() {
sumostart = false; sumostart = false;
sumorobot.send("stop"); sumorobot.send("stop");
workspace.highlightBlock(highlighted, false); workspace.highlightBlock(lastHighlighted, false);
}); });
/* remote control enable listener */ /* remote control enable listener */
@ -475,16 +467,22 @@ window.onload = function() {
$(this).addClass("btn-selected"); $(this).addClass("btn-selected");
/* update robot IDs in local storage */ /* update robot IDs in local storage */
setLocalStorageItem("sumorobot.robotID" + index, robotID); setLocalStorageItem("sumorobot.robotID" + index, robotID);
/* in case there is a open connection */
if (sumorobot && blockHighlight) {
/* close the connections */
sumorobot.close();
blockHighlight.close();
}
/* connect to the selected robots WebSocket */ /* connect to the selected robots WebSocket */
sumorobot = new Sumorobot("ws://" + robotServer + ":80/p2p/browser/" + robotID + "/"); sumorobot = new Sumorobot("ws://" + robotServer + ":80/p2p/browser/" + robotID + "/");
/* connect to the other block highlight WebSocket */ /* connect to the other block highlight WebSocket */
block_highlight = new WebSocket("ws://" + robotServer + ":80/p2p/browser/" + robotID + "-highlight/"); blockHighlight = new WebSocket("ws://" + robotServer + ":80/p2p/browser/" + robotID + "-highlight/");
/* when there is a message from the WebSocket */ /* when there is a message from the WebSocket */
block_highlight.onmessage = function(evt) { blockHighlight.onmessage = function(evt) {
/* when scope is received */ /* when scope is received */
if (evt.data.length == 20 && sumostart) { if (evt.data.length == 20 && sumostart) {
workspace.highlightBlock(evt.data); workspace.highlightBlock(evt.data);
highlighted = evt.data; lastHighlighted = evt.data;
} }
}; };
/* hide the configuration panel */ /* hide the configuration panel */
@ -493,4 +491,20 @@ window.onload = function() {
/* load the Mixer stream */ /* load the Mixer stream */
$("#stream").html('<iframe width="100%" height="100%" allowfullscreen="true" src="https://mixer.com/embed/player/14551694"></iframe>'); $("#stream").html('<iframe width="100%" height="100%" allowfullscreen="true" src="https://mixer.com/embed/player/14551694"></iframe>');
/* set a click listener on the document */
$(document).click(function(e) {
var target = e.target;
/* when control_if block is in use */
if (controlBlockId) {
/* 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 */
workspace.getBlockById(controlBlockId).mutator.setVisible(false);
}
}
}
});
} }