| 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 18 matching lines...) Expand all Loading... |
| 29 * </template> | 29 * </template> |
| 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 attached: function() { | 39 ready: function() { |
| 40 var scrollableElements = this.root.querySelectorAll('[scrollable]'); | 40 var scrollableElements = this.root.querySelectorAll('[scrollable]'); |
| 41 | 41 |
| 42 // Setup the intial scrolling related classes for each scrollable container. | 42 // Setup the intial scrolling related classes for each scrollable container. |
| 43 requestAnimationFrame(function() { | 43 requestAnimationFrame(function() { |
| 44 for (let scrollable of scrollableElements) | 44 for (let scrollable of scrollableElements) |
| 45 this.updateScroll_(scrollable); | 45 this.updateScroll_(scrollable); |
| 46 }.bind(this)); | 46 }.bind(this)); |
| 47 | 47 |
| 48 // Listen to the 'scroll' event for each scrollable container. | 48 // Listen to the 'scroll' event for each scrollable container. |
| 49 for (let scrollable of scrollableElements) | 49 for (let scrollable of scrollableElements) |
| 50 scrollable.addEventListener('scroll', this.updateScrollEvent_.bind(this)); | 50 scrollable.addEventListener('scroll', this.updateScrollEvent_.bind(this)); |
| 51 }, | 51 }, |
| 52 | 52 |
| 53 detached: function() { | |
| 54 // Remove 'scroll' event listeners. | |
| 55 var scrollableElements = this.root.querySelectorAll('[scrollable]'); | |
| 56 for (let scrollable of scrollableElements) { | |
| 57 scrollable.removeEventListener( | |
| 58 'scroll', this.updateScrollEvent_.bind(this)); | |
| 59 } | |
| 60 }, | |
| 61 | |
| 62 /** | 53 /** |
| 63 * Called any time the contents of a scrollable container may have changed. | 54 * Called any time the contents of a scrollable container may have changed. |
| 64 * This ensures that the <iron-list> contents of dynamically sized | 55 * This ensures that the <iron-list> contents of dynamically sized |
| 65 * containers are resized correctly. | 56 * containers are resized correctly. |
| 66 */ | 57 */ |
| 67 updateScrollableContents() { | 58 updateScrollableContents() { |
| 68 let nodeList = this.root.querySelectorAll('[scrollable] iron-list'); | 59 let nodeList = this.root.querySelectorAll('[scrollable] iron-list'); |
| 69 // Use setTimeout to avoid initial render / sizing issues. | 60 // Use setTimeout to avoid initial render / sizing issues. |
| 70 let intervalId = setInterval(function() { | 61 let intervalId = setInterval(function() { |
| 71 let unreadyNodes = []; | 62 let unreadyNodes = []; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 101 */ | 92 */ |
| 102 updateScroll_(scrollable) { | 93 updateScroll_(scrollable) { |
| 103 scrollable.classList.toggle( | 94 scrollable.classList.toggle( |
| 104 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); | 95 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); |
| 105 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); | 96 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); |
| 106 scrollable.classList.toggle( | 97 scrollable.classList.toggle( |
| 107 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= | 98 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= |
| 108 scrollable.scrollHeight); | 99 scrollable.scrollHeight); |
| 109 }, | 100 }, |
| 110 }; | 101 }; |
| OLD | NEW |