-
Notifications
You must be signed in to change notification settings - Fork 0
/
collapsible.js
283 lines (234 loc) · 8.63 KB
/
collapsible.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* Collapsible - A plug and play plugin for expanding and
* collapsing elements (i.e. accordion) on a website.
*
* @author Murtada al Mousawy (https://murtada.nl)
*/
(function() {
'use strict';
/**
* Creates an instance of Collapsible.
*
* @constructor
* @param {Object} options
* @param {(HTMLElement|NodeList)} options.node The HTML elements that will be manipulated.
* @param {HTMLElement} [options.eventNode] The HTML element on which the eventListener will be attached.
* @param {Boolean} [options.isCollapsed] Assign the state of the node element.
* @param {Boolean} [options.observe] Assign a MutationObserver to observe child DOM changes.
* @param {Function} [options.expandCallback] Assign a callback for the [{@link Collapsible.prototype.expand} event.
* @param {Function} [options.collapseCallback] Assign a callback for the {@link Collapsible.prototype.collapse} event.
* @param {Function} [options.observeCallback] Assign a callback for the {@link Collapsible.prototype.initObserver} event.
*/
var Collapsible = function(options) {
// Initialize HTML nodes
if (NodeList.prototype.isPrototypeOf(options.node)) {
options.node.forEach(function(nodeItem) {
var singleNodeOptions = options;
singleNodeOptions.node = nodeItem;
new Collapsible(singleNodeOptions);
});
return;
} else if (options.node instanceof HTMLElement) {
this.node = options.node;
this.eventNode = (options.eventNode ? this.node.querySelector(options.eventNode) : this.node);
this.isCollapsed = (typeof this.node.dataset.collapsibleCollapsed !== 'undefined'
? true
: null);
if (!this.isCollapsed) {
this.isCollapsed = ((options.isCollapsed
&& typeof options.isCollapsed === 'boolean')
? options.isCollapsed
: false);
}
this.observe = (typeof options.observe === 'boolean' ? options.observe : false);
this.expandCallback = (typeof options.expandCallback === 'function' ? options.expandCallback : null);
this.collapseCallback = (typeof options.collapseCallback === 'function' ? options.collapseCallback : null);
this.observeCallback = (typeof options.observeCallback === 'function' ? options.observeCallback : null);
this.mutationCallback = (typeof options.mutationCallback === 'function' ? options.mutationCallback : null);
this.init();
} else {
console.error(options.node, 'is not a NodeList or an instance of HTMLElement');
}
};
/**
* Initialize the collapsing and expanding events.
*/
Collapsible.prototype.init = function() {
this.updateHeights();
if (this.isCollapsed) {
this.node.style.height = this.collapsedHeight + 'px';
this.node.classList.add('is-collapsed');
} else {
this.node.classList.add('is-expanded');
}
this.eventNode.addEventListener('click', function() {
this.toggleCollapse();
}.bind(this));
window.addEventListener('resize', this.updateHeights.bind(this, null));
// Observe children of the node
if (this.observe) {
this.initObserver();
}
// Attach the prototype instance to the node
this.node.collapsible = this;
};
/**
* Update the collapsed and expanded heights on page resize.
*
* @param {int} [heightDifference] Height value to add or subtract from the parent.
*/
Collapsible.prototype.updateHeights = function(heightDifference) {
heightDifference = heightDifference || 0;
// Calculate the collapsed height
this.collapsedHeight = Collapsible.parseNumber(
window.getComputedStyle(this.eventNode)['height']
);
// Calculate the expanded height
this.node.style.height = 'auto';
this.expandedHeight = Collapsible.parseNumber(
window.getComputedStyle(this.node)['height']
);
// Add or subtract the childNode's height difference
this.expandedHeight += heightDifference;
this.expandedHeight = Math.max(this.expandedHeight, this.collapsedHeight);
// Reset height to what it was before
if (this.isCollapsed) {
this.node.style.height = this.collapsedHeight + 'px';
}
this.updateParentNode(this.expandedHeight - this.collapsedHeight);
};
/**
* Toggle the node state and calls the appropriate function.
*/
Collapsible.prototype.toggleCollapse = function() {
if (this.isCollapsed) {
this.expand();
} else {
this.collapse();
}
};
/**
* Expand the node.
*/
Collapsible.prototype.expand = function() {
this.updateHeights();
void this.node.offsetWidth;
this.node.style.height = 'auto';
this.node.style.height = this.expandedHeight + 'px';
this.node.classList.remove('is-collapsed');
this.node.classList.add('is-expanded');
this.isCollapsed = false;
// Create a custom event
var expandEvent = new CustomEvent('toggle', {
bubbles: true,
detail: {
action: 'expand',
origin: this.eventNode
}
});
this.node.dispatchEvent(expandEvent);
// Run callback if it exists
if (this.expandCallback) {
this.expandCallback.bind(this, expandEvent)();
}
this.updateParentNode(this.expandedHeight - this.collapsedHeight);
};
/**
* Collapse the node.
*/
Collapsible.prototype.collapse = function() {
this.node.style.height = window.getComputedStyle(this.node)['height'];
void this.node.offsetWidth;
this.node.style.height = this.collapsedHeight + 'px';
this.node.classList.remove('is-expanded');
this.node.classList.add('is-collapsed');
this.isCollapsed = true;
// Create a custom event
var collapseEvent = new CustomEvent('toggle', {
bubbles: true,
detail: {
action: 'collapse',
origin: this.eventNode
}
});
this.node.dispatchEvent(collapseEvent);
// Run callback if it exists
if (this.collapseCallback) {
this.collapseCallback.bind(this, collapseEvent)();
}
this.updateParentNode(-(this.expandedHeight - this.collapsedHeight));
};
/**
* Update parent heights if collapsible.
*
* @see {@link Collapsible.prototype.updateHeights}
*/
Collapsible.prototype.updateParentNode = function(heightDifference) {
if (this.node.parentNode && this.node.parentNode.collapsible) {
this.node.parentNode.collapsible.updateHeights(heightDifference);
}
};
/**
* Observe the direct children list of the node.
* Will adjust the expanded height automatically if necessary.
*/
Collapsible.prototype.initObserver = function() {
this.mutationObserver = new window.MutationObserver(function(mutationsList) {
var mutatedNode,
mutationAction;
if (mutationsList[0]['addedNodes'].length > 0) {
mutationAction = 'add';
mutatedNode = mutationsList[0]['addedNodes'][0];
} else {
mutationAction = 'remove';
mutatedNode = mutationsList[0]['removedNodes'][0];
}
if (!this.isCollapsed) {
var mutatedNodeStyle = window.getComputedStyle(mutatedNode);
var mutatedNodeHeight =
Collapsible.parseNumber(
mutatedNodeStyle['height']) +
Collapsible.parseNumber(
mutatedNodeStyle['margin-top']) +
Collapsible.parseNumber(
mutatedNodeStyle['margin-bottom']);
this.node.style.height = 'auto';
var currentHeight = Collapsible.parseNumber(
window.getComputedStyle(this.node)['height']
);
if (mutationAction == 'add') {
this.node.style.height = (currentHeight - mutatedNodeHeight) + 'px';
void this.node.offsetWidth;
this.node.style.height = currentHeight + 'px';
} else {
this.node.style.height = this.expandedHeight + 'px';
void this.node.offsetWidth;
this.node.style.height = currentHeight + 'px';
}
}
this.expandedHeight = currentHeight;
// Create a custom event
var mutationEvent = new CustomEvent('mutate', {
bubbles: true,
detail: {
action: mutationAction,
node: mutatedNode
}
});
this.node.dispatchEvent(mutationEvent);
// Run callback if it exists
if (this.mutationCallback) {
this.mutationCallback.bind(this, mutationEvent)();
}
}.bind(this));
this.mutationObserver.observe(this.node, {
childList: true
});
};
// Helper functions
Collapsible.parseNumber = function(numberString) {
return Number.parseInt(numberString.slice(0, -2));
};
// Expose the prototype function to the global scope
window.Collapsible = Collapsible;
})();