From 6a4982c93966a1fccad4abee6e1b7d6b0d913509 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Mon, 15 Jan 2018 01:25:07 +0100 Subject: [PATCH] fix closing control_if mutator --- sumorobot.js | 58 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/sumorobot.js b/sumorobot.js index 90e1112..1039d6b 100755 --- a/sumorobot.js +++ b/sumorobot.js @@ -17,10 +17,12 @@ var sumostart = false; /* disable / enable Python code */ var pythonEnabled = false; +/* control_if block id */ +var controlBlockId = ""; /* last hightlighted block id */ -var highlighted = ""; +var lastHighlighted = ""; /* block highlight WebSocket */ -var block_highlight = null; +var blockHighlight = null; var Sumorobot = function(wsUri) { /* assign the WebSocket URI */ @@ -280,31 +282,21 @@ 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") { + 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 */ + var block = workspace.getBlockById(event.blockId); block.elseCount_ = 1; block.updateShape_(); /* disable the if condition block */ $("block[type=controls_if]").replaceWith(""); 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") { + /* remove the control_if block id */ + controlBlockId = ""; /* enable the if condition block */ $("block[type=controls_if]").replaceWith(""); workspace.updateToolbox(document.getElementById("toolbox")); @@ -446,7 +438,7 @@ window.onload = function() { $(".btn-stop").click(function() { sumostart = false; sumorobot.send("stop"); - workspace.highlightBlock(highlighted, false); + workspace.highlightBlock(lastHighlighted, false); }); /* remote control enable listener */ @@ -475,16 +467,22 @@ window.onload = function() { $(this).addClass("btn-selected"); /* update robot IDs in local storage */ 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 */ sumorobot = new Sumorobot("ws://" + robotServer + ":80/p2p/browser/" + robotID + "/"); /* 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 */ - block_highlight.onmessage = function(evt) { + blockHighlight.onmessage = function(evt) { /* when scope is received */ if (evt.data.length == 20 && sumostart) { workspace.highlightBlock(evt.data); - highlighted = evt.data; + lastHighlighted = evt.data; } }; /* hide the configuration panel */ @@ -493,4 +491,20 @@ window.onload = function() { /* load the Mixer stream */ $("#stream").html(''); + + /* 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); + } + } + } + }); }