]> git.proxmox.com Git - mirror_xterm.js.git/blob - dist/addons/terminado/terminado.js
Merge remote-tracking branch 'ups/master' into 207_selection_manager
[mirror_xterm.js.git] / dist / addons / terminado / terminado.js
1 /**
2 * This module provides methods for attaching a terminal to a terminado WebSocket stream.
3 *
4 * @module xterm/addons/terminado/terminado
5 * @license MIT
6 */
7
8 (function (attach) {
9 if (typeof exports === 'object' && typeof module === 'object') {
10 /*
11 * CommonJS environment
12 */
13 module.exports = attach(require('../../xterm'));
14 } else if (typeof define == 'function') {
15 /*
16 * Require.js is available
17 */
18 define(['../../xterm'], attach);
19 } else {
20 /*
21 * Plain browser environment
22 */
23 attach(window.Terminal);
24 }
25 })(function (Xterm) {
26 'use strict';
27
28 var exports = {};
29
30 /**
31 * Attaches the given terminal to the given socket.
32 *
33 * @param {Xterm} term - The terminal to be attached to the given socket.
34 * @param {WebSocket} socket - The socket to attach the current terminal.
35 * @param {boolean} bidirectional - Whether the terminal should send data
36 * to the socket as well.
37 * @param {boolean} buffered - Whether the rendering of incoming data
38 * should happen instantly or at a maximum
39 * frequency of 1 rendering per 10ms.
40 */
41 exports.terminadoAttach = function (term, socket, bidirectional, buffered) {
42 bidirectional = (typeof bidirectional == 'undefined') ? true : bidirectional;
43 term.socket = socket;
44
45 term._flushBuffer = function () {
46 term.write(term._attachSocketBuffer);
47 term._attachSocketBuffer = null;
48 clearTimeout(term._attachSocketBufferTimer);
49 term._attachSocketBufferTimer = null;
50 };
51
52 term._pushToBuffer = function (data) {
53 if (term._attachSocketBuffer) {
54 term._attachSocketBuffer += data;
55 } else {
56 term._attachSocketBuffer = data;
57 setTimeout(term._flushBuffer, 10);
58 }
59 };
60
61 term._getMessage = function (ev) {
62 var data = JSON.parse(ev.data)
63 if( data[0] == "stdout" ) {
64 if (buffered) {
65 term._pushToBuffer(data[1]);
66 } else {
67 term.write(data[1]);
68 }
69 }
70 };
71
72 term._sendData = function (data) {
73 socket.send(JSON.stringify(['stdin', data]));
74 };
75
76 term._setSize = function (size) {
77 socket.send(JSON.stringify(['set_size', size.rows, size.cols]));
78 };
79
80 socket.addEventListener('message', term._getMessage);
81
82 if (bidirectional) {
83 term.on('data', term._sendData);
84 }
85 term.on('resize', term._setSize);
86
87 socket.addEventListener('close', term.terminadoDetach.bind(term, socket));
88 socket.addEventListener('error', term.terminadoDetach.bind(term, socket));
89 };
90
91 /**
92 * Detaches the given terminal from the given socket
93 *
94 * @param {Xterm} term - The terminal to be detached from the given socket.
95 * @param {WebSocket} socket - The socket from which to detach the current
96 * terminal.
97 */
98 exports.terminadoDetach = function (term, socket) {
99 term.off('data', term._sendData);
100
101 socket = (typeof socket == 'undefined') ? term.socket : socket;
102
103 if (socket) {
104 socket.removeEventListener('message', term._getMessage);
105 }
106
107 delete term.socket;
108 };
109
110 /**
111 * Attaches the current terminal to the given socket
112 *
113 * @param {WebSocket} socket - The socket to attach the current terminal.
114 * @param {boolean} bidirectional - Whether the terminal should send data
115 * to the socket as well.
116 * @param {boolean} buffered - Whether the rendering of incoming data
117 * should happen instantly or at a maximum
118 * frequency of 1 rendering per 10ms.
119 */
120 Xterm.prototype.terminadoAttach = function (socket, bidirectional, buffered) {
121 return exports.terminadoAttach(this, socket, bidirectional, buffered);
122 };
123
124 /**
125 * Detaches the current terminal from the given socket.
126 *
127 * @param {WebSocket} socket - The socket from which to detach the current
128 * terminal.
129 */
130 Xterm.prototype.terminadoDetach = function (socket) {
131 return exports.terminadoDetach(this, socket);
132 };
133
134 return exports;
135 });