-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst-templ.ts
151 lines (143 loc) · 6.31 KB
/
first-templ.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
import { CssObserve } from 'css-observe/css-observe.js';
import { TemplMount } from './templ-mount.js';
import {decorate} from 'trans-render/plugins/decorate.js';
const listening = Symbol();
const hrefSym = Symbol();
const hrefSym2 = Symbol();
const propAdded = Symbol();
export class FirstTempl {
constructor(public tm: TemplMount) {
if (tm[listening] === true) return;
tm[listening] = true;
const remoteTemplateObserver = document.createElement(CssObserve.is) as CssObserve;
remoteTemplateObserver.observe = true;
remoteTemplateObserver.selector = "template[import][href][as]";
remoteTemplateObserver.customStyles = /* css */`
template[import][as]{
display:block;
}
template[import][as][loaded]:not({
display:none;
}
`;
remoteTemplateObserver.addEventListener('latest-match-changed', e => {
const t = (<any>e).detail.value as HTMLTemplateElement;
const href = t.getAttribute('href') || t.getAttribute('last-href');
if(!t[propAdded]){
decorate(t, {
propDefs:{
'href': href
},
methods:{
onPropsChange(name, newVal){
t.setAttribute('href', newVal);
}
}
});
}
t[propAdded] = true;
TemplMount.template(href, {
tm: this.tm,
template: t
});
const as = t.getAttribute('as');
this.watchElVisibility(as, href, t);
});
this.tm.appendChild(remoteTemplateObserver);
const localTemplateObserver = document.createElement(CssObserve.is) as CssObserve;
localTemplateObserver.observe = true;
localTemplateObserver.selector = "template[import][as]:not([href]):not([last-href])";
localTemplateObserver.addEventListener('latest-match-changed', e => {
const t = (<any>e).detail.value as HTMLTemplateElement;
if (t.hasAttribute('href') || t.hasAttribute('last-href') || t.hasAttribute('loaded')) {
return; //why is this necessary?
}
t.setAttribute('loaded', '');
const as = t.getAttribute('as');
this.watchElVisibility(as, null, t);
});
this.tm.appendChild(localTemplateObserver);
}
async callback(entries: any, observer: any) {
const first = entries[0];
if (first.intersectionRatio > 0) {
const newlyVisibleElement = first.target as HTMLElement;
const alias = newlyVisibleElement.getAttribute(this.tm._importKey);
const template = this._templateLookup[alias];
let href = template.getAttribute('href') || template.getAttribute('last-href');
if (href !== null) {
template.removeAttribute('when-needed');
await TemplMount.template(href, {
tm: this.tm,
template: template,
target: newlyVisibleElement,
});
} else {
href = 'none';
}
if (newlyVisibleElement[hrefSym2] === href) return; //why?
newlyVisibleElement[hrefSym2] = href;
const clone = template.content.cloneNode(true);
if (template.hasAttribute('enable-filter')) {
this.tm.emit<'template-cloned'>(newlyVisibleElement, 'template-cloned', {
clone: clone,
template: template
});
}
if (template.hasAttribute('without-shadow')) {
newlyVisibleElement.innerHTML = '';
newlyVisibleElement.appendChild(clone);
} else {
if (newlyVisibleElement.shadowRoot === null) {
newlyVisibleElement.attachShadow({ mode: 'open' });
}
newlyVisibleElement.shadowRoot.innerHTML = '';
newlyVisibleElement.shadowRoot.appendChild(clone);
const templMt = document.createElement('templ-mount') as TemplMount;
//TODO: Populate recursive stuff
newlyVisibleElement.shadowRoot.appendChild(templMt);
}
observer.disconnect();
}
}
_templateLookup: { [as: string]: HTMLTemplateElement } = {};
watchElVisibility(as: string, href: string | null, t: HTMLTemplateElement) {
this._templateLookup[as] = t;
const selector = `[${this.tm._importKey}="${as}"]`;
const alreadyExistingImpTObserver = this.tm.querySelector(`[as-q="${as}"]`) as CssObserve;
if (alreadyExistingImpTObserver !== null) {
//alreadyExistingImpTObserver[hrefSym] = href;
alreadyExistingImpTObserver.remove();
}
const impTObserver = document.createElement(CssObserve.is) as CssObserve;
impTObserver[hrefSym] = href;
impTObserver.setAttribute('as-q', as);
impTObserver.observe = true;
impTObserver.selector = selector;
impTObserver.addEventListener('latest-match-changed', e => {
const elementToWatchForTurningVisible = (<any>e).detail.value;
if (href === null) {
const ioi: IntersectionObserverInit = {
threshold: 0.01
};
const observer = new IntersectionObserver(this.callback.bind(this), ioi);
observer.observe(elementToWatchForTurningVisible);
} else {
if (elementToWatchForTurningVisible[hrefSym] === e.target[hrefSym]) return;
elementToWatchForTurningVisible[hrefSym] = href;
TemplMount.template(href, {
tm: this.tm,
template: t,
target: elementToWatchForTurningVisible,
}).then(val => {
const ioi: IntersectionObserverInit = {
threshold: 0.01
};
const observer = new IntersectionObserver(this.callback.bind(this), ioi);
observer.observe(elementToWatchForTurningVisible);
})
}
});
this.tm.appendChild(impTObserver);
}
}