-
Notifications
You must be signed in to change notification settings - Fork 0
/
plastik-collapsible.html
executable file
·361 lines (318 loc) · 10.6 KB
/
plastik-collapsible.html
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!--
Copyright (c) 2015 The Plastikit Project Authors. All rights reserved.
This code may only be used under the BSD 3-clause license found at
https://plastikit.github.io/LICENSE.txt
The complete set of authors may be found at
https://plastikit.github.io/AUTHORS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-selector/iron-multi-selectable.html">
<!--
`plastik-collapsible` is a list of collapsible and expandable items. It extends
`iron-selector`, and thus supports the same functionality, with the exception of
the ability to change the `selectable` attribute.
Items must be tagged with the `.collapsible-item` class, and must have two
children: an `.item-header` and an `.item-content`.
`plastik-collapsible` is minimally styled and therefore must have styles applied
to it. The element will only give itself borders between and around items, style
backgrounds, and style the element's overall layout.
Additionally, `plastik-collapsible` does not require explicitly defined knowledge
of the header heights and will calculate them automatically and individually.
All of this was done to allow for ample flexibility.
### Example
```html
<plastik-collapsible>
<div class="collapsible-item">
<div class="item-header">Header</div>
<div class="item-content">
...
</div>
</div>
<div class="collapsible-item">
<div class="item-header">Header</div>
<div class="item-content">
...
</div>
</div>
</plastik-collapsible>
```
## Multi-selection
Just like `iron-selector`, `plastik-collapsible` supports multi-selection. When
multi-selection is enabled, more than one item can be expanded at a time. Simply
add the `multi` attribute or set the `multi` property to true.
### Example
```html
<plastik-collapsible multi>
<div class="collapsible-item">
<div class="item-header">Header</div>
<div class="item-content">
...
</div>
</div>
<div class="collapsible-item">
<div class="item-header">Header</div>
<div class="item-content">
...
</div>
</div>
</plastik-collapsible>
```
## Styling
`plastik-collapsible` applies the `.item-expanded` CSS class by default to any
items which have been expanded. Collapsed items do not have any special class
applied to them.
### Example
```html
<dom-module id="my-custom-element">
<style>
:host {
.item-expanded {
background-color: #B3E5FC;
color: #000;
};
}
</style>
<template>
<plastik-collapsible multi>
<div class="collapsible-item">
<div class="item-header">Header</div>
<div class="item-content">
...
</div>
</div>
<div class="collapsible-item">
<div class="item-header">Header</div>
<div class="item-content">
...
</div>
</div>
</plastik-collapsible>
...
```
Be aware that because `plastik-collapsible` does not rely on statically supplied
header heights, and instead calculates them when items are collapsed or expanded,
that if you change any style or content such that it affects an item's height,
you must call `updateCollapseStyles()` to recalculate the heights appropriately.
If the change only affects an individual item, you can call
`updateCollapseStyles(value)` with the value (or index, if attrForSelected is
unspecified) of the specific item in question.
@group UI Elements
@element plastik-collapsible
@demo demo/index.html
-->
<dom-module id="plastik-collapsible">
<style is="custom-style">
:host, ::content .item-header {
display: -webkit-flex;
display: flex;
}
:host {
-webkit-flex-direction: column;
flex-direction: column;
}
::content .item-header {
background: #fff;
}
::content .collapsible-item {
border-bottom: 1px solid #ddd;
}
::content .collapsible-item:first-of-type {
border-top: 1px solid #ddd;
}
::content .collapsible-item {
outline: none;
overflow: hidden;
background: #fafafa;
transition: height ease-in-out .38s;
}
::content .item-header {
cursor: pointer;
-webkit-user-select: none;
user-select: none;
outline: none;
}
::content .item-header:focus, ::content .item-header:hover {
background: #eee;
}
</style>
<template>
<content></content>
</template>
<script>
Polymer({
is: 'plastik-collapsible',
behaviors: [
Polymer.IronMultiSelectableBehavior
],
properties: {
/**
* The CSS selector that matches collapsible items. This property can
* not be changed.
*
* @attribute selectable
* @type {string}
*/
selectable: {
type: String,
readOnly: true,
value: '.collapsible-item'
},
/**
* Gets or sets the CSS selector that matches collapsible items which
* have been expanded.
*
* @attribute selectedClass
* @type {string}
*/
selectedClass: {
type: String,
value: 'item-expanded'
}
},
listeners: {
'iron-select': '_itemSelected',
'iron-deselect': '_itemDeselected'
},
/**
* Expands or collapses the item with the given value, as appropriate.
*
* @method select
* @param {*} value The value of the item to select.
*/
select: function (value) {
if (!this.multi && this.selected === value) {
this.selected = null;
} else {
Polymer.IronMultiSelectableBehaviorImpl.select.call(this, value);
}
},
/**
* Iterates through all the items (or the item which value matches the one
* given) and sets their height to the appropriate size based on whether or
* not they are supposed to be expanded or collapsed.
*
* Use this if you change any CSS or styling that would affect the sizes of
* the items.
*
* @method select
* @param {*} value (optional) The value of the item of which style to update.
*/
updateCollapseStyles: function (value) {
if (typeof value !== 'undefined') {
var item = this._valueToItem(value);
if (item) {
if (item.classList.contains(this.selectedClass)) {
item.style.height = this._getExpandedItemHeight(item);
} else {
item.style.height = this._getItemHeaderHeight(item);
}
}
} else {
for (var i = 0; i < this.items.length; i++) {
if (this.items[i].classList.contains(this.selectedClass)) {
this.items[i].style.height = this._getExpandedItemHeight(this.items[i]);
} else {
this.items[i].style.height = this._getItemHeaderHeight(this.items[i]);
}
}
}
},
_useScopeSelector: undefined,
_activateHandler: function(e) {
var t = e.target;
var items = this.items;
while (t && t != this) {
if (t.classList.contains('item-content') &&
t.parentNode.classList.contains('collapsible-item')) {
return;
}
var i = items.indexOf(t);
if (i >= 0) {
var value = this._indexToValue(i);
this._itemActivate(value, t);
return;
}
t = t.parentNode;
}
},
// Overloading IronSelectableBehavior's _observeItems
// to update the collapsed/expanded item styles whenever
// the DOM is mutated.
_observeItems: function(node) {
var observer = new MutationObserver(function() {
if (this.selected != null) {
this._updateSelected();
}
this.updateCollapseStyles();
}.bind(this));
observer.observe(node, {
childList: true,
subtree: true
});
return observer;
},
_itemSelected: function (e) {
e.detail.item.style.height = this._getExpandedItemHeight(e.detail.item);
},
_itemDeselected: function (e) {
e.detail.item.style.height = this._getItemHeaderHeight(e.detail.item);
},
ready: function () {
this._useScopeSelector = ((window.Plastikit || (window.Plastikit = {}))
.supported || (window.Plastikit.supported = {})).scopeSelector;
if (typeof this._useScopeSelector === 'undefined') {
var elem = document.createElement('div');
elem.appendChild(document.createElement('span'));
try {
this._useScopeSelector = window.Plastikit.supported.scopeSelector =
elem.querySelector(':scope > span') ? true : false;
} catch (err) {
this._useScopeSelector = window.Plastikit.supported.scopeSelector = false;
}
}
},
attached: function () {
this.updateCollapseStyles();
},
_getItemHeaderHeight: function (item) {
var itemHeader;
if (this._useScopeSelector) {
itemHeader = item.querySelector(':scope > .item-header');
} else {
for (var i = 0; i < item.childNodes.length; i++) {
if (item.childNodes[i].nodeType !== 1) {
continue;
}
if (item.childNodes[i].classList.contains('item-header')) {
itemHeader = item.childNodes[i];
break;
}
}
}
return itemHeader.offsetHeight + 'px';
},
_getExpandedItemHeight: function (item) {
var itemHeader, itemContent;
if (this._useScopeSelector) {
itemHeader = item.querySelector(':scope > .item-header');
itemContent = item.querySelector(':scope > .item-content');
} else {
for (var i = 0; i < item.childNodes.length; i++) {
if (item.childNodes[i].nodeType !== 1) {
continue;
}
if (item.childNodes[i].classList.contains('item-header')) {
itemHeader = item.childNodes[i];
} else if (item.childNodes[i].classList.contains('item-content')) {
itemContent = item.childNodes[i];
}
if (itemHeader && itemContent) {
break;
}
}
}
return itemHeader.offsetHeight + itemContent.offsetHeight + 'px';
}
});
</script>
</dom-module>