From 531123b10f8bedd21376caa9f456eeb815d70219 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Fri, 19 Jan 2018 12:35:17 +0100 Subject: [PATCH] fix ace editor change listener removal --- sumorobot.js | 71 +++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/sumorobot.js b/sumorobot.js index 89eab84..fd09b86 100755 --- a/sumorobot.js +++ b/sumorobot.js @@ -7,7 +7,9 @@ var remoteControl = false; var robotServer = "iot.koodur.com"; /* ace editor object */ -var pythonEditor = null; +var codingEditor = null; +/* ace editor change callback */ +var codingCallback = null; /* the sumorobot code */ var sumocode = ""; @@ -17,8 +19,8 @@ var sumorobot = null; var workspace = null; /* the sumorobot state */ var sumostart = false; -/* disable / enable Python code */ -var pythonEnabled = false; +/* disable / enable coding mode */ +var codingEnabled = false; /* control_if block id */ var controlBlockId = ""; @@ -105,23 +107,23 @@ function setLocalStorageItem(item, value) { window.onload = function() { $("#robot-id").val(getLocalStorageItem("sumorobot.robotId")); /* load ace editor */ - pythonEditor = ace.edit("blocklyCode"); + codingEditor = ace.edit("blocklyCode"); /* set the style */ - pythonEditor.setTheme("ace/theme/textmate"); - pythonEditor.session.setMode("ace/mode/python"); - pythonEditor.session.setTabSize(2); - pythonEditor.setReadOnly(true); + codingEditor.setTheme("ace/theme/textmate"); + codingEditor.session.setMode("ace/mode/python"); + codingEditor.session.setTabSize(2); + codingEditor.setReadOnly(true); /* disable scrolling warning */ - pythonEditor.$blockScrolling = Infinity; + codingEditor.$blockScrolling = Infinity; /* enable autocomplete */ ace.require("ace/ext/language_tools"); - pythonEditor.setOptions({ + codingEditor.setOptions({ enableSnippets: true, enableLiveAutocompletion: true, enableBasicAutocompletion: true }); /* add autocomplete keywords */ - pythonEditor.completers.push({ + codingEditor.completers.push({ getCompletions: function(editor, session, pos, prefix, callback) { callback(null, [ {value: "STOP", score: 1000, meta: "sumorobot"}, @@ -361,8 +363,8 @@ window.onload = function() { sumocode = Blockly.Python.workspaceToCode(workspace); /* show the code in the ace editor, filter out block IDs */ - pythonEditor.setValue("\n" + sumocode.replace(/[,]?[ ]?"(.*?)"/g, "")); - pythonEditor.clearSelection(); + codingEditor.setValue("\n" + sumocode.replace(/[,]?[ ]?"(.*?)"/g, "")); + codingEditor.clearSelection(); /* save the code to the local storage */ var xml = Blockly.Xml.workspaceToDom(workspace); @@ -420,28 +422,29 @@ window.onload = function() { case 80: // p $("#blocklyDiv").toggle(); $("#blocklyArea").toggle(); - /* resize the Python code editor */ - pythonEditor.resize(); + /* resize the code editor */ + codingEditor.resize(); /* disable / enable ace editor */ - pythonEditor.setReadOnly(pythonEnabled); - /* toggle python enabled */ - pythonEnabled = !pythonEnabled; - if (pythonEnabled) { - /* get the saved Python code from local storage or set empty */ - pythonEditor.setValue(getLocalStorageItem("sumorobot.python") || ""); - /* add an input listener for the code editor */ - pythonEditor.on("change", function() { - setLocalStorageItem("sumorobot.python", pythonEditor.getValue()) - }); - pythonEditor.clearSelection(); - pythonEditor.focus(); + codingEditor.setReadOnly(codingEnabled); + /* toggle coding enabled */ + codingEnabled = !codingEnabled; + if (codingEnabled) { + /* get the saved code from local storage or set empty */ + codingEditor.setValue(getLocalStorageItem("sumorobot.code") || ""); + /* add an change listener for the code editor */ + codingCallback = function() { + setLocalStorageItem("sumorobot.code", codingEditor.getValue()) + }; + codingEditor.on("change", codingCallback); + codingEditor.clearSelection(); + codingEditor.focus(); } else { - /* remove input listener from the code editor */ - pythonEditor.session.removeAllListeners("change"); - /* fire CHANGE event in Blockly workspace to change the Python code */ + /* remove change listener from the coding editor */ + codingEditor.off("change", codingCallback); + /* fire CHANGE event in Blockly workspace to change the code */ var event = {type: Blockly.Events.CHANGE}; workspace.fireChangeListener(event); - pythonEditor.blur(); + codingEditor.blur(); } break; case 82: // r @@ -477,10 +480,10 @@ window.onload = function() { /* start button listener */ $(".btn-start").click(function() { sumostart = true; - /* if we are in Python mode */ - if (pythonEnabled) { + /* if we are in coding mode */ + if (codingEnabled) { /* send the code from the textarea to the SumoRobot */ - sumorobot.send("start:" + pythonEditor.getValue()); + sumorobot.send("start:" + codingEditor.getValue()); /* otherwise when we are in Blockly mode */ } else { /* send the code from the blocks to the SumoRobot */