Debian Codegraph @

closure-library/closure/goog/ui/animatedzippy.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Animated zippy widget implementation.
17 *
18 * @author [email protected] (Emil A Eklund)
19 * @see ../demos/zippy.html
20 */
21
22 goog.provide('goog.ui.AnimatedZippy');
23
24 goog.require('goog.a11y.aria.Role');
25 goog.require('goog.dom');
26 goog.require('goog.dom.TagName');
27 goog.require('goog.events');
28 goog.require('goog.fx.Animation');
29 goog.require('goog.fx.Transition');
30 goog.require('goog.fx.easing');
31 goog.require('goog.ui.Zippy');
32 goog.require('goog.ui.ZippyEvent');
33
34
35
36 /**
37 * Zippy widget. Expandable/collapsible container, clicking the header toggles
38 * the visibility of the content.
39 *
40 * @param {Element|string|null} header Header element, either element
41 * reference, string id or null if no header exists.
42 * @param {Element|string} content Content element, either element reference or
43 * string id.
44 * @param {boolean=} opt_expanded Initial expanded/visibility state. Defaults to
45 * false.
46 * @param {goog.dom.DomHelper=} opt_domHelper An optional DOM helper.
47 * @param {goog.a11y.aria.Role<string>=} opt_role ARIA role, default TAB.
48 * @constructor
49 * @extends {goog.ui.Zippy}
50 */
51 goog.ui.AnimatedZippy = function(
52 header, content, opt_expanded, opt_domHelper, opt_role) {
53 var domHelper = opt_domHelper || goog.dom.getDomHelper();
54
55 // Create wrapper element and move content into it.
56 var elWrapper =
57 domHelper.createDom(goog.dom.TagName.DIV, {'style': 'overflow:hidden'});
58 var elContent = domHelper.getElement(content);
59 elContent.parentNode.replaceChild(elWrapper, elContent);
60 elWrapper.appendChild(elContent);
61
62 /**
63 * Content wrapper, used for animation.
64 * @type {Element}
65 * @private
66 */
67 this.elWrapper_ = elWrapper;
68
69 /**
70 * Reference to animation or null if animation is not active.
71 * @type {goog.fx.Animation}
72 * @private
73 */
74 this.anim_ = null;
75
76 // Call constructor of super class.
77 goog.ui.Zippy.call(
78 this, header, elContent, opt_expanded, undefined, domHelper, opt_role);
79
80 // Set initial state.
81 // NOTE: Set the class names as well otherwise animated zippys
82 // start with empty class names.
83 var expanded = this.isExpanded();
84 this.elWrapper_.style.display = expanded ? '' : 'none';
85 this.updateHeaderClassName(expanded);
86 };
87 goog.inherits(goog.ui.AnimatedZippy, goog.ui.Zippy);
88 goog.tagUnsealableClass(goog.ui.AnimatedZippy);
89
90
91 /**
92 * Constants for event names.
93 *
94 * @const
95 */
96 goog.ui.AnimatedZippy.Events = {
97 // The beginning of the animation when the zippy state toggles.
98 TOGGLE_ANIMATION_BEGIN: goog.events.getUniqueId('toggleanimationbegin'),
99 // The end of the animation when the zippy state toggles.
100 TOGGLE_ANIMATION_END: goog.events.getUniqueId('toggleanimationend')
101 };
102
103
104 /**
105 * Duration of expand/collapse animation, in milliseconds.
106 * @type {number}
107 */
108 goog.ui.AnimatedZippy.prototype.animationDuration = 500;
109
110
111 /**
112 * Acceleration function for expand/collapse animation.
113 * @type {!Function}
114 */
115 goog.ui.AnimatedZippy.prototype.animationAcceleration = goog.fx.easing.easeOut;
116
117
118 /**
119 * @return {boolean} Whether the zippy is in the process of being expanded or
120 * collapsed.
121 */
122 goog.ui.AnimatedZippy.prototype.isBusy = function() {
123 return this.anim_ != null;
124 };
125
126
127 /**
128 * Sets expanded state.
129 *
130 * @param {boolean} expanded Expanded/visibility state.
131 * @override
132 */
133 goog.ui.AnimatedZippy.prototype.setExpanded = function(expanded) {
134 if (this.isExpanded() == expanded && !this.anim_) {
135 return;
136 }
137
138 // Reset display property of wrapper to allow content element to be
139 // measured.
140 if (this.elWrapper_.style.display == 'none') {
141 this.elWrapper_.style.display = '';
142 }
143
144 // Measure content element.
145 var h = this.getContentElement().offsetHeight;
146
147 // Stop active animation (if any) and determine starting height.
148 var startH = 0;
149 if (this.anim_) {
150 expanded = this.isExpanded();
151 goog.events.removeAll(this.anim_);
152 this.anim_.stop(false);
153
154 var marginTop = parseInt(this.getContentElement().style.marginTop, 10);
155 startH = h - Math.abs(marginTop);
156 } else {
157 startH = expanded ? 0 : h;
158 }
159
160 // Updates header class name after the animation has been stopped.
161 this.updateHeaderClassName(expanded);
162
163 // Set up expand/collapse animation.
164 this.anim_ = new goog.fx.Animation(
165 [0, startH], [0, expanded ? h : 0], this.animationDuration,
166 this.animationAcceleration);
167
168 var events = [
169 goog.fx.Transition.EventType.BEGIN, goog.fx.Animation.EventType.ANIMATE,
170 goog.fx.Transition.EventType.END
171 ];
172 goog.events.listen(this.anim_, events, this.onAnimate_, false, this);
173 goog.events.listen(
174 this.anim_, goog.fx.Transition.EventType.BEGIN,
175 goog.bind(this.onAnimationBegin_, this, expanded));
176 goog.events.listen(
177 this.anim_, goog.fx.Transition.EventType.END,
178 goog.bind(this.onAnimationCompleted_, this, expanded));
179
180 // Start animation.
181 this.anim_.play(false);
182 };
183
184
185 /**
186 * Called during animation
187 *
188 * @param {goog.events.Event} e The event.
189 * @private
190 */
191 goog.ui.AnimatedZippy.prototype.onAnimate_ = function(e) {
192 var contentElement = this.getContentElement();
193 var h = contentElement.offsetHeight;
194 contentElement.style.marginTop = (e.y - h) + 'px';
195 };
196
197
198 /**
199 * Called once the expand/collapse animation has started.
200 *
201 * @param {boolean} expanding Expanded/visibility state.
202 * @private
203 */
204 goog.ui.AnimatedZippy.prototype.onAnimationBegin_ = function(expanding) {
205 this.dispatchEvent(new goog.ui.ZippyEvent(
206 goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_BEGIN, this, expanding));
207 };
208
209
210 /**
211 * Called once the expand/collapse animation has completed.
212 *
213 * @param {boolean} expanded Expanded/visibility state.
214 * @private
215 */
216 goog.ui.AnimatedZippy.prototype.onAnimationCompleted_ = function(expanded) {
217 // Fix wrong end position if the content has changed during the animation.
218 if (expanded) {
219 this.getContentElement().style.marginTop = '0';
220 }
221
222 goog.events.removeAll(/** @type {!goog.fx.Animation} */ (this.anim_));
223 this.setExpandedInternal(expanded);
224 this.anim_ = null;
225
226 if (!expanded) {
227 this.elWrapper_.style.display = 'none';
228 }
229
230 // Fire toggle event.
231 this.dispatchEvent(
232 new goog.ui.ZippyEvent(goog.ui.Zippy.Events.TOGGLE, this, expanded));
233 this.dispatchEvent(new goog.ui.ZippyEvent(
234 goog.ui.AnimatedZippy.Events.TOGGLE_ANIMATION_END, this, expanded));
235 };