| 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 * @fileoverview |GlobalScrollTargetBehavior| allows an element to be aware of | 6 * @fileoverview |GlobalScrollTargetBehavior| allows an element to be aware of |
| 7 * the global scroll target. |scrollTarget| will be populated async by | 7 * the global scroll target. |
| 8 * |setGlobalScrollTarget|. |setGlobalScrollTarget| should only be called once. | 8 * |
| 9 * |scrollTarget| will be populated async by |setGlobalScrollTarget|. |
| 10 * |
| 11 * |subpageScrollTarget| will be equal to the |scrollTarget|, but will only be |
| 12 * populated when the current route is the |subpageRoute|. |
| 13 * |
| 14 * |setGlobalScrollTarget| should only be called once. |
| 9 */ | 15 */ |
| 10 | 16 |
| 11 cr.define('settings', function() { | 17 cr.define('settings', function() { |
| 12 var scrollTargetResolver = new PromiseResolver(); | 18 var scrollTargetResolver = new PromiseResolver(); |
| 13 | 19 |
| 14 /** @polymerBehavior */ | 20 /** @polymerBehavior */ |
| 15 var GlobalScrollTargetBehavior = { | 21 var GlobalScrollTargetBehaviorImpl = { |
| 16 properties: { | 22 properties: { |
| 17 /** | 23 /** |
| 18 * Read only property for the scroll target. | 24 * Read only property for the scroll target. |
| 19 * @type {HTMLElement} | 25 * @type {HTMLElement} |
| 20 */ | 26 */ |
| 21 scrollTarget: { | 27 scrollTarget: { |
| 22 type: Object, | 28 type: Object, |
| 23 readOnly: true, | 29 readOnly: true, |
| 24 }, | 30 }, |
| 31 |
| 32 /** |
| 33 * Read only property for the scroll target that a subpage should use. |
| 34 * It will be set/cleared based on the current route. |
| 35 * @type {HTMLElement} |
| 36 */ |
| 37 subpageScrollTarget: { |
| 38 type: Object, |
| 39 computed: 'getActiveTarget_(scrollTarget, active_)', |
| 40 }, |
| 41 |
| 42 /** |
| 43 * The |subpageScrollTarget| should only be set for this route. |
| 44 * @type {settings.Route} |
| 45 * @private |
| 46 */ |
| 47 subpageRoute: Object, |
| 48 |
| 49 /** Whether the |subpageRoute| is active or not. */ |
| 50 active_: Boolean, |
| 25 }, | 51 }, |
| 26 | 52 |
| 27 /** @override */ | 53 /** @override */ |
| 28 attached: function() { | 54 attached: function() { |
| 29 scrollTargetResolver.promise.then(this._setScrollTarget.bind(this)); | 55 scrollTargetResolver.promise.then(this._setScrollTarget.bind(this)); |
| 30 }, | 56 }, |
| 57 |
| 58 /** @param {!settings.Route} route */ |
| 59 currentRouteChanged: function(route) { |
| 60 this.active_ = route == this.subpageRoute; |
| 61 }, |
| 62 |
| 63 /** |
| 64 * Returns the target only when the route is active. |
| 65 * @param {HTMLElement} target |
| 66 * @param {boolean} active |
| 67 * @return {?HTMLElement} |
| 68 * @private |
| 69 */ |
| 70 getActiveTarget_: function(target, active) { |
| 71 return active ? target : null; |
| 72 }, |
| 31 }; | 73 }; |
| 32 | 74 |
| 33 /** | 75 /** |
| 34 * This should only be called once. | 76 * This should only be called once. |
| 35 * @param {HTMLElement} scrollTarget | 77 * @param {HTMLElement} scrollTarget |
| 36 */ | 78 */ |
| 37 var setGlobalScrollTarget = function(scrollTarget) { | 79 var setGlobalScrollTarget = function(scrollTarget) { |
| 38 scrollTargetResolver.resolve(scrollTarget); | 80 scrollTargetResolver.resolve(scrollTarget); |
| 39 }; | 81 }; |
| 40 | 82 |
| 41 return { | 83 return { |
| 42 GlobalScrollTargetBehavior: GlobalScrollTargetBehavior, | 84 GlobalScrollTargetBehaviorImpl: GlobalScrollTargetBehaviorImpl, |
| 43 setGlobalScrollTarget: setGlobalScrollTarget, | 85 setGlobalScrollTarget: setGlobalScrollTarget, |
| 86 scrollTargetResolver: scrollTargetResolver, |
| 44 }; | 87 }; |
| 45 }); | 88 }); |
| 89 |
| 90 // This is done to make the closure compiler happy: it needs fully qualified |
| 91 // names when specifying an array of behaviors. |
| 92 /** @polymerBehavior */ |
| 93 settings.GlobalScrollTargetBehavior = |
| 94 [settings.RouteObserverBehavior, settings.GlobalScrollTargetBehaviorImpl]; |
| OLD | NEW |