OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 @license |
| 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 |
| 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 |
| 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 |
| 9 --> |
| 10 |
| 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 <link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html
"> |
| 13 <link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html
"> |
| 14 <link rel="import" href="../iron-behaviors/iron-control-state.html"> |
| 15 <link rel="import" href="../iron-overlay-behavior/iron-overlay-behavior.html"> |
| 16 <link rel="import" href="../neon-animation/neon-animation-runner-behavior.html"> |
| 17 <link rel="import" href="../neon-animation/animations/opaque-animation.html"> |
| 18 <link rel="import" href="iron-dropdown-scroll-manager.html"> |
| 19 |
| 20 <!-- |
| 21 `<iron-dropdown>` is a generalized element that is useful when you have |
| 22 hidden content (`.dropdown-content`) that is revealed due to some change in |
| 23 state that should cause it to do so. |
| 24 |
| 25 Note that this is a low-level element intended to be used as part of other |
| 26 composite elements that cause dropdowns to be revealed. |
| 27 |
| 28 Examples of elements that might be implemented using an `iron-dropdown` |
| 29 include comboboxes, menubuttons, selects. The list goes on. |
| 30 |
| 31 The `<iron-dropdown>` element exposes attributes that allow the position |
| 32 of the `.dropdown-content` relative to the `.dropdown-trigger` to be |
| 33 configured. |
| 34 |
| 35 <iron-dropdown horizontal-align="right" vertical-align="top"> |
| 36 <div class="dropdown-content">Hello!</div> |
| 37 </iron-dropdown> |
| 38 |
| 39 In the above example, the `<div>` with class `.dropdown-content` will be |
| 40 hidden until the dropdown element has `opened` set to true, or when the `open` |
| 41 method is called on the element. |
| 42 |
| 43 @demo demo/index.html |
| 44 --> |
| 45 |
| 46 <dom-module id="iron-dropdown"> |
| 47 <style> |
| 48 :host { |
| 49 position: fixed; |
| 50 } |
| 51 |
| 52 #contentWrapper ::content > * { |
| 53 overflow: auto; |
| 54 } |
| 55 |
| 56 #contentWrapper.animating ::content > * { |
| 57 overflow: hidden; |
| 58 } |
| 59 </style> |
| 60 <template> |
| 61 <div id="contentWrapper"> |
| 62 <content id="content" select=".dropdown-content"></content> |
| 63 </div> |
| 64 </template> |
| 65 |
| 66 <script> |
| 67 (function() { |
| 68 'use strict'; |
| 69 |
| 70 Polymer({ |
| 71 is: 'iron-dropdown', |
| 72 |
| 73 behaviors: [ |
| 74 Polymer.IronControlState, |
| 75 Polymer.IronA11yKeysBehavior, |
| 76 Polymer.IronOverlayBehavior, |
| 77 Polymer.NeonAnimationRunnerBehavior |
| 78 ], |
| 79 |
| 80 properties: { |
| 81 /** |
| 82 * The orientation against which to align the dropdown content |
| 83 * horizontally relative to the dropdown trigger. |
| 84 */ |
| 85 horizontalAlign: { |
| 86 type: String, |
| 87 value: 'left', |
| 88 reflectToAttribute: true |
| 89 }, |
| 90 |
| 91 /** |
| 92 * The orientation against which to align the dropdown content |
| 93 * vertically relative to the dropdown trigger. |
| 94 */ |
| 95 verticalAlign: { |
| 96 type: String, |
| 97 value: 'top', |
| 98 reflectToAttribute: true |
| 99 }, |
| 100 |
| 101 /** |
| 102 * The element that should be used to position the dropdown when |
| 103 * it is opened. |
| 104 */ |
| 105 positionTarget: { |
| 106 type: Object, |
| 107 observer: '_positionTargetChanged' |
| 108 }, |
| 109 |
| 110 /** |
| 111 * An animation config. If provided, this will be used to animate the |
| 112 * opening of the dropdown. |
| 113 */ |
| 114 openAnimationConfig: { |
| 115 type: Object |
| 116 }, |
| 117 |
| 118 /** |
| 119 * An animation config. If provided, this will be used to animate the |
| 120 * closing of the dropdown. |
| 121 */ |
| 122 closeAnimationConfig: { |
| 123 type: Object |
| 124 }, |
| 125 |
| 126 /** |
| 127 * Set to true to disable animations when opening and closing the |
| 128 * dropdown. |
| 129 */ |
| 130 noAnimations: { |
| 131 type: Boolean, |
| 132 value: false |
| 133 }, |
| 134 |
| 135 /** |
| 136 * We memoize the positionTarget bounding rectangle so that we can |
| 137 * limit the number of times it is queried per resize / relayout. |
| 138 * @type {?Object} |
| 139 */ |
| 140 _positionRectMemo: { |
| 141 type: Object |
| 142 } |
| 143 }, |
| 144 |
| 145 listeners: { |
| 146 'neon-animation-finish': '_onNeonAnimationFinish' |
| 147 }, |
| 148 |
| 149 observers: [ |
| 150 '_updateOverlayPosition(verticalAlign, horizontalAlign)' |
| 151 ], |
| 152 |
| 153 attached: function() { |
| 154 if (this.positionTarget === undefined) { |
| 155 this.positionTarget = this._defaultPositionTarget; |
| 156 } |
| 157 }, |
| 158 |
| 159 /** |
| 160 * The element that is contained by the dropdown, if any. |
| 161 */ |
| 162 get containedElement() { |
| 163 return Polymer.dom(this.$.content).getDistributedNodes()[0]; |
| 164 }, |
| 165 |
| 166 get _defaultPositionTarget() { |
| 167 var parent = Polymer.dom(this).parentNode; |
| 168 |
| 169 if (parent.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { |
| 170 parent = parent.host; |
| 171 } |
| 172 |
| 173 return parent; |
| 174 }, |
| 175 |
| 176 get _positionRect() { |
| 177 if (!this._positionRectMemo && this.positionTarget) { |
| 178 this._positionRectMemo = this.positionTarget.getBoundingClientRect()
; |
| 179 } |
| 180 |
| 181 return this._positionRectMemo; |
| 182 }, |
| 183 |
| 184 get _horizontalAlignTargetValue() { |
| 185 var target; |
| 186 |
| 187 if (this.horizontalAlign === 'right') { |
| 188 target = document.documentElement.clientWidth - this._positionRect.r
ight; |
| 189 } else { |
| 190 target = this._positionRect.left; |
| 191 } |
| 192 |
| 193 return Math.max(target, 0); |
| 194 }, |
| 195 |
| 196 get _verticalAlignTargetValue() { |
| 197 var target; |
| 198 |
| 199 if (this.verticalAlign === 'bottom') { |
| 200 target = document.documentElement.clientHeight - this._positionRect.
bottom; |
| 201 } else { |
| 202 target = this._positionRect.top; |
| 203 } |
| 204 |
| 205 return Math.max(target, 0); |
| 206 }, |
| 207 |
| 208 _openedChanged: function(opened) { |
| 209 if (opened && this.disabled) { |
| 210 this.cancel(); |
| 211 } else { |
| 212 this._cancelAnimations(); |
| 213 this._prepareDropdown(); |
| 214 Polymer.IronOverlayBehaviorImpl._openedChanged.apply(this, arguments
); |
| 215 } |
| 216 }, |
| 217 |
| 218 _renderOpened: function() { |
| 219 Polymer.IronDropdownScrollManager.pushScrollLock(this); |
| 220 if (!this.noAnimations && this.animationConfig && this.animationConfig
.open) { |
| 221 this.$.contentWrapper.classList.add('animating'); |
| 222 this.playAnimation('open'); |
| 223 } else { |
| 224 this._focusContent(); |
| 225 Polymer.IronOverlayBehaviorImpl._renderOpened.apply(this, arguments)
; |
| 226 } |
| 227 }, |
| 228 |
| 229 _renderClosed: function() { |
| 230 Polymer.IronDropdownScrollManager.removeScrollLock(this); |
| 231 if (!this.noAnimations && this.animationConfig && this.animationConfig
.close) { |
| 232 this.$.contentWrapper.classList.add('animating'); |
| 233 this.playAnimation('close'); |
| 234 } else { |
| 235 Polymer.IronOverlayBehaviorImpl._renderClosed.apply(this, arguments)
; |
| 236 } |
| 237 }, |
| 238 |
| 239 _onNeonAnimationFinish: function() { |
| 240 this.$.contentWrapper.classList.remove('animating'); |
| 241 if (this.opened) { |
| 242 Polymer.IronOverlayBehaviorImpl._renderOpened.apply(this); |
| 243 } else { |
| 244 Polymer.IronOverlayBehaviorImpl._renderClosed.apply(this); |
| 245 } |
| 246 }, |
| 247 |
| 248 _onIronResize: function() { |
| 249 var containedElement = this.containedElement; |
| 250 var scrollTop; |
| 251 var scrollLeft; |
| 252 |
| 253 if (containedElement) { |
| 254 scrollTop = containedElement.scrollTop; |
| 255 scrollLeft = containedElement.scrollLeft; |
| 256 } |
| 257 |
| 258 if (this.opened) { |
| 259 this._updateOverlayPosition(); |
| 260 } |
| 261 |
| 262 Polymer.IronOverlayBehaviorImpl._onIronResize.apply(this, arguments); |
| 263 |
| 264 if (containedElement) { |
| 265 containedElement.scrollTop = scrollTop; |
| 266 containedElement.scrollLeft = scrollLeft; |
| 267 } |
| 268 }, |
| 269 |
| 270 _positionTargetChanged: function() { |
| 271 this._updateOverlayPosition(); |
| 272 }, |
| 273 |
| 274 _cancelAnimations: function() { |
| 275 this.cancelAnimation(); |
| 276 }, |
| 277 |
| 278 _updateAnimationConfig: function() { |
| 279 var animationConfig = {}; |
| 280 var animations = []; |
| 281 |
| 282 if (this.openAnimationConfig) { |
| 283 // NOTE(cdata): When making `display:none` elements visible in Safar
i, |
| 284 // the element will paint once in a fully visible state, causing the |
| 285 // dropdown to flash before it fades in. We prepend an |
| 286 // `opaque-animation` to fix this problem: |
| 287 animationConfig.open = [{ |
| 288 name: 'opaque-animation', |
| 289 }].concat(this.openAnimationConfig); |
| 290 animations = animations.concat(animationConfig.open); |
| 291 } |
| 292 |
| 293 if (this.closeAnimationConfig) { |
| 294 animationConfig.close = this.closeAnimationConfig; |
| 295 animations = animations.concat(animationConfig.close); |
| 296 } |
| 297 |
| 298 animations.forEach(function(animation) { |
| 299 animation.node = this.containedElement; |
| 300 }, this); |
| 301 |
| 302 this.animationConfig = animationConfig; |
| 303 }, |
| 304 |
| 305 _prepareDropdown: function() { |
| 306 this.sizingTarget = this.containedElement || this.sizingTarget; |
| 307 this._updateAnimationConfig(); |
| 308 this._updateOverlayPosition(); |
| 309 }, |
| 310 |
| 311 _updateOverlayPosition: function() { |
| 312 this._positionRectMemo = null; |
| 313 |
| 314 if (!this.positionTarget) { |
| 315 return; |
| 316 } |
| 317 |
| 318 this.style[this.horizontalAlign] = |
| 319 this._horizontalAlignTargetValue + 'px'; |
| 320 |
| 321 this.style[this.verticalAlign] = |
| 322 this._verticalAlignTargetValue + 'px'; |
| 323 |
| 324 // NOTE(cdata): We re-memoize inline styles here, otherwise |
| 325 // calling `refit` from `IronFitBehavior` will reset inline styles |
| 326 // to whatever they were when the dropdown first opened. |
| 327 if (this._fitInfo) { |
| 328 this._fitInfo.inlineStyle[this.horizontalAlign] = |
| 329 this.style[this.horizontalAlign]; |
| 330 |
| 331 this._fitInfo.inlineStyle[this.verticalAlign] = |
| 332 this.style[this.verticalAlign]; |
| 333 } |
| 334 }, |
| 335 |
| 336 _focusContent: function() { |
| 337 if (this.containedElement) { |
| 338 this.containedElement.focus(); |
| 339 } |
| 340 } |
| 341 }); |
| 342 })(); |
| 343 </script> |
| 344 </dom-module> |
| 345 |
OLD | NEW |