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"> |
| 7 | <dt class="d-inline font-weight-bold mr-1"> |
| 8 | {{ $t('pageKvm.status') }}: |
| 9 | </dt> |
| 10 | <dd class="d-inline"> |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 11 | <status-icon :status="serverStatusIcon" /> |
| 12 | <span class="d-none d-md-inline"> {{ serverStatus }}</span> |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 13 | </dd> |
| 14 | </dl> |
| 15 | </b-col> |
| 16 | |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 17 | <b-col class="d-flex justify-content-end pr-1"> |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 18 | <b-button |
| 19 | v-if="isConnected" |
| 20 | variant="link" |
| 21 | type="button" |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 22 | @click="sendCtrlAltDel" |
| 23 | > |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 24 | <icon-arrow-down /> |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 25 | {{ $t('pageKvm.buttonCtrlAltDelete') }} |
| 26 | </b-button> |
| 27 | <b-button |
| 28 | v-if="!isFullWindow" |
| 29 | variant="link" |
| 30 | type="button" |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 31 | @click="openConsoleWindow()" |
| 32 | > |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 33 | <icon-launch /> |
| 34 | {{ $t('pageKvm.openNewTab') }} |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 35 | </b-button> |
| 36 | </b-col> |
| 37 | </b-row> |
| 38 | </div> |
| 39 | <div id="terminal-kvm" ref="panel" :class="terminalClass"></div> |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 40 | </div> |
| 41 | </template> |
| 42 | |
| 43 | <script> |
| 44 | import RFB from '@novnc/novnc/core/rfb'; |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 45 | import StatusIcon from '@/components/Global/StatusIcon'; |
| 46 | import IconLaunch from '@carbon/icons-vue/es/launch/20'; |
| 47 | import IconArrowDown from '@carbon/icons-vue/es/arrow--down/16'; |
Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 48 | import { throttle } from 'lodash'; |
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 { |
| 65 | rfb: null, |
| 66 | isConnected: false, |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 67 | terminalClass: this.isFullWindow ? 'full-window' : '', |
| 68 | marginClass: this.isFullWindow ? 'margin-left-full-window' : '', |
| 69 | status: Connecting, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 70 | convasRef: null, |
Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 71 | resizeKvmWindow: null, |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 72 | }; |
| 73 | }, |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 74 | computed: { |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 75 | serverStatusIcon() { |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 76 | if (this.status === Connected) { |
| 77 | return 'success'; |
| 78 | } else if (this.status === Disconnected) { |
| 79 | return 'danger'; |
| 80 | } |
| 81 | return 'secondary'; |
| 82 | }, |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 83 | serverStatus() { |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 84 | 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 Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 90 | }, |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 91 | }, |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 92 | mounted() { |
| 93 | this.openTerminal(); |
| 94 | }, |
Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 95 | beforeDestroy() { |
| 96 | window.removeEventListener('resize', this.resizeKvmWindow); |
Lei YU | 6641cc4 | 2021-06-18 19:22:57 +0800 | [diff] [blame] | 97 | this.closeTerminal(); |
Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 98 | }, |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 99 | methods: { |
| 100 | sendCtrlAltDel() { |
| 101 | this.rfb.sendCtrlAltDel(); |
| 102 | }, |
Lei YU | 6641cc4 | 2021-06-18 19:22:57 +0800 | [diff] [blame] | 103 | closeTerminal() { |
| 104 | this.rfb.disconnect(); |
| 105 | this.rfb = null; |
| 106 | }, |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 107 | openTerminal() { |
| 108 | const token = this.$store.getters['authentication/token']; |
| 109 | this.rfb = new RFB( |
| 110 | this.$refs.panel, |
| 111 | `wss://${window.location.host}/kvm/0`, |
| 112 | { wsProtocols: [token] } |
| 113 | ); |
| 114 | |
| 115 | this.rfb.scaleViewport = true; |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 116 | this.rfb.clipViewport = true; |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 117 | const that = this; |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 118 | |
Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 119 | this.resizeKvmWindow = throttle(() => { |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 120 | setTimeout(that.setWidthToolbar, 0); |
Konstantin Aladyshev | 69e8ec5 | 2021-02-22 14:07:57 +0300 | [diff] [blame] | 121 | }, 1000); |
| 122 | window.addEventListener('resize', this.resizeKvmWindow); |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 123 | |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 124 | this.rfb.addEventListener('connect', () => { |
| 125 | that.isConnected = true; |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 126 | that.status = Connected; |
| 127 | that.setWidthToolbar(); |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 128 | }); |
| 129 | |
| 130 | this.rfb.addEventListener('disconnect', () => { |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 131 | this.isConnected = false; |
| 132 | that.status = Disconnected; |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 133 | }); |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 134 | }, |
| 135 | setWidthToolbar() { |
| 136 | if ( |
| 137 | this.$refs.panel.children && |
| 138 | this.$refs.panel.children.length > 0 && |
| 139 | this.$refs.panel.children[0].children.length > 0 |
| 140 | ) { |
| 141 | this.$refs.toolbar.style.width = |
| 142 | this.$refs.panel.children[0].children[0].clientWidth - 10 + 'px'; |
| 143 | } |
| 144 | }, |
| 145 | openConsoleWindow() { |
| 146 | window.open( |
| 147 | '#/console/kvm', |
| 148 | '_blank', |
| 149 | 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=700,height=550' |
| 150 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 151 | }, |
| 152 | }, |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 153 | }; |
| 154 | </script> |
| 155 | |
| 156 | <style scoped lang="scss"> |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 157 | .button-ctrl-alt-delete { |
| 158 | float: right; |
| 159 | } |
| 160 | |
| 161 | .kvm-status { |
| 162 | padding-top: $spacer / 2; |
| 163 | padding-left: $spacer / 4; |
| 164 | display: inline-block; |
| 165 | } |
Mateusz Gapski | 96b37af | 2020-09-04 14:11:55 +0200 | [diff] [blame] | 166 | |
| 167 | .margin-left-full-window { |
| 168 | margin-left: 5px; |
| 169 | } |
Mateusz Gapski | 632de22 | 2020-07-09 09:21:33 +0200 | [diff] [blame] | 170 | </style> |