]>
git.proxmox.com Git - mirror_xterm.js.git/blob - demo/main.js
9 var terminalContainer
= document
.getElementById('terminal-container'),
11 findNext
: document
.querySelector('#find-next'),
12 findPrevious
: document
.querySelector('#find-previous')
15 cursorBlink
: document
.querySelector('#option-cursor-blink'),
16 cursorStyle
: document
.querySelector('#option-cursor-style'),
17 scrollback
: document
.querySelector('#option-scrollback'),
18 tabstopwidth
: document
.querySelector('#option-tabstopwidth')
20 colsElement
= document
.getElementById('cols'),
21 rowsElement
= document
.getElementById('rows');
23 function setTerminalSize () {
24 var cols
= parseInt(colsElement
.value
, 10),
25 rows
= parseInt(rowsElement
.value
, 10),
26 width
= (cols
* charWidth
).toString() + 'px',
27 height
= (rows
* charHeight
).toString() + 'px';
29 terminalContainer
.style
.width
= width
;
30 terminalContainer
.style
.height
= height
;
31 term
.resize(cols
, rows
);
34 colsElement
.addEventListener('change', setTerminalSize
);
35 rowsElement
.addEventListener('change', setTerminalSize
);
37 actionElements
.findNext
.addEventListener('keypress', function (e
) {
38 if (e
.key
=== "Enter") {
40 term
.findNext(actionElements
.findNext
.value
);
43 actionElements
.findPrevious
.addEventListener('keypress', function (e
) {
44 if (e
.key
=== "Enter") {
46 term
.findPrevious(actionElements
.findPrevious
.value
);
50 optionElements
.cursorBlink
.addEventListener('change', function () {
51 term
.setOption('cursorBlink', optionElements
.cursorBlink
.checked
);
53 optionElements
.cursorStyle
.addEventListener('change', function () {
54 term
.setOption('cursorStyle', optionElements
.cursorStyle
.value
);
56 optionElements
.scrollback
.addEventListener('change', function () {
57 term
.setOption('scrollback', parseInt(optionElements
.scrollback
.value
, 10));
59 optionElements
.tabstopwidth
.addEventListener('change', function () {
60 term
.setOption('tabStopWidth', parseInt(optionElements
.tabstopwidth
.value
, 10));
65 function createTerminal() {
67 while (terminalContainer
.children
.length
) {
68 terminalContainer
.removeChild(terminalContainer
.children
[0]);
71 cursorBlink
: optionElements
.cursorBlink
.checked
,
72 scrollback
: parseInt(optionElements
.scrollback
.value
, 10),
73 tabStopWidth
: parseInt(optionElements
.tabstopwidth
.value
, 10)
75 term
.on('resize', function (size
) {
81 url
= '/terminals/' + pid
+ '/size?cols=' + cols
+ '&rows=' + rows
;
83 fetch(url
, {method
: 'POST'});
85 protocol
= (location
.protocol
=== 'https:') ? 'wss://' : 'ws://';
86 socketURL
= protocol
+ location
.hostname
+ ((location
.port
) ? (':' + location
.port
) : '') + '/terminals/';
88 term
.open(terminalContainer
);
91 var initialGeometry
= term
.proposeGeometry(),
92 cols
= initialGeometry
.cols
,
93 rows
= initialGeometry
.rows
;
95 colsElement
.value
= cols
;
96 rowsElement
.value
= rows
;
98 fetch('/terminals?cols=' + cols
+ '&rows=' + rows
, {method
: 'POST'}).then(function (res
) {
100 charWidth
= Math
.ceil(term
.element
.offsetWidth
/ cols
);
101 charHeight
= Math
.ceil(term
.element
.offsetHeight
/ rows
);
103 res
.text().then(function (pid
) {
106 socket
= new WebSocket(socketURL
);
107 socket
.onopen
= runRealTerminal
;
108 socket
.onclose
= runFakeTerminal
;
109 socket
.onerror
= runFakeTerminal
;
114 function runRealTerminal() {
116 term
._initialized
= true;
119 function runFakeTerminal() {
120 if (term
._initialized
) {
124 term
._initialized
= true;
126 var shellprompt
= '$ ';
128 term
.prompt = function () {
129 term
.write('\r\n' + shellprompt
);
132 term
.writeln('Welcome to xterm.js');
133 term
.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
134 term
.writeln('Type some keys and commands to play around.');
138 term
.on('key', function (key
, ev
) {
140 !ev
.altKey
&& !ev
.altGraphKey
&& !ev
.ctrlKey
&& !ev
.metaKey
143 if (ev
.keyCode
== 13) {
145 } else if (ev
.keyCode
== 8) {
146 // Do not delete the prompt
150 } else if (printable
) {
155 term
.on('paste', function (data
, ev
) {