-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathturtle.js
More file actions
55 lines (47 loc) · 1.44 KB
/
turtle.js
File metadata and controls
55 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Turtle from '../turtle';
export default {
turtlecanvas: null,
turtlescreen: null,
resetTurtle: true,
initTurtle: function () {
if (this.resetTurtle) {
this.resetTurtle = false;
this.turtlecanvas = $('#turtlecanvas');
this.turtlescreen = new Turtle(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');
}
}
};