| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 1 | <template> |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 2 | <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"> |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 7 | <dt class="d-inline fw-bold me-1">{{ $t('pageKvm.status') }}:</dt> |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 8 | <dd class="d-inline"> |
| Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 9 | <status-icon :status="serverStatusIcon" /> |
| 10 | <span class="d-none d-md-inline"> {{ serverStatus }}</span> |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 11 | </dd> |
| 12 | </dl> |
| 13 | </b-col> |
| 14 | |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 15 | <b-col class="d-flex justify-content-end pe-1"> |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 16 | <b-button |
| 17 | v-if="isConnected" |
| 18 | variant="link" |
| 19 | type="button" |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 20 | @click="sendCtrlAltDel" |
| 21 | > |
| Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 22 | <icon-arrow-down /> |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 23 | {{ $t('pageKvm.buttonCtrlAltDelete') }} |
| 24 | </b-button> |
| 25 | <b-button |
| 26 | v-if="!isFullWindow" |
| 27 | variant="link" |
| 28 | type="button" |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 29 | @click="openConsoleWindow()" |
| 30 | > |
| Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 31 | <icon-launch /> |
| 32 | {{ $t('pageKvm.openNewTab') }} |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 33 | </b-button> |
| 34 | </b-col> |
| 35 | </b-row> |
| 36 | </div> |
| 37 | <div id="terminal-kvm" ref="panel" :class="terminalClass"></div> |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 38 | </div> |
| 39 | </template> |
| 40 | |
| 41 | <script> |
| 42 | import RFB from '@novnc/novnc/core/rfb'; |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 43 | import StatusIcon from '@/components/Global/StatusIcon'; |
| 44 | import IconLaunch from '@carbon/icons-vue/es/launch/20'; |
| 45 | import IconArrowDown from '@carbon/icons-vue/es/arrow--down/16'; |
| Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 46 | import { throttle } from 'lodash'; |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 47 | import { useI18n } from 'vue-i18n'; |
| 48 | import i18n from '@/i18n'; |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 49 | |
| 50 | const Connecting = 0; |
| 51 | const Connected = 1; |
| 52 | const Disconnected = 2; |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 53 | |
| 54 | export default { |
| 55 | name: 'KvmConsole', |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 56 | components: { StatusIcon, IconLaunch, IconArrowDown }, |
| 57 | props: { |
| 58 | isFullWindow: { |
| 59 | type: Boolean, |
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 60 | default: true, |
| 61 | }, |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 62 | }, |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 63 | data() { |
| 64 | return { |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 65 | $t: useI18n().t, |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 66 | rfb: null, |
| 67 | isConnected: false, |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 68 | terminalClass: this.isFullWindow ? 'full-window' : '', |
| 69 | marginClass: this.isFullWindow ? 'margin-left-full-window' : '', |
| 70 | status: Connecting, |
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 71 | convasRef: null, |
| Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 72 | resizeKvmWindow: null, |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 73 | }; |
| 74 | }, |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 75 | computed: { |
| Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 76 | serverStatusIcon() { |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 77 | if (this.status === Connected) { |
| 78 | return 'success'; |
| 79 | } else if (this.status === Disconnected) { |
| 80 | return 'danger'; |
| 81 | } |
| 82 | return 'secondary'; |
| 83 | }, |
| Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 84 | serverStatus() { |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 85 | if (this.status === Connected) { |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 86 | return i18n.global.t('pageKvm.connected'); |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 87 | } else if (this.status === Disconnected) { |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 88 | return i18n.global.t('pageKvm.disconnected'); |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 89 | } |
| Surya V | de23ea2 | 2024-07-11 15:19:46 +0530 | [diff] [blame] | 90 | return i18n.global.t('pageKvm.connecting'); |
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 91 | }, |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 92 | }, |
| kirankumarb07 | e78d10b | 2023-03-03 12:10:29 +0530 | [diff] [blame] | 93 | created() { |
| 94 | this.$store.dispatch('global/getSystemInfo'); |
| 95 | }, |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 96 | mounted() { |
| 97 | this.openTerminal(); |
| 98 | }, |
| Ed Tanous | 7d6b44c | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 99 | beforeUnmount() { |
| Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 100 | window.removeEventListener('resize', this.resizeKvmWindow); |
| Lei YU | 6641cc4 | 2021-06-18 19:22:57 +0800 | [diff] [blame] | 101 | this.closeTerminal(); |
| Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 102 | }, |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 103 | methods: { |
| 104 | sendCtrlAltDel() { |
| 105 | this.rfb.sendCtrlAltDel(); |
| 106 | }, |
| Lei YU | 6641cc4 | 2021-06-18 19:22:57 +0800 | [diff] [blame] | 107 | closeTerminal() { |
| 108 | this.rfb.disconnect(); |
| 109 | this.rfb = null; |
| 110 | }, |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 111 | openTerminal() { |
| 112 | const token = this.$store.getters['authentication/token']; |
| 113 | this.rfb = new RFB( |
| 114 | this.$refs.panel, |
| 115 | `wss://${window.location.host}/kvm/0`, |
| Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 116 | { wsProtocols: [token] }, |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 117 | ); |
| 118 | |
| 119 | this.rfb.scaleViewport = true; |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 120 | this.rfb.clipViewport = true; |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 121 | const that = this; |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 122 | |
| Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 123 | this.resizeKvmWindow = throttle(() => { |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 124 | setTimeout(that.setWidthToolbar, 0); |
| Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 125 | }, 1000); |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 126 | window.addEventListener('resize', this.resizeKvmWindow, { |
| 127 | passive: true, |
| 128 | }); |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 129 | |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 130 | this.rfb.addEventListener('connect', () => { |
| 131 | that.isConnected = true; |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 132 | that.status = Connected; |
| 133 | that.setWidthToolbar(); |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 134 | }); |
| 135 | |
| 136 | this.rfb.addEventListener('disconnect', () => { |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 137 | this.isConnected = false; |
| 138 | that.status = Disconnected; |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 139 | }); |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 140 | }, |
| 141 | setWidthToolbar() { |
| 142 | if ( |
| 143 | this.$refs.panel.children && |
| 144 | this.$refs.panel.children.length > 0 && |
| 145 | this.$refs.panel.children[0].children.length > 0 |
| 146 | ) { |
| 147 | this.$refs.toolbar.style.width = |
| 148 | this.$refs.panel.children[0].children[0].clientWidth - 10 + 'px'; |
| 149 | } |
| 150 | }, |
| 151 | openConsoleWindow() { |
| Konstantin | fb6c6de | 2023-06-14 17:23:14 +0300 | [diff] [blame] | 152 | // If consoleWindow is not null |
| kirankumarb07 | b89eed2 | 2023-01-12 15:50:30 +0530 | [diff] [blame] | 153 | // Check the newly opened window is closed or not |
| Konstantin | fb6c6de | 2023-06-14 17:23:14 +0300 | [diff] [blame] | 154 | if (this.$eventBus.$consoleWindow) { |
| kirankumarb07 | b89eed2 | 2023-01-12 15:50:30 +0530 | [diff] [blame] | 155 | // If window is not closed set focus to new window |
| 156 | // If window is closed, do open new window |
| Konstantin | fb6c6de | 2023-06-14 17:23:14 +0300 | [diff] [blame] | 157 | if (!this.$eventBus.$consoleWindow.closed) { |
| 158 | this.$eventBus.$consoleWindow.focus(); |
| kirankumarb07 | b89eed2 | 2023-01-12 15:50:30 +0530 | [diff] [blame] | 159 | return; |
| 160 | } else { |
| 161 | this.openNewWindow(); |
| 162 | } |
| 163 | } else { |
| Konstantin | fb6c6de | 2023-06-14 17:23:14 +0300 | [diff] [blame] | 164 | // If consoleWindow is null, open new window |
| kirankumarb07 | b89eed2 | 2023-01-12 15:50:30 +0530 | [diff] [blame] | 165 | this.openNewWindow(); |
| 166 | } |
| 167 | }, |
| 168 | openNewWindow() { |
| Konstantin | fb6c6de | 2023-06-14 17:23:14 +0300 | [diff] [blame] | 169 | this.$eventBus.$consoleWindow = window.open( |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 170 | '#/console/kvm', |
| kirankumarb07 | b89eed2 | 2023-01-12 15:50:30 +0530 | [diff] [blame] | 171 | 'kvmConsoleWindow', |
| Ed Tanous | 8132399 | 2024-02-27 11:26:24 -0800 | [diff] [blame] | 172 | 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=700,height=550', |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 173 | ); |
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 174 | }, |
| 175 | }, |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 176 | }; |
| 177 | </script> |
| 178 | |
| suryav9724 | 24b377d | 2025-01-24 15:06:35 +0530 | [diff] [blame] | 179 | <style scoped lang="scss"> |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 180 | .button-ctrl-alt-delete { |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 181 | float: inline-end; |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | .kvm-status { |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 185 | padding-top: calc(#{$spacer} / 2); |
| 186 | padding-inline-start: calc(#{$spacer} / 4); |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 187 | display: inline-block; |
| 188 | } |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 189 | |
| 190 | .margin-left-full-window { |
| jason westover | d36ac8a | 2025-11-03 20:58:59 -0600 | [diff] [blame^] | 191 | margin-inline-start: 5px; |
| Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 192 | } |
| Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 193 | </style> |