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"> |
| 10 | <status-icon :status="hostStatusIcon" /> {{ connectionStatus }} |
| 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'; |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 30 | import IconLaunch from '@carbon/icons-vue/es/launch/20'; |
| 31 | import StatusIcon from '@/components/Global/StatusIcon'; |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 32 | |
| 33 | export default { |
| 34 | name: 'SerialOverLanConsole', |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 35 | components: { |
| 36 | IconLaunch, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 37 | StatusIcon, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 38 | }, |
| 39 | props: { |
| 40 | isFullWindow: { |
| 41 | type: Boolean, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 42 | default: true, |
| 43 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 44 | }, |
| 45 | computed: { |
| 46 | hostStatus() { |
| 47 | return this.$store.getters['global/hostStatus']; |
| 48 | }, |
| 49 | hostStatusIcon() { |
| 50 | return this.hostStatus === 'on' ? 'success' : 'danger'; |
| 51 | }, |
| 52 | connectionStatus() { |
| 53 | return this.hostStatus === 'on' |
| 54 | ? this.$t('pageSerialOverLan.connected') |
| 55 | : this.$t('pageSerialOverLan.disconnected'); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 56 | }, |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 57 | }, |
| 58 | created() { |
| 59 | this.$store.dispatch('global/getHostStatus'); |
| 60 | }, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 61 | mounted() { |
| 62 | this.openTerminal(); |
| 63 | }, |
| 64 | methods: { |
| 65 | openTerminal() { |
| 66 | const token = this.$store.getters['authentication/token']; |
| 67 | |
| 68 | const ws = new WebSocket(`wss://${window.location.host}/console0`, [ |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 69 | token, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 70 | ]); |
| 71 | |
| 72 | // Refer https://github.com/xtermjs/xterm.js/ for xterm implementation and addons. |
| 73 | |
| 74 | const term = new Terminal({ |
| 75 | fontSize: 15, |
| 76 | fontFamily: |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 77 | 'SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace', |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 78 | }); |
| 79 | |
| 80 | const attachAddon = new AttachAddon(ws); |
| 81 | term.loadAddon(attachAddon); |
| 82 | |
| 83 | const fitAddon = new FitAddon(); |
| 84 | term.loadAddon(fitAddon); |
| 85 | |
| 86 | const SOL_THEME = { |
| 87 | background: '#19273c', |
| 88 | cursor: 'rgba(83, 146, 255, .5)', |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 89 | scrollbar: 'rgba(83, 146, 255, .5)', |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 90 | }; |
| 91 | term.setOption('theme', SOL_THEME); |
| 92 | |
| 93 | term.open(this.$refs.panel); |
| 94 | fitAddon.fit(); |
| 95 | |
| 96 | try { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 97 | ws.onopen = function () { |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 98 | console.log('websocket console0/ opened'); |
| 99 | }; |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 100 | ws.onclose = function (event) { |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 101 | console.log( |
| 102 | 'websocket console0/ closed. code: ' + |
| 103 | event.code + |
| 104 | ' reason: ' + |
| 105 | event.reason |
| 106 | ); |
| 107 | }; |
| 108 | } catch (error) { |
| 109 | console.log(error); |
| 110 | } |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 111 | }, |
| 112 | openConsoleWindow() { |
| 113 | window.open( |
| 114 | '#/console/serial-over-lan-console', |
| 115 | '_blank', |
| 116 | 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550' |
| 117 | ); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 118 | }, |
| 119 | }, |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 120 | }; |
| 121 | </script> |
| 122 | |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 123 | <style lang="scss" scoped> |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 124 | @import '~xterm/css/xterm.css'; |
| 125 | |
Sukanya Pandey | 615b315 | 2020-08-19 19:13:49 +0530 | [diff] [blame] | 126 | .terminal-container { |
| 127 | #terminal { |
| 128 | overflow: auto; |
| 129 | } |
| 130 | } |
| 131 | @include media-breakpoint-up(xs) { |
| 132 | .terminal-container { |
| 133 | width: 100%; |
| 134 | } |
| 135 | } |
| 136 | @include media-breakpoint-up(sm) { |
| 137 | .terminal-container { |
| 138 | width: 80%; |
| 139 | } |
| 140 | } |
| 141 | @include media-breakpoint-up(md) { |
| 142 | .terminal-container { |
| 143 | width: 65%; |
| 144 | } |
| 145 | } |
| 146 | @include media-breakpoint-up(lg) { |
| 147 | .terminal-container { |
| 148 | width: 80%; |
| 149 | } |
| 150 | } |
| 151 | @include media-breakpoint-up(xl) { |
| 152 | .terminal-container { |
| 153 | width: 60%; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | .full-window-container { |
| 158 | width: 97%; |
| 159 | max-width: 960px; |
| 160 | margin: 1.5%; |
| 161 | #terminal { |
| 162 | overflow: auto; |
| 163 | } |
Sukanya Pandey | 96f69ca | 2020-05-20 15:32:57 +0530 | [diff] [blame] | 164 | } |
| 165 | </style> |