-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-lazy.void
90 lines (78 loc) · 2.8 KB
/
be-lazy.void
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
import {define, BeDecoratedProps} from 'be-decorated/DE.js';
import {VirtualProps, Actions, PP} from './types.js';
import {BeIntersectional} from 'be-intersectional/be-intersectional.js';
import {register} from 'be-hive/register.js';
import {RenderContext} from 'trans-render/lib/types';
import { ProxyProps } from '../be-intersectional/types.js';
export class BeLazy extends BeIntersectional implements Actions{
onNotIntersecting(pp: ProxyProps): void {
}
async onIntersecting({exitDelay, transform, host, self, proxy, ctx}: PP){
if(transform !== undefined && host === undefined){
//wait for host to be passed in
return;
}
if(self.nextElementSibling === null){
const clone = (self as HTMLTemplateElement).content.cloneNode(true);
if(ctx !== undefined){
const {self} = ctx;
self!.flushCache();
await self!.transform(clone as DocumentFragment);
self!.flushCache();
}else if(transform !== undefined){
const {DTR} = await import('trans-render/lib/DTR.js');
const ctx: RenderContext = {
host,
match: transform
};
const dtr = new DTR(ctx);
await dtr.transform(clone as DocumentFragment);
}
self.parentElement!.appendChild(clone);
}else{
const {insertAdjacentTemplate} = await import('trans-render/lib/insertAdjacentTemplate.js');
insertAdjacentTemplate(self as HTMLTemplateElement, self, 'afterend');
}
this.disconnect();
setTimeout(() => {
self!.remove();
}, exitDelay);
}
}
const tagName = 'be-lazy';
const ifWantsToBe = 'lazy';
const upgrade = 'template';
define<VirtualProps & BeDecoratedProps<VirtualProps, Actions>, Actions>({
config:{
tagName,
propDefaults:{
upgrade,
ifWantsToBe,
forceVisible: [upgrade],
virtualProps: [
'options', 'isIntersecting', 'isIntersectingEcho',
'enterDelay', 'rootClosest', 'transform', 'host', 'ctx'
],
finale: 'finale',
proxyPropDefaults:{
options: {
threshold: 0,
rootMargin: '0px',
},
enterDelay: 16,
exitDelay: 16
}
},
actions:{
onOptions: 'options',
onIntersecting: {
ifAllOf: ['isIntersecting', 'isIntersectingEcho'],
ifKeyIn: ['host', 'transform', 'ctx'],
}
},
},
complexPropDefaults:{
controller: BeLazy,
}
});
register(ifWantsToBe, upgrade, tagName);