-
Notifications
You must be signed in to change notification settings - Fork 957
Expand file tree
/
Copy pathcollapsableList.js
More file actions
155 lines (143 loc) · 3.35 KB
/
collapsableList.js
File metadata and controls
155 lines (143 loc) · 3.35 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import tag from "html-tag-js";
import tile from "./tile";
/**
* @typedef {object} CollapsibleBase
* @property {HTMLElement} $title
* @property {HTMLUListElement} $ul
* @property {function(void):void} ontoggle
* @property {function(void):void} collapse
* @property {function(void):void} expand
* @property {boolean} collapsed
* @property {boolean} unclasped
*/
/**
* @typedef {CollapsibleBase & HTMLElement} Collapsible
*/
/**
* Create a collapsible list
* @param {string} titleText Title of the list
* @param {boolean} hidden If true, the list will be hidden
* @param {"indicator"|"folder"} type Type of the list toggle indicator
* @param {object} [options] Configuration options
* @param {HTMLElement} [options.tail] Tail element of the title
* @param {string} [options.type] Type of the list element
* @param {boolean} [options.allCaps] If true, the title will be in all caps
* @param {function(this:Collapsible):void} [options.ontoggle] Called when the list is toggled
* @returns {Collapsible}
*/
export default function collapsableList(
titleText,
type = "indicator",
options = {},
) {
let onscroll = null;
const $ul = tag("ul", {
className: "scroll",
onscroll: onUlScroll,
});
const $collapseIndicator = tag("span", {
className: `icon ${type}`,
});
const $title = tile({
lead: $collapseIndicator,
type: "div",
text: options.allCaps ? titleText.toUpperCase() : titleText,
tail: options.tail,
});
const $mainWrapper = tag(options.type || "div", {
className: "list collapsible hidden",
children: [$title, $ul],
});
let collapse = () => {
$mainWrapper.classList.add("hidden");
if ($mainWrapper.ontoggle) $mainWrapper.ontoggle.call($mainWrapper);
delete $ul.dataset.scrollTop;
};
let expand = () => {
$mainWrapper.classList.remove("hidden");
if ($mainWrapper.ontoggle) $mainWrapper.ontoggle.call($mainWrapper);
};
$title.classList.add("light");
$title.addEventListener("click", toggle);
[$title, $mainWrapper].forEach(defineProperties);
$mainWrapper.dataset.id = `${Math.random().toString(36).substring(2, 15)}`;
return $mainWrapper;
function onUlScroll() {
if (onscroll) onscroll.call($ul);
$ul.dataset.scrollTop = $ul.scrollTop;
}
function toggle() {
if ($title.collapsed) {
expand();
} else {
collapse();
}
}
function defineProperties($el) {
Object.defineProperties($el, {
$title: {
get() {
return $title;
},
},
$ul: {
get() {
return $ul;
},
},
ontoggle: {
get() {
return options.ontoggle;
},
set(fun) {
if (typeof fun === "function") options.ontoggle = fun;
},
},
collapse: {
get() {
return collapse || (() => {});
},
set(fun) {
if (typeof fun === "function") collapse = fun;
},
},
expand: {
get() {
return expand || (() => {});
},
set(fun) {
if (typeof fun === "function") expand = fun;
},
},
collapsed: {
get() {
return $mainWrapper.classList.contains("hidden");
},
},
unclasped: {
get() {
return !this.collapsed;
},
},
onscroll: {
get() {
return onscroll;
},
set(fun) {
if (typeof fun === "function") {
onscroll = fun;
}
},
},
scrollTop: {
get() {
return $ul.dataset.scrollTop || 0;
},
set(val) {
$ul.dataset.scrollTop = val;
$ul.scrollTop = val;
},
},
});
}
}