blob: d7ec31e01442cae671c4d2a514db2bfb41aa86a9 [file] [log] [blame]
Mateusz Gapski632de222020-07-09 09:21:33 +02001<template>
Mateusz Gapski96b37af2020-09-04 14:11:55 +02002 <div :class="marginClass">
3 <div ref="toolbar" class="kvm-toolbar">
4 <b-row class="d-flex">
5 <b-col class="d-flex flex-column justify-content-end" cols="4">
6 <dl class="mb-2" sm="2" md="2">
7 <dt class="d-inline font-weight-bold mr-1">
8 {{ $t('pageKvm.status') }}:
9 </dt>
10 <dd class="d-inline">
Derick Montague71114fe2021-05-06 18:17:34 -050011 <status-icon :status="serverStatusIcon" />
12 <span class="d-none d-md-inline"> {{ serverStatus }}</span>
Mateusz Gapski96b37af2020-09-04 14:11:55 +020013 </dd>
14 </dl>
15 </b-col>
16
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060017 <b-col class="d-flex justify-content-end pr-1">
Mateusz Gapski96b37af2020-09-04 14:11:55 +020018 <b-button
19 v-if="isConnected"
20 variant="link"
21 type="button"
Mateusz Gapski96b37af2020-09-04 14:11:55 +020022 @click="sendCtrlAltDel"
23 >
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060024 <icon-arrow-down />
Mateusz Gapski96b37af2020-09-04 14:11:55 +020025 {{ $t('pageKvm.buttonCtrlAltDelete') }}
26 </b-button>
27 <b-button
28 v-if="!isFullWindow"
29 variant="link"
30 type="button"
Mateusz Gapski96b37af2020-09-04 14:11:55 +020031 @click="openConsoleWindow()"
32 >
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060033 <icon-launch />
34 {{ $t('pageKvm.openNewTab') }}
Mateusz Gapski96b37af2020-09-04 14:11:55 +020035 </b-button>
36 </b-col>
37 </b-row>
38 </div>
39 <div id="terminal-kvm" ref="panel" :class="terminalClass"></div>
Mateusz Gapski632de222020-07-09 09:21:33 +020040 </div>
41</template>
42
43<script>
44import RFB from '@novnc/novnc/core/rfb';
Mateusz Gapski96b37af2020-09-04 14:11:55 +020045import StatusIcon from '@/components/Global/StatusIcon';
46import IconLaunch from '@carbon/icons-vue/es/launch/20';
47import IconArrowDown from '@carbon/icons-vue/es/arrow--down/16';
Konstantin Aladyshev69e8ec52021-02-22 14:07:57 +030048import { throttle } from 'lodash';
Mateusz Gapski96b37af2020-09-04 14:11:55 +020049
50const Connecting = 0;
51const Connected = 1;
52const Disconnected = 2;
Mateusz Gapski632de222020-07-09 09:21:33 +020053
54export default {
55 name: 'KvmConsole',
Mateusz Gapski96b37af2020-09-04 14:11:55 +020056 components: { StatusIcon, IconLaunch, IconArrowDown },
57 props: {
58 isFullWindow: {
59 type: Boolean,
Derick Montague602e98a2020-10-21 16:20:00 -050060 default: true,
61 },
Mateusz Gapski96b37af2020-09-04 14:11:55 +020062 },
Mateusz Gapski632de222020-07-09 09:21:33 +020063 data() {
64 return {
65 rfb: null,
66 isConnected: false,
Mateusz Gapski96b37af2020-09-04 14:11:55 +020067 terminalClass: this.isFullWindow ? 'full-window' : '',
68 marginClass: this.isFullWindow ? 'margin-left-full-window' : '',
69 status: Connecting,
Derick Montague602e98a2020-10-21 16:20:00 -050070 convasRef: null,
Konstantin Aladyshev69e8ec52021-02-22 14:07:57 +030071 resizeKvmWindow: null,
Mateusz Gapski632de222020-07-09 09:21:33 +020072 };
73 },
Mateusz Gapski96b37af2020-09-04 14:11:55 +020074 computed: {
Derick Montague71114fe2021-05-06 18:17:34 -050075 serverStatusIcon() {
Mateusz Gapski96b37af2020-09-04 14:11:55 +020076 if (this.status === Connected) {
77 return 'success';
78 } else if (this.status === Disconnected) {
79 return 'danger';
80 }
81 return 'secondary';
82 },
Derick Montague71114fe2021-05-06 18:17:34 -050083 serverStatus() {
Mateusz Gapski96b37af2020-09-04 14:11:55 +020084 if (this.status === Connected) {
85 return this.$t('pageKvm.connected');
86 } else if (this.status === Disconnected) {
87 return this.$t('pageKvm.disconnected');
88 }
89 return this.$t('pageKvm.connecting');
Derick Montague602e98a2020-10-21 16:20:00 -050090 },
Mateusz Gapski96b37af2020-09-04 14:11:55 +020091 },
Mateusz Gapski632de222020-07-09 09:21:33 +020092 mounted() {
93 this.openTerminal();
94 },
Konstantin Aladyshev69e8ec52021-02-22 14:07:57 +030095 beforeDestroy() {
96 window.removeEventListener('resize', this.resizeKvmWindow);
97 },
Mateusz Gapski632de222020-07-09 09:21:33 +020098 methods: {
99 sendCtrlAltDel() {
100 this.rfb.sendCtrlAltDel();
101 },
102 openTerminal() {
103 const token = this.$store.getters['authentication/token'];
104 this.rfb = new RFB(
105 this.$refs.panel,
106 `wss://${window.location.host}/kvm/0`,
107 { wsProtocols: [token] }
108 );
109
110 this.rfb.scaleViewport = true;
Mateusz Gapski96b37af2020-09-04 14:11:55 +0200111 this.rfb.clipViewport = true;
Mateusz Gapski632de222020-07-09 09:21:33 +0200112 const that = this;
Mateusz Gapski96b37af2020-09-04 14:11:55 +0200113
Konstantin Aladyshev69e8ec52021-02-22 14:07:57 +0300114 this.resizeKvmWindow = throttle(() => {
Mateusz Gapski96b37af2020-09-04 14:11:55 +0200115 setTimeout(that.setWidthToolbar, 0);
Konstantin Aladyshev69e8ec52021-02-22 14:07:57 +0300116 }, 1000);
117 window.addEventListener('resize', this.resizeKvmWindow);
Mateusz Gapski96b37af2020-09-04 14:11:55 +0200118
Mateusz Gapski632de222020-07-09 09:21:33 +0200119 this.rfb.addEventListener('connect', () => {
120 that.isConnected = true;
Mateusz Gapski96b37af2020-09-04 14:11:55 +0200121 that.status = Connected;
122 that.setWidthToolbar();
Mateusz Gapski632de222020-07-09 09:21:33 +0200123 });
124
125 this.rfb.addEventListener('disconnect', () => {
Mateusz Gapski96b37af2020-09-04 14:11:55 +0200126 this.isConnected = false;
127 that.status = Disconnected;
Mateusz Gapski632de222020-07-09 09:21:33 +0200128 });
Mateusz Gapski96b37af2020-09-04 14:11:55 +0200129 },
130 setWidthToolbar() {
131 if (
132 this.$refs.panel.children &&
133 this.$refs.panel.children.length > 0 &&
134 this.$refs.panel.children[0].children.length > 0
135 ) {
136 this.$refs.toolbar.style.width =
137 this.$refs.panel.children[0].children[0].clientWidth - 10 + 'px';
138 }
139 },
140 openConsoleWindow() {
141 window.open(
142 '#/console/kvm',
143 '_blank',
144 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=700,height=550'
145 );
Derick Montague602e98a2020-10-21 16:20:00 -0500146 },
147 },
Mateusz Gapski632de222020-07-09 09:21:33 +0200148};
149</script>
150
151<style scoped lang="scss">
Mateusz Gapski632de222020-07-09 09:21:33 +0200152.button-ctrl-alt-delete {
153 float: right;
154}
155
156.kvm-status {
157 padding-top: $spacer / 2;
158 padding-left: $spacer / 4;
159 display: inline-block;
160}
Mateusz Gapski96b37af2020-09-04 14:11:55 +0200161
162.margin-left-full-window {
163 margin-left: 5px;
164}
Mateusz Gapski632de222020-07-09 09:21:33 +0200165</style>