-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathon-to-me.ts
68 lines (63 loc) · 2.26 KB
/
on-to-me.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
import {asAttr} from './types.d.js';
import {convert, getProp} from './prop-mixin.js';
import {passVal} from './to-mixin.js';
import {nudge, getPreviousSib} from './on-mixin.js';
export class OnToMe extends HTMLElement {
//_lastEvent: Event
_lastVal: any;
_g: any;
connectedCallback(){
this.style.display = 'none';
const g = this._g = this.getAttribute.bind(this);
const elToObserve = getPreviousSib(this, g('observe'))!;
nudge(elToObserve);
elToObserve.addEventListener(g('on')!, (e:Event) => {
e.stopPropagation();
this.getVal(e);
});
const mutateEvent = g('mutate-event');
if(mutateEvent !== null) this.parentElement?.addEventListener(mutateEvent, (e:Event) => {
this.putVal();
});
const initVal = g('init-val');
if(initVal !== null){
this.setInit(elToObserve, initVal);
const initEvent = g('init-event');
if(initEvent !== null){
elToObserve.addEventListener(initEvent, (e:Event) => {
this.setInit(elToObserve, initVal);
}, {once: true});
}
}
}
setInit(elToObserve: Element, initVal: string){
const g = this._g;
let val = getProp(elToObserve, initVal.split('.'), this);
val = convert(val, g('parse-val-as'));
this._lastVal = val;
const me = g('me');
const m = me === null ? Infinity : parseInt(me);
passVal(val, this, g('to')!, g('care-of'), m, g('from'), g('prop'), g('as') as asAttr);
}
getVal(lastEvent: Event){
let val = getProp(lastEvent, this._g('val')?.split('.'), this);
if(val === undefined) return;
val = convert(val, this._g('parse-val-as'));
this._lastVal = val;
this.putVal();
}
putVal(){
if(this._lastVal === undefined) return;
const g = this._g;
const to = this._g('to')!;
const careOf = this._g('care-of');
passVal(this._lastVal, this, g('to')!, g('care-of'), g('me'), g('from'), g('prop'), g('as'));
}
}
const otm = 'on-to-me';
if(!customElements.get(otm)) customElements.define(otm, OnToMe);
declare global {
interface HTMLElementTagNameMap {
[otm]: OnToMe,
}
}