Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Behavior for scrollable containers with <iron-list>. | 6 * @fileoverview Behavior for scrollable containers with <iron-list>. |
| 7 * | 7 * |
| 8 * Any containers with the 'scrollable' attribute set will have the following | 8 * Any containers with the 'scrollable' attribute set will have the following |
| 9 * classes toggled appropriately: can-scroll, is-scrolled, scrolled-to-bottom. | 9 * classes toggled appropriately: can-scroll, is-scrolled, scrolled-to-bottom. |
| 10 * These classes are used to style the container div and list elements | 10 * These classes are used to style the container div and list elements |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 * </dom-module> | 30 * </dom-module> |
| 31 * | 31 * |
| 32 * NOTE: If 'container' is not fixed size, it is important to call | 32 * NOTE: If 'container' is not fixed size, it is important to call |
| 33 * updateScrollableContents() when [[items]] changes, otherwise the container | 33 * updateScrollableContents() when [[items]] changes, otherwise the container |
| 34 * will not be sized correctly. | 34 * will not be sized correctly. |
| 35 */ | 35 */ |
| 36 | 36 |
| 37 /** @polymerBehavior */ | 37 /** @polymerBehavior */ |
| 38 var CrScrollableBehavior = { | 38 var CrScrollableBehavior = { |
| 39 properties: { | 39 properties: { |
| 40 /** @private */ | 40 /** @private {number|null} */ |
| 41 intervalId_: Number, | 41 intervalId_: { |
| 42 type: Number, | |
| 43 value: null | |
| 44 } | |
| 42 }, | 45 }, |
| 43 | 46 |
| 44 ready: function() { | 47 ready: function() { |
| 45 var scrollableElements = this.root.querySelectorAll('[scrollable]'); | 48 var scrollableElements = this.root.querySelectorAll('[scrollable]'); |
| 46 | 49 |
| 47 // Setup the intial scrolling related classes for each scrollable container. | 50 // Setup the intial scrolling related classes for each scrollable container. |
| 48 requestAnimationFrame(function() { | 51 requestAnimationFrame(function() { |
| 49 for (let scrollable of scrollableElements) | 52 for (let scrollable of scrollableElements) |
| 50 this.updateScroll_(scrollable); | 53 this.updateScroll_(scrollable); |
| 51 }.bind(this)); | 54 }.bind(this)); |
| 52 | 55 |
| 53 // Listen to the 'scroll' event for each scrollable container. | 56 // Listen to the 'scroll' event for each scrollable container. |
| 54 for (let scrollable of scrollableElements) | 57 for (let scrollable of scrollableElements) |
| 55 scrollable.addEventListener('scroll', this.updateScrollEvent_.bind(this)); | 58 scrollable.addEventListener('scroll', this.updateScrollEvent_.bind(this)); |
| 56 }, | 59 }, |
| 57 | 60 |
| 58 detached: function() { | 61 detached: function() { |
| 59 if (this.intervalId_) | 62 if (this.intervalId_ !== null) |
| 60 clearInterval(this.intervalId_); | 63 clearInterval(this.intervalId_); |
| 61 }, | 64 }, |
| 62 | 65 |
| 63 /** | 66 /** |
| 64 * Called any time the contents of a scrollable container may have changed. | 67 * Called any time the contents of a scrollable container may have changed. |
| 65 * This ensures that the <iron-list> contents of dynamically sized | 68 * This ensures that the <iron-list> contents of dynamically sized |
| 66 * containers are resized correctly. | 69 * containers are resized correctly. |
| 67 */ | 70 */ |
| 68 updateScrollableContents() { | 71 updateScrollableContents() { |
|
dpapad
2016/11/23 18:30:21
updateScrollableContents: function() {
stevenjb
2016/11/23 18:37:15
Sigh. Still a C++ developer at heart. Done.
| |
| 69 let nodeList = this.root.querySelectorAll('[scrollable] iron-list'); | 72 let nodeList = this.root.querySelectorAll('[scrollable] iron-list'); |
| 70 // Use setTimeout to avoid initial render / sizing issues. | 73 // Use setInterval to avoid initial render / sizing issues. |
| 71 this.intervalId_ = setInterval(function() { | 74 if (this.intervalId_ !== null) |
|
dpapad
2016/11/23 18:30:21
Can you move this before the querySelectorAll? So
stevenjb
2016/11/23 18:37:15
Good catch. Done.
| |
| 75 return; | |
| 76 this.intervalId_ = window.setInterval(function() { | |
| 72 let unreadyNodes = []; | 77 let unreadyNodes = []; |
| 73 for (let node of nodeList) { | 78 for (let node of nodeList) { |
| 74 if (node.parentNode.scrollHeight == 0) { | 79 if (node.parentNode.scrollHeight == 0) { |
| 75 unreadyNodes.push(node); | 80 unreadyNodes.push(node); |
| 76 continue; | 81 continue; |
| 77 } | 82 } |
| 78 let ironList = /** @type {!IronListElement} */ (node); | 83 let ironList = /** @type {!IronListElement} */ (node); |
| 79 ironList.notifyResize(); | 84 ironList.notifyResize(); |
| 80 } | 85 } |
| 81 if (unreadyNodes.length == 0) | 86 if (unreadyNodes.length == 0) { |
| 82 clearInterval(this.intervalId_); | 87 window.clearInterval(this.intervalId_); |
| 83 else | 88 this.intervalId_ = null; |
| 89 } else { | |
| 84 nodeList = unreadyNodes; | 90 nodeList = unreadyNodes; |
| 91 } | |
| 85 }.bind(this), 10); | 92 }.bind(this), 10); |
| 86 }, | 93 }, |
| 87 | 94 |
| 88 /** | 95 /** |
| 89 * Event wrapper for updateScroll_. | 96 * Event wrapper for updateScroll_. |
| 90 * @param {!Event} event | 97 * @param {!Event} event |
| 91 * @private | 98 * @private |
| 92 */ | 99 */ |
| 93 updateScrollEvent_(event) { | 100 updateScrollEvent_(event) { |
| 94 let scrollable = /** @type {!HTMLElement} */ (event.target); | 101 let scrollable = /** @type {!HTMLElement} */ (event.target); |
| 95 this.updateScroll_(scrollable); | 102 this.updateScroll_(scrollable); |
| 96 }, | 103 }, |
| 97 | 104 |
| 98 /** | 105 /** |
| 99 * This gets called once intially and any time a scrollable container scrolls. | 106 * This gets called once intially and any time a scrollable container scrolls. |
| 100 * @param {!HTMLElement} scrollable | 107 * @param {!HTMLElement} scrollable |
| 101 * @private | 108 * @private |
| 102 */ | 109 */ |
| 103 updateScroll_(scrollable) { | 110 updateScroll_(scrollable) { |
| 104 scrollable.classList.toggle( | 111 scrollable.classList.toggle( |
| 105 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); | 112 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); |
| 106 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); | 113 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); |
| 107 scrollable.classList.toggle( | 114 scrollable.classList.toggle( |
| 108 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= | 115 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= |
| 109 scrollable.scrollHeight); | 116 scrollable.scrollHeight); |
| 110 }, | 117 }, |
| 111 }; | 118 }; |
| OLD | NEW |