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', function(e) { | |
51 this.updateScroll_(e.target); | |
52 }.bind(this)); | |
dschuyler
2016/08/17 23:07:40
When/where are these listeners be removed?
stevenjb
2016/08/18 00:05:24
Added detached(). (Which rarely gets called in pra
| |
53 } | |
54 }, | |
55 | |
56 /** | |
57 * Called any time the contents of a scrollable container may have changed. | |
58 * This ensures that the <iron-list> contents of dynamically sized | |
59 * containers are resized correctly. | |
60 */ | |
61 updateScrollableContents() { | |
62 var nodeList = this.root.querySelectorAll('[scrollable] iron-list'); | |
63 for (let node of nodeList) { | |
64 let ironList = /** @type {!IronListElement} */ (node); | |
65 ironList.notifyResize(); | |
hcarmona
2016/08/17 23:43:23
Are there non-IronListElements that need to be not
stevenjb
2016/08/18 00:05:24
As far as I am aware, notfyResize is an iron-list
| |
66 } | |
67 }, | |
68 | |
69 /** | |
70 * This gets called once intially and any time a scrollable container scrolls. | |
71 * @private | |
hcarmona
2016/08/17 23:43:23
Can we add an @param for |scrollable|?
stevenjb
2016/08/18 00:05:24
Effectively done; passing |event| to this now, wit
| |
72 */ | |
73 updateScroll_(scrollable) { | |
74 scrollable.classList.toggle( | |
75 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); | |
76 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); | |
77 scrollable.classList.toggle( | |
78 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= | |
79 scrollable.scrollHeight); | |
80 }, | |
81 }; | |
OLD | NEW |