Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 1 | <template> |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 2 | <div :class="isFullWindow ? 'full-window-container' : 'terminal-container'"> |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 3 | <b-row class="d-flex"> |
Nikhil Ashoka | 1d9257e | 2022-01-31 14:06:37 +0530 | [diff] [blame] | 4 | <b-col sm="4" md="6"> |
| 5 | <alert |
| 6 | v-if="serverStatus === 'on' ? false : true" |
| 7 | variant="warning" |
| 8 | :small="true" |
| 9 | class="mt-4" |
| 10 | > |
| 11 | <p class="col-form-label"> |
| 12 | {{ $t('pageSerialOverLan.alert.disconnectedAlertMessage') }} |
| 13 | </p> |
| 14 | </alert> |
| 15 | </b-col> |
| 16 | </b-row> |
| 17 | <b-row class="d-flex"> |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 18 | <b-col class="d-flex flex-column justify-content-end"> |
| 19 | <dl class="mb-2" sm="6" md="6"> |
| 20 | <dt class="d-inline font-weight-bold mr-1"> |
| 21 | {{ $t('pageSerialOverLan.status') }}: |
| 22 | </dt> |
| 23 | <dd class="d-inline"> |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 24 | <status-icon :status="serverStatusIcon" /> {{ connectionStatus }} |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 25 | </dd> |
| 26 | </dl> |
| 27 | </b-col> |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 28 | |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 29 | <b-col v-if="!isFullWindow" class="d-flex justify-content-end"> |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 30 | <b-button variant="link" type="button" @click="openConsoleWindow()"> |
| 31 | <icon-launch /> |
Sukanya Pandey | d9dc6fb | 2020-11-03 11:13:23 +0530 | [diff] [blame] | 32 | {{ $t('pageSerialOverLan.openNewTab') }} |
| 33 | </b-button> |
| 34 | </b-col> |
| 35 | </b-row> |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 36 | <div id="terminal" ref="panel"></div> |
| 37 | </div> |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 38 | </template> |
| 39 | |
| 40 | <script> |
Nikhil Ashoka | 1d9257e | 2022-01-31 14:06:37 +0530 | [diff] [blame] | 41 | import Alert from '@/components/Global/Alert'; |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 42 | import { AttachAddon } from 'xterm-addon-attach'; |
| 43 | import { FitAddon } from 'xterm-addon-fit'; |
| 44 | import { Terminal } from 'xterm'; |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 45 | import { throttle } from 'lodash'; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 46 | import IconLaunch from '@carbon/icons-vue/es/launch/20'; |
| 47 | import StatusIcon from '@/components/Global/StatusIcon'; |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 48 | |
| 49 | export default { |
| 50 | name: 'SerialOverLanConsole', |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 51 | components: { |
Nikhil Ashoka | 1d9257e | 2022-01-31 14:06:37 +0530 | [diff] [blame] | 52 | Alert, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 53 | IconLaunch, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 54 | StatusIcon, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 55 | }, |
| 56 | props: { |
| 57 | isFullWindow: { |
| 58 | type: Boolean, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 59 | default: true, |
| 60 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 61 | }, |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 62 | data() { |
| 63 | return { |
| 64 | resizeConsoleWindow: null, |
| 65 | }; |
| 66 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 67 | computed: { |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 68 | serverStatus() { |
| 69 | return this.$store.getters['global/serverStatus']; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 70 | }, |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 71 | serverStatusIcon() { |
| 72 | return this.serverStatus === 'on' ? 'success' : 'danger'; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 73 | }, |
| 74 | connectionStatus() { |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 75 | return this.serverStatus === 'on' |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 76 | ? this.$t('pageSerialOverLan.connected') |
| 77 | : this.$t('pageSerialOverLan.disconnected'); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 78 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 79 | }, |
| 80 | created() { |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 81 | this.$store.dispatch('global/getServerStatus'); |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 82 | }, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 83 | mounted() { |
| 84 | this.openTerminal(); |
| 85 | }, |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 86 | beforeDestroy() { |
| 87 | window.removeEventListener('resize', this.resizeConsoleWindow); |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 88 | this.closeTerminal(); |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 89 | }, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 90 | methods: { |
| 91 | openTerminal() { |
| 92 | const token = this.$store.getters['authentication/token']; |
| 93 | |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 94 | this.ws = new WebSocket(`wss://${window.location.host}/console0`, [ |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 95 | token, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 96 | ]); |
| 97 | |
| 98 | // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons. |
| 99 | |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 100 | this.term = new Terminal({ |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 101 | fontSize: 15, |
| 102 | fontFamily: |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 103 | 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace', |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 104 | }); |
| 105 | |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 106 | const attachAddon = new AttachAddon(this.ws); |
| 107 | this.term.loadAddon(attachAddon); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 108 | |
| 109 | const fitAddon = new FitAddon(); |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 110 | this.term.loadAddon(fitAddon); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 111 | |
| 112 | const SOL_THEME = { |
| 113 | background: '#19273c', |
| 114 | cursor: 'rgba(83, 146, 255, .5)', |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 115 | scrollbar: 'rgba(83, 146, 255, .5)', |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 116 | }; |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 117 | this.term.setOption('theme', SOL_THEME); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 118 | |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 119 | this.term.open(this.$refs.panel); |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 120 | fitAddon.fit(); |
| 121 | |
Konstantin Aladyshev | 64f260c | 2021-02-17 17:23:22 +0300 | [diff] [blame] | 122 | this.resizeConsoleWindow = throttle(() => { |
| 123 | fitAddon.fit(); |
| 124 | }, 1000); |
| 125 | window.addEventListener('resize', this.resizeConsoleWindow); |
| 126 | |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 127 | try { |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 128 | this.ws.onopen = function () { |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 129 | console.log('websocket console0/ opened'); |
| 130 | }; |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 131 | this.ws.onclose = function (event) { |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 132 | console.log( |
| 133 | 'websocket console0/ closed. code: ' + |
| 134 | event.code + |
| 135 | ' reason: ' + |
| 136 | event.reason |
| 137 | ); |
| 138 | }; |
| 139 | } catch (error) { |
| 140 | console.log(error); |
| 141 | } |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 142 | }, |
Xiaochao Ma | 78372d6 | 2022-03-08 20:13:00 +0800 | [diff] [blame] | 143 | closeTerminal() { |
| 144 | console.log('closeTerminal'); |
| 145 | this.term.dispose(); |
| 146 | this.term = null; |
| 147 | this.ws.close(); |
| 148 | this.ws = null; |
| 149 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 150 | openConsoleWindow() { |
| 151 | window.open( |
| 152 | '#/console/serial-over-lan-console', |
| 153 | '_blank', |
| 154 | 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550' |
| 155 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 156 | }, |
| 157 | }, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 158 | }; |
| 159 | </script> |
| 160 | |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 161 | <style lang="scss" scoped> |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 162 | @import '~xterm/css/xterm.css'; |
| 163 | |
Sukanya Pandey | b0fadef | 2021-03-12 14:58:31 +0530 | [diff] [blame] | 164 | #terminal { |
| 165 | overflow: auto; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | .full-window-container { |
| 169 | width: 97%; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 170 | margin: 1.5%; |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 171 | } |
| 172 | </style> |