Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: ui/webui/resources/cr_elements/cr_scrollable_behavior.js

Issue 2627373002: MD Settings: Save and restore scroll position in iron-list (Closed)
Patch Set: Use scrollTop Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 (let scrollable of scrollableElements) 56 for (let scrollable of scrollableElements)
50 this.updateScroll_(scrollable); 57 this.updateScroll_(scrollable);
51 }.bind(this)); 58 }.bind(this));
52 59
(...skipping 30 matching lines...) Expand all
83 } 90 }
84 if (unreadyNodes.length == 0) { 91 if (unreadyNodes.length == 0) {
85 window.clearInterval(this.intervalId_); 92 window.clearInterval(this.intervalId_);
86 this.intervalId_ = null; 93 this.intervalId_ = null;
87 } else { 94 } else {
88 nodeList = unreadyNodes; 95 nodeList = unreadyNodes;
89 } 96 }
90 }.bind(this), 10); 97 }.bind(this), 10);
91 }, 98 },
92 99
100 /** @param {!IronListElement} list */
101 saveScroll(list) {
102 let target = list.scrollTarget;
103 // Ignore scrollTop of 0 in case it is intermittent (we do not need to
104 // explicity scroll to 0).
105 let scroll = target.scrollTop || -1;
106 this.saveScrollTop_[target.id] = this.saveScrollTop_[target.id] || [];
107 this.saveScrollTop_[target.id].push(scroll);
108 },
109
110 /** @param {!IronListElement} list */
111 restoreScroll(list) {
112 let target = list.scrollTarget;
113 this.async(function() {
114 let scroll = this.saveScrollTop_[target.id].shift();
115 if (scroll != -1)
116 list.scroll(0, scroll);
117 });
118 },
119
93 /** 120 /**
94 * Event wrapper for updateScroll_. 121 * Event wrapper for updateScroll_.
95 * @param {!Event} event 122 * @param {!Event} event
96 * @private 123 * @private
97 */ 124 */
98 updateScrollEvent_: function(event) { 125 updateScrollEvent_: function(event) {
99 let scrollable = /** @type {!HTMLElement} */ (event.target); 126 let scrollable = /** @type {!HTMLElement} */ (event.target);
100 this.updateScroll_(scrollable); 127 this.updateScroll_(scrollable);
101 }, 128 },
102 129
103 /** 130 /**
104 * This gets called once intially and any time a scrollable container scrolls. 131 * This gets called once intially and any time a scrollable container scrolls.
105 * @param {!HTMLElement} scrollable 132 * @param {!HTMLElement} scrollable
106 * @private 133 * @private
107 */ 134 */
108 updateScroll_: function(scrollable) { 135 updateScroll_: function(scrollable) {
109 scrollable.classList.toggle( 136 scrollable.classList.toggle(
110 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); 137 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight);
111 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); 138 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0);
112 scrollable.classList.toggle( 139 scrollable.classList.toggle(
113 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= 140 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >=
114 scrollable.scrollHeight); 141 scrollable.scrollHeight);
115 }, 142 },
116 }; 143 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698