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 | |
dschuyler
2016/06/30 01:24:04
Please indent code within braces.
michaelpg
2016/07/07 05:27:43
Done.
| |
7 /** | |
8 * Freezes the section's height so its card can be removed from the flow | |
9 * without affecting the layout of the surrounding sections. | |
10 * @param {!HTMLElement} section | |
11 */ | |
12 function freezeSection(section) { | |
13 var card = section.$['card']; | |
14 section.style.height = section.clientHeight + 'px'; | |
15 | |
16 var cardHeight = card.offsetHeight; | |
17 var cardWidth = card.offsetWidth; | |
18 | |
19 // If the section is not displayed yet (e.g., navigated directly to a | |
20 // sub-page), cardHeight and cardWidth are 0, so do not set the height or | |
21 // width explicitly. | |
22 // TODO(michaelpg): Improve this logic when refactoring | |
23 // settings-animated-pages. | |
24 if (cardHeight && cardWidth) { | |
25 // TODO(michaelpg): Temporary hack to store the height the section should | |
26 // collapse to when it closes. | |
27 card.origHeight_ = cardHeight; | |
28 | |
29 card.style.height = cardHeight + 'px'; | |
30 card.style.width = cardWidth + 'px'; | |
31 } else { | |
32 // Set an invalid value so we don't try to use it later. | |
33 card.origHeight_ = NaN; | |
34 } | |
35 | |
36 // Place the section's card at its current position but removed from the | |
37 // flow. | |
38 card.style.top = card.getBoundingClientRect().top + 'px'; | |
39 section.classList.add('frozen'); | |
40 } | |
41 | |
42 /** | |
43 * After freezeSection, restores the section to its normal height. | |
44 * @param {!HTMLElement} section | |
45 */ | |
46 function unfreezeSection(section) { | |
47 if (!section.classList.contains('frozen')) | |
48 return; | |
49 var card = section.$['card']; | |
50 section.classList.remove('frozen'); | |
51 card.style.top = ''; | |
52 card.style.height = ''; | |
53 card.style.width = ''; | |
54 section.style.height = ''; | |
55 } | |
56 | |
57 /** | |
58 * Opens a section by hiding other sections and expanding the section's card to | |
59 * fill the screen vertically. | |
60 * @constructor | |
61 * @extends {settings.animation.Transition} | |
62 * @param {!SettingsSectionElement} section | |
63 * @param {!HTMLElement} container Scrolling container of sections. | |
64 */ | |
65 function OpenSectionTransition(section, container) { | |
66 this.section = section; | |
67 this.container = container; | |
68 | |
69 this.expandCardTransition_ = | |
70 new settings.ExpandCardTransition(section.$['card'], container); | |
71 } | |
72 | |
73 OpenSectionTransition.prototype = { | |
74 __proto__: settings.animation.Transition.prototype, | |
75 | |
76 /** @override */ | |
77 play: function() { | |
78 // TODO(michaelpg): Change elevation of sections. | |
79 freezeSection(this.section); | |
80 this.section.classList.add('expanding'); | |
81 | |
82 this.expandCardTransition_.play(); | |
83 | |
84 this.finished = this.expandCardTransition_.finished | |
85 .then(function() { | |
86 this.cleanUp_(true); | |
87 }.bind(this)) | |
88 .catch(function(e) { | |
89 this.cleanUp_(false); | |
90 throw e; | |
91 }.bind(this)); | |
92 return this.finished; | |
93 }, | |
94 | |
95 /** @override */ | |
96 finish: function() { | |
97 if (this.expandCardTransition_) | |
98 this.expandCardTransition_.finish(); | |
99 }, | |
100 | |
101 /** @override */ | |
102 cancel: function() { | |
103 if (this.expandCardTransition_) | |
104 this.expandCardTransition_.cancel(); | |
105 }, | |
106 | |
107 /** | |
108 * @param {boolean} finished Whether the animation finished. | |
109 * @private | |
110 */ | |
111 cleanUp_: function(finished) { | |
112 this.expandCardTransition_ = null; | |
113 if (finished) { | |
114 this.section.classList.add('expanded'); | |
115 } else { | |
116 // Animation was canceled; restore the section. | |
117 unfreezeSection(this.section); | |
118 } | |
119 | |
120 var card = this.section.$['card']; | |
121 this.section.classList.remove('expanding'); | |
122 this.section.style.height = ''; | |
123 card.style.top = ''; | |
124 card.style.height = ''; | |
125 card.style.width = ''; | |
126 }, | |
127 }; | |
128 | |
129 /** | |
130 * Closes a section by showing the other sections and collapsing the section's | |
131 * card back into place. | |
132 * @constructor | |
133 * @extends {settings.animation.Transition} | |
134 * @param {!SettingsSectionElement} section | |
135 * @param {!HTMLElement} container Scrolling container of sections. | |
136 */ | |
137 function CloseSectionTransition(section, container) { | |
138 this.section = section; | |
139 this.container = container; | |
140 | |
141 assert(!section.classList.contains('expanding')); | |
142 assert(!section.classList.contains('collapsing')); | |
143 assert(section.classList.contains('expanded')); | |
144 | |
145 this.collapseCardTransition_ = | |
146 new settings.CollapseCardTransition(section, container); | |
147 } | |
148 | |
149 CloseSectionTransition.prototype = { | |
150 __proto__: settings.animation.Transition.prototype, | |
151 | |
152 /** | |
153 * Prepares the section for the transition by making it position: fixed. This | |
154 * is useful as a separate step so the page can unhide everything before | |
155 * starting the transition without this section affecting the flow. | |
156 */ | |
157 setUp: function() { | |
158 this.collapseCardTransition_.setUp(); | |
159 this.section.classList.add('collapsing'); | |
160 this.section.classList.remove('expanding', 'expanded'); | |
161 var card = this.section.$['card']; | |
162 if (!isNaN(card.origHeight_)) { | |
163 this.section.style.height = | |
164 this.section.clientHeight + card.origHeight_ + 'px'; | |
165 } | |
166 }, | |
167 | |
168 /** @override */ | |
169 play: function() { | |
170 var card = this.section.$['card']; | |
171 if (!isNaN(card.origHeight_)) | |
172 this.finished = this.collapseCardTransition_.play(); | |
173 | |
174 // If we navigated here directly, we don't know the original height of the | |
175 // section, so we skip the animation. | |
176 // TODO(michaelpg): remove this condition once sliding is implemented as a | |
177 // transition. | |
178 if (isNaN(card.origHeight_)) | |
179 this.finished = Promise.resolve(); | |
180 | |
181 this.finished.then( | |
182 function() { | |
183 unfreezeSection(this.section); | |
184 }.bind(this), function() { | |
dschuyler
2016/06/30 01:24:04
since these are separate parameters, let's line
br
michaelpg
2016/07/07 05:27:43
Done.
| |
185 this.section.classList.add('expanded'); | |
186 }.bind(this)); | |
187 | |
188 // Whether the animation finishes or cancels, clean up. | |
189 this.finished.catch(function() {}).then(function() { | |
190 this.section.classList.remove('collapsing'); | |
191 this.collapseCardTransition_ = null; | |
192 }.bind(this)); | |
193 | |
194 return this.finished; | |
195 }, | |
196 | |
197 /** @override */ | |
198 finish: function() { | |
199 if (this.collapseCardTransition_) | |
200 this.collapseCardTransition_.finish(); | |
201 }, | |
202 | |
203 /** @override */ | |
204 cancel: function() { | |
205 if (this.collapseCardTransition_) | |
206 this.collapseCardTransition_.cancel(); | |
207 }, | |
208 }; | |
209 | |
210 return { | |
211 CloseSectionTransition: CloseSectionTransition, | |
212 OpenSectionTransition: OpenSectionTransition, | |
213 }; | |
214 | |
215 }); | |
OLD | NEW |