This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webterminal.js
191 lines (169 loc) · 4.8 KB
/
webterminal.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(function() {
var webterminals = {}
var sockets = {};
function initSocket(url) {
if(sockets[url])
return sockets[url];
var s = io.connect(url);
s.on("ptydata", function (data) { webterminals[data.id].data(data.data) });
s.on("ptyexit", function (data) { webterminals[data.id].exit() });
s.on("disconnect", function (data) {
for(var id in webterminals) {
if(webterminals[id].socket === s)
webterminals[id].exit();
}
});
return sockets[url] = s;
}
function extend(o) {
for(var i = 1; i < arguments.length; i++)
for(var key in arguments[i])
o[key] = arguments[i][key];
return o;
}
function WebTerminal(element, options) {
var self = this;
this.id = Math.random().toString().substr(2);
webterminals[this.id] = this;
options = options || {}
this.terminal = new terminal.Terminal(80, 24, options);
this.termDiff = new terminal.TermDiff(this.terminal)
this.box = document.createElement('pre');
element.appendChild(this.box);
element.className += ' webterminal';
this.box.className = this.attr2Class(this.terminal.attr);
this.socket = initSocket(options.url);
this.socket.emit("ptyinit", { id: this.id });
this.blur();
if(this.box.addEventListener)
this.box.addEventListener("click", function() {self.focus(); }, true)
else
this.box.attachEvent("onclick", function() {self.focus(); }, true)
this.oldKeypress = window.onkeypress;
this.oldKeydown = window.onkeydown;
this.oldClick = window.onclick;
this.blur();
}
WebTerminal.prototype = {
focus: function() {
var self = this;
for(var id in webterminals) {
webterminals[id].blur();
}
this.oldKeypress = window.onkeypress;
this.oldKeydown = window.onkeydown;
this.oldKeydown = window.onclick;
window.onkeypress = function(event) { return self.keypress(event) };
window.onkeydown = function(event) { return self.keydown(event) };
setTimeout(function() {
window.onclick = function() { self.blur(); }
},100);
this.box.className += " focus";
},
blur: function() {
window.onkeypress = this.oldKeypress;
window.onkeydown = this.oldKeydown;
window.onclick = this.oldClick;
var l = this.box.className.split(/ +/)
var n = []
for(var i = 0; i < l.length; i++)
(l[i] != "focus") && n.push(l[i])
this.box.className = n.join(' ');
},
sendInput: function(data) {
this.socket.emit("ptyinput", { id: this.id, data: data });
},
sendSignal: function(signal) {
this.socket.emit("ptysignal", { id: this.id, data: signal });
},
keypress: function(event) {
this.sendInput(this.terminal.eventToKey(event));
return false;
},
exit: function() {
this.box.className += " terminated";
},
keydown: function(event) {
switch(event.which) {
case 27:
case 38: // up
case 40: // down
case 39: // right
case 37: // left
case 8:
case 9:
this.sendInput(this.terminal.eventToKey(event));
return false;
}
return true;
},
data: function(data) {
this.terminal.write(data);
var diff = this.termDiff.diff();
this.render(diff);
},
render: function(diff) {
var children = this.box.childNodes;
for(var i in diff) {
var action = diff[i].act;
var line = diff[i].line;
var deleted = diff[i].rm;
var attr = diff[i].attr;
while(deleted--)
this.box.removeChild(children[i]);
var element = children[i];
if(action === '+') {
element = document.createElement('div');
if(children[i])
this.box.insertBefore(element, children[i]);
else
this.box.appendChild(element);
}
if(line) {
element.className = this.attr2Class(attr);
var frag = document.createDocumentFragment();
var j;
for(j = 0; j < line.length; j++) {
if(line[j]) {
var chr = document.createElement('span');
chr.appendChild(document.createTextNode(line[j].chr || ' '))
frag.appendChild(chr);
chr.className = this.attr2Class(line[j].attr) + (line[j].cursor ? " cursor_true" : "");
}
else
frag.appendChild(document.createTextNode(' '))
}
if(j == 0)
frag.appendChild(document.createTextNode(' '));
element.innerHTML = "";
element.appendChild(frag);
}
else if(element) {
element.appendChild(document.createTextNode(' '))
}
}
},
attr2Class: function(attr) {
if(!attr)
return '';
if(attr.str)
return attr.str;
var classes = [];
for(var k in attr) {
if(k != 'str')
classes.push(k+"_"+attr[k]);
}
return attr.str = classes.join(' ');
}
}
window.WebTerminal = WebTerminal;
if(window.jQuery && window.jQuery().jquery) {
window.jQuery.fn.webterminal = function(options) {
this.each(function() {
if(!$(this).data('webterminal'))
$(this).data('webterminal', new WebTerminal(this, options));
return $(this).data('webterminal')
});
}
}
})()