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