OLD | NEW |
1 | 1 |
2 | 2 |
3 Polymer({ | 3 Polymer({ |
4 | 4 |
5 is: 'iron-collapse', | 5 is: 'iron-collapse', |
6 | 6 |
7 properties: { | 7 properties: { |
8 | 8 |
9 /** | 9 /** |
10 * If true, the orientation is horizontal; otherwise is vertical. | 10 * If true, the orientation is horizontal; otherwise is vertical. |
11 * | 11 * |
12 * @attribute horizontal | 12 * @attribute horizontal |
13 * @type Boolean | |
14 * @default false | |
15 */ | 13 */ |
16 horizontal: { | 14 horizontal: { |
17 type: Boolean, | 15 type: Boolean, |
18 value: false, | 16 value: false, |
19 observer: 'horizontalChanged' | 17 observer: '_horizontalChanged' |
20 }, | 18 }, |
21 | 19 |
22 /** | 20 /** |
23 * Set opened to true to show the collapse element and to false to hide it
. | 21 * Set opened to true to show the collapse element and to false to hide it
. |
24 * | 22 * |
25 * @attribute opened | 23 * @attribute opened |
26 * @type Boolean | |
27 * @default false | |
28 */ | 24 */ |
29 opened: { | 25 opened: { |
30 type: Boolean, | 26 type: Boolean, |
31 value: false, | 27 value: false, |
32 notify: true, | 28 notify: true, |
33 observer: 'openedChanged' | 29 observer: '_openedChanged' |
34 } | 30 } |
35 | 31 |
36 }, | 32 }, |
37 | 33 |
| 34 hostAttributes: { |
| 35 role: 'group', |
| 36 'aria-expanded': 'false', |
| 37 tabindex: 0 |
| 38 }, |
| 39 |
38 listeners: { | 40 listeners: { |
39 transitionend: 'transitionEnd' | 41 transitionend: '_transitionEnd' |
40 }, | 42 }, |
41 | 43 |
42 ready: function() { | 44 ready: function() { |
43 // Avoid transition at the beginning e.g. page loads and enable | 45 // Avoid transition at the beginning e.g. page loads and enable |
44 // transitions only after the element is rendered and ready. | 46 // transitions only after the element is rendered and ready. |
45 this._enableTransition = true; | 47 this._enableTransition = true; |
46 }, | 48 }, |
47 | 49 |
48 horizontalChanged: function() { | |
49 this.dimension = this.horizontal ? 'width' : 'height'; | |
50 this.style.transitionProperty = this.dimension; | |
51 }, | |
52 | |
53 openedChanged: function() { | |
54 this[this.opened ? 'show' : 'hide'](); | |
55 }, | |
56 | |
57 /** | 50 /** |
58 * Toggle the opened state. | 51 * Toggle the opened state. |
59 * | 52 * |
60 * @method toggle | 53 * @method toggle |
61 */ | 54 */ |
62 toggle: function() { | 55 toggle: function() { |
63 this.opened = !this.opened; | 56 this.opened = !this.opened; |
64 }, | 57 }, |
65 | 58 |
66 show: function() { | 59 show: function() { |
67 this.toggleClass('iron-collapse-closed', false); | 60 this.toggleClass('iron-collapse-closed', false); |
68 this.updateSize('auto', false); | 61 this.updateSize('auto', false); |
69 var s = this.calcSize(); | 62 var s = this._calcSize(); |
70 this.updateSize('0px', false); | 63 this.updateSize('0px', false); |
71 // force layout to ensure transition will go | 64 // force layout to ensure transition will go |
72 this.offsetHeight; | 65 this.offsetHeight; |
73 this.updateSize(s, true); | 66 this.updateSize(s, true); |
74 }, | 67 }, |
75 | 68 |
76 hide: function() { | 69 hide: function() { |
77 this.toggleClass('iron-collapse-opened', false); | 70 this.toggleClass('iron-collapse-opened', false); |
78 this.updateSize(this.calcSize(), false); | 71 this.updateSize(this._calcSize(), false); |
79 // force layout to ensure transition will go | 72 // force layout to ensure transition will go |
80 this.offsetHeight; | 73 this.offsetHeight; |
81 this.updateSize('0px', true); | 74 this.updateSize('0px', true); |
82 }, | 75 }, |
83 | 76 |
84 updateSize: function(size, animated) { | 77 updateSize: function(size, animated) { |
85 this.enableTransition(animated); | 78 this.enableTransition(animated); |
86 var s = this.style; | 79 var s = this.style; |
87 var nochange = s[this.dimension] === size; | 80 var nochange = s[this.dimension] === size; |
88 s[this.dimension] = size; | 81 s[this.dimension] = size; |
89 if (animated && nochange) { | 82 if (animated && nochange) { |
90 this.transitionEnd(); | 83 this._transitionEnd(); |
91 } | 84 } |
92 }, | 85 }, |
93 | 86 |
94 calcSize: function() { | |
95 return this.getBoundingClientRect()[this.dimension] + 'px'; | |
96 }, | |
97 | |
98 enableTransition: function(enabled) { | 87 enableTransition: function(enabled) { |
99 this.style.transitionDuration = (enabled && this._enableTransition) ? '' :
'0s'; | 88 this.style.transitionDuration = (enabled && this._enableTransition) ? '' :
'0s'; |
100 }, | 89 }, |
101 | 90 |
102 transitionEnd: function() { | 91 _horizontalChanged: function() { |
| 92 this.dimension = this.horizontal ? 'width' : 'height'; |
| 93 this.style.transitionProperty = this.dimension; |
| 94 }, |
| 95 |
| 96 _openedChanged: function() { |
| 97 this[this.opened ? 'show' : 'hide'](); |
| 98 this.setAttribute('aria-expanded', this.opened ? 'true' : 'false'); |
| 99 |
| 100 }, |
| 101 |
| 102 _transitionEnd: function() { |
103 if (this.opened) { | 103 if (this.opened) { |
104 this.updateSize('auto', false); | 104 this.updateSize('auto', false); |
105 } | 105 } |
106 this.toggleClass('iron-collapse-closed', !this.opened); | 106 this.toggleClass('iron-collapse-closed', !this.opened); |
107 this.toggleClass('iron-collapse-opened', this.opened); | 107 this.toggleClass('iron-collapse-opened', this.opened); |
108 this.enableTransition(false); | 108 this.enableTransition(false); |
109 } | 109 }, |
| 110 |
| 111 _calcSize: function() { |
| 112 return this.getBoundingClientRect()[this.dimension] + 'px'; |
| 113 }, |
| 114 |
110 | 115 |
111 }); | 116 }); |
112 | 117 |
OLD | NEW |