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

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: Rebase Created 3 years, 10 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 (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
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(list) {
Dan Beam 2017/01/31 04:06:44 drop ES6 stuff
stevenjb 2017/01/31 18:43:09 Done.
105 let target = list.scrollTarget;
106 // Ignore scrollTop of 0 in case it is intermittent (we do not need to
107 // explicity scroll to 0).
108 let scroll = target.scrollTop || -1;
Dan Beam 2017/01/31 04:06:44 why wouldn't we just do list.prevScrollTop_ = lis
stevenjb 2017/01/31 18:43:09 Code evolution. Done.
109 this.saveScrollTop_[target.id] = this.saveScrollTop_[target.id] || [];
110 this.saveScrollTop_[target.id].push(scroll);
111 },
112
113 /** @param {!IronListElement} list */
114 restoreScroll(list) {
115 let target = list.scrollTarget;
116 this.async(function() {
117 let scroll = this.saveScrollTop_[target.id].shift();
118 if (scroll != -1)
119 list.scroll(0, scroll);
Dan Beam 2017/01/31 04:06:44 if (list.prevScrollTop_) list.scroll(0, list.pre
stevenjb 2017/01/31 18:43:09 Done.
120 });
121 },
122
96 /** 123 /**
97 * Event wrapper for updateScroll_. 124 * Event wrapper for updateScroll_.
98 * @param {!Event} event 125 * @param {!Event} event
99 * @private 126 * @private
100 */ 127 */
101 updateScrollEvent_: function(event) { 128 updateScrollEvent_: function(event) {
102 var scrollable = /** @type {!HTMLElement} */ (event.target); 129 var scrollable = /** @type {!HTMLElement} */ (event.target);
103 this.updateScroll_(scrollable); 130 this.updateScroll_(scrollable);
104 }, 131 },
105 132
106 /** 133 /**
107 * This gets called once intially and any time a scrollable container scrolls. 134 * This gets called once intially and any time a scrollable container scrolls.
108 * @param {!HTMLElement} scrollable 135 * @param {!HTMLElement} scrollable
109 * @private 136 * @private
110 */ 137 */
111 updateScroll_: function(scrollable) { 138 updateScroll_: function(scrollable) {
112 scrollable.classList.toggle( 139 scrollable.classList.toggle(
113 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight); 140 'can-scroll', scrollable.clientHeight < scrollable.scrollHeight);
114 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0); 141 scrollable.classList.toggle('is-scrolled', scrollable.scrollTop > 0);
115 scrollable.classList.toggle( 142 scrollable.classList.toggle(
116 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >= 143 'scrolled-to-bottom', scrollable.scrollTop + scrollable.clientHeight >=
117 scrollable.scrollHeight); 144 scrollable.scrollHeight);
118 }, 145 },
119 }; 146 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698