-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfragment-element.ts
86 lines (71 loc) · 1.77 KB
/
fragment-element.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
import { createFragmentUUID } from '../lib/uuid';
export const elementName = 'collage-fragment';
function customStyle(): string {
return `
${elementName} {
display: flex;
justify-content: stretch;
align-items: stretch;
}
${elementName} > iframe {
border: none;
flex: 1;
height: auto%;
width: 100%;
}
`;
}
export class Fragment extends HTMLElement {
_context: unknown;
set context(context: unknown) {
this._context = context;
}
get context() {
return this._context;
}
set url(url: string) {
this.setAttribute('url', url);
}
get url(): string {
return this.getAttribute('url') || '';
}
set name(name: string) {
this.setAttribute('name', name);
}
get name(): string {
return this.getAttribute('name') || '';
}
get uuid(): string {
return this.dataset.uuid || '';
}
isComplete(): boolean {
return !!this.url;
}
connectedCallback(): void {
if (!this.querySelector('style[data-collage-child-element-style]')) {
const style = document.createElement('style');
style.dataset.collageChildElementStyle = '';
style.innerHTML = customStyle();
this.appendChild(style);
}
if (this.isComplete()) {
this.createFragment();
}
}
disconnectedCallback(): void {
document.dispatchEvent(new CustomEvent(
'collage-fragment-disconnected',
{ detail: this.dataset.uuid },
));
}
createFragment(): void {
const iframe = document.createElement('iframe');
iframe.name = createFragmentUUID();
this.dataset.uuid = iframe.name;
iframe.src = this.url || '';
this.appendChild(iframe);
}
}
if (!window.customElements.get(elementName)) {
window.customElements.define(elementName, Fragment);
}