Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 cr.define('settings', function() { | |
| 6 /** | |
| 7 * Expands a card to fill its container's client height, so it covers the | |
| 8 * container from the top padding edge to the bottom padding edge. | |
| 9 * @see https://www.w3.org/TR/cssom-view-1/#terminology | |
| 10 * @constructor | |
| 11 * @implements {settings.animation.AnimationGroup} | |
| 12 * @param {!HTMLElement} card Card to expand. | |
| 13 * @param {!HTMLElement} container Container whose height the card will span. | |
| 14 */ | |
| 15 function ExpandCardTransition(card, container) { | |
| 16 /** @private */ | |
| 17 this.card_ = card; | |
| 18 /** @private */ | |
| 19 this.container_ = container; | |
| 20 } | |
| 21 | |
| 22 ExpandCardTransition.prototype = { | |
| 23 __proto__: settings.animation.AnimationGroup.prototype, | |
| 24 | |
| 25 /** @override */ | |
| 26 play: function() { | |
| 27 // If we're running this transition before anything else, we won't have | |
| 28 // the information we need, so just expand immediately and finish. | |
| 29 if (typeof this.card_.origHeight_ == 'undefined' || | |
| 30 this.card_.style.top == '' || this.card_.style.height == '') { | |
| 31 this.finished = Promise.resolve(); | |
| 32 return this.finished; | |
| 33 } | |
| 34 | |
| 35 // Align the card with the container's padding edge. | |
| 36 var startingTop = this.card_.getBoundingClientRect().top; | |
| 37 | |
| 38 // Target position is the container's top edge in the viewport. | |
| 39 var targetTop = this.container_.getBoundingClientRect().top; | |
| 40 | |
| 41 // The target height shouldn't use the container's current height, because | |
| 42 // the container may resize when the window resizes. And height: 100% | |
| 43 // wouldn't use the container's height because we're position: fixed. | |
| 44 // | |
| 45 // Instead, find the part of the window height *not* used by the container | |
| 46 // (e.g., for a toolbar) and calc the height dynamically. This assumes the | |
| 47 // excluded height stays constant. | |
| 48 var excludedHeight = window.innerHeight - this.container_.clientHeight; | |
| 49 var targetHeight = 'calc(100% - ' + excludedHeight + 'px)'; | |
| 50 | |
| 51 // Expand the card. The card's height must be 100% of the container's | |
| 52 // height or taller, so we use minHeight rather than height. | |
| 53 var keyframes = [{ | |
| 54 top: startingTop + 'px', | |
| 55 minHeight: this.card_.style.height, | |
| 56 easing: settings.animation.Timing.EASING, | |
| 57 }, { | |
| 58 top: targetTop + 'px', | |
| 59 minHeight: 'calc(100% - ' + targetTop + 'px)', | |
| 60 }]; | |
| 61 var options = /** @type {!KeyframeEffectOptions} */({ | |
| 62 duration: settings.animation.Timing.DURATION, | |
| 63 }); | |
| 64 | |
| 65 /** @type {?settings.animation.Animation} */ | |
| 66 this.animation_ = | |
| 67 new settings.animation.Animation(this.card_, keyframes, options); | |
| 68 this.finished = this.animation_.finished; | |
| 69 return this.finished; | |
| 70 }, | |
| 71 | |
| 72 /** @override */ | |
| 73 finish: function() { | |
| 74 if (!this.animation_) | |
| 75 return; | |
| 76 this.animation_.finish(); | |
| 77 this.animation_ = null; | |
| 78 }, | |
| 79 | |
| 80 /** @override */ | |
| 81 cancel: function() { | |
| 82 if (!this.animation_) | |
| 83 return; | |
| 84 this.animation_.cancel(); | |
| 85 this.animation_ = null; | |
| 86 }, | |
| 87 | |
| 88 /** @override */ | |
| 89 finished: null, | |
| 90 }; | |
| 91 | |
| 92 /** | |
| 93 * Collapses an expanded card back into its original section. | |
| 94 * @constructor | |
| 95 * @implements {settings.animation.AnimationGroup} | |
| 96 * @param {!SettingsSectionElement} section Section that contains the card. | |
| 97 * @param {!HTMLElement} container Container the card currently spans. | |
| 98 */ | |
| 99 function CollapseCardTransition(section, container) { | |
| 100 /** @private {!HTMLElement} */ | |
| 101 this.card_ = assert(section.$['card']); | |
|
Dan Beam
2016/07/12 02:01:54
can you make this not poke into $.idName? can we
michaelpg
2016/07/20 22:10:57
Done.
| |
| 102 /** @private */ | |
| 103 this.section_ = section; | |
| 104 /** @private */ | |
| 105 this.container_ = container; | |
| 106 } | |
| 107 | |
| 108 CollapseCardTransition.prototype = { | |
| 109 __proto__: settings.animation.AnimationGroup.prototype, | |
| 110 | |
| 111 /** Sets up the transition before other page content has been unhidden. */ | |
|
Dan Beam
2016/07/12 02:01:54
can you note how this should be used? i.e. call b
michaelpg
2016/07/20 22:10:57
Done.
| |
| 112 setUp: function() { | |
| 113 this.card_.style.width = this.card_.clientWidth + 'px'; | |
| 114 this.card_.style.height = this.card_.clientHeight + 'px'; | |
| 115 }, | |
| 116 | |
| 117 /** @override */ | |
| 118 play: function() { | |
| 119 var startingTop = this.container_.getBoundingClientRect().top; | |
| 120 | |
| 121 // The card is unpositioned, so use its position as the ending state, | |
| 122 // but account for scroll. | |
| 123 var targetTop = this.card_.getBoundingClientRect().top - | |
| 124 this.container_.scrollTop; | |
| 125 | |
| 126 // Account for the section header. | |
| 127 var headerStyle = getComputedStyle(this.section_.$['header']); | |
|
Dan Beam
2016/07/12 02:01:54
same here: can we expose header rather than use th
michaelpg
2016/07/20 22:10:57
Done.
| |
| 128 targetTop += this.section_.$['header'].offsetHeight + | |
| 129 parseFloat(headerStyle.marginBottom) + | |
| 130 parseFloat(headerStyle.marginTop); | |
| 131 | |
| 132 var keyframes = [{ | |
| 133 top: startingTop + 'px', | |
| 134 minHeight: this.card_.style.height, | |
| 135 easing: settings.animation.Timing.EASING, | |
| 136 }, { | |
| 137 top: targetTop + 'px', | |
| 138 minHeight: | |
| 139 /** @type {{origHeight_: number}} */(this.card_).origHeight_ + 'px', | |
| 140 }]; | |
| 141 var options = /** @type {!KeyframeEffectOptions} */({ | |
| 142 duration: settings.animation.Timing.DURATION, | |
| 143 }); | |
| 144 | |
| 145 this.card_.style.height = ''; | |
| 146 | |
| 147 /** @type {?settings.animation.Animation} */ | |
| 148 this.animation_ = | |
| 149 new settings.animation.Animation(this.card_, keyframes, options); | |
| 150 this.finished = this.animation_.finished; | |
| 151 // Clean up after the animation finishes or cancels. | |
| 152 this.finished.catch(function() {}).then(function() { | |
| 153 this.card_.style.width = ''; | |
| 154 }.bind(this)); | |
| 155 return this.finished; | |
| 156 }, | |
| 157 | |
| 158 /** @override */ | |
| 159 finish: function() { | |
| 160 if (!this.animation_) | |
| 161 return; | |
| 162 this.animation_.finish(); | |
| 163 this.animation_ = null; | |
| 164 }, | |
| 165 | |
| 166 /** @override */ | |
| 167 cancel: function() { | |
| 168 if (!this.animation_) | |
| 169 return; | |
| 170 this.animation_.cancel(); | |
| 171 this.animation_ = null; | |
| 172 }, | |
| 173 | |
| 174 /** @override */ | |
| 175 finished: null, | |
| 176 }; | |
| 177 | |
| 178 return { | |
| 179 CollapseCardTransition: CollapseCardTransition, | |
| 180 ExpandCardTransition: ExpandCardTransition, | |
| 181 }; | |
| 182 }); | |
| OLD | NEW |