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 * 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 27 matching lines...) Expand all Loading... | |
38 attached: function() { | 38 attached: function() { |
39 if (this.domHost && this.domHost.parentNode.tagName == 'PAPER-HEADER-PANEL') | 39 if (this.domHost && this.domHost.parentNode.tagName == 'PAPER-HEADER-PANEL') |
40 this.scroller = this.domHost.parentNode.scroller; | 40 this.scroller = this.domHost.parentNode.scroller; |
41 else | 41 else |
42 this.scroller = document.body; // Used in unit tests. | 42 this.scroller = document.body; // Used in unit tests. |
43 }, | 43 }, |
44 | 44 |
45 /** | 45 /** |
46 * @param {!settings.Route} newRoute | 46 * @param {!settings.Route} newRoute |
47 * @param {settings.Route} oldRoute | 47 * @param {settings.Route} oldRoute |
48 * @param {boolean} isPopstate | |
48 */ | 49 */ |
49 currentRouteChanged: function(newRoute, oldRoute) { | 50 currentRouteChanged: function(newRoute, oldRoute, isPopstate) { |
50 // Allow the page to load before expanding the section. TODO(michaelpg): | 51 // Allow the page to load before expanding the section. TODO(michaelpg): |
51 // Time this better when refactoring settings-animated-pages. | 52 // Time this better when refactoring settings-animated-pages. |
52 if (!oldRoute && newRoute.isSubpage()) { | 53 if (!oldRoute && newRoute.isSubpage()) { |
53 setTimeout(this.tryTransitionToSection_.bind(this)); | 54 setTimeout(this.tryTransitionToSection_.bind(this, isPopstate)); |
54 } else { | 55 } else { |
55 doWhenReady( | 56 doWhenReady( |
56 function() { | 57 function() { |
57 return this.scrollHeight > 0; | 58 return this.scrollHeight > 0; |
58 }.bind(this), | 59 }.bind(this), |
59 this.tryTransitionToSection_.bind(this)); | 60 this.tryTransitionToSection_.bind(this, isPopstate)); |
60 } | 61 } |
61 }, | 62 }, |
62 | 63 |
63 /** | 64 /** |
64 * If possible, transitions to the current route's section (by expanding or | 65 * If possible, transitions to the current route's section (by expanding or |
65 * scrolling to it). If another transition is running, finishes or cancels | 66 * scrolling to it). If another transition is running, finishes or cancels |
66 * that one, then schedules this function again. This ensures the current | 67 * that one, then schedules this function again. This ensures the current |
67 * section is quickly shown, without getting the page into a broken state -- | 68 * section is quickly shown, without getting the page into a broken state -- |
68 * if currentRoute changes in between calls, just transition to the new route. | 69 * if currentRoute changes in between calls, just transition to the new route. |
70 * @param {boolean} isPopstate | |
69 * @private | 71 * @private |
70 */ | 72 */ |
71 tryTransitionToSection_: function() { | 73 tryTransitionToSection_: function(isPopstate) { |
72 var currentRoute = settings.getCurrentRoute(); | 74 var currentRoute = settings.getCurrentRoute(); |
73 var currentSection = this.getSection(currentRoute.section); | 75 var currentSection = this.getSection(currentRoute.section); |
74 | 76 |
75 // If an animation is already playing, try finishing or canceling it. | 77 // If an animation is already playing, try finishing or canceling it. |
76 if (this.currentAnimation_) { | 78 if (this.currentAnimation_) { |
77 this.maybeStopCurrentAnimation_(); | 79 this.maybeStopCurrentAnimation_(); |
78 // Either way, this function will be called again once the current | 80 // Either way, this function will be called again once the current |
79 // animation ends. | 81 // animation ends. |
80 return; | 82 return; |
81 } | 83 } |
82 | 84 |
83 var promise; | 85 var promise; |
84 var expandedSection = /** @type {?SettingsSectionElement} */( | 86 var expandedSection = /** @type {?SettingsSectionElement} */( |
85 this.$$('settings-section.expanded')); | 87 this.$$('settings-section.expanded')); |
86 if (expandedSection) { | 88 if (expandedSection) { |
87 // If the section shouldn't be expanded, collapse it. | 89 // If the section shouldn't be expanded, collapse it. |
88 if (!currentRoute.isSubpage() || expandedSection != currentSection) { | 90 if (!currentRoute.isSubpage() || expandedSection != currentSection) { |
89 promise = this.collapseSection_(expandedSection); | 91 promise = this.collapseSection_(expandedSection); |
90 // Scroll to the collapsed section. | 92 // Scroll to the collapsed section. |
91 if (currentSection) | 93 if (currentSection && !isPopstate) |
92 currentSection.scrollIntoView(); | 94 currentSection.scrollIntoView(); |
93 } | 95 } |
94 } else if (currentSection) { | 96 } else if (currentSection) { |
95 // Expand the section into a subpage or scroll to it on the main page. | 97 // Expand the section into a subpage or scroll to it on the main page. |
96 if (currentRoute.isSubpage()) | 98 if (currentRoute.isSubpage()) |
97 promise = this.expandSection_(currentSection); | 99 promise = this.expandSection_(currentSection); |
98 else | 100 else if (!isPopstate) |
99 currentSection.scrollIntoView(); | 101 currentSection.scrollIntoView(); |
100 } | 102 } |
101 | 103 |
102 // When this animation ends, another may be necessary. Call this function | 104 // When this animation ends, another may be necessary. Call this function |
103 // again after the promise resolves. | 105 // again after the promise resolves. |
104 if (promise) | 106 if (promise) |
105 promise.then(this.tryTransitionToSection_.bind(this)); | 107 promise.then(this.tryTransitionToSection_.bind(this, isPopstate)); |
michaelpg
2016/09/02 19:37:42
tryTransitionToSection transitions to whatever the
tommycli
2016/09/02 20:53:43
Done.
| |
106 }, | 108 }, |
107 | 109 |
108 /** | 110 /** |
109 * If the current animation is inconsistent with the current route, stops the | 111 * If the current animation is inconsistent with the current route, stops the |
110 * animation by finishing or canceling it so the new route can be animated to. | 112 * animation by finishing or canceling it so the new route can be animated to. |
111 * @private | 113 * @private |
112 */ | 114 */ |
113 maybeStopCurrentAnimation_: function() { | 115 maybeStopCurrentAnimation_: function() { |
114 var currentRoute = settings.getCurrentRoute(); | 116 var currentRoute = settings.getCurrentRoute(); |
115 var animatingSection = /** @type {?SettingsSectionElement} */( | 117 var animatingSection = /** @type {?SettingsSectionElement} */( |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 return Promise.resolve(); | 223 return Promise.resolve(); |
222 } | 224 } |
223 | 225 |
224 // Play the actual collapse animation. | 226 // Play the actual collapse animation. |
225 return new Promise(function(resolve, reject) { | 227 return new Promise(function(resolve, reject) { |
226 // Wait for the other sections to show up so we can scroll properly. | 228 // Wait for the other sections to show up so we can scroll properly. |
227 setTimeout(function() { | 229 setTimeout(function() { |
228 var newSection = settings.getCurrentRoute().section && | 230 var newSection = settings.getCurrentRoute().section && |
229 this.getSection(settings.getCurrentRoute().section); | 231 this.getSection(settings.getCurrentRoute().section); |
230 | 232 |
231 // Scroll to the section if indicated by the route. TODO(michaelpg): Is | 233 this.scroller.scrollTop = this.origScrollTop_; |
232 // this the right behavior, or should we return to the previous scroll | |
233 // position? | |
234 if (newSection) | |
235 newSection.scrollIntoView(); | |
236 else | |
237 this.scroller.scrollTop = this.origScrollTop_; | |
238 | 234 |
239 this.currentAnimation_ = section.animateCollapse( | 235 this.currentAnimation_ = section.animateCollapse( |
240 /** @type {!HTMLElement} */(this.scroller)); | 236 /** @type {!HTMLElement} */(this.scroller)); |
241 | 237 |
242 this.currentAnimation_.finished.catch(function() { | 238 this.currentAnimation_.finished.catch(function() { |
243 // The collapse was canceled, so the page is showing a subpage still. | 239 // The collapse was canceled, so the page is showing a subpage still. |
244 this.fire('subpage-expand'); | 240 this.fire('subpage-expand'); |
245 }.bind(this)).then(function() { | 241 }.bind(this)).then(function() { |
246 // Clean up after the animation succeeds or cancels. | 242 // Clean up after the animation succeeds or cancels. |
247 section.setFrozen(false); | 243 section.setFrozen(false); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 this.scroller.style.width = 'calc(100% - ' + scrollbarWidth + 'px)'; | 294 this.scroller.style.width = 'calc(100% - ' + scrollbarWidth + 'px)'; |
299 } | 295 } |
300 } | 296 } |
301 }; | 297 }; |
302 | 298 |
303 /** @polymerBehavior */ | 299 /** @polymerBehavior */ |
304 var MainPageBehavior = [ | 300 var MainPageBehavior = [ |
305 settings.RouteObserverBehavior, | 301 settings.RouteObserverBehavior, |
306 MainPageBehaviorImpl, | 302 MainPageBehaviorImpl, |
307 ]; | 303 ]; |
OLD | NEW |