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 * Responds to route changes by expanding, collapsing, or scrolling to sections | 6 * Responds to route changes by expanding, collapsing, or scrolling to sections |
| 7 * on the page. Expanded sections take up the full height of the container. At | 7 * on the page. Expanded sections take up the full height of the container. At |
| 8 * most one section should be expanded at any given time. | 8 * most one section should be expanded at any given time. |
| 9 * @polymerBehavior MainPageBehavior | 9 * @polymerBehavior MainPageBehavior |
| 10 */ | 10 */ |
| 11 var MainPageBehaviorImpl = { | 11 var MainPageBehaviorImpl = { |
| 12 /** @type {?HTMLElement} The scrolling container. */ | 12 /** @type {?HTMLElement} The scrolling container. */ |
| 13 scroller: null, | 13 scroller: null, |
| 14 | 14 |
| 15 /** @override */ | 15 /** @override */ |
| 16 attached: function() { | 16 attached: function() { |
| 17 if (this.domHost && this.domHost.parentNode.tagName == 'PAPER-HEADER-PANEL') | 17 if (this.domHost && this.domHost.parentNode.tagName == 'PAPER-HEADER-PANEL') |
| 18 this.scroller = this.domHost.parentNode.scroller; | 18 this.scroller = this.domHost.parentNode.scroller; |
| 19 else | 19 else |
| 20 this.scroller = document.body; // Used in unit tests. | 20 this.scroller = document.body; // Used in unit tests. |
| 21 }, | 21 }, |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * @param {!settings.Route} newRoute | 24 * @param {!settings.Route} newRoute |
| 25 * @param {settings.Route} oldRoute | 25 * @param {settings.Route} oldRoute |
| 26 */ | 26 */ |
| 27 currentRouteChanged: function(newRoute, oldRoute) { | 27 currentRouteChanged: function(newRoute, oldRoute) { |
| 28 // Scroll to the section for any non-popstate navigation. Also scroll for | |
| 29 // any popstate navigations that are in-page. | |
|
Dan Beam
2016/11/15 01:24:22
this is kinda confusing. "Scroll to the section e
tommycli
2016/11/15 21:18:23
Done.
| |
| 30 var oldRouteWasSection = oldRoute && oldRoute.parent && oldRoute.section && | |
| 31 oldRoute.parent.section != oldRoute.section; | |
| 32 var scrollToSection = | |
| 33 !settings.lastRouteChangeWasPopstate() || oldRouteWasSection; | |
|
Dan Beam
2016/11/15 01:24:21
nit: 2 more spaces
tommycli
2016/11/15 21:18:23
Done.
| |
| 34 | |
| 28 // If this is the first route, or the page was hidden, allow the page to | 35 // If this is the first route, or the page was hidden, allow the page to |
| 29 // render before expanding the section. | 36 // render before expanding the section. |
| 30 if (!oldRoute && newRoute.contains(settings.getCurrentRoute()) || | 37 if (!oldRoute && newRoute.contains(settings.getCurrentRoute()) || |
| 31 this.scrollHeight == 0) { | 38 this.scrollHeight == 0) { |
| 32 setTimeout(this.tryTransitionToSection_.bind(this)); | 39 setTimeout(this.tryTransitionToSection_.bind(this, scrollToSection)); |
| 33 } else { | 40 } else { |
| 34 this.tryTransitionToSection_(); | 41 this.tryTransitionToSection_(scrollToSection); |
| 35 } | 42 } |
| 36 }, | 43 }, |
| 37 | 44 |
| 38 /** | 45 /** |
| 39 * If possible, transitions to the current route's section (by expanding or | 46 * If possible, transitions to the current route's section (by expanding or |
| 40 * scrolling to it). If another transition is running, finishes or cancels | 47 * scrolling to it). If another transition is running, finishes or cancels |
| 41 * that one, then schedules this function again. This ensures the current | 48 * that one, then schedules this function again. This ensures the current |
| 42 * section is quickly shown, without getting the page into a broken state -- | 49 * section is quickly shown, without getting the page into a broken state -- |
| 43 * if currentRoute changes in between calls, just transition to the new route. | 50 * if currentRoute changes in between calls, just transition to the new route. |
| 51 * @param {boolean} scrollToSection | |
| 44 * @private | 52 * @private |
| 45 */ | 53 */ |
| 46 tryTransitionToSection_: function() { | 54 tryTransitionToSection_: function(scrollToSection) { |
| 47 var currentRoute = settings.getCurrentRoute(); | 55 var currentRoute = settings.getCurrentRoute(); |
| 48 var currentSection = this.getSection(currentRoute.section); | 56 var currentSection = this.getSection(currentRoute.section); |
| 49 | 57 |
| 50 // If an animation is already playing, try finishing or canceling it. | 58 // If an animation is already playing, try finishing or canceling it. |
| 51 if (this.currentAnimation_) { | 59 if (this.currentAnimation_) { |
| 52 this.maybeStopCurrentAnimation_(); | 60 this.maybeStopCurrentAnimation_(); |
| 53 // Either way, this function will be called again once the current | 61 // Either way, this function will be called again once the current |
| 54 // animation ends. | 62 // animation ends. |
| 55 return; | 63 return; |
| 56 } | 64 } |
| 57 | 65 |
| 58 var promise; | 66 var promise; |
| 59 var expandedSection = /** @type {?SettingsSectionElement} */( | 67 var expandedSection = /** @type {?SettingsSectionElement} */( |
| 60 this.$$('settings-section.expanded')); | 68 this.$$('settings-section.expanded')); |
| 61 if (expandedSection) { | 69 if (expandedSection) { |
| 62 // If the section shouldn't be expanded, collapse it. | 70 // If the section shouldn't be expanded, collapse it. |
| 63 if (!currentRoute.isSubpage() || expandedSection != currentSection) { | 71 if (!currentRoute.isSubpage() || expandedSection != currentSection) { |
| 64 promise = this.collapseSection_(expandedSection); | 72 promise = this.collapseSection_(expandedSection); |
| 65 // Scroll to the collapsed section. | 73 // Scroll to the collapsed section. |
| 66 if (currentSection && !settings.lastRouteChangeWasPopstate()) | 74 if (currentSection && scrollToSection) |
| 67 currentSection.scrollIntoView(); | 75 currentSection.scrollIntoView(); |
| 68 } else { | 76 } else { |
| 69 // Scroll to top while sliding to another subpage. | 77 // Scroll to top while sliding to another subpage. |
| 70 this.scroller.scrollTop = 0; | 78 this.scroller.scrollTop = 0; |
| 71 } | 79 } |
| 72 } else if (currentSection) { | 80 } else if (currentSection) { |
| 73 // Expand the section into a subpage or scroll to it on the main page. | 81 // Expand the section into a subpage or scroll to it on the main page. |
| 74 if (currentRoute.isSubpage()) | 82 if (currentRoute.isSubpage()) |
| 75 promise = this.expandSection_(currentSection); | 83 promise = this.expandSection_(currentSection); |
| 76 else if (!settings.lastRouteChangeWasPopstate()) | 84 else if (scrollToSection) |
| 77 currentSection.scrollIntoView(); | 85 currentSection.scrollIntoView(); |
| 78 } | 86 } |
| 79 | 87 |
| 80 // When this animation ends, another may be necessary. Call this function | 88 // When this animation ends, another may be necessary. Call this function |
| 81 // again after the promise resolves. | 89 // again after the promise resolves. |
| 82 if (promise) | 90 if (promise) |
| 83 promise.then(this.tryTransitionToSection_.bind(this)); | 91 promise.then(this.tryTransitionToSection_.bind(this, scrollToSection)); |
| 84 }, | 92 }, |
| 85 | 93 |
| 86 /** | 94 /** |
| 87 * If the current animation is inconsistent with the current route, stops the | 95 * If the current animation is inconsistent with the current route, stops the |
| 88 * animation by finishing or canceling it so the new route can be animated to. | 96 * animation by finishing or canceling it so the new route can be animated to. |
| 89 * @private | 97 * @private |
| 90 */ | 98 */ |
| 91 maybeStopCurrentAnimation_: function() { | 99 maybeStopCurrentAnimation_: function() { |
| 92 var currentRoute = settings.getCurrentRoute(); | 100 var currentRoute = settings.getCurrentRoute(); |
| 93 var animatingSection = /** @type {?SettingsSectionElement} */( | 101 var animatingSection = /** @type {?SettingsSectionElement} */( |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 return /** @type {?SettingsSectionElement} */( | 267 return /** @type {?SettingsSectionElement} */( |
| 260 this.$$('settings-section[section="' + section + '"]')); | 268 this.$$('settings-section[section="' + section + '"]')); |
| 261 }, | 269 }, |
| 262 }; | 270 }; |
| 263 | 271 |
| 264 /** @polymerBehavior */ | 272 /** @polymerBehavior */ |
| 265 var MainPageBehavior = [ | 273 var MainPageBehavior = [ |
| 266 settings.RouteObserverBehavior, | 274 settings.RouteObserverBehavior, |
| 267 MainPageBehaviorImpl, | 275 MainPageBehaviorImpl, |
| 268 ]; | 276 ]; |
| OLD | NEW |