/
xtal-tree-deco.ts
136 lines (127 loc) · 4.8 KB
/
xtal-tree-deco.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
127
128
129
130
131
132
133
134
135
import {decorate, } from 'trans-render/plugins/decorate.js';
import {XtalDeco, propActions } from 'xtal-deco/xtal-deco.js';
import { DecorateArgs } from 'trans-render/types.d.js';
import {define, PropAction, AttributeProps} from 'xtal-element/xtal-latx.js';
function getStrVal(el: HTMLElement) : string {
switch (el.localName) {
case 'details':
return el.querySelector('summary')!.textContent as string;
default:
return el.textContent as string;
}
}
// interface ExtendedHTMLDetailsElement extends HTMLDetailsElement{
// self: ExtendedHTMLDetailsElement;
// }
const init = ({self}: any) => {
self.allExpanded = false;
self.allCollapsed = false;
self.searchString = null;
self.sortDir = null;
};
const actions = [
({allExpanded, self} : any) => {
if(!allExpanded) return;
if (allExpanded) {
const t0 = performance.now();
self.open = true;
self.querySelectorAll('details').forEach(details => details.open = true);
const t1 = performance.now();
console.log(t1 - t0 + ' milliseconds');
}
self.allCollapsed = false;
},
({allCollapsed, self}: any) => {
if(!allCollapsed) return;
if (allCollapsed) {
self.removeAttribute('open');
self.querySelectorAll('details').forEach((details: any) => details.removeAttribute('open'));
}
self.allExpanded = false;
},
({searchString, self}: any) => {
const t0 = performance.now();
if (searchString === undefined || searchString === null || searchString === '')
return;
self.allCollapsed = true;
const newValLC = searchString.toLowerCase();
const tNodes = Array.from(self.querySelectorAll('div, summary'));
tNodes.forEach((el: any) => {
if (el.textContent!.toLowerCase().indexOf(newValLC) > -1) {
el.classList.add('match');
}
else {
el.classList.remove('match');
}
});
Array.from(self.querySelectorAll('details')).forEach((detailsEl: any) =>{
if(detailsEl.querySelector('.match') !== null) detailsEl.open = true;
});
const firstMatch = self.querySelector('.match');
if(firstMatch !== null){
self.open = true;
firstMatch.scrollIntoView();
}
const t1 = performance.now();
console.log(t1 - t0 + ' milliseconds');
},
({sortDir, self}: any) => {
if (sortDir === undefined) return;
Array.from(self.querySelectorAll('section')).forEach((section: any) =>{
const sectionChildren = Array.from(section.children);
const one = sortDir === 'asc' ? 1 : -1;
const minusOne = sortDir === 'asc' ? -1 : 1;
sectionChildren.sort((a: any, b: any) => {
const lhs = getStrVal(a);
const rhs = getStrVal(b);
if (lhs < rhs)
return minusOne;
if (lhs > rhs)
return one;
return 0;
});
let count = 1;
sectionChildren.forEach(child => {
const child2 = child as HTMLElement;
const count$ = count.toString();
child2.style.order = count$;
child2.tabIndex = parseInt(count$);
count++;
});
})
}
]
export const treePropActions = [ ...propActions,
({allExpanded, mainProxy}: XtalTreeDeco) => {
if(mainProxy === undefined) return;
(<any>mainProxy).allExpanded = allExpanded;
},
({allCollapsed, mainProxy}: XtalTreeDeco) => {
if(mainProxy === undefined) return;
(<any>mainProxy).allCollapsed = allCollapsed;
},
({searchString, mainProxy}: XtalTreeDeco) => {
if(mainProxy === undefined) return;
(<any>mainProxy).searchString = searchString;
},
({sortDir, mainProxy}: XtalTreeDeco) => {
if(mainProxy === undefined) return;
(<any>mainProxy).sortDir = sortDir;
}
] as PropAction<any>[];
export class XtalTreeDeco<ExtendedHTMLDetailsElement = HTMLDetailsElement> extends XtalDeco {
static is = 'xtal-tree-deco';
static attributeProps: any = ({allExpanded, allCollapsed, searchString, sortDir}: XtalTreeDeco) => ({
bool: [allExpanded, allCollapsed],
str: [searchString, sortDir]
} as AttributeProps);
init = init as PropAction; //TODO -- figure out how to de-any-fy
actions = actions as PropAction<any>[];
virtualProps = ['allExpanded', 'allCollapsed', 'searchString'];
propActions = treePropActions;
allExpanded: boolean | undefined;
allCollapsed: boolean | undefined;
searchString: string | undefined;
sortDir: string | undefined;
}
define(XtalTreeDeco);