OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Behavior for scrollable containers with <iron-list>. |
| 7 * |
| 8 * Any containers with the 'scrollable' attribute set will have the following |
| 9 * classes toggled appropriately: can-scroll, is-scrolled, scrolled-to-bottom. |
| 10 * These classes are used to style the container div and list elements |
| 11 * appropriately, see shared_style_css.html. |
| 12 * |
| 13 * The associated HTML should look something like: |
| 14 * <div id="container" scrollable> |
| 15 * <iron-list items="[[items]]" scroll-target="container"> |
| 16 * <template> |
| 17 * <my-element item="[[item]] tabindex$="[[tabIndex]]"></my-element> |
| 18 * </template> |
| 19 * </iron-list> |
| 20 * </div> |
| 21 * |
| 22 * In order to get correct keyboard focus (tab) behavior within the list, |
| 23 * any elements with tabbable sub-elements also need to set tabindex, e.g: |
| 24 * |
| 25 * <dom-module id="my-element> |
| 26 * <template> |
| 27 * ... |
| 28 * <paper-icon-button toggles active="{{opened}}" tabindex$="[[tabindex]]"> |
| 29 * </template> |
| 30 * </dom-module> |
| 31 * |
| 32 * NOTE: If 'container' is not fixed size, it is important to call |
| 33 * updateScrollableContents() when [[items]] changes, otherwise the container |
| 34 * will not be sized correctly. |
| 35 */ |
| 36 |
| 37 /** @polymerBehavior */ |
| 38 var CrScrollableBehavior = { |
| 39 attached: function() { |
| 40 var scrollableElements = this.root.querySelectorAll('[scrollable]'); |
| 41 |
| 42 // Setup the intial scrolling related classes for each scrollable container. |
| 43 requestAnimationFrame(function() { |
| 44 for (let scrollable of scrollableElements) |
| 45 this.updateScroll_(scrollable); |
| 46 }.bind(this)); |
| 47 |
| 48 // Listen to the 'scroll' event for each scrollable container. |
| 49 for (let scrollable of scrollableElements) |
| 50 scrollable.addEventListener('scroll', this.updateScrollEvent_.bind(this)); |
| 51 }, |
| 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 /** |
| 63 * Called any time the contents of a scrollable container may have changed. |
| 64 * This ensures that the <iron-list> contents of dynamically sized |
| 65 * containers are resized correctly. |
| 66 */ |
| 67 updateScrollableContents() { |
| 68 let nodeList = this.root.querySelectorAll('[scrollable] iron-list'); |
| 69 // Use setTimeout to avoid initial render / sizing issues. |
| 70 let intervalId = setInterval(function() { |
| 71 let unreadyNodes = []; |
| 72 for (let node of nodeList) { |
| 73 if (node.parentNode.scrollHeight == 0) { |
| 74 unreadyNodes.push(node); |
| 75 continue; |
| 76 } |
| 77 let ironList = /** @type {!IronListElement} */ (node); |
| 78 ironList.notifyResize(); |
| 79 } |
| 80 if (unreadyNodes.length == 0) |
| 81 clearInterval(intervalId); |
| 82 else |
| 83 nodeList = unreadyNodes; |
| 84 }, 10); |
| 85 }, |
| 86 |
| 87 /** |
| 88 * Event wrapper for updateScroll_. |
| 89 * @param {!Event} event |
| 90 * @private |
| 91 */ |
| 92 updateScrollEvent_(event) { |
| 93 let scrollable = /** @type {!HTMLElement} */ (event.target); |
| 94 this.updateScroll_(scrollable); |
| 95 }, |
| 96 |
| 97 /** |
| 98 * This gets called once intially and any time a scrollable container scrolls. |
| 99 * @param {!HTMLElement} scrollable |
| 100 * @private |
| 101 */ |
| 102 updateScroll_(scrollable) { |
| 103 scrollable.classList.toggle( |
| 104 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); |
| 105 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); |
| 106 scrollable.classList.toggle( |
| 107 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= |
| 108 scrollable.scrollHeight); |
| 109 }, |
| 110 }; |
OLD | NEW |