-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachAndResolve.void
30 lines (29 loc) · 1.13 KB
/
attachAndResolve.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
import {Attachable} from 'trans-render/lib/types';
import {lispToCamel} from 'trans-render/lib/lispToCamel.js';
export function attachAndResolve(target: Element, behaviorName: string, defaultVals?: any): Promise<void>{
return new Promise(async resolve => {
const ifWantsToBe = behaviorName.replace('be-', '');
const camel = lispToCamel(ifWantsToBe);
const vals = defaultVals === undefined ? {} : defaultVals;
if((<any>target).beDecorated === undefined){
(<any>target).beDecorated = {}
}
const bed = (<any>target).beDecorated;
if(bed[camel] === undefined){
bed[camel] === vals;
}else{
Object.assign(bed[camel], vals);
}
const bc = bed[camel];
if(bc.self === target){
resolve();
return;
}
target.addEventListener('be-decorated.' + ifWantsToBe + '.resolved', e => {
resolve();
}, {once: true});
await customElements.whenDefined(behaviorName);
const beH = document.createElement(behaviorName) as any as Attachable;
beH.attach(target);
});
}