| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 @license | 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also | 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> | 9 --> |
| 10 | 10 |
| 11 <link rel="import" href="../polymer/polymer.html"> | 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 <link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html
"> | 12 <link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html
"> |
| 13 | 13 |
| 14 <!-- | 14 <!-- |
| 15 `iron-collapse` creates a collapsible block of content. By default, the content | 15 `iron-collapse` creates a collapsible block of content. By default, the content |
| 16 will be collapsed. Use `opened` or `toggle()` to show/hide the content. | 16 will be collapsed. Use `opened` or `toggle()` to show/hide the content. |
| 17 | 17 |
| 18 <button on-click="toggle">toggle collapse</button> | 18 <button on-click="toggle">toggle collapse</button> |
| 19 | 19 |
| 20 <iron-collapse id="collapse"> | 20 <iron-collapse id="collapse"> |
| 21 <div>Content goes here...</div> | 21 <div>Content goes here...</div> |
| 22 </iron-collapse> | 22 </iron-collapse> |
| 23 | 23 |
| 24 ... | 24 ... |
| 25 | 25 |
| 26 toggle: function() { | 26 toggle: function() { |
| 27 this.$.collapse.toggle(); | 27 this.$.collapse.toggle(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 `iron-collapse` adjusts the height/width of the collapsible element to show/hide | 30 `iron-collapse` adjusts the max-height/max-width of the collapsible element to s
how/hide |
| 31 the content. So avoid putting padding/margin/border on the collapsible directly
, | 31 the content. So avoid putting padding/margin/border on the collapsible directly
, |
| 32 and instead put a div inside and style that. | 32 and instead put a div inside and style that. |
| 33 | 33 |
| 34 <style> | 34 <style> |
| 35 .collapse-content { | 35 .collapse-content { |
| 36 padding: 15px; | 36 padding: 15px; |
| 37 border: 1px solid #dedede; | 37 border: 1px solid #dedede; |
| 38 } | 38 } |
| 39 </style> | 39 </style> |
| 40 | 40 |
| 41 <iron-collapse> | 41 <iron-collapse> |
| 42 <div class="collapse-content"> | 42 <div class="collapse-content"> |
| 43 <div>Content goes here...</div> | 43 <div>Content goes here...</div> |
| 44 </div> | 44 </div> |
| 45 </iron-collapse> | 45 </iron-collapse> |
| 46 | 46 |
| 47 @group Iron Elements | 47 @group Iron Elements |
| 48 @hero hero.svg | 48 @hero hero.svg |
| 49 @demo demo/index.html | 49 @demo demo/index.html |
| 50 @element iron-collapse | 50 @element iron-collapse |
| 51 --> | 51 --> |
| 52 | 52 |
| 53 <dom-module id="iron-collapse"> | 53 <dom-module id="iron-collapse"> |
| 54 | 54 |
| 55 <style> | 55 <template> |
| 56 | 56 |
| 57 :host { | 57 <style> |
| 58 display: block; | 58 :host { |
| 59 transition-duration: 300ms; | 59 display: block; |
| 60 overflow: visible; | 60 transition-duration: 300ms; |
| 61 } | 61 overflow: visible; |
| 62 } |
| 62 | 63 |
| 63 :host(.iron-collapse-closed) { | 64 :host(.iron-collapse-closed) { |
| 64 display: none; | 65 display: none; |
| 65 } | 66 } |
| 66 | 67 |
| 67 :host(:not(.iron-collapse-opened)) { | 68 :host(:not(.iron-collapse-opened)) { |
| 68 overflow: hidden; | 69 overflow: hidden; |
| 69 } | 70 } |
| 70 | 71 </style> |
| 71 </style> | |
| 72 | |
| 73 <template> | |
| 74 | 72 |
| 75 <content></content> | 73 <content></content> |
| 76 | 74 |
| 77 </template> | 75 </template> |
| 78 | 76 |
| 79 </dom-module> | 77 </dom-module> |
| 80 | 78 |
| 81 <script> | 79 <script> |
| 82 | 80 |
| 83 Polymer({ | 81 Polymer({ |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 noAnimation: { | 119 noAnimation: { |
| 122 type: Boolean | 120 type: Boolean |
| 123 }, | 121 }, |
| 124 | 122 |
| 125 }, | 123 }, |
| 126 | 124 |
| 127 get dimension() { | 125 get dimension() { |
| 128 return this.horizontal ? 'width' : 'height'; | 126 return this.horizontal ? 'width' : 'height'; |
| 129 }, | 127 }, |
| 130 | 128 |
| 129 /** |
| 130 * `maxWidth` or `maxHeight`. |
| 131 * @private |
| 132 */ |
| 133 get _dimensionMax() { |
| 134 return this.horizontal ? 'maxWidth' : 'maxHeight'; |
| 135 }, |
| 136 |
| 137 /** |
| 138 * `max-width` or `max-height`. |
| 139 * @private |
| 140 */ |
| 141 get _dimensionMaxCss() { |
| 142 return this.horizontal ? 'max-width' : 'max-height'; |
| 143 }, |
| 144 |
| 131 hostAttributes: { | 145 hostAttributes: { |
| 132 role: 'group', | 146 role: 'group', |
| 133 'aria-hidden': 'true', | 147 'aria-hidden': 'true', |
| 134 'aria-expanded': 'false' | 148 'aria-expanded': 'false' |
| 135 }, | 149 }, |
| 136 | 150 |
| 137 listeners: { | 151 listeners: { |
| 138 transitionend: '_transitionEnd' | 152 transitionend: '_transitionEnd' |
| 139 }, | 153 }, |
| 140 | 154 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 153 }, | 167 }, |
| 154 | 168 |
| 155 show: function() { | 169 show: function() { |
| 156 this.opened = true; | 170 this.opened = true; |
| 157 }, | 171 }, |
| 158 | 172 |
| 159 hide: function() { | 173 hide: function() { |
| 160 this.opened = false; | 174 this.opened = false; |
| 161 }, | 175 }, |
| 162 | 176 |
| 177 /** |
| 178 * Updates the size of the element. |
| 179 * @param {!String} size The new value for `maxWidth`/`maxHeight` as css pro
perty value, usually `auto` or `0px`. |
| 180 * @param {boolean=} animated if `true` updates the size with an animation,
otherwise without. |
| 181 */ |
| 163 updateSize: function(size, animated) { | 182 updateSize: function(size, animated) { |
| 164 // No change! | 183 // No change! |
| 165 if (this.style[this.dimension] === size) { | 184 var curSize = this.style[this._dimensionMax]; |
| 185 if (curSize === size || (size === 'auto' && !curSize)) { |
| 166 return; | 186 return; |
| 167 } | 187 } |
| 168 | 188 |
| 169 this._updateTransition(false); | 189 this._updateTransition(false); |
| 170 // If we can animate, must do some prep work. | 190 // If we can animate, must do some prep work. |
| 171 if (animated && !this.noAnimation && this._isDisplayed) { | 191 if (animated && !this.noAnimation && this._isDisplayed) { |
| 172 // Animation will start at the current size. | 192 // Animation will start at the current size. |
| 173 var startSize = this._calcSize(); | 193 var startSize = this._calcSize(); |
| 174 // For `auto` we must calculate what is the final size for the animation
. | 194 // For `auto` we must calculate what is the final size for the animation
. |
| 175 // After the transition is done, _transitionEnd will set the size back t
o `auto`. | 195 // After the transition is done, _transitionEnd will set the size back t
o `auto`. |
| 176 if (size === 'auto') { | 196 if (size === 'auto') { |
| 177 this.style[this.dimension] = size; | 197 this.style[this._dimensionMax] = ''; |
| 178 size = this._calcSize(); | 198 size = this._calcSize(); |
| 179 } | 199 } |
| 180 // Go to startSize without animation. | 200 // Go to startSize without animation. |
| 181 this.style[this.dimension] = startSize; | 201 this.style[this._dimensionMax] = startSize; |
| 182 // Force layout to ensure transition will go. Set offsetHeight to itself | 202 // Force layout to ensure transition will go. Set offsetHeight to itself |
| 183 // so that compilers won't remove it. | 203 // so that compilers won't remove it. |
| 184 this.offsetHeight = this.offsetHeight; | 204 this.offsetHeight = this.offsetHeight; |
| 185 // Enable animation. | 205 // Enable animation. |
| 186 this._updateTransition(true); | 206 this._updateTransition(true); |
| 187 } | 207 } |
| 188 // Set the final size. | 208 // Set the final size. |
| 189 this.style[this.dimension] = size; | 209 if (size === 'auto') { |
| 210 this.style[this._dimensionMax] = ''; |
| 211 } else { |
| 212 this.style[this._dimensionMax] = size; |
| 213 } |
| 190 }, | 214 }, |
| 191 | 215 |
| 192 /** | 216 /** |
| 193 * enableTransition() is deprecated, but left over so it doesn't break exist
ing code. | 217 * enableTransition() is deprecated, but left over so it doesn't break exist
ing code. |
| 194 * Please use `noAnimation` property instead. | 218 * Please use `noAnimation` property instead. |
| 195 * | 219 * |
| 196 * @method enableTransition | 220 * @method enableTransition |
| 197 * @deprecated since version 1.0.4 | 221 * @deprecated since version 1.0.4 |
| 198 */ | 222 */ |
| 199 enableTransition: function(enabled) { | 223 enableTransition: function(enabled) { |
| 200 console.warn('`enableTransition()` is deprecated, use `noAnimation` instea
d.'); | 224 Polymer.Base._warn('`enableTransition()` is deprecated, use `noAnimation`
instead.'); |
| 201 this.noAnimation = !enabled; | 225 this.noAnimation = !enabled; |
| 202 }, | 226 }, |
| 203 | 227 |
| 204 _updateTransition: function(enabled) { | 228 _updateTransition: function(enabled) { |
| 205 this.style.transitionDuration = (enabled && !this.noAnimation) ? '' : '0s'
; | 229 this.style.transitionDuration = (enabled && !this.noAnimation) ? '' : '0s'
; |
| 206 }, | 230 }, |
| 207 | 231 |
| 208 _horizontalChanged: function() { | 232 _horizontalChanged: function() { |
| 209 this.style.transitionProperty = this.dimension; | 233 this.style.transitionProperty = this._dimensionMaxCss; |
| 210 var otherDimension = this.dimension === 'width' ? 'height' : 'width'; | 234 var otherDimension = this._dimensionMax === 'maxWidth' ? 'maxHeight' : 'ma
xWidth'; |
| 211 this.style[otherDimension] = ''; | 235 this.style[otherDimension] = ''; |
| 212 this.updateSize(this.opened ? 'auto' : '0px', false); | 236 this.updateSize(this.opened ? 'auto' : '0px', false); |
| 213 }, | 237 }, |
| 214 | 238 |
| 215 _openedChanged: function() { | 239 _openedChanged: function() { |
| 216 this.setAttribute('aria-expanded', this.opened); | 240 this.setAttribute('aria-expanded', this.opened); |
| 217 this.setAttribute('aria-hidden', !this.opened); | 241 this.setAttribute('aria-hidden', !this.opened); |
| 218 | 242 |
| 219 this.toggleClass('iron-collapse-closed', false); | 243 this.toggleClass('iron-collapse-closed', false); |
| 220 this.toggleClass('iron-collapse-opened', false); | 244 this.toggleClass('iron-collapse-opened', false); |
| 221 this.updateSize(this.opened ? 'auto' : '0px', true); | 245 this.updateSize(this.opened ? 'auto' : '0px', true); |
| 222 | 246 |
| 223 // Focus the current collapse. | 247 // Focus the current collapse. |
| 224 if (this.opened) { | 248 if (this.opened) { |
| 225 this.focus(); | 249 this.focus(); |
| 226 } | 250 } |
| 227 if (this.noAnimation) { | 251 if (this.noAnimation) { |
| 228 this._transitionEnd(); | 252 this._transitionEnd(); |
| 229 } | 253 } |
| 230 }, | 254 }, |
| 231 | 255 |
| 232 _transitionEnd: function() { | 256 _transitionEnd: function() { |
| 233 if (this.opened) { | 257 if (this.opened) { |
| 234 this.style[this.dimension] = 'auto'; | 258 this.style[this._dimensionMax] = ''; |
| 235 } | 259 } |
| 236 this.toggleClass('iron-collapse-closed', !this.opened); | 260 this.toggleClass('iron-collapse-closed', !this.opened); |
| 237 this.toggleClass('iron-collapse-opened', this.opened); | 261 this.toggleClass('iron-collapse-opened', this.opened); |
| 238 this._updateTransition(false); | 262 this._updateTransition(false); |
| 239 this.notifyResize(); | 263 this.notifyResize(); |
| 240 }, | 264 }, |
| 241 | 265 |
| 242 /** | 266 /** |
| 243 * Simplistic heuristic to detect if element has a parent with display: none | 267 * Simplistic heuristic to detect if element has a parent with display: none |
| 244 * | 268 * |
| 245 * @private | 269 * @private |
| 246 */ | 270 */ |
| 247 get _isDisplayed() { | 271 get _isDisplayed() { |
| 248 var rect = this.getBoundingClientRect(); | 272 var rect = this.getBoundingClientRect(); |
| 249 for (var prop in rect) { | 273 for (var prop in rect) { |
| 250 if (rect[prop] !== 0) return true; | 274 if (rect[prop] !== 0) return true; |
| 251 } | 275 } |
| 252 return false; | 276 return false; |
| 253 }, | 277 }, |
| 254 | 278 |
| 255 _calcSize: function() { | 279 _calcSize: function() { |
| 256 return this.getBoundingClientRect()[this.dimension] + 'px'; | 280 return this.getBoundingClientRect()[this.dimension] + 'px'; |
| 257 } | 281 } |
| 258 | 282 |
| 259 }); | 283 }); |
| 260 | 284 |
| 261 </script> | 285 </script> |
| OLD | NEW |