beccabroek | 457ca04 | 2018-09-07 17:18:24 -0500 | [diff] [blame] | 1 | import {Terminal} from 'xterm'; |
| 2 | import style from 'xterm/dist/xterm.css'; |
| 3 | import * as attach from 'xterm/lib/addons/attach/attach'; |
| 4 | import * as fit from 'xterm/lib/addons/fit/fit'; |
Yang Cheng | 6d1d600 | 2019-02-27 11:16:24 +0800 | [diff] [blame] | 5 | var configJSON = require('../../../config.json'); |
| 6 | if (configJSON.keyType == 'VT100+') { |
| 7 | var vt100PlusKey = require('./vt100plus'); |
| 8 | } |
beccabroek | 457ca04 | 2018-09-07 17:18:24 -0500 | [diff] [blame] | 9 | |
Yang Cheng | 6d1d600 | 2019-02-27 11:16:24 +0800 | [diff] [blame] | 10 | var customKeyHandlers = function(ev) { |
| 11 | if (configJSON.keyType == 'VT100+') { |
| 12 | return vt100PlusKey.customVT100PlusKey(ev, this); |
| 13 | } |
| 14 | return true; |
| 15 | }; |
beccabroek | 75697f9 | 2018-09-04 09:34:44 -0500 | [diff] [blame] | 16 | |
| 17 | window.angular && (function(angular) { |
| 18 | 'use strict'; |
| 19 | |
| 20 | angular.module('app.common.directives').directive('serialConsole', [ |
| 21 | function() { |
| 22 | return { |
| 23 | 'restrict': 'E', |
| 24 | 'template': require('./serial-console.html'), |
beccabroek | a788cf0 | 2018-09-04 09:38:13 -0500 | [diff] [blame] | 25 | 'scope': {'path': '=', 'showTabBtn': '=?'}, |
beccabroek | 75697f9 | 2018-09-04 09:34:44 -0500 | [diff] [blame] | 26 | 'controller': [ |
| 27 | '$scope', '$window', 'dataService', |
| 28 | function($scope, $window, dataService) { |
| 29 | $scope.dataService = dataService; |
| 30 | |
beccabroek | 457ca04 | 2018-09-07 17:18:24 -0500 | [diff] [blame] | 31 | // See https://github.com/xtermjs/xterm.js/ for available xterm |
| 32 | // options |
beccabroek | 75697f9 | 2018-09-04 09:34:44 -0500 | [diff] [blame] | 33 | |
beccabroek | 457ca04 | 2018-09-07 17:18:24 -0500 | [diff] [blame] | 34 | Terminal.applyAddon(attach); // Apply the `attach` addon |
| 35 | Terminal.applyAddon(fit); // Apply the `fit` addon |
beccabroek | 75697f9 | 2018-09-04 09:34:44 -0500 | [diff] [blame] | 36 | |
beccabroek | 457ca04 | 2018-09-07 17:18:24 -0500 | [diff] [blame] | 37 | var term = new Terminal(); |
| 38 | term.open(document.getElementById('terminal')); |
| 39 | term.fit(); |
Yang Cheng | 6d1d600 | 2019-02-27 11:16:24 +0800 | [diff] [blame] | 40 | if (configJSON.customKeyEnable == true) { |
| 41 | term.attachCustomKeyEventHandler(customKeyHandlers); |
| 42 | } |
beccabroek | 457ca04 | 2018-09-07 17:18:24 -0500 | [diff] [blame] | 43 | var SOL_THEME = { |
| 44 | background: '#19273c', |
| 45 | cursor: 'rgba(83, 146, 255, .5)', |
| 46 | scrollbar: 'rgba(83, 146, 255, .5)' |
| 47 | }; |
| 48 | term.setOption('theme', SOL_THEME); |
beccabroek | 75697f9 | 2018-09-04 09:34:44 -0500 | [diff] [blame] | 49 | var hostname = dataService.getHost().replace('https://', ''); |
| 50 | var host = 'wss://' + hostname + '/console0'; |
| 51 | var ws = new WebSocket(host); |
beccabroek | 457ca04 | 2018-09-07 17:18:24 -0500 | [diff] [blame] | 52 | term.attach(ws); |
beccabroek | 75697f9 | 2018-09-04 09:34:44 -0500 | [diff] [blame] | 53 | ws.onopen = function() { |
| 54 | console.log('websocket opened'); |
| 55 | }; |
| 56 | ws.onclose = function(event) { |
| 57 | console.log( |
| 58 | 'websocket closed. code: ' + event.code + |
| 59 | ' reason: ' + event.reason); |
| 60 | }; |
beccabroek | a788cf0 | 2018-09-04 09:38:13 -0500 | [diff] [blame] | 61 | $scope.openTerminalWindow = function() { |
| 62 | $window.open( |
| 63 | '#/server-control/remote-console-window', |
| 64 | 'Remote Console Window', |
| 65 | 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=600,height=550'); |
| 66 | }; |
beccabroek | 75697f9 | 2018-09-04 09:34:44 -0500 | [diff] [blame] | 67 | } |
| 68 | ] |
| 69 | }; |
| 70 | } |
| 71 | ]); |
| 72 | })(window.angular); |