-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshell.js
51 lines (51 loc) · 1.81 KB
/
shell.js
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
(function (window, document, undefined) {
function handleEventByEval(event) {
event.stopPropagation();
event.preventDefault();
try {eval(this.input.value);}
catch (err) {console.log(err);}
return false;
}
function handleEventBySplit(event) {
event.stopPropagation();
event.preventDefault();
var raw = this.input.value;
var has_repeat = raw.match(/(\d+)x:/);
var count = 1;
if (has_repeat) {
count = has_repeat[1];
raw = raw.replace(/\d+x: */, '');
}
var cmds = raw.split(/ *\. */);
for (var j = 0; j < count; j++) {
for (var i = 0; i < cmds.length; i++) {
var cmd = cmds[i];
var args = cmd.split(/ +/);
var method = args.shift().toLowerCase();
if (!method)
continue;
try {this.scope[method].apply(this.scope, args);}
catch (err) {
console.log("cannot call "+method+"() with ", args, "\n", err);
}
}
}
return false;
}
function TurtleShell(inputElement, scope){
var self = Turtle.extend(this, {input: inputElement});
if (scope)
Turtle.extend(self, {scope: scope, handleEvent: handleEventBySplit});
else
Turtle.extend(self, {handleEvent: handleEventByEval});
inputElement.form.addEventListener("submit", function (e) {
e.stopPropagation();
e.preventDefault();
return false;
});
inputElement.addEventListener("change", function (e) {return self.handleEvent(e);});
return self;
}
window.Turtle.Shell = TurtleShell;
window.Turtle.ShellLite = TurtleShell;
})(this, this.document);