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.updateScroll_); | |
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('scroll', this.updateScroll_); | |
58 }, | |
59 | |
60 /** | |
61 * Called any time the contents of a scrollable container may have changed. | |
62 * This ensures that the <iron-list> contents of dynamically sized | |
63 * containers are resized correctly. | |
64 */ | |
65 updateScrollableContents() { | |
66 let nodeList = this.root.querySelectorAll('[scrollable] iron-list'); | |
67 // Use setTimeout to avoid initial render / sizing issues. | |
dschuyler
2016/08/18 01:25:11
Would doWhenReady help?
https://cs.chromium.org/ch
| |
68 setTimeout(function() { | |
69 for (let node of nodeList) { | |
70 let ironList = /** @type {!IronListElement} */ (node); | |
71 ironList.notifyResize(); | |
72 } | |
73 }); | |
74 }, | |
75 | |
76 /** | |
77 * This gets called once intially and any time a scrollable container scrolls. | |
78 * @param {Event} event | |
79 * @private | |
80 */ | |
81 updateScroll_(event) { | |
82 let scrollable = event.target; | |
83 scrollable.classList.toggle( | |
84 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); | |
85 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); | |
86 scrollable.classList.toggle( | |
87 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= | |
88 scrollable.scrollHeight); | |
89 }, | |
90 }; | |
OLD | NEW |