forked from dreamland-mud/mudjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
87 lines (77 loc) · 2.93 KB
/
input.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var keydown = function(e) {};
$(document).ready(function() {
var input_history = localStorage.history ? JSON.parse(localStorage.history) : [],
position = input_history.length,
current_cmd = $('#input input').val();
function scrollPage(dir) {
var wrap = $('#terminal-wrap');
wrap.scrollTop(wrap.scrollTop() + wrap.height()*dir);
}
$('#input input').keydown(function(e) {
e.stopPropagation();
if(!e.shiftKey && !e.ctrlKey && !e.altKey) {
switch(e.which) {
case 33: // page up
e.preventDefault();
scrollPage(-0.8);
return;
case 34: // page down
e.preventDefault();
scrollPage(0.8);
return;
case 38: // up
e.preventDefault();
if(position > 0) {
if(position == input_history.length)
current_cmd = $('#input input').val();
for(var i=position-1;i >= 0;i--) {
if(input_history[i].indexOf(current_cmd) >= 0) {
var v = input_history[(position=i)];
$('#input input').val(v);
return;
}
}
}
return;
case 40: // down
e.preventDefault();
if(position < input_history.length) {
var i;
for(i=position+1;i < input_history.length;i++) {
if(input_history[i].indexOf(current_cmd) >= 0) {
var v = input_history[(position=i)];
$('#input input').val(v);
return;
}
}
position=i;
$('#input input').val(current_cmd);
}
return;
}
}
keydown(e);
});
$('#input').on('submit', function(e) {
e.preventDefault();
var input = $('#input input'),
t = input.val();
input.val('');
if(t) {
position = input_history.length;
input_history[position++] = t;
var drop = input_history.length - 1000; // store only 1000 most recent entries;
if(drop < 0)
drop = 0;
localStorage.history = JSON.stringify(input_history.slice(drop));
}
var lines = t.split('\n');
$(lines).each(function() {
$('#terminal').trigger('output', ['' + this + '\r\n']);
$('.trigger').trigger('input', ['' + this]);
});
});
$('#triggers').on('input', function(e, text) {
send(text);
});
});