fix ace editor change listener removal

This commit is contained in:
Silver Kuusik 2018-01-19 12:35:17 +01:00
parent 5da853a3bd
commit 531123b10f

View File

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