-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathAbstractUIComponent.ts
177 lines (157 loc) · 5.49 KB
/
AbstractUIComponent.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Connectable, FDC3_VERSION } from '@finos/fdc3-standard';
import { Logger } from '../util/Logger';
import { BrowserTypes } from '@finos/fdc3-schema';
const { isFdc3UserInterfaceHello, isFdc3UserInterfaceRestyle } = BrowserTypes;
type Fdc3UserInterfaceHandshake = BrowserTypes.Fdc3UserInterfaceHandshake;
type InitialCSS = BrowserTypes.InitialCSS;
type UpdatedCSS = BrowserTypes.UpdatedCSS;
export interface CSSPositioning {
[key: string]: string;
}
export const INITIAL_CONTAINER_CSS = {
width: '0',
height: '0',
position: 'fixed',
};
export const ALLOWED_CSS_ELEMENTS = [
'width',
'height',
'position',
'zIndex',
'left',
'right',
'top',
'bottom',
'transition',
'maxHeight',
'maxWidth',
'display',
];
export const DEFAULT_UI_ROOT_URL = 'https://fdc3.finos.org/toolbox/fdc3-reference-ui/';
/** Abstract implementation of an injected UI, used as the base for communication
* with injected Channel Selector and Intent Resolver UIs.
*/
export abstract class AbstractUIComponent implements Connectable {
private container: HTMLDivElement | undefined = undefined;
private iframe: HTMLIFrameElement | undefined = undefined;
private url: string;
private name: string;
protected port: MessagePort | null = null;
protected messagePortIsReady: Promise<void>;
private markMessagePortReady: (() => void) | null = null;
constructor(url: string, name: string) {
this.url = url;
this.name = name;
this.messagePortIsReady = new Promise<void>(resolve => (this.markMessagePortReady = resolve));
}
/**
* Connect the UI component by creating the UI iframe, then wait on
* a Fdc3UserInterfaceHello message.
*
* This function is NOT properly async as we don't want to block the
* Desktop Agent connection on the UI frames as they may be blocked by
* security policies. I.e. awaiting this will not block.
*/
connect(): Promise<void> {
Logger.debug(`AbstractUIComponent (${this.name}): Awaiting hello from `, this.name, ', url: ', this.url);
const portPromise = this.awaitHello();
this.openFrame();
portPromise.then(port => {
this.port = port;
this.setupMessagePort(port).then(() => {
this.messagePortReady(port);
});
});
return Promise.resolve();
}
async disconnect() {
this.port?.close();
}
/**
* Override and extend this method to provide functionality specific to the UI in question
*/
async setupMessagePort(port: MessagePort): Promise<void> {
port.addEventListener('message', e => {
const data = e.data;
if (isFdc3UserInterfaceRestyle(data)) {
Logger.debug(`AbstractUIComponent (${this.name}): Restyling: `, data.payload);
const css = data.payload.updatedCSS;
this.themeContainer(css);
}
});
port.start();
}
async messagePortReady(port: MessagePort) {
// tells the iframe it can start posting
const message: Fdc3UserInterfaceHandshake = {
type: 'Fdc3UserInterfaceHandshake',
payload: {
fdc3Version: FDC3_VERSION,
},
};
Logger.debug(`AbstractUIComponent (${this.name}): Sending handshake: `, message);
port.postMessage(message);
this.markMessagePortReady!();
}
private awaitHello(): Promise<MessagePort> {
return new Promise(resolve => {
const ml = (e: MessageEvent) => {
//only respond to messages from this UI's iframe
if (e.source == this.iframe?.contentWindow) {
if (isFdc3UserInterfaceHello(e.data)) {
const helloData = e.data;
this.themeContainer(helloData.payload.initialCSS);
const port = e.ports[0];
globalThis.window.removeEventListener('message', ml);
resolve(port);
} else {
Logger.warn(
`AbstractUIComponent (${this.name}): ignored UI Message from UI iframe while awaiting hello: `,
e.data
);
}
} else {
//as there are two UIs, we expect some cross-over between their messages
Logger.debug(
`AbstractUIComponent (${this.name}): ignored Message that didn't come from expected UI frame: `,
e.data,
'my URL: ',
this.url
);
}
};
globalThis.window.addEventListener('message', ml);
});
}
private openFrame(): void {
this.container = globalThis.document.createElement('div');
this.iframe = globalThis.document.createElement('iframe');
this.themeContainer(INITIAL_CONTAINER_CSS);
this.themeFrame(this.iframe);
this.iframe.setAttribute('src', this.url);
this.iframe.setAttribute('name', this.name);
this.container.appendChild(this.iframe);
document.body.appendChild(this.container);
}
private toKebabCase(str: string) {
return str.replace(/[A-Z]/g, match => '-' + match.toLowerCase());
}
themeContainer(css: UpdatedCSS | InitialCSS) {
Logger.debug(`AbstractUIComponent (${this.name}): Applying styles to container`, css);
for (let i = 0; i < ALLOWED_CSS_ELEMENTS.length; i++) {
const k = ALLOWED_CSS_ELEMENTS[i];
const value: string | undefined = css[k as string];
if (value != null) {
this.container!.style.setProperty(this.toKebabCase(k), value);
} else {
this.container!.style.removeProperty(this.toKebabCase(k));
}
}
}
themeFrame(ifrm: HTMLIFrameElement) {
Logger.debug(`AbstractUIComponent (${this.name}): Applying 100% size style to iframe`);
ifrm.style.width = '100%';
ifrm.style.height = '100%';
ifrm.style.border = '0';
}
}