blob: 59ffe782c9a054b22ed2842a78619f22dc2eda8d [file] [log] [blame]
beccabroek457ca042018-09-07 17:18:24 -05001import {Terminal} from 'xterm';
2import style from 'xterm/dist/xterm.css';
3import * as attach from 'xterm/lib/addons/attach/attach';
4import * as fit from 'xterm/lib/addons/fit/fit';
Yang Cheng6d1d6002019-02-27 11:16:24 +08005var configJSON = require('../../../config.json');
6if (configJSON.keyType == 'VT100+') {
7 var vt100PlusKey = require('./vt100plus');
8}
beccabroek457ca042018-09-07 17:18:24 -05009
Yang Cheng6d1d6002019-02-27 11:16:24 +080010var customKeyHandlers = function(ev) {
11 if (configJSON.keyType == 'VT100+') {
12 return vt100PlusKey.customVT100PlusKey(ev, this);
13 }
14 return true;
15};
beccabroek75697f92018-09-04 09:34:44 -050016
Yang Cheng511a2bb2019-03-06 22:20:35 +080017function measureChar(term) {
18 var span = document.createElement('span');
19 var fontFamily = 'courier-new';
20 var fontSize = 15;
21 var rect;
22
23 span.textContent = 'W';
24 try {
25 fontFamily = term.getOption('fontFamily');
26 fontSize = term.getOption('fontSize');
27 } catch (err) {
28 console.log('get option failure');
29 }
30 span.style.fontFamily = fontFamily;
31 span.style.fontSize = fontSize + 'px';
32 document.body.appendChild(span);
33 rect = span.getBoundingClientRect();
34 document.body.removeChild(span);
35 return rect;
36}
37
Ed Tanousd8713592020-07-16 07:33:07 -070038// Add a TextEncoder polyfill to handle ie9 properly. (does anyone still use
39// that anymore? Grabbed from
40// https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder#Polyfill
41if (typeof TextEncoder === 'undefined') {
42 TextEncoder = function TextEncoder() {};
43 TextEncoder.prototype.encode = function encode(str) {
44 'use strict';
45 var Len = str.length, resPos = -1;
46 // The Uint8Array's length must be at least 3x the length of the string
47 // because an invalid UTF-16
48 // takes up the equivelent space of 3 UTF-8 characters to encode it
49 // properly. However, Array's have an auto expanding length and 1.5x should
50 // be just the right balance for most uses.
51 var resArr = typeof Uint8Array === 'undefined' ? new Array(Len * 1.5) :
52 new Uint8Array(Len * 3);
53 for (var point = 0, nextcode = 0, i = 0; i !== Len;) {
54 point = str.charCodeAt(i), i += 1;
55 if (point >= 0xD800 && point <= 0xDBFF) {
56 if (i === Len) {
57 resArr[resPos += 1] = 0xef /*0b11101111*/;
58 resArr[resPos += 1] = 0xbf /*0b10111111*/;
59 resArr[resPos += 1] = 0xbd /*0b10111101*/;
60 break;
61 }
62 // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
63 nextcode = str.charCodeAt(i);
64 if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) {
65 point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000;
66 i += 1;
67 if (point > 0xffff) {
68 resArr[resPos += 1] = (0x1e /*0b11110*/ << 3) | (point >>> 18);
69 resArr[resPos += 1] =
70 (0x2 /*0b10*/ << 6) | ((point >>> 12) & 0x3f /*0b00111111*/);
71 resArr[resPos += 1] =
72 (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f /*0b00111111*/);
73 resArr[resPos += 1] =
74 (0x2 /*0b10*/ << 6) | (point & 0x3f /*0b00111111*/);
75 continue;
76 }
77 } else {
78 resArr[resPos += 1] = 0xef /*0b11101111*/;
79 resArr[resPos += 1] = 0xbf /*0b10111111*/;
80 resArr[resPos += 1] = 0xbd /*0b10111101*/;
81 continue;
82 }
83 }
84 if (point <= 0x007f) {
85 resArr[resPos += 1] = (0x0 /*0b0*/ << 7) | point;
86 } else if (point <= 0x07ff) {
87 resArr[resPos += 1] = (0x6 /*0b110*/ << 5) | (point >>> 6);
88 resArr[resPos += 1] =
89 (0x2 /*0b10*/ << 6) | (point & 0x3f /*0b00111111*/);
90 } else {
91 resArr[resPos += 1] = (0xe /*0b1110*/ << 4) | (point >>> 12);
92 resArr[resPos += 1] =
93 (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f /*0b00111111*/);
94 resArr[resPos += 1] =
95 (0x2 /*0b10*/ << 6) | (point & 0x3f /*0b00111111*/);
96 }
97 }
98 if (typeof Uint8Array !== 'undefined')
99 return resArr.subarray(0, resPos + 1);
100 // else // IE 6-9
101 resArr.length = resPos + 1; // trim off extra weight
102 return resArr;
103 };
104 TextEncoder.prototype.toString = function() {
105 return '[object TextEncoder]'
106 };
107 try { // Object.defineProperty only works on DOM prototypes in IE8
108 Object.defineProperty(TextEncoder.prototype, 'encoding', {
109 get: function() {
110 if (TextEncoder.prototype.isPrototypeOf(this))
111 return 'utf-8';
112 else
113 throw TypeError('Illegal invocation');
114 }
115 });
116 } catch (e) { /*IE6-8 fallback*/
117 TextEncoder.prototype.encoding = 'utf-8';
118 }
119 if (typeof Symbol !== 'undefined')
120 TextEncoder.prototype[Symbol.toStringTag] = 'TextEncoder';
Kuiying Wangf4a43cc2019-08-26 15:56:10 +0800121}
122
beccabroek75697f92018-09-04 09:34:44 -0500123window.angular && (function(angular) {
124 'use strict';
125
126 angular.module('app.common.directives').directive('serialConsole', [
127 function() {
128 return {
129 'restrict': 'E',
130 'template': require('./serial-console.html'),
beccabroeka788cf02018-09-04 09:38:13 -0500131 'scope': {'path': '=', 'showTabBtn': '=?'},
beccabroek75697f92018-09-04 09:34:44 -0500132 'controller': [
James Feist6a8d1802020-04-08 14:04:19 -0700133 '$scope', '$cookies', '$window', 'dataService', '$element',
134 function($scope, $cookies, $window, dataService, $element) {
beccabroek75697f92018-09-04 09:34:44 -0500135 $scope.dataService = dataService;
136
beccabroek457ca042018-09-07 17:18:24 -0500137 // See https://github.com/xtermjs/xterm.js/ for available xterm
138 // options
beccabroek75697f92018-09-04 09:34:44 -0500139
beccabroek457ca042018-09-07 17:18:24 -0500140 Terminal.applyAddon(attach); // Apply the `attach` addon
141 Terminal.applyAddon(fit); // Apply the `fit` addon
beccabroek75697f92018-09-04 09:34:44 -0500142
Yang Cheng511a2bb2019-03-06 22:20:35 +0800143 var border = 10;
beccabroek457ca042018-09-07 17:18:24 -0500144 var term = new Terminal();
Yoshie Muranaka198ce1f2019-09-20 10:33:04 -0700145 // Should be a reference to <div id="terminal"></div>
146 var terminal = $element[0].firstElementChild.firstElementChild;
Yang Cheng511a2bb2019-03-06 22:20:35 +0800147 var customConsole;
148 var charSize;
149 var termContainer;
150
151 term.open(terminal);
152 customConsole = configJSON.customConsoleDisplaySize;
153
154 if (customConsole != null) {
155 charSize = measureChar(term);
156 termContainer = document.getElementById('term-container');
157 if (termContainer != null) {
158 if (customConsole.width) {
159 termContainer.style.width =
160 (charSize.width * customConsole.width + border) + 'px';
161 }
162 if (customConsole.height) {
163 terminal.style.height =
164 (charSize.height * customConsole.height + border) + 'px';
165 }
166 }
167 }
beccabroek457ca042018-09-07 17:18:24 -0500168 term.fit();
Yang Cheng6d1d6002019-02-27 11:16:24 +0800169 if (configJSON.customKeyEnable == true) {
170 term.attachCustomKeyEventHandler(customKeyHandlers);
171 }
beccabroek457ca042018-09-07 17:18:24 -0500172 var SOL_THEME = {
173 background: '#19273c',
174 cursor: 'rgba(83, 146, 255, .5)',
175 scrollbar: 'rgba(83, 146, 255, .5)'
176 };
177 term.setOption('theme', SOL_THEME);
beccabroek75697f92018-09-04 09:34:44 -0500178 var hostname = dataService.getHost().replace('https://', '');
179 var host = 'wss://' + hostname + '/console0';
James Feist6a8d1802020-04-08 14:04:19 -0700180 var token = $cookies.get('XSRF-TOKEN');
Yoshie Muranakafa8697b2019-08-26 11:09:35 -0700181 try {
James Feist6a8d1802020-04-08 14:04:19 -0700182 var ws = new WebSocket(host, [token]);
Yoshie Muranakafa8697b2019-08-26 11:09:35 -0700183 term.attach(ws);
184 ws.onopen = function() {
185 console.log('websocket opened');
186 };
187 ws.onclose = function(event) {
188 console.log(
189 'websocket closed. code: ' + event.code +
190 ' reason: ' + event.reason);
191 };
192 } catch (error) {
193 console.log(JSON.stringify(error));
194 }
beccabroeka788cf02018-09-04 09:38:13 -0500195 $scope.openTerminalWindow = function() {
196 $window.open(
197 '#/server-control/remote-console-window',
198 'Remote Console Window',
199 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550');
200 };
beccabroek75697f92018-09-04 09:34:44 -0500201 }
202 ]
203 };
204 }
205 ]);
206})(window.angular);