-
Notifications
You must be signed in to change notification settings - Fork 2
/
window-handler.ts
54 lines (42 loc) · 1.17 KB
/
window-handler.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
type WindowShowInput = {
url: string;
width?: number;
height?: number;
};
const DEFAULT_WIDTH = 400;
const DEFAULT_HEIGHT = 500;
export class WindowHandler {
private windowRef: Window | null = null;
show({url, width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT}: WindowShowInput) {
const windowRef = openWindow({url, width, height, win: window});
if (!windowRef) {
throw new Error("Window is not initialized");
}
this.windowRef = windowRef;
return windowRef;
}
close() {
if (!this.windowRef) {
throw new Error("Window is not initialized");
}
this.windowRef.close();
}
}
type OpenWindowInput = {
url: string;
width: number;
height: number;
win: Window;
};
function openWindow({url, width, height, win}: OpenWindowInput): Window | null {
if (!win.top) {
return null;
}
const y = win.top.outerHeight / 2 + win.top.screenY - height / 2;
const x = win.top.outerWidth / 2 + win.top.screenX - width / 2;
return window.open(
url,
"",
`toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${width}, height=${height}, top=${y}, left=${x}`
);
}