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

Side by Side Diff: chrome/browser/resources/settings/settings_page/main_page_behavior.js

Issue 2230123002: MD Settings: fix collapse animation once and for all (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@Overscroll2
Patch Set: for review Created 4 years, 4 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 * Calls |readyTest| repeatedly until it returns true, then calls 6 * Calls |readyTest| repeatedly until it returns true, then calls
7 * |readyCallback|. 7 * |readyCallback|.
8 * @param {function():boolean} readyTest 8 * @param {function():boolean} readyTest
9 * @param {!Function} readyCallback 9 * @param {!Function} readyCallback
10 */ 10 */
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 return; 70 return;
71 } 71 }
72 72
73 var promise; 73 var promise;
74 var expandedSection = /** @type {?SettingsSectionElement} */( 74 var expandedSection = /** @type {?SettingsSectionElement} */(
75 this.$$('settings-section.expanded')); 75 this.$$('settings-section.expanded'));
76 if (expandedSection) { 76 if (expandedSection) {
77 // If the section shouldn't be expanded, collapse it. 77 // If the section shouldn't be expanded, collapse it.
78 if (!currentRoute.isSubpage() || expandedSection != currentSection) { 78 if (!currentRoute.isSubpage() || expandedSection != currentSection) {
79 promise = this.collapseSection_(expandedSection); 79 promise = this.collapseSection_(expandedSection);
80 // Scroll to the collapsed section. TODO(michaelpg): This can look weird 80 // Scroll to the collapsed section.
81 // because the collapse we just scheduled calculated its end target
82 // based on the current scroll position. This bug existed before, and is
83 // fixed in the next patch by making the card position: absolute.
84 if (currentSection) 81 if (currentSection)
85 this.scrollToSection_(); 82 this.scrollToSection_();
86 } 83 }
87 } else if (currentSection) { 84 } else if (currentSection) {
88 // Expand the section into a subpage or scroll to it on the main page. 85 // Expand the section into a subpage or scroll to it on the main page.
89 if (currentRoute.isSubpage()) 86 if (currentRoute.isSubpage())
90 promise = this.expandSection_(currentSection); 87 promise = this.expandSection_(currentSection);
91 else 88 else
92 this.scrollToSection_(); 89 this.scrollToSection_();
93 } 90 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 assert(this.scroller); 143 assert(this.scroller);
147 assert(section.canAnimateExpand()); 144 assert(section.canAnimateExpand());
148 145
149 // Save the scroller position before freezing it. 146 // Save the scroller position before freezing it.
150 this.origScrollTop_ = this.scroller.scrollTop; 147 this.origScrollTop_ = this.scroller.scrollTop;
151 this.toggleScrolling_(false); 148 this.toggleScrolling_(false);
152 149
153 // Freeze the section's height so its card can be removed from the flow. 150 // Freeze the section's height so its card can be removed from the flow.
154 section.setFrozen(true); 151 section.setFrozen(true);
155 152
153 var finished;
156 this.currentAnimation_ = section.animateExpand(this.scroller); 154 this.currentAnimation_ = section.animateExpand(this.scroller);
157 var promise = this.currentAnimation_ ? 155 return this.currentAnimation_.finished.then(function() {
158 this.currentAnimation_.finished : Promise.resolve(); 156 // Hide other sections and scroll to the top of the subpage.
159 157 this.classList.add('showing-subpage');
160 var finished; 158 this.toggleOtherSectionsHidden_(section.section, true);
161 return promise.then(function() {
162 this.scroller.scrollTop = 0; 159 this.scroller.scrollTop = 0;
163 this.toggleOtherSectionsHidden_(section.section, true); 160 // Notify that the page is fully expanded.
161 this.fire('subpage-expand');
164 finished = true; 162 finished = true;
Dan Beam 2016/08/17 03:01:02 could we maybe add some new lines in this code?
michaelpg 2016/08/19 21:55:01 Done.
165 }.bind(this), function() { 163 }.bind(this), function() {
166 // The animation was canceled; restore the section. 164 // The animation was canceled; restore the section and scroll position.
167 section.setFrozen(false); 165 section.setFrozen(false);
166 this.scroller.scrollTop = this.origScrollTop_;
168 finished = false; 167 finished = false;
169 }).then(function() { 168 }.bind(this)).then(function() {
170 section.cleanUpAnimateExpand(finished);
171 this.toggleScrolling_(true); 169 this.toggleScrolling_(true);
172 this.currentAnimation_ = null; 170 this.currentAnimation_ = null;
173 }.bind(this)); 171 }.bind(this));
174 }, 172 },
175 173
176 /** 174 /**
177 * Animates the card in |section|, collapsing it back into its section. 175 * Animates the card in |section|, collapsing it back into its section.
178 * @param {!SettingsSectionElement} section 176 * @param {!SettingsSectionElement} section
179 * @return {!Promise} Resolved when the transition is finished or canceled. 177 * @return {!Promise} Resolved when the transition is finished or canceled.
180 * @private
181 */ 178 */
182 collapseSection_: function(section) { 179 collapseSection_: function(section) {
183 assert(this.scroller); 180 assert(this.scroller);
184 assert(section.canAnimateCollapse()); 181 assert(section.classList.contains('expanded'));
185 182
183 var canAnimateCollapse = section.canAnimateCollapse();
184 if (canAnimateCollapse) {
185 this.toggleScrolling_(false);
186 // Do the initial collapse setup, which takes the section out of the flow,
187 // before showing everything.
188 section.setUpAnimateCollapse(this.scroller);
189 } else {
190 section.classList.remove('expanded');
191 }
192
193 // Show everything.
186 this.toggleOtherSectionsHidden_(section.section, false); 194 this.toggleOtherSectionsHidden_(section.section, false);
187 this.toggleScrolling_(false); 195 this.classList.remove('showing-subpage');
188 196
189 this.currentAnimation_ = 197 if (!canAnimateCollapse) {
190 section.animateCollapse(this.scroller, this.origScrollTop_); 198 // Finish by restoring the section into the page.
191 var promise = this.currentAnimation_ ? 199 section.setFrozen(false);
192 this.currentAnimation_.finished : Promise.resolve(); 200 return Promise.resolve();
201 }
193 202
194 return promise.then(function() { 203 // Play the actual collapse animation.
195 section.cleanUpAnimateCollapse(true); 204 return new Promise(function(resolve, reject) {
196 }, function() { 205 // Wait for the other sections to show up so we can scroll properly.
197 section.cleanUpAnimateCollapse(false); 206 setTimeout(function() {
198 }).then(function() { 207 var newSection = settings.getCurrentRoute().section &&
199 section.setFrozen(false); 208 this.getSection(settings.getCurrentRoute().section);
200 section.classList.remove('collapsing'); 209
201 this.toggleScrolling_(true); 210 // Scroll to the section if indicated by the route. TODO(michaelpg): Is
202 this.currentAnimation_ = null; 211 // this the right behavior, or should we return to the previous scroll
212 // position?
213 if (newSection)
214 newSection.scrollIntoView();
215 else
216 this.scroller.scrollTop = this.origScrollTop_;
217
218 this.currentAnimation_ = section.animateCollapse(
219 /** @type {!HTMLElement} */(this.scroller));
220
221 this.currentAnimation_.finished.catch(function() {
222 // The collapse was canceled, so the page is showing a subpage still.
223 this.fire('subpage-expand');
224 }.bind(this)).then(function() {
225 // Clean up after the animation succeeds or cancels.
226 section.setFrozen(false);
227 section.classList.remove('collapsing');
228 this.toggleScrolling_(true);
229 this.currentAnimation_ = null;
230 resolve();
231 }.bind(this));
232 }.bind(this));
203 }.bind(this)); 233 }.bind(this));
204 }, 234 },
205 235
206 /**
207 * Hides or unhides the sections not being expanded.
208 * @param {string} sectionName The section to keep visible.
209 * @param {boolean} hidden Whether the sections should be hidden.
210 * @private
211 */
212 toggleOtherSectionsHidden_: function(sectionName, hidden) {
213 var sections = Polymer.dom(this.root).querySelectorAll(
214 'settings-section');
215 for (var section of sections)
216 section.hidden = hidden && (section.section != sectionName);
217 },
218
219 /** @private */ 236 /** @private */
220 scrollToSection_: function() { 237 scrollToSection_: function() {
221 doWhenReady( 238 doWhenReady(
222 function() { 239 function() {
223 return this.scrollHeight > 0; 240 return this.scrollHeight > 0;
224 }.bind(this), 241 }.bind(this),
225 function() { 242 function() {
226 // If the current section changes while we are waiting for the page to 243 // If the current section changes while we are waiting for the page to
227 // be ready, scroll to the newest requested section. 244 // be ready, scroll to the newest requested section.
228 var section = this.getSection(settings.getCurrentRoute().section); 245 var section = this.getSection(settings.getCurrentRoute().section);
229 if (section) 246 if (section)
230 section.scrollIntoView(); 247 section.scrollIntoView();
231 }.bind(this)); 248 }.bind(this));
232 }, 249 },
233 250
234 /** 251 /**
252 /**
253 * Hides or unhides the sections not being expanded.
254 * @param {string} sectionName The section to keep visible.
255 * @param {boolean} hidden Whether the sections should be hidden.
256 * @private
257 */
258 toggleOtherSectionsHidden_: function(sectionName, hidden) {
259 var sections = Polymer.dom(this.root).querySelectorAll(
260 'settings-section');
261 for (var section of sections)
262 section.hidden = hidden && (section.section != sectionName);
263 },
264
265 /**
235 * Helper function to get a section from the local DOM. 266 * Helper function to get a section from the local DOM.
236 * @param {string} section Section name of the element to get. 267 * @param {string} section Section name of the element to get.
237 * @return {?SettingsSectionElement} 268 * @return {?SettingsSectionElement}
238 */ 269 */
239 getSection: function(section) { 270 getSection: function(section) {
240 if (!section) 271 if (!section)
241 return null; 272 return null;
242 return /** @type {?SettingsSectionElement} */( 273 return /** @type {?SettingsSectionElement} */(
243 this.$$('settings-section[section="' + section + '"]')); 274 this.$$('settings-section[section="' + section + '"]'));
244 }, 275 },
(...skipping 16 matching lines...) Expand all
261 this.scroller.style.width = 'calc(100% - ' + scrollbarWidth + 'px)'; 292 this.scroller.style.width = 'calc(100% - ' + scrollbarWidth + 'px)';
262 } 293 }
263 } 294 }
264 }; 295 };
265 296
266 /** @polymerBehavior */ 297 /** @polymerBehavior */
267 var MainPageBehavior = [ 298 var MainPageBehavior = [
268 settings.RouteObserverBehavior, 299 settings.RouteObserverBehavior,
269 MainPageBehaviorImpl, 300 MainPageBehaviorImpl,
270 ]; 301 ];
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698