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 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 properties: { | 39 |
| 40 /** @private {number|null} */ | 40 /** @private {number|null} */ |
| 41 intervalId_: {type: Number, value: null} | 41 intervalId_: null, |
| 42 }, | 42 |
| 43 /** | |
| 44 * A dictionary of integer arrays, one entry per scrollable contianer id. | |
| 45 * Each array keeps a queue of scroll positions. This ensures that multiple | |
| 46 * calls to save/restore scroll do not interfere with each other. | |
| 47 * @private {!Object<!Array<number>>} | |
| 48 */ | |
| 49 saveScrollTop_: {}, | |
| 43 | 50 |
| 44 ready: function() { | 51 ready: function() { |
| 45 var scrollableElements = this.root.querySelectorAll('[scrollable]'); | 52 var scrollableElements = this.root.querySelectorAll('[scrollable]'); |
| 46 | 53 |
| 47 // Setup the intial scrolling related classes for each scrollable container. | 54 // Setup the intial scrolling related classes for each scrollable container. |
| 48 requestAnimationFrame(function() { | 55 requestAnimationFrame(function() { |
| 49 for (var i = 0; i < scrollableElements.length; i++) | 56 for (var i = 0; i < scrollableElements.length; i++) |
| 50 this.updateScroll_(scrollableElements[i]); | 57 this.updateScroll_(scrollableElements[i]); |
| 51 }.bind(this)); | 58 }.bind(this)); |
| 52 | 59 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 } | 93 } |
| 87 if (unreadyNodes.length == 0) { | 94 if (unreadyNodes.length == 0) { |
| 88 window.clearInterval(this.intervalId_); | 95 window.clearInterval(this.intervalId_); |
| 89 this.intervalId_ = null; | 96 this.intervalId_ = null; |
| 90 } else { | 97 } else { |
| 91 nodeList = unreadyNodes; | 98 nodeList = unreadyNodes; |
| 92 } | 99 } |
| 93 }.bind(this), 10); | 100 }.bind(this), 10); |
| 94 }, | 101 }, |
| 95 | 102 |
| 103 /** @param {!IronListElement} list */ | |
| 104 saveScroll: function(list) { | |
| 105 let target = list.scrollTarget; | |
|
Dan Beam
2017/02/01 06:13:17
no let until we vulcanize
stevenjb
2017/02/02 01:17:47
Oops, missed that. Will vulcanize be part of the b
Dan Beam
2017/02/02 04:18:00
yes
| |
| 106 this.saveScrollTop_[target.id] = this.saveScrollTop_[target.id] || []; | |
|
Dan Beam
2017/02/01 06:13:17
just save on the list itself, i.e.
list.prevScrol
stevenjb
2017/02/02 01:17:47
The C++ programmer in me cringes at decorating ano
| |
| 107 this.saveScrollTop_[target.id].push(target.scrollTop); | |
| 108 }, | |
| 109 | |
| 110 /** @param {!IronListElement} list */ | |
| 111 restoreScroll: function(list) { | |
| 112 let target = list.scrollTarget; | |
| 113 this.async(function() { | |
| 114 let scrollTop = this.saveScrollTop_[target.id].shift(); | |
| 115 // Ignore scrollTop of 0 in case it was intermittent (we do not need to | |
| 116 // explicity scroll to 0). | |
| 117 if (scrollTop != 0) | |
| 118 list.scroll(0, scrollTop); | |
| 119 }); | |
| 120 }, | |
| 121 | |
| 96 /** | 122 /** |
| 97 * Event wrapper for updateScroll_. | 123 * Event wrapper for updateScroll_. |
| 98 * @param {!Event} event | 124 * @param {!Event} event |
| 99 * @private | 125 * @private |
| 100 */ | 126 */ |
| 101 updateScrollEvent_: function(event) { | 127 updateScrollEvent_: function(event) { |
| 102 var scrollable = /** @type {!HTMLElement} */ (event.target); | 128 var scrollable = /** @type {!HTMLElement} */ (event.target); |
| 103 this.updateScroll_(scrollable); | 129 this.updateScroll_(scrollable); |
| 104 }, | 130 }, |
| 105 | 131 |
| 106 /** | 132 /** |
| 107 * This gets called once intially and any time a scrollable container scrolls. | 133 * This gets called once intially and any time a scrollable container scrolls. |
| 108 * @param {!HTMLElement} scrollable | 134 * @param {!HTMLElement} scrollable |
| 109 * @private | 135 * @private |
| 110 */ | 136 */ |
| 111 updateScroll_: function(scrollable) { | 137 updateScroll_: function(scrollable) { |
| 112 scrollable.classList.toggle( | 138 scrollable.classList.toggle( |
| 113 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); | 139 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); |
| 114 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); | 140 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); |
| 115 scrollable.classList.toggle( | 141 scrollable.classList.toggle( |
| 116 'scrolled-to-bottom', | 142 'scrolled-to-bottom', |
| 117 scrollable.scrollTop + scrollable.clientHeight >= | 143 scrollable.scrollTop + scrollable.clientHeight >= |
| 118 scrollable.scrollHeight); | 144 scrollable.scrollHeight); |
| 119 }, | 145 }, |
| 120 }; | 146 }; |
| OLD | NEW |