Implement fully featured terminal demo w/ static fallback

This commit is contained in:
Paris
2016-06-07 08:41:41 +00:00
parent 180a7af75d
commit 53e8df40bb
7 changed files with 113 additions and 29 deletions
+47 -25
View File
@@ -1,38 +1,60 @@
var terminalContainer = document.getElementById('terminal-container'),
term = new Terminal(),
shellprompt = '$ ';
protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://',
socketURL = protocol + location.hostname + ((location.port) ? (':' + location.port) : '') + '/bash',
socket = new WebSocket(socketURL);
term.open(terminalContainer);
term.fit();
term.prompt = function () {
term.write('\r\n' + shellprompt);
};
function runRealTerminal() {
term.attach(socket);
term._initialized = true;
}
term.writeln('Welcome to xterm.js');
term.writeln('Just type some keys in the prompt below.');
term.writeln('');
term.prompt();
function runFakeTerminal() {
if (term._initialized) {
return;
}
term.on('key', function (key, ev) {
var printable = (
!ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
);
term._initialized = true;
if (ev.keyCode == 13) {
term.prompt();
} else if (ev.keyCode == 8) {
/*
var shellprompt = '$ ';
term.prompt = function () {
term.write('\r\n' + shellprompt);
};
term.writeln('Welcome to xterm.js');
term.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
term.writeln('Type some keys and commands to play around.');
term.writeln('');
term.prompt();
term.on('key', function (key, ev) {
var printable = (
!ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
);
if (ev.keyCode == 13) {
term.prompt();
} else if (ev.keyCode == 8) {
/*
* Do not delete the prompt
*/
if (term.x > 2) {
term.write('\b \b');
if (term.x > 2) {
term.write('\b \b');
}
} else if (printable) {
term.write(key);
}
} else if (printable) {
term.write(key);
}
});
});
term.on('paste', function (data, ev) {
term.write(data);
});
term.on('paste', function (data, ev) {
term.write(data);
});
}
socket.onopen = runRealTerminal;
socket.onclose = runFakeTerminal;
socket.onerror = runFakeTerminal;