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 * @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 | |
|
michaelpg
2016/12/06 02:52:56
nit: @return
| |
| 67 * @private | |
| 68 */ | |
| 69 getActiveTarget_: function(target, active) { | |
| 70 return active ? target : null; | |
| 71 }, | |
| 31 }; | 72 }; |
| 32 | 73 |
| 33 /** | 74 /** |
| 34 * This should only be called once. | 75 * This should only be called once. |
| 35 * @param {HTMLElement} scrollTarget | 76 * @param {HTMLElement} scrollTarget |
| 36 */ | 77 */ |
| 37 var setGlobalScrollTarget = function(scrollTarget) { | 78 var setGlobalScrollTarget = function(scrollTarget) { |
| 38 scrollTargetResolver.resolve(scrollTarget); | 79 scrollTargetResolver.resolve(scrollTarget); |
| 39 }; | 80 }; |
| 40 | 81 |
| 41 return { | 82 return { |
| 42 GlobalScrollTargetBehavior: GlobalScrollTargetBehavior, | 83 GlobalScrollTargetBehaviorImpl: GlobalScrollTargetBehaviorImpl, |
| 43 setGlobalScrollTarget: setGlobalScrollTarget, | 84 setGlobalScrollTarget: setGlobalScrollTarget, |
| 85 scrollTargetResolver: scrollTargetResolver, | |
| 44 }; | 86 }; |
| 45 }); | 87 }); |
| 88 | |
| 89 // This is done to make the closure compiler happy: it needs fully qualified | |
| 90 // names when specifying an array of behaviors. | |
| 91 /** @polymerBehavior */ | |
| 92 settings.GlobalScrollTargetBehavior = | |
| 93 [settings.RouteObserverBehavior, settings.GlobalScrollTargetBehaviorImpl]; | |
| OLD | NEW |