-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccordion-container.html
103 lines (87 loc) · 2.45 KB
/
accordion-container.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="collapsible-container.html">
<!--
`accordion-container`
Polymer 1.X and 2.X accordion container
### Usage
```html
<accordion-container>
<collapsible-container header="Test">
Content goes here
</collapsible-container>
<collapsible-container header="Test 2">
Content goes here
</collapsible-container>
<collapsible-container header="Test 3">
Content goes here
</collapsible-container>
</accordion-container>
```
@demo demo/accordion.html
-->
<dom-module id="accordion-container">
<template>
<style>
:host {
display: block;
}
</style>
<slot></slot>
</template>
<script>
Polymer({
is: "accordion-container",
_collapsibleToggled: function(evt, self) {
var containers = self.getAllCollapsibles();
if (containers && containers.length) {
for (var i = 0; i < containers.length; i++) {
var item = containers[i];
if (item.open && item != evt.target) {
item.close();
}
}
}
},
attached: function() {
var self = this;
var containers = this.getAllCollapsibles();
if (!containers || containers.length === 0) {
throw 'No collapsible-containers could be found inside ' + this + ' !';
}
if (!this.getOpenCollapsible()) {
containers[0].set('open', true);
}
if (containers && containers.length) {
for (var i = 0; i < containers.length; i++) {
var item = containers[i];
item.addEventListener('collapsibleopened', function (evt) {
self._collapsibleToggled(evt, self);
}, false);
}
}
},
/**
* Get array of all the `collapsible-container` nodes
* @return Array
*/
getAllCollapsibles: function() {
return this.querySelectorAll('collapsible-container');
},
/**
* Get the current open `collapsible-container` node
* @return collapsible-container
*/
getOpenCollapsible: function() {
var containers = this.getAllCollapsibles();
var container = null;
for (var i = 0; i < containers.length; i++) {
if (containers[i].open) {
container = containers[i];
break;
}
}
return container;
}
});
</script>
</dom-module>