| OLD | NEW |
| 1 /** | 1 /** |
| 2 * `Polymer.IronScrollTargetBehavior` allows an element to respond to scroll e
vents from a | 2 * `Polymer.IronScrollTargetBehavior` allows an element to respond to scroll e
vents from a |
| 3 * designated scroll target. | 3 * designated scroll target. |
| 4 * | 4 * |
| 5 * Elements that consume this behavior can override the `_scrollHandler` | 5 * Elements that consume this behavior can override the `_scrollHandler` |
| 6 * method to add logic on the scroll event. | 6 * method to add logic on the scroll event. |
| 7 * | 7 * |
| 8 * @demo demo/scrolling-region.html Scrolling Region | 8 * @demo demo/scrolling-region.html Scrolling Region |
| 9 * @demo demo/document.html Document Element | 9 * @demo demo/document.html Document Element |
| 10 * @polymerBehavior | 10 * @polymerBehavior |
| 11 */ | 11 */ |
| 12 Polymer.IronScrollTargetBehavior = { | 12 Polymer.IronScrollTargetBehavior = { |
| 13 | 13 |
| 14 properties: { | 14 properties: { |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Specifies the element that will handle the scroll event | 17 * Specifies the element that will handle the scroll event |
| 18 * on the behalf of the current element. This is typically a reference to
an `Element`, | 18 * on the behalf of the current element. This is typically a reference to
an element, |
| 19 * but there are a few more posibilities: | 19 * but there are a few more posibilities: |
| 20 * | 20 * |
| 21 * ### Elements id | 21 * ### Elements id |
| 22 * | 22 * |
| 23 *```html | 23 *```html |
| 24 * <div id="scrollable-element" style="overflow-y: auto;"> | 24 * <div id="scrollable-element" style="overflow: auto;"> |
| 25 * <x-element scroll-target="scrollable-element"> | 25 * <x-element scroll-target="scrollable-element"> |
| 26 * Content | 26 * <!-- Content--> |
| 27 * </x-element> | 27 * </x-element> |
| 28 * </div> | 28 * </div> |
| 29 *``` | 29 *``` |
| 30 * In this case, `scrollTarget` will point to the outer div element. Alter
natively, | 30 * In this case, the `scrollTarget` will point to the outer div element. |
| 31 * you can set the property programatically: | 31 * |
| 32 * ### Document scrolling |
| 33 * |
| 34 * For document scrolling, you can use the reserved word `document`: |
| 35 * |
| 36 *```html |
| 37 * <x-element scroll-target="document"> |
| 38 * <!-- Content --> |
| 39 * </x-element> |
| 40 *``` |
| 41 * |
| 42 * ### Elements reference |
| 32 * | 43 * |
| 33 *```js | 44 *```js |
| 34 * appHeader.scrollTarget = document.querySelector('#scrollable-element'); | 45 * appHeader.scrollTarget = document.querySelector('#scrollable-element'); |
| 35 *``` | 46 *``` |
| 36 * | 47 * |
| 37 * @type {HTMLElement} | 48 * @type {HTMLElement} |
| 38 */ | 49 */ |
| 39 scrollTarget: { | 50 scrollTarget: { |
| 40 type: HTMLElement, | 51 type: HTMLElement, |
| 41 value: function() { | 52 value: function() { |
| 42 return this._defaultScrollTarget; | 53 return this._defaultScrollTarget; |
| 43 } | 54 } |
| 44 } | 55 } |
| 45 }, | 56 }, |
| 46 | 57 |
| 47 observers: [ | 58 observers: [ |
| 48 '_scrollTargetChanged(scrollTarget, isAttached)' | 59 '_scrollTargetChanged(scrollTarget, isAttached)' |
| 49 ], | 60 ], |
| 50 | 61 |
| 51 _scrollTargetChanged: function(scrollTarget, isAttached) { | 62 _scrollTargetChanged: function(scrollTarget, isAttached) { |
| 52 // Remove lister to the current scroll target | 63 var eventTarget; |
| 64 |
| 53 if (this._oldScrollTarget) { | 65 if (this._oldScrollTarget) { |
| 54 if (this._oldScrollTarget === this._doc) { | 66 eventTarget = this._oldScrollTarget === this._doc ? window : this._oldSc
rollTarget; |
| 55 window.removeEventListener('scroll', this._boundScrollHandler); | 67 eventTarget.removeEventListener('scroll', this._boundScrollHandler); |
| 56 } else if (this._oldScrollTarget.removeEventListener) { | |
| 57 this._oldScrollTarget.removeEventListener('scroll', this._boundScrollH
andler); | |
| 58 } | |
| 59 this._oldScrollTarget = null; | 68 this._oldScrollTarget = null; |
| 60 } | 69 } |
| 61 if (isAttached) { | |
| 62 // Support element id references | |
| 63 if (typeof scrollTarget === 'string') { | |
| 64 | 70 |
| 65 var host = this.domHost; | 71 if (!isAttached) { |
| 66 this.scrollTarget = host && host.$ ? host.$[scrollTarget] : | 72 return; |
| 67 Polymer.dom(this.ownerDocument).querySelector('#' + scrollTarget); | 73 } |
| 74 // Support element id references |
| 75 if (scrollTarget === 'document') { |
| 68 | 76 |
| 69 } else if (this._scrollHandler) { | 77 this.scrollTarget = this._doc; |
| 70 | 78 |
| 71 this._boundScrollHandler = this._boundScrollHandler || this._scrollHan
dler.bind(this); | 79 } else if (typeof scrollTarget === 'string') { |
| 72 // Add a new listener | 80 |
| 73 if (scrollTarget === this._doc) { | 81 this.scrollTarget = this.domHost ? this.domHost.$[scrollTarget] : |
| 74 window.addEventListener('scroll', this._boundScrollHandler); | 82 Polymer.dom(this.ownerDocument).querySelector('#' + scrollTarget); |
| 75 if (this._scrollTop !== 0 || this._scrollLeft !== 0) { | 83 |
| 76 this._scrollHandler(); | 84 } else if (this._isValidScrollTarget()) { |
| 77 } | 85 |
| 78 } else if (scrollTarget && scrollTarget.addEventListener) { | 86 eventTarget = scrollTarget === this._doc ? window : scrollTarget; |
| 79 scrollTarget.addEventListener('scroll', this._boundScrollHandler); | 87 this._boundScrollHandler = this._boundScrollHandler || this._scrollHandl
er.bind(this); |
| 80 } | 88 this._oldScrollTarget = scrollTarget; |
| 81 this._oldScrollTarget = scrollTarget; | 89 |
| 82 } | 90 eventTarget.addEventListener('scroll', this._boundScrollHandler); |
| 83 } | 91 } |
| 84 }, | 92 }, |
| 85 | 93 |
| 86 /** | 94 /** |
| 87 * Runs on every scroll event. Consumer of this behavior may want to overrid
e this method. | 95 * Runs on every scroll event. Consumer of this behavior may override this m
ethod. |
| 88 * | 96 * |
| 89 * @protected | 97 * @protected |
| 90 */ | 98 */ |
| 91 _scrollHandler: function scrollHandler() {}, | 99 _scrollHandler: function scrollHandler() {}, |
| 92 | 100 |
| 93 /** | 101 /** |
| 94 * The default scroll target. Consumers of this behavior may want to customi
ze | 102 * The default scroll target. Consumers of this behavior may want to customi
ze |
| 95 * the default scroll target. | 103 * the default scroll target. |
| 96 * | 104 * |
| 97 * @type {Element} | 105 * @type {Element} |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 | 209 |
| 202 /** | 210 /** |
| 203 * Returns true if the scroll target is a valid HTMLElement. | 211 * Returns true if the scroll target is a valid HTMLElement. |
| 204 * | 212 * |
| 205 * @return {boolean} | 213 * @return {boolean} |
| 206 */ | 214 */ |
| 207 _isValidScrollTarget: function() { | 215 _isValidScrollTarget: function() { |
| 208 return this.scrollTarget instanceof HTMLElement; | 216 return this.scrollTarget instanceof HTMLElement; |
| 209 } | 217 } |
| 210 }; | 218 }; |
| OLD | NEW |