OLD | NEW |
1 <!-- | 1 <!-- |
2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | 2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. |
3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | 3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | 4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | 5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
6 Code distributed by Google as part of the polymer project is also | 6 Code distributed by Google as part of the polymer project is also |
7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | 7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
8 --> | 8 --> |
9 | 9 |
10 <!-- | 10 <!-- |
11 `core-collapse` creates a collapsible block of content. By default, the content | 11 `core-collapse` creates a collapsible block of content. By default, the content |
12 will be collapsed. Use `opened` to show/hide the content. | 12 will be collapsed. Use `opened` or `toggle()` to show/hide the content. |
13 | 13 |
14 <button on-click="{{toggle}}">toggle collapse</button> | 14 <button on-click="{{toggle}}">toggle collapse</button> |
15 | 15 |
16 <core-collapse id="collapse"> | 16 <core-collapse id="collapse"> |
17 ... | 17 Content goes here... |
18 </core-collapse> | 18 </core-collapse> |
19 | 19 |
20 ... | 20 ... |
21 | 21 |
22 toggle: function() { | 22 toggle: function() { |
23 this.$.collapse.toggle(); | 23 this.$.collapse.toggle(); |
24 } | 24 } |
| 25 |
| 26 `core-collapse` adjusts the height/width of the collapsible element to show/hide |
| 27 the content. So avoid putting padding/margin/border on the collapsible directly
, |
| 28 and instead put a div inside and style that. |
| 29 |
| 30 <style> |
| 31 .collapse-content { |
| 32 padding: 15px; |
| 33 border: 1px solid #dedede; |
| 34 } |
| 35 </style> |
| 36 |
| 37 <core-collapse> |
| 38 <div class="collapse-content"> |
| 39 Content goes here... |
| 40 </div> |
| 41 </core-collapse> |
25 | 42 |
26 @group Polymer Core Elements | 43 @group Polymer Core Elements |
27 @element core-collapse | 44 @element core-collapse |
28 --> | 45 --> |
29 | 46 |
30 <link rel="import" href="../polymer/polymer.html"> | 47 <link rel="import" href="../polymer/polymer.html"> |
31 | 48 |
32 <link rel="stylesheet" href="core-collapse.css" shim-shadowdom> | 49 <link rel="stylesheet" href="core-collapse.css" shim-shadowdom> |
33 | 50 |
34 <polymer-element name="core-collapse" attributes="target horizontal opened durat
ion fixedSize"> | 51 <polymer-element name="core-collapse" attributes="target horizontal opened durat
ion fixedSize allowOverflow"> |
35 <template> | 52 <template> |
36 | 53 |
37 <content></content> | 54 <content></content> |
38 | 55 |
39 </template> | 56 </template> |
40 <script> | 57 <script> |
41 | 58 |
42 Polymer('core-collapse', { | 59 Polymer('core-collapse', { |
43 | 60 |
44 /** | 61 /** |
45 * Fired when the `core-collapse`'s `opened` property changes. | 62 * Fired when the `core-collapse`'s `opened` property changes. |
46 * | 63 * |
47 * @event core-collapse-open | 64 * @event core-collapse-open |
48 */ | 65 */ |
49 | 66 |
50 /** | 67 /** |
51 * Fired when the target element has been resized as a result of the opened | 68 * Fired when the target element has been resized as a result of the opened |
52 * state changing. | 69 * state changing. |
53 * | 70 * |
54 * @event core-resize | 71 * @event core-resize |
55 */ | 72 */ |
56 | 73 |
57 /** | 74 /** |
58 * The target element. | 75 * The target element that will be opened when the `core-collapse` is |
| 76 * opened. If unspecified, the `core-collapse` itself is the target. |
59 * | 77 * |
60 * @attribute target | 78 * @attribute target |
61 * @type object | 79 * @type object |
62 * @default null | 80 * @default null |
63 */ | 81 */ |
64 target: null, | 82 target: null, |
65 | 83 |
66 /** | 84 /** |
67 * If true, the orientation is horizontal; otherwise is vertical. | 85 * If true, the orientation is horizontal; otherwise is vertical. |
68 * | 86 * |
(...skipping 25 matching lines...) Expand all Loading... |
94 * If true, the size of the target element is fixed and is set | 112 * If true, the size of the target element is fixed and is set |
95 * on the element. Otherwise it will try to | 113 * on the element. Otherwise it will try to |
96 * use auto to determine the natural size to use | 114 * use auto to determine the natural size to use |
97 * for collapsing/expanding. | 115 * for collapsing/expanding. |
98 * | 116 * |
99 * @attribute fixedSize | 117 * @attribute fixedSize |
100 * @type boolean | 118 * @type boolean |
101 * @default false | 119 * @default false |
102 */ | 120 */ |
103 fixedSize: false, | 121 fixedSize: false, |
| 122 |
| 123 /** |
| 124 * By default the collapsible element is set to overflow hidden. This helps |
| 125 * avoid element bleeding outside the region and provides consistent overflo
w |
| 126 * style across opened and closed states. Set this property to true to allow
|
| 127 * the collapsible element to overflow when it's opened. |
| 128 * |
| 129 * @attribute allowOverflow |
| 130 * @type boolean |
| 131 * @default false |
| 132 */ |
| 133 allowOverflow: false, |
104 | 134 |
105 created: function() { | 135 created: function() { |
106 this.transitionEndListener = this.transitionEnd.bind(this); | 136 this.transitionEndListener = this.transitionEnd.bind(this); |
107 }, | 137 }, |
108 | 138 |
109 ready: function() { | 139 ready: function() { |
110 this.target = this.target || this; | 140 this.target = this.target || this; |
111 }, | 141 }, |
112 | 142 |
113 domReady: function() { | 143 domReady: function() { |
(...skipping 10 matching lines...) Expand all Loading... |
124 | 154 |
125 targetChanged: function(old) { | 155 targetChanged: function(old) { |
126 if (old) { | 156 if (old) { |
127 this.removeListeners(old); | 157 this.removeListeners(old); |
128 } | 158 } |
129 if (!this.target) { | 159 if (!this.target) { |
130 return; | 160 return; |
131 } | 161 } |
132 this.isTargetReady = !!this.target; | 162 this.isTargetReady = !!this.target; |
133 this.classList.toggle('core-collapse-closed', this.target !== this); | 163 this.classList.toggle('core-collapse-closed', this.target !== this); |
134 this.target.style.overflow = 'hidden'; | 164 this.toggleOpenedStyle(false); |
135 this.horizontalChanged(); | 165 this.horizontalChanged(); |
136 this.addListeners(this.target); | 166 this.addListeners(this.target); |
137 // set core-collapse-closed class initially to hide the target | 167 // set core-collapse-closed class initially to hide the target |
138 this.toggleClosedClass(true); | 168 this.toggleClosedClass(true); |
139 this.update(); | 169 this.update(); |
140 }, | 170 }, |
141 | 171 |
142 addListeners: function(node) { | 172 addListeners: function(node) { |
143 node.addEventListener('transitionend', this.transitionEndListener); | 173 node.addEventListener('transitionend', this.transitionEndListener); |
144 }, | 174 }, |
(...skipping 26 matching lines...) Expand all Loading... |
171 if (duration === 0) { | 201 if (duration === 0) { |
172 this.async('transitionEnd'); | 202 this.async('transitionEnd'); |
173 } | 203 } |
174 }, | 204 }, |
175 | 205 |
176 transitionEnd: function() { | 206 transitionEnd: function() { |
177 if (this.opened && !this.fixedSize) { | 207 if (this.opened && !this.fixedSize) { |
178 this.updateSize('auto', null); | 208 this.updateSize('auto', null); |
179 } | 209 } |
180 this.setTransitionDuration(null); | 210 this.setTransitionDuration(null); |
| 211 this.toggleOpenedStyle(this.opened); |
181 this.toggleClosedClass(!this.opened); | 212 this.toggleClosedClass(!this.opened); |
182 this.asyncFire('core-resize', null, this.target); | 213 this.asyncFire('core-resize', null, this.target); |
183 }, | 214 }, |
184 | 215 |
185 toggleClosedClass: function(closed) { | 216 toggleClosedClass: function(closed) { |
186 this.hasClosedClass = closed; | 217 this.hasClosedClass = closed; |
187 this.target.classList.toggle('core-collapse-closed', closed); | 218 this.target.classList.toggle('core-collapse-closed', closed); |
188 }, | 219 }, |
| 220 |
| 221 toggleOpenedStyle: function(opened) { |
| 222 this.target.style.overflow = this.allowOverflow && opened ? '' : 'hidden'; |
| 223 }, |
189 | 224 |
190 updateSize: function(size, duration, forceEnd) { | 225 updateSize: function(size, duration, forceEnd) { |
191 this.setTransitionDuration(duration); | 226 this.setTransitionDuration(duration); |
192 this.calcSize(); | 227 this.calcSize(); |
193 var s = this.target.style; | 228 var s = this.target.style; |
194 var nochange = s[this.dimension] === size; | 229 var nochange = s[this.dimension] === size; |
195 s[this.dimension] = size; | 230 s[this.dimension] = size; |
196 // transitonEnd will not be called if the size has not changed | 231 // transitonEnd will not be called if the size has not changed |
197 if (forceEnd && nochange) { | 232 if (forceEnd && nochange) { |
198 this.transitionEnd(); | 233 this.transitionEnd(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 return; | 269 return; |
235 } | 270 } |
236 this.updateSize(0, null); | 271 this.updateSize(0, null); |
237 } | 272 } |
238 this.async(function() { | 273 this.async(function() { |
239 this.updateSize(this.size || s, this.duration, true); | 274 this.updateSize(this.size || s, this.duration, true); |
240 }); | 275 }); |
241 }, | 276 }, |
242 | 277 |
243 hide: function() { | 278 hide: function() { |
| 279 this.toggleOpenedStyle(false); |
244 // don't need to do anything if it's already hidden | 280 // don't need to do anything if it's already hidden |
245 if (this.hasClosedClass && !this.fixedSize) { | 281 if (this.hasClosedClass && !this.fixedSize) { |
246 return; | 282 return; |
247 } | 283 } |
248 if (this.fixedSize) { | 284 if (this.fixedSize) { |
249 // save the size before hiding it | 285 // save the size before hiding it |
250 this.size = this.getComputedSize(); | 286 this.size = this.getComputedSize(); |
251 } else { | 287 } else { |
252 this.updateSize(this.calcSize(), null); | 288 this.updateSize(this.calcSize(), null); |
253 } | 289 } |
254 this.async(function() { | 290 this.async(function() { |
255 this.updateSize(0, this.duration); | 291 this.updateSize(0, this.duration); |
256 }); | 292 }); |
257 } | 293 } |
258 | 294 |
259 }); | 295 }); |
260 | 296 |
261 </script> | 297 </script> |
262 </polymer-element> | 298 </polymer-element> |
OLD | NEW |