blob: 0a821db145d852d5eeeeef8d8ae10663efe96e1c [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
Kuiying Wangf4a43cc2019-08-26 15:56:10 +080038/*
39TextEncoder/TextDecoder does not support IE.
40Use text-encoding instead in IE
41More detail at https://caniuse.com/#feat=textencoder
42*/
43import {TextDecoder} from 'text-encoding';
44if (!window['TextDecoder']) {
45 window['TextDecoder'] = TextDecoder;
46}
47
beccabroek75697f92018-09-04 09:34:44 -050048window.angular && (function(angular) {
49 'use strict';
50
51 angular.module('app.common.directives').directive('serialConsole', [
52 function() {
53 return {
54 'restrict': 'E',
55 'template': require('./serial-console.html'),
beccabroeka788cf02018-09-04 09:38:13 -050056 'scope': {'path': '=', 'showTabBtn': '=?'},
beccabroek75697f92018-09-04 09:34:44 -050057 'controller': [
Yoshie Muranaka198ce1f2019-09-20 10:33:04 -070058 '$scope', '$window', 'dataService', '$element',
59 function($scope, $window, dataService, $element) {
beccabroek75697f92018-09-04 09:34:44 -050060 $scope.dataService = dataService;
61
beccabroek457ca042018-09-07 17:18:24 -050062 // See https://github.com/xtermjs/xterm.js/ for available xterm
63 // options
beccabroek75697f92018-09-04 09:34:44 -050064
beccabroek457ca042018-09-07 17:18:24 -050065 Terminal.applyAddon(attach); // Apply the `attach` addon
66 Terminal.applyAddon(fit); // Apply the `fit` addon
beccabroek75697f92018-09-04 09:34:44 -050067
Yang Cheng511a2bb2019-03-06 22:20:35 +080068 var border = 10;
beccabroek457ca042018-09-07 17:18:24 -050069 var term = new Terminal();
Yoshie Muranaka198ce1f2019-09-20 10:33:04 -070070 // Should be a reference to <div id="terminal"></div>
71 var terminal = $element[0].firstElementChild.firstElementChild;
Yang Cheng511a2bb2019-03-06 22:20:35 +080072 var customConsole;
73 var charSize;
74 var termContainer;
75
76 term.open(terminal);
77 customConsole = configJSON.customConsoleDisplaySize;
78
79 if (customConsole != null) {
80 charSize = measureChar(term);
81 termContainer = document.getElementById('term-container');
82 if (termContainer != null) {
83 if (customConsole.width) {
84 termContainer.style.width =
85 (charSize.width * customConsole.width + border) + 'px';
86 }
87 if (customConsole.height) {
88 terminal.style.height =
89 (charSize.height * customConsole.height + border) + 'px';
90 }
91 }
92 }
beccabroek457ca042018-09-07 17:18:24 -050093 term.fit();
Yang Cheng6d1d6002019-02-27 11:16:24 +080094 if (configJSON.customKeyEnable == true) {
95 term.attachCustomKeyEventHandler(customKeyHandlers);
96 }
beccabroek457ca042018-09-07 17:18:24 -050097 var SOL_THEME = {
98 background: '#19273c',
99 cursor: 'rgba(83, 146, 255, .5)',
100 scrollbar: 'rgba(83, 146, 255, .5)'
101 };
102 term.setOption('theme', SOL_THEME);
beccabroek75697f92018-09-04 09:34:44 -0500103 var hostname = dataService.getHost().replace('https://', '');
104 var host = 'wss://' + hostname + '/console0';
Yoshie Muranakafa8697b2019-08-26 11:09:35 -0700105 try {
106 var ws = new WebSocket(host);
107 term.attach(ws);
108 ws.onopen = function() {
109 console.log('websocket opened');
110 };
111 ws.onclose = function(event) {
112 console.log(
113 'websocket closed. code: ' + event.code +
114 ' reason: ' + event.reason);
115 };
116 } catch (error) {
117 console.log(JSON.stringify(error));
118 }
beccabroeka788cf02018-09-04 09:38:13 -0500119 $scope.openTerminalWindow = function() {
120 $window.open(
121 '#/server-control/remote-console-window',
122 'Remote Console Window',
123 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550');
124 };
beccabroek75697f92018-09-04 09:34:44 -0500125 }
126 ]
127 };
128 }
129 ]);
130})(window.angular);