blob: 694083fd0e06275c8d9b1e47705b1b0ba3845fe5 [file] [log] [blame]
Sukanya Pandey96f69ca2020-05-20 15:32:57 +05301<template>
Sukanya Pandey615b3152020-08-19 19:13:49 +05302 <div :class="isFullWindow ? 'full-window-container' : 'terminal-container'">
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +05303 <b-row class="d-flex">
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +05304 <b-col sm="4" md="6">
5 <alert
6 v-if="serverStatus === 'on' ? false : true"
7 variant="warning"
8 :small="true"
9 class="mt-4"
10 >
11 <p class="col-form-label">
12 {{ $t('pageSerialOverLan.alert.disconnectedAlertMessage') }}
13 </p>
14 </alert>
15 </b-col>
16 </b-row>
17 <b-row class="d-flex">
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053018 <b-col class="d-flex flex-column justify-content-end">
19 <dl class="mb-2" sm="6" md="6">
20 <dt class="d-inline font-weight-bold mr-1">
21 {{ $t('pageSerialOverLan.status') }}:
22 </dt>
23 <dd class="d-inline">
Derick Montague71114fe2021-05-06 18:17:34 -050024 <status-icon :status="serverStatusIcon" /> {{ connectionStatus }}
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053025 </dd>
26 </dl>
27 </b-col>
Sukanya Pandey615b3152020-08-19 19:13:49 +053028
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053029 <b-col v-if="!isFullWindow" class="d-flex justify-content-end">
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060030 <b-button variant="link" type="button" @click="openConsoleWindow()">
31 <icon-launch />
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053032 {{ $t('pageSerialOverLan.openNewTab') }}
33 </b-button>
34 </b-col>
35 </b-row>
Sukanya Pandey615b3152020-08-19 19:13:49 +053036 <div id="terminal" ref="panel"></div>
37 </div>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053038</template>
39
40<script>
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +053041import Alert from '@/components/Global/Alert';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053042import { AttachAddon } from 'xterm-addon-attach';
43import { FitAddon } from 'xterm-addon-fit';
44import { Terminal } from 'xterm';
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030045import { throttle } from 'lodash';
Sukanya Pandey615b3152020-08-19 19:13:49 +053046import IconLaunch from '@carbon/icons-vue/es/launch/20';
47import StatusIcon from '@/components/Global/StatusIcon';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053048
49export default {
50 name: 'SerialOverLanConsole',
Sukanya Pandey615b3152020-08-19 19:13:49 +053051 components: {
Nikhil Ashoka1d9257e2022-01-31 14:06:37 +053052 Alert,
Sukanya Pandey615b3152020-08-19 19:13:49 +053053 IconLaunch,
Derick Montague602e98a2020-10-21 16:20:00 -050054 StatusIcon,
Sukanya Pandey615b3152020-08-19 19:13:49 +053055 },
56 props: {
57 isFullWindow: {
58 type: Boolean,
Derick Montague602e98a2020-10-21 16:20:00 -050059 default: true,
60 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053061 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030062 data() {
63 return {
64 resizeConsoleWindow: null,
65 };
66 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053067 computed: {
Derick Montague71114fe2021-05-06 18:17:34 -050068 serverStatus() {
69 return this.$store.getters['global/serverStatus'];
Sukanya Pandey615b3152020-08-19 19:13:49 +053070 },
Derick Montague71114fe2021-05-06 18:17:34 -050071 serverStatusIcon() {
72 return this.serverStatus === 'on' ? 'success' : 'danger';
Sukanya Pandey615b3152020-08-19 19:13:49 +053073 },
74 connectionStatus() {
Derick Montague71114fe2021-05-06 18:17:34 -050075 return this.serverStatus === 'on'
Sukanya Pandey615b3152020-08-19 19:13:49 +053076 ? this.$t('pageSerialOverLan.connected')
77 : this.$t('pageSerialOverLan.disconnected');
Derick Montague602e98a2020-10-21 16:20:00 -050078 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053079 },
80 created() {
Derick Montague71114fe2021-05-06 18:17:34 -050081 this.$store.dispatch('global/getServerStatus');
Sukanya Pandey615b3152020-08-19 19:13:49 +053082 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053083 mounted() {
84 this.openTerminal();
85 },
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030086 beforeDestroy() {
87 window.removeEventListener('resize', this.resizeConsoleWindow);
Xiaochao Ma78372d62022-03-08 20:13:00 +080088 this.closeTerminal();
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +030089 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053090 methods: {
91 openTerminal() {
92 const token = this.$store.getters['authentication/token'];
93
Xiaochao Ma78372d62022-03-08 20:13:00 +080094 this.ws = new WebSocket(`wss://${window.location.host}/console0`, [
Derick Montague602e98a2020-10-21 16:20:00 -050095 token,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053096 ]);
97
98 // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
99
Xiaochao Ma78372d62022-03-08 20:13:00 +0800100 this.term = new Terminal({
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530101 fontSize: 15,
102 fontFamily:
Derick Montague602e98a2020-10-21 16:20:00 -0500103 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530104 });
105
Xiaochao Ma78372d62022-03-08 20:13:00 +0800106 const attachAddon = new AttachAddon(this.ws);
107 this.term.loadAddon(attachAddon);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530108
109 const fitAddon = new FitAddon();
Xiaochao Ma78372d62022-03-08 20:13:00 +0800110 this.term.loadAddon(fitAddon);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530111
112 const SOL_THEME = {
113 background: '#19273c',
114 cursor: 'rgba(83, 146, 255, .5)',
Derick Montague602e98a2020-10-21 16:20:00 -0500115 scrollbar: 'rgba(83, 146, 255, .5)',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530116 };
Xiaochao Ma78372d62022-03-08 20:13:00 +0800117 this.term.setOption('theme', SOL_THEME);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530118
Xiaochao Ma78372d62022-03-08 20:13:00 +0800119 this.term.open(this.$refs.panel);
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530120 fitAddon.fit();
121
Konstantin Aladyshev64f260c2021-02-17 17:23:22 +0300122 this.resizeConsoleWindow = throttle(() => {
123 fitAddon.fit();
124 }, 1000);
125 window.addEventListener('resize', this.resizeConsoleWindow);
126
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530127 try {
Xiaochao Ma78372d62022-03-08 20:13:00 +0800128 this.ws.onopen = function () {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530129 console.log('websocket console0/ opened');
130 };
Xiaochao Ma78372d62022-03-08 20:13:00 +0800131 this.ws.onclose = function (event) {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530132 console.log(
133 'websocket console0/ closed. code: ' +
134 event.code +
135 ' reason: ' +
136 event.reason
137 );
138 };
139 } catch (error) {
140 console.log(error);
141 }
Sukanya Pandey615b3152020-08-19 19:13:49 +0530142 },
Xiaochao Ma78372d62022-03-08 20:13:00 +0800143 closeTerminal() {
144 console.log('closeTerminal');
145 this.term.dispose();
146 this.term = null;
147 this.ws.close();
148 this.ws = null;
149 },
Sukanya Pandey615b3152020-08-19 19:13:49 +0530150 openConsoleWindow() {
151 window.open(
152 '#/console/serial-over-lan-console',
153 '_blank',
154 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550'
155 );
Derick Montague602e98a2020-10-21 16:20:00 -0500156 },
157 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530158};
159</script>
160
Sukanya Pandey615b3152020-08-19 19:13:49 +0530161<style lang="scss" scoped>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530162@import '~xterm/css/xterm.css';
163
Sukanya Pandeyb0fadef2021-03-12 14:58:31 +0530164#terminal {
165 overflow: auto;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530166}
167
168.full-window-container {
169 width: 97%;
Sukanya Pandey615b3152020-08-19 19:13:49 +0530170 margin: 1.5%;
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530171}
172</style>