| 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 | |
| 7 * |readyCallback|. | |
| 8 * @param {function():boolean} readyTest | |
| 9 * @param {!Function} readyCallback | |
| 10 */ | |
| 11 function doWhenReady(readyTest, readyCallback) { | |
| 12 if (readyTest()) { | |
| 13 readyCallback(); | |
| 14 return; | |
| 15 } | |
| 16 | |
| 17 // TODO(michaelpg): Remove this hack. | |
| 18 // See also: https://github.com/Polymer/polymer/issues/3629 | |
| 19 var intervalId = setInterval(function() { | |
| 20 if (readyTest()) { | |
| 21 clearInterval(intervalId); | |
| 22 readyCallback(); | |
| 23 } | |
| 24 }, 10); | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * Responds to route changes by expanding, collapsing, or scrolling to sections | 6 * Responds to route changes by expanding, collapsing, or scrolling to sections |
| 29 * 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 |
| 30 * most one section should be expanded at any given time. | 8 * most one section should be expanded at any given time. |
| 31 * @polymerBehavior MainPageBehavior | 9 * @polymerBehavior MainPageBehavior |
| 32 */ | 10 */ |
| 33 var MainPageBehaviorImpl = { | 11 var MainPageBehaviorImpl = { |
| 34 properties: { | |
| 35 // Name of the root route corresponding to this page. | |
| 36 route: String, | |
| 37 }, | |
| 38 | |
| 39 /** @type {?HTMLElement} The scrolling container. */ | 12 /** @type {?HTMLElement} The scrolling container. */ |
| 40 scroller: null, | 13 scroller: null, |
| 41 | 14 |
| 42 /** @override */ | 15 /** @override */ |
| 43 attached: function() { | 16 attached: function() { |
| 44 if (this.domHost && this.domHost.parentNode.tagName == 'PAPER-HEADER-PANEL') | 17 if (this.domHost && this.domHost.parentNode.tagName == 'PAPER-HEADER-PANEL') |
| 45 this.scroller = this.domHost.parentNode.scroller; | 18 this.scroller = this.domHost.parentNode.scroller; |
| 46 else | 19 else |
| 47 this.scroller = document.body; // Used in unit tests. | 20 this.scroller = document.body; // Used in unit tests. |
| 48 }, | 21 }, |
| 49 | 22 |
| 50 /** | 23 /** |
| 51 * @param {!settings.Route} newRoute | 24 * @param {!settings.Route} newRoute |
| 52 * @param {settings.Route} oldRoute | 25 * @param {settings.Route} oldRoute |
| 53 */ | 26 */ |
| 54 currentRouteChanged: function(newRoute, oldRoute) { | 27 currentRouteChanged: function(newRoute, oldRoute) { |
| 55 // Allow the page to load before expanding the section. TODO(michaelpg): | 28 // If this is the first route, or the page was hidden, allow the page to |
| 56 // Time this better when refactoring settings-animated-pages. | 29 // render before expanding the section. |
| 57 if (!oldRoute && newRoute.isSubpage()) { | 30 if (!oldRoute && newRoute.contains(settings.getCurrentRoute()) || |
| 31 this.scrollHeight == 0) { |
| 58 setTimeout(this.tryTransitionToSection_.bind(this)); | 32 setTimeout(this.tryTransitionToSection_.bind(this)); |
| 59 } else { | 33 } else { |
| 60 doWhenReady( | 34 this.tryTransitionToSection_(); |
| 61 function() { | |
| 62 return this.scrollHeight > 0 || | |
| 63 !settings.Route[this.route].contains(settings.getCurrentRoute()); | |
| 64 }.bind(this), | |
| 65 this.tryTransitionToSection_.bind(this)); | |
| 66 } | 35 } |
| 67 }, | 36 }, |
| 68 | 37 |
| 69 /** | 38 /** |
| 70 * If possible, transitions to the current route's section (by expanding or | 39 * If possible, transitions to the current route's section (by expanding or |
| 71 * scrolling to it). If another transition is running, finishes or cancels | 40 * scrolling to it). If another transition is running, finishes or cancels |
| 72 * that one, then schedules this function again. This ensures the current | 41 * that one, then schedules this function again. This ensures the current |
| 73 * section is quickly shown, without getting the page into a broken state -- | 42 * section is quickly shown, without getting the page into a broken state -- |
| 74 * if currentRoute changes in between calls, just transition to the new route. | 43 * if currentRoute changes in between calls, just transition to the new route. |
| 75 * @private | 44 * @private |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 return /** @type {?SettingsSectionElement} */( | 259 return /** @type {?SettingsSectionElement} */( |
| 291 this.$$('settings-section[section="' + section + '"]')); | 260 this.$$('settings-section[section="' + section + '"]')); |
| 292 }, | 261 }, |
| 293 }; | 262 }; |
| 294 | 263 |
| 295 /** @polymerBehavior */ | 264 /** @polymerBehavior */ |
| 296 var MainPageBehavior = [ | 265 var MainPageBehavior = [ |
| 297 settings.RouteObserverBehavior, | 266 settings.RouteObserverBehavior, |
| 298 MainPageBehaviorImpl, | 267 MainPageBehaviorImpl, |
| 299 ]; | 268 ]; |
| OLD | NEW |