-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp-h-d.ts
62 lines (54 loc) · 1.7 KB
/
p-h-d.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
import {PDX} from './p-d-x.js';
import {define} from 'xtal-element/xtal-latx.js';
import {XtalStateWatch} from 'xtal-state/xtal-state-watch.js';
import { AttributeProps } from '../xtal-element/types.js';
export const doNotCCEventToState = 'dncc';
/**
* Pass history to downstream elements
* @element p-h-d
*/
export class PhD extends PDX{
static is = 'p-h-d';
static attributeProps: any = ({initAndPopStateOnly, fromPath} : PhD) => ({
bool: [initAndPopStateOnly],
str: [fromPath],
reflect: [fromPath, initAndPopStateOnly]
} as AttributeProps);
getDetail(val: any){
return {
value: val,
[doNotCCEventToState]: true
};
}
/**
* Only pass down history if is initial history and/or popstate event
* @attr init-and-pop-state-only
*/
initAndPopStateOnly!: boolean;
/**
* JS path within history.state to pass down.
* @attr from-path
*/
fromPath!: string
connectedCallback(){
super.connectedCallback();
const xtalWatch = document.createElement(XtalStateWatch.is) as XtalStateWatch;
xtalWatch.guid = this.guid;
if(this.val === undefined){
const path = this.fromPath === undefined ? '' : '.' + this.fromPath;
this.val = 'target.history.state' + path;
}
xtalWatch.addEventListener('history-changed', e =>{
const cei = e as CustomEventInit;
if(this.initAndPopStateOnly && !cei.detail.isInitialEvent && !cei.detail.isPopstate) return;
this.pass(e);
});
this.appendChild(xtalWatch);
}
}
define(PhD);
declare global {
interface HTMLElementTagNameMap {
"p-h-d": PhD,
}
}