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