Files
codeocean/app/assets/javascripts/editor/turtle.js
Sebastian Serth c18aadaf61 Don't throw an exception without turtle canvas
Fixes CODEOCEAN-FRONTEND-59
2023-11-17 14:59:49 +01:00

54 lines
1.4 KiB
JavaScript

CodeOceanEditorTurtle = {
turtlecanvas: null,
turtlescreen: null,
resetTurtle: true,
initTurtle: function () {
if (this.resetTurtle) {
this.resetTurtle = false;
this.turtlecanvas = $('#turtlecanvas');
this.turtlescreen = new Turtle(this.websocket, this.turtlecanvas);
}
},
cleanUpTurtle: function() {
this.resetTurtle = true;
},
handleTurtleCommand: function (msg) {
this.initTurtle();
this.showCanvas();
if (msg.action in this.turtlescreen) {
var result = this.turtlescreen[msg.action].apply(this.turtlescreen, msg.args);
this.websocket.send(JSON.stringify({cmd: 'result', 'result': result}));
} else {
this.websocket.send(JSON.stringify({cmd: 'exception', exception: 'AttributeError', message: msg.action}));
}
this.websocket.flush();
},
handleTurtlebatchCommand: function (msg) {
this.initTurtle();
this.showCanvas();
for (var i = 0; i < msg.batch.length; i++) {
var cmd = msg.batch[i];
this.turtlescreen[cmd[0]]?.apply(this.turtlescreen, cmd[1]);
}
},
showCanvas: function () {
const turtlediv = $('#turtlediv');
if (turtlediv.isPresent() && turtlediv.hasClass('d-none')) {
turtlediv.removeClass('d-none');
}
},
hideCanvas: function () {
const turtlediv = $('#turtlediv');
if (turtlediv && turtlediv.isPresent() && !turtlediv.hasClass('d-none')) {
turtlediv.addClass('d-none');
}
}
};