-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-detail-oriented.ts
126 lines (115 loc) · 4.11 KB
/
be-detail-oriented.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {BE, propDefaults, propInfo} from 'be-enhanced/BE.js';
import {BEConfig} from 'be-enhanced/types';
import {XE} from 'xtal-element/XE.js';
import {Actions, AllProps, AP, PAP, ProPAP, POA, ProPOA} from './types';
import {register} from 'be-hive/register.js';
import {AP as beImpAP} from 'be-importing/types';
export class BeDetailOriented extends BE<AP, Actions> implements Actions{
static override get beConfig(){
return {
parse: true,
} as BEConfig;
}
#plusMinus: WeakRef<Element> | undefined;
async defineExpander(self: this): ProPOA {
import('xtal-element/xtal-element.js');
import('be-importing/behivior.js');
const {summaryElSelector, enhancedElement, expanderPlacement, plusMinusFrom} = self;
const summaryEl = enhancedElement.querySelector(summaryElSelector!);
if(summaryEl === null) throw {msg: '404', summaryElSelector};
if(customElements.get('plus-minus') === undefined){
//TODO: figure out why this is necessary.
const plusMinusDefiner = document.createElement('plus-minus');
plusMinusDefiner.setAttribute('enh-be-importing', plusMinusFrom!);
plusMinusDefiner.style.display = 'none';
document.body.appendChild(plusMinusDefiner);
}
await customElements.whenDefined('plus-minus');
let plusMinus = summaryEl.querySelector('plus-minus');
let alreadyExisted = true;
if(plusMinus === null){
plusMinus = document.createElement('plus-minus');
alreadyExisted = false;
}
if(!alreadyExisted){
const verb = expanderPlacement === 'left' ? 'prepend' : 'appendChild';
(<any>summaryEl)[verb](plusMinus);
}
if(enhancedElement.id === ''){
enhancedElement.id = crypto.randomUUID();
}
plusMinus.setAttribute('aria-owns', enhancedElement.id);
this.#plusMinus = new WeakRef(plusMinus);
return [{
resolved: true
}, {
toggleExpander: {
on: 'expanded-changed',
of: plusMinus,
doInit: true,
}
}]
}
toggleExpander(self: this, e?: CustomEvent<any> | undefined): PAP {
const open = e?.detail.value;
return {
open
}
}
modifyVisibility(self: this): void {
const {enhancedElement, open, summaryElSelector, openCss, openPart} = self;
if(this.#plusMinus !== undefined){
const plusMinus = this.#plusMinus.deref();
if(plusMinus !== undefined){
(<any>plusMinus).expanded = open;
}
}
const {children} = enhancedElement;
const summaryEl = enhancedElement.querySelector(summaryElSelector!);
for(const child of children){
if(child === summaryEl) continue;
if(open){
(<any>child).hidden = false;
}else{
(<any>child).hidden = 'until-found';
}
}
const verb = open ? 'add' : 'remove';
enhancedElement.classList[verb](openCss!);
enhancedElement.part[verb](openPart!);
}
}
export interface BeDetailOriented extends AllProps{}
const tagName = 'be-detail-oriented';
const ifWantsToBe = 'detail-oriented';
const upgrade = '*';
const xe = new XE<AP, Actions>({
config:{
tagName,
propDefaults:{
...propDefaults,
plusMinusFrom: 'plus-minus/root.html',
openCss: 'detail-oriented-open',
openPart: 'detail-oriented-open',
expanderPlacement: 'left',
summaryElSelector: '*',
open: false,
},
propInfo:{
...propInfo,
open:{
notify:{
dispatchFromEnhancedElement: true,
}
}
},
actions:{
defineExpander: 'summaryElSelector',
modifyVisibility:{
ifKeyIn: ['open'],
}
}
},
superclass: BeDetailOriented
});
register(ifWantsToBe, upgrade, tagName);