]> git.proxmox.com Git - mirror_xterm.js.git/blame - demo/main.js
Add AUTHORS and AUTHORS generator script
[mirror_xterm.js.git] / demo / main.js
CommitLineData
e0c0fb69
DI
1var term,
2 protocol,
3 socketURL,
4 socket;
5
6var terminalContainer = document.getElementById('terminal-container');
7var optionElements = {
8 cursorBlink: document.querySelector('#option-cursor-blink')
9};
10
11optionElements.cursorBlink.addEventListener('change', createTerminal);
12
13createTerminal();
14
15function createTerminal() {
16 while (terminalContainer.children.length) {
17 terminalContainer.removeChild(terminalContainer.children[0]);
18 }
19 term = new Terminal({
20 cursorBlink: optionElements.cursorBlink.checked
21 });
22 protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://';
23 socketURL = protocol + location.hostname + ((location.port) ? (':' + location.port) : '') + '/bash';
24 socket = new WebSocket(socketURL);
25
26 term.open(terminalContainer);
27 term.fit();
28
29 socket.onopen = runRealTerminal;
30 socket.onclose = runFakeTerminal;
31 socket.onerror = runFakeTerminal;
32}
dc3946f6 33
7a6fb27a 34
53e8df40
PK
35function runRealTerminal() {
36 term.attach(socket);
37 term._initialized = true;
38}
c5ae1cea 39
53e8df40
PK
40function runFakeTerminal() {
41 if (term._initialized) {
42 return;
43 }
44
45 term._initialized = true;
46
47 var shellprompt = '$ ';
48
49 term.prompt = function () {
50 term.write('\r\n' + shellprompt);
51 };
c5ae1cea 52
53e8df40
PK
53 term.writeln('Welcome to xterm.js');
54 term.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
55 term.writeln('Type some keys and commands to play around.');
56 term.writeln('');
57 term.prompt();
46bb446e 58
53e8df40
PK
59 term.on('key', function (key, ev) {
60 var printable = (
61 !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
62 );
63
64 if (ev.keyCode == 13) {
65 term.prompt();
66 } else if (ev.keyCode == 8) {
67 /*
567b39dc
PK
68 * Do not delete the prompt
69 */
53e8df40
PK
70 if (term.x > 2) {
71 term.write('\b \b');
72 }
73 } else if (printable) {
74 term.write(key);
567b39dc 75 }
53e8df40
PK
76 });
77
78 term.on('paste', function (data, ev) {
79 term.write(data);
80 });
81}