-
Notifications
You must be signed in to change notification settings - Fork 13
/
terminal.ts
143 lines (116 loc) · 4.91 KB
/
terminal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/
import { Subject } from 'rxjs';
declare var org_zowe_terminal_tn3270: any;
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable, throwError} from 'rxjs';
import {catchError} from 'rxjs/operators';
export class TerminalStateHelper {
private url:string;
constructor(public http: HttpClient,
public log: ZLUX.ComponentLogger,
pluginDefinition: any){
this.url = ZoweZLUX.uriBroker.pluginRESTUri(pluginDefinition.getBasePlugin(), 'stateDiscovery', 'zosDiscovery/system/tn3270');
}
getAll(luname?:string): Observable<any> {
return this.http.get(luname ? this.url+'?luname='+luname : this.url)
.pipe(catchError(this.handleError));
}
handleError(error: any): Observable<void> {
let errorMsg = error.message || 'Failure to retrieve TN3270/VTAM/TSO data';
return throwError(() => new Error(errorMsg));
}
}
export type TerminalWebsocketError = {
code: number;
reason: string;
terminalMessage: string;
}
export class Terminal {
virtualScreen: any;
contextMenuEmitter: Subject<any> = new Subject();
wsErrorEmitter: Subject<TerminalWebsocketError> = new Subject();
constructor(
private terminalElement: HTMLElement,
private terminalParentElement: HTMLElement,
public http: HttpClient,
public pluginDefinition: ZLUX.ContainerPluginDefinition,
private log: ZLUX.ComponentLogger
) { }
connectToHost(connectionSettings: any) {
if (this.virtualScreen) {
return;
}
const computedStyle = getComputedStyle(this.terminalElement, null);
const width = parseInt(computedStyle.getPropertyValue('width'));
const height = parseInt(computedStyle.getPropertyValue('height'));
const helper:TerminalStateHelper = new TerminalStateHelper(this.http,this.log,this.pluginDefinition);
let plugin:ZLUX.Plugin = this.pluginDefinition.getBasePlugin();
connectionSettings.url = ZoweZLUX.uriBroker.pluginWSUri(plugin, 'terminalstream', '');
connectionSettings.connect = true;
connectionSettings.security.type = connectionSettings.security.type == 'tls' ? 2 : 0;
// logic for using dispatcher goes here
// should be in Tn3270Service.js eventually
let latestContext = {};
const screenLoadedCallback = () => {
helper.getAll(this.virtualScreen.getLUName()).subscribe(data=> {
if (data?.rows?.length === 1) {
latestContext = data.rows[0];
this.log.debug("screenContext from discovery="+JSON.stringify(latestContext, null, 2));
}
});
};
const contextCallback = (mouseEvent, screenContext) => {
var x = mouseEvent.offsetX;
var y = mouseEvent.offsetY;
mouseEvent.preventDefault();
this.log.debug("screenContext scraped=" + JSON.stringify(screenContext, null, 2));
let dataKeys = Object.keys(latestContext);
for (let i = 0; i < dataKeys.length; i++) {
if (!screenContext[dataKeys[i]]) {
screenContext[dataKeys[i]] = latestContext[dataKeys[i]];
}
}
this.log.debug("Context callback. screenID="+screenContext.screenID+" x="+x+" y="+y);
this.log.debug("screenContext combined=" + JSON.stringify(screenContext, null, 2));
this.contextMenuEmitter.next({ x: x, y: y, screenContext: screenContext});
}
const wsErrorCallback = (wsCode: number, wsReason: string, terminalMessage: string) => {
this.virtualScreen = null;
this.wsErrorEmitter.next({code: wsCode, reason: wsReason, terminalMessage: terminalMessage});
};
this.virtualScreen = org_zowe_terminal_tn3270.start3270({parentDiv:this.terminalElement,
width: width, height: height},
connectionSettings,
null,{contextCallback:contextCallback,
screenLoadedCallback: screenLoadedCallback,
wsErrorCallback: wsErrorCallback});
}
isConnected(): boolean {
return this.virtualScreen;
}
close() {
if (this.virtualScreen) {
this.virtualScreen.closeConnection(4000, "Closed by user");
}
this.virtualScreen = null;
}
performResize() {
if (this.virtualScreen) {
this.virtualScreen.handleContainerResizeFromUI(this.terminalElement, this.virtualScreen);
}
}
}
/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/