-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-wandering.js
64 lines (64 loc) · 1.99 KB
/
be-wandering.js
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
import { define } from 'be-decorated/be-decorated.js';
import { getElementToObserve, addListener } from 'be-observant/be-observant.js';
export class BeWanderingController {
target;
intro(proxy, target, beDecorProps) {
this.target = target;
}
scout({ pathObserver }) {
let observeParams;
switch (typeof pathObserver) {
case 'string':
observeParams = { vft: pathObserver, prop: 'selector' };
break;
case 'object':
observeParams = pathObserver;
break;
}
const elementToObserve = getElementToObserve(this.proxy, observeParams);
if (elementToObserve === null) {
console.warn({ observeParams, msg: '404' });
return;
}
addListener(elementToObserve, observeParams, 'selector', this.proxy);
}
wander({ selector, insertPosition, target }) {
const relativeTo = this.proxy.getRootNode().querySelector(selector);
if (relativeTo === null) {
console.warn({ selector, msg: '404' });
return;
}
switch (insertPosition) {
case 'beforeend':
if (this.target === relativeTo.lastElementChild)
return;
break;
}
relativeTo.insertAdjacentElement(insertPosition, target);
}
}
const tagName = 'be-wandering';
define({
config: {
tagName,
propDefaults: {
upgrade: '*',
ifWantsToBe: 'wandering',
forceVisible: true,
virtualProps: ['insertPosition', 'pathObserver', 'selector'],
intro: 'intro'
},
actions: {
'scout': {
ifAllOf: ['pathObserver']
},
'wander': {
ifAllOf: ['selector', 'insertPosition']
}
}
},
complexPropDefaults: {
controller: BeWanderingController
}
});
document.head.appendChild(document.createElement(tagName));