blob: a0e47870c3ab2925e186ba9de102d071eeb2f9c5 [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">
4 <b-col class="d-flex flex-column justify-content-end">
5 <dl class="mb-2" sm="6" md="6">
6 <dt class="d-inline font-weight-bold mr-1">
7 {{ $t('pageSerialOverLan.status') }}:
8 </dt>
9 <dd class="d-inline">
10 <status-icon :status="hostStatusIcon" /> {{ connectionStatus }}
11 </dd>
12 </dl>
13 </b-col>
Sukanya Pandey615b3152020-08-19 19:13:49 +053014
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053015 <b-col v-if="!isFullWindow" class="d-flex justify-content-end">
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060016 <b-button variant="link" type="button" @click="openConsoleWindow()">
17 <icon-launch />
Sukanya Pandeyd9dc6fb2020-11-03 11:13:23 +053018 {{ $t('pageSerialOverLan.openNewTab') }}
19 </b-button>
20 </b-col>
21 </b-row>
Sukanya Pandey615b3152020-08-19 19:13:49 +053022 <div id="terminal" ref="panel"></div>
23 </div>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053024</template>
25
26<script>
27import { AttachAddon } from 'xterm-addon-attach';
28import { FitAddon } from 'xterm-addon-fit';
29import { Terminal } from 'xterm';
Sukanya Pandey615b3152020-08-19 19:13:49 +053030import IconLaunch from '@carbon/icons-vue/es/launch/20';
31import StatusIcon from '@/components/Global/StatusIcon';
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053032
33export default {
34 name: 'SerialOverLanConsole',
Sukanya Pandey615b3152020-08-19 19:13:49 +053035 components: {
36 IconLaunch,
Derick Montague602e98a2020-10-21 16:20:00 -050037 StatusIcon,
Sukanya Pandey615b3152020-08-19 19:13:49 +053038 },
39 props: {
40 isFullWindow: {
41 type: Boolean,
Derick Montague602e98a2020-10-21 16:20:00 -050042 default: true,
43 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053044 },
45 computed: {
46 hostStatus() {
47 return this.$store.getters['global/hostStatus'];
48 },
49 hostStatusIcon() {
50 return this.hostStatus === 'on' ? 'success' : 'danger';
51 },
52 connectionStatus() {
53 return this.hostStatus === 'on'
54 ? this.$t('pageSerialOverLan.connected')
55 : this.$t('pageSerialOverLan.disconnected');
Derick Montague602e98a2020-10-21 16:20:00 -050056 },
Sukanya Pandey615b3152020-08-19 19:13:49 +053057 },
58 created() {
59 this.$store.dispatch('global/getHostStatus');
60 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053061 mounted() {
62 this.openTerminal();
63 },
64 methods: {
65 openTerminal() {
66 const token = this.$store.getters['authentication/token'];
67
68 const ws = new WebSocket(`wss://${window.location.host}/console0`, [
Derick Montague602e98a2020-10-21 16:20:00 -050069 token,
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053070 ]);
71
72 // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons.
73
74 const term = new Terminal({
75 fontSize: 15,
76 fontFamily:
Derick Montague602e98a2020-10-21 16:20:00 -050077 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053078 });
79
80 const attachAddon = new AttachAddon(ws);
81 term.loadAddon(attachAddon);
82
83 const fitAddon = new FitAddon();
84 term.loadAddon(fitAddon);
85
86 const SOL_THEME = {
87 background: '#19273c',
88 cursor: 'rgba(83, 146, 255, .5)',
Derick Montague602e98a2020-10-21 16:20:00 -050089 scrollbar: 'rgba(83, 146, 255, .5)',
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053090 };
91 term.setOption('theme', SOL_THEME);
92
93 term.open(this.$refs.panel);
94 fitAddon.fit();
95
96 try {
Derick Montague602e98a2020-10-21 16:20:00 -050097 ws.onopen = function () {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +053098 console.log('websocket console0/ opened');
99 };
Derick Montague602e98a2020-10-21 16:20:00 -0500100 ws.onclose = function (event) {
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530101 console.log(
102 'websocket console0/ closed. code: ' +
103 event.code +
104 ' reason: ' +
105 event.reason
106 );
107 };
108 } catch (error) {
109 console.log(error);
110 }
Sukanya Pandey615b3152020-08-19 19:13:49 +0530111 },
112 openConsoleWindow() {
113 window.open(
114 '#/console/serial-over-lan-console',
115 '_blank',
116 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550'
117 );
Derick Montague602e98a2020-10-21 16:20:00 -0500118 },
119 },
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530120};
121</script>
122
Sukanya Pandey615b3152020-08-19 19:13:49 +0530123<style lang="scss" scoped>
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530124@import '~xterm/css/xterm.css';
125
Sukanya Pandey615b3152020-08-19 19:13:49 +0530126.terminal-container {
127 #terminal {
128 overflow: auto;
129 }
130}
131@include media-breakpoint-up(xs) {
132 .terminal-container {
133 width: 100%;
134 }
135}
136@include media-breakpoint-up(sm) {
137 .terminal-container {
138 width: 80%;
139 }
140}
141@include media-breakpoint-up(md) {
142 .terminal-container {
143 width: 65%;
144 }
145}
146@include media-breakpoint-up(lg) {
147 .terminal-container {
148 width: 80%;
149 }
150}
151@include media-breakpoint-up(xl) {
152 .terminal-container {
153 width: 60%;
154 }
155}
156
157.full-window-container {
158 width: 97%;
159 max-width: 960px;
160 margin: 1.5%;
161 #terminal {
162 overflow: auto;
163 }
Sukanya Pandey96f69ca2020-05-20 15:32:57 +0530164}
165</style>