blob: b734bb130492504a9f004fc00396af3c23b75e5c [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'">
3 <template>
4 <b-row class="d-flex">
5 <b-col class="d-flex flex-column justify-content-end">
6 <dl class="mb-2" sm="6" md="6">
7 <dt class="d-inline font-weight-bold mr-1">
8 {{ $t('pageSerialOverLan.status') }}:
9 </dt>
10 <dd class="d-inline">
11 <status-icon :status="hostStatusIcon" /> {{ connectionStatus }}
12 </dd>
13 </dl>
14 </b-col>
15
16 <b-col v-if="!isFullWindow" class="d-flex justify-content-end">
17 <b-button
18 variant="link"
19 type="button"
20 class="pr-0 button-launch"
21 @click="openConsoleWindow()"
22 >
23 <icon-launch aria-hidden="true" />
24
25 {{ $t('pageSerialOverLan.openNewTab') }}
26 </b-button>
27 </b-col>
28 </b-row>
29 </template>
30 <div id="terminal" ref="panel"></div>
31 </div>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053032</template>
33
34<script>
35import { AttachAddon } from 'xterm-addon-attach';
36import { FitAddon } from 'xterm-addon-fit';
37import { Terminal } from 'xterm';
Sukanya Pandey615b3152020-08-19 19:13:49 +053038import IconLaunch from '@carbon/icons-vue/es/launch/20';
39import StatusIcon from '@/components/Global/StatusIcon';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053040
41export default {
42 name: 'SerialOverLanConsole',
Sukanya Pandey615b3152020-08-19 19:13:49 +053043 components: {
44 IconLaunch,
Derick Montague602e98a2020-10-21 16:20:00 -050045 StatusIcon,
Sukanya Pandey615b3152020-08-19 19:13:49 +053046 },
47 props: {
48 isFullWindow: {
49 type: Boolean,
Derick Montague602e98a2020-10-21 16:20:00 -050050 default: true,
51 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053052 },
53 computed: {
54 hostStatus() {
55 return this.$store.getters['global/hostStatus'];
56 },
57 hostStatusIcon() {
58 return this.hostStatus === 'on' ? 'success' : 'danger';
59 },
60 connectionStatus() {
61 return this.hostStatus === 'on'
62 ? this.$t('pageSerialOverLan.connected')
63 : this.$t('pageSerialOverLan.disconnected');
Derick Montague602e98a2020-10-21 16:20:00 -050064 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053065 },
66 created() {
67 this.$store.dispatch('global/getHostStatus');
68 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053069 mounted() {
70 this.openTerminal();
71 },
72 methods: {
73 openTerminal() {
74 const token = this.$store.getters['authentication/token'];
75
76 const ws = new WebSocket(`wss://${window.location.host}/console0`, [
Derick Montague602e98a2020-10-21 16:20:00 -050077 token,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053078 ]);
79
80 // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
81
82 const term = new Terminal({
83 fontSize: 15,
84 fontFamily:
Derick Montague602e98a2020-10-21 16:20:00 -050085 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053086 });
87
88 const attachAddon = new AttachAddon(ws);
89 term.loadAddon(attachAddon);
90
91 const fitAddon = new FitAddon();
92 term.loadAddon(fitAddon);
93
94 const SOL_THEME = {
95 background: '#19273c',
96 cursor: 'rgba(83, 146, 255, .5)',
Derick Montague602e98a2020-10-21 16:20:00 -050097 scrollbar: 'rgba(83, 146, 255, .5)',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053098 };
99 term.setOption('theme', SOL_THEME);
100
101 term.open(this.$refs.panel);
102 fitAddon.fit();
103
104 try {
Derick Montague602e98a2020-10-21 16:20:00 -0500105 ws.onopen = function () {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530106 console.log('websocket console0/ opened');
107 };
Derick Montague602e98a2020-10-21 16:20:00 -0500108 ws.onclose = function (event) {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530109 console.log(
110 'websocket console0/ closed. code: ' +
111 event.code +
112 ' reason: ' +
113 event.reason
114 );
115 };
116 } catch (error) {
117 console.log(error);
118 }
Sukanya Pandey615b3152020-08-19 19:13:49 +0530119 },
120 openConsoleWindow() {
121 window.open(
122 '#/console/serial-over-lan-console',
123 '_blank',
124 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550'
125 );
Derick Montague602e98a2020-10-21 16:20:00 -0500126 },
127 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530128};
129</script>
130
Sukanya Pandey615b3152020-08-19 19:13:49 +0530131<style lang="scss" scoped>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530132@import '~xterm/css/xterm.css';
133
Sukanya Pandey615b3152020-08-19 19:13:49 +0530134.terminal-container {
135 #terminal {
136 overflow: auto;
137 }
138}
139@include media-breakpoint-up(xs) {
140 .terminal-container {
141 width: 100%;
142 }
143}
144@include media-breakpoint-up(sm) {
145 .terminal-container {
146 width: 80%;
147 }
148}
149@include media-breakpoint-up(md) {
150 .terminal-container {
151 width: 65%;
152 }
153}
154@include media-breakpoint-up(lg) {
155 .terminal-container {
156 width: 80%;
157 }
158}
159@include media-breakpoint-up(xl) {
160 .terminal-container {
161 width: 60%;
162 }
163}
164
165.full-window-container {
166 width: 97%;
167 max-width: 960px;
168 margin: 1.5%;
169 #terminal {
170 overflow: auto;
171 }
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530172}
173</style>