-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathshare.component.ts
More file actions
71 lines (58 loc) · 1.96 KB
/
Copy pathshare.component.ts
File metadata and controls
71 lines (58 loc) · 1.96 KB
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
import { ActivatedRoute } from '@angular/router';
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
@Component({
standalone: false,
selector: 'pages-share',
templateUrl: 'share.component.html'
})
export class PagesShareComponent implements OnInit, AfterViewInit {
@ViewChild('iFrame', { static: false }) iframeRef: ElementRef;
public ping;
public id: string = '';
public type: string = '';
public shareId: string = '';
public iframeURL: string = '';
constructor(private _route: ActivatedRoute) {}
ngOnInit(): void {
this.id = 'window-' + Math.random().toString(36).substr(2);
this.shareId = this._route.snapshot.params['id'];
this.type = this._route.snapshot.queryParams['type'];
const queryParams = new URLSearchParams();
Object.entries(this._route.snapshot.queryParams).forEach(([key, value]) => {
if (value !== null && value !== undefined) {
queryParams.append(key, value as string);
}
});
const queryString = queryParams.toString();
const baseUrl = `${window.location.protocol}//${window.location.host}`;
if (this.type && this.type === 'lion') {
this.iframeURL = `${baseUrl}/lion/share/${this.shareId}?${queryString}`;
return;
}
this.iframeURL = `${baseUrl}/koko/share/${this.shareId}?${queryString}`;
}
ngAfterViewInit(): void {
this.handleK8sIframeEvent();
}
handleK8sIframeEvent() {
this.ping = setInterval(() => {
this.iframeRef.nativeElement.contentWindow.postMessage({ name: 'PING', id: this.id }, '*');
}, 1000);
window.addEventListener('message', event => {
const msg = event.data;
switch (msg.name) {
case 'PONG':
clearInterval(this.ping);
break;
case 'PING':
this.iframeRef.nativeElement.contentWindow.postMessage(
{ name: 'PING', id: this.id },
'*'
);
break;
default:
break;
}
});
}
}