diff options
-rw-r--r-- | app/assets/javascripts/terminal/terminal.js.es6 | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/app/assets/javascripts/terminal/terminal.js.es6 b/app/assets/javascripts/terminal/terminal.js.es6 index d1bbab64b2c..f797f1ac637 100644 --- a/app/assets/javascripts/terminal/terminal.js.es6 +++ b/app/assets/javascripts/terminal/terminal.js.es6 @@ -1,63 +1,67 @@ -window.gl = window.gl || {}; +((global) => { -gl.Terminal = class { + class GLTerminal { - constructor(options) { - this.options = options || {}; + constructor(options) { + this.options = options || {}; - options.cursorBlink = options.cursorBlink || true; - options.screenKeys = options.screenKeys || true; - options.cols = options.cols || 100; - options.rows = options.rows || 40; - this.container = document.querySelector(options.selector); + options.cursorBlink = options.cursorBlink || true; + options.screenKeys = options.screenKeys || true; + options.cols = options.cols || 100; + options.rows = options.rows || 40; + this.container = document.querySelector(options.selector); - this.setSocketUrl(); - this.createTerminal(); - } + this.setSocketUrl(); + this.createTerminal(); + } - setSocketUrl() { - const {protocol, hostname, port} = window.location; - const wsProtocol = protocol === 'https:' ? 'wss://' : 'ws://'; - const path = this.container.dataset.projectPath; + setSocketUrl() { + const {protocol, hostname, port} = window.location; + const wsProtocol = protocol === 'https:' ? 'wss://' : 'ws://'; + const path = this.container.dataset.projectPath; - this.socketUrl = `${wsProtocol}${hostname}:${port}${path}/deployments/4/terminal_websocket` - } + this.socketUrl = `${wsProtocol}${hostname}:${port}${path}/deployments/4/terminal_websocket` + } - createTerminal() { - this.terminal = new Terminal(this.options); - this.socket = new WebSocket(this.socketUrl); + createTerminal() { + this.terminal = new Terminal(this.options); + this.socket = new WebSocket(this.socketUrl); - this.terminal.open(this.container); - this.terminal.fit(); + this.terminal.open(this.container); + this.terminal.fit(); - this.socket.onopen = () => { this.runTerminal() }; - this.socket.onclose = () => { this.handleSocketFailure() }; - this.socket.onerror = () => { this.handleSocketFailure() }; - } + this.socket.onopen = () => { this.runTerminal() }; + this.socket.onclose = () => { this.handleSocketFailure() }; + this.socket.onerror = () => { this.handleSocketFailure() }; + } - runTerminal() { - const {cols, rows} = this.terminal.proposeGeometry(); - const {offsetWidth, offsetHeight} = this.terminal.element; + runTerminal() { + const {cols, rows} = this.terminal.proposeGeometry(); + const {offsetWidth, offsetHeight} = this.terminal.element; - this.charWidth = Math.ceil(offsetWidth / cols); - this.charHeight = Math.ceil(offsetHeight / rows); + this.charWidth = Math.ceil(offsetWidth / cols); + this.charHeight = Math.ceil(offsetHeight / rows); - this.terminal.attach(this.socket); - this.isTerminalInitialized = true; - this.setTerminalSize(cols, rows); - } + this.terminal.attach(this.socket); + this.isTerminalInitialized = true; + this.setTerminalSize(cols, rows); + } - setTerminalSize (cols, rows) { - const width = `${(cols * this.charWidth).toString()}px`; - const height = `${(rows * this.charHeight).toString()}px`; + setTerminalSize (cols, rows) { + const width = `${(cols * this.charWidth).toString()}px`; + const height = `${(rows * this.charHeight).toString()}px`; - this.container.style.width = width; - this.container.style.height = height; - this.terminal.resize(cols, rows); - } + this.container.style.width = width; + this.container.style.height = height; + this.terminal.resize(cols, rows); + } + + handleSocketFailure() { + console.error('There is a problem with Terminal connection. Please try again!'); + } - handleSocketFailure() { - console.error('There is a problem with Terminal connection. Please try again!'); } -} + global.Terminal = GLTerminal; + +})(window.gl || (window.gl = {})) |