]> git.proxmox.com Git - mirror_xterm.js.git/blame - demo/main.js
Expose API on Terminal
[mirror_xterm.js.git] / demo / main.js
CommitLineData
e0c0fb69
DI
1var term,
2 protocol,
3 socketURL,
f57a40ee 4 socket,
0f8fc63a
PK
5 pid,
6 charWidth,
7 charHeight;
8
9var terminalContainer = document.getElementById('terminal-container'),
10 optionElements = {
5a932b2a 11 cursorBlink: document.querySelector('#option-cursor-blink'),
46d62f49 12 cursorStyle: document.querySelector('#option-cursor-style'),
f4293a6d
PK
13 scrollback: document.querySelector('#option-scrollback'),
14 tabstopwidth: document.querySelector('#option-tabstopwidth')
0f8fc63a
PK
15 },
16 colsElement = document.getElementById('cols'),
17 rowsElement = document.getElementById('rows');
18
19function setTerminalSize () {
6e1628bc
PK
20 var cols = parseInt(colsElement.value, 10),
21 rows = parseInt(rowsElement.value, 10),
0f8fc63a
PK
22 width = (cols * charWidth).toString() + 'px',
23 height = (rows * charHeight).toString() + 'px';
24
25 terminalContainer.style.width = width;
26 terminalContainer.style.height = height;
27 term.resize(cols, rows);
28}
e0c0fb69 29
0f8fc63a
PK
30colsElement.addEventListener('change', setTerminalSize);
31rowsElement.addEventListener('change', setTerminalSize);
e0c0fb69 32
7679475b
DI
33optionElements.cursorBlink.addEventListener('change', function () {
34 term.setOption('cursorBlink', optionElements.cursorBlink.checked);
35});
46d62f49
DI
36optionElements.cursorStyle.addEventListener('change', function () {
37 term.setOption('cursorStyle', optionElements.cursorStyle.value);
38});
5a932b2a 39optionElements.scrollback.addEventListener('change', function () {
f4293a6d
PK
40 term.setOption('scrollback', parseInt(optionElements.scrollback.value, 10));
41});
42optionElements.tabstopwidth.addEventListener('change', function () {
6e1628bc 43 term.setOption('tabStopWidth', parseInt(optionElements.tabstopwidth.value, 10));
5a932b2a 44});
e0c0fb69
DI
45
46createTerminal();
47
48function createTerminal() {
f57a40ee 49 // Clean terminal
e0c0fb69
DI
50 while (terminalContainer.children.length) {
51 terminalContainer.removeChild(terminalContainer.children[0]);
52 }
53 term = new Terminal({
5a932b2a 54 cursorBlink: optionElements.cursorBlink.checked,
f4293a6d 55 scrollback: parseInt(optionElements.scrollback.value, 10),
6e1628bc 56 tabStopWidth: parseInt(optionElements.tabstopwidth.value, 10)
e0c0fb69 57 });
f57a40ee
PK
58 term.on('resize', function (size) {
59 if (!pid) {
60 return;
61 }
0f8fc63a
PK
62 var cols = size.cols,
63 rows = size.rows,
64 url = '/terminals/' + pid + '/size?cols=' + cols + '&rows=' + rows;
65
66 fetch(url, {method: 'POST'});
f57a40ee 67 });
e0c0fb69 68 protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://';
f57a40ee 69 socketURL = protocol + location.hostname + ((location.port) ? (':' + location.port) : '') + '/terminals/';
e0c0fb69
DI
70
71 term.open(terminalContainer);
0f8fc63a 72 term.fit();
e0c0fb69 73
f57a40ee
PK
74 var initialGeometry = term.proposeGeometry(),
75 cols = initialGeometry.cols,
76 rows = initialGeometry.rows;
77
0f8fc63a
PK
78 colsElement.value = cols;
79 rowsElement.value = rows;
80
f57a40ee 81 fetch('/terminals?cols=' + cols + '&rows=' + rows, {method: 'POST'}).then(function (res) {
0f8fc63a
PK
82
83 charWidth = Math.ceil(term.element.offsetWidth / cols);
84 charHeight = Math.ceil(term.element.offsetHeight / rows);
85
f57a40ee
PK
86 res.text().then(function (pid) {
87 window.pid = pid;
88 socketURL += pid;
89 socket = new WebSocket(socketURL);
90 socket.onopen = runRealTerminal;
91 socket.onclose = runFakeTerminal;
92 socket.onerror = runFakeTerminal;
93 });
94 });
e0c0fb69 95}
dc3946f6 96
53e8df40
PK
97function runRealTerminal() {
98 term.attach(socket);
99 term._initialized = true;
100}
c5ae1cea 101
53e8df40
PK
102function runFakeTerminal() {
103 if (term._initialized) {
104 return;
105 }
106
107 term._initialized = true;
108
109 var shellprompt = '$ ';
110
111 term.prompt = function () {
112 term.write('\r\n' + shellprompt);
113 };
c5ae1cea 114
53e8df40
PK
115 term.writeln('Welcome to xterm.js');
116 term.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
117 term.writeln('Type some keys and commands to play around.');
118 term.writeln('');
119 term.prompt();
46bb446e 120
53e8df40
PK
121 term.on('key', function (key, ev) {
122 var printable = (
123 !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
124 );
125
126 if (ev.keyCode == 13) {
127 term.prompt();
128 } else if (ev.keyCode == 8) {
0f8fc63a 129 // Do not delete the prompt
53e8df40
PK
130 if (term.x > 2) {
131 term.write('\b \b');
132 }
133 } else if (printable) {
134 term.write(key);
567b39dc 135 }
53e8df40
PK
136 });
137
138 term.on('paste', function (data, ev) {
139 term.write(data);
140 });
141}