2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('widget-anim', function (Y, NAME) {
11 * Provides a plugin which can be used to animate widget visibility changes.
15 var BOUNDING_BOX = "boundingBox",
24 RENDERED = "rendered",
29 DURATION = "duration",
30 ANIM_SHOW = "animShow",
31 ANIM_HIDE = "animHide",
33 _UI_SET_VISIBLE = "_uiSetVisible",
35 ANIM_SHOW_CHANGE = "animShowChange",
36 ANIM_HIDE_CHANGE = "animHideChange";
39 * A plugin class which can be used to animate widget visibility changes.
42 * @extends Plugin.Base
45 function WidgetAnim(config) {
46 WidgetAnim.superclass.constructor.apply(this, arguments);
50 * The namespace for the plugin. This will be the property on the widget, which will
51 * reference the plugin instance, when it's plugged in.
58 WidgetAnim.NS = "anim";
61 * The NAME of the WidgetAnim class. Used to prefix events generated
62 * by the plugin class.
67 * @default "pluginWidgetAnim"
69 WidgetAnim.NAME = "pluginWidgetAnim";
72 * Pre-Packaged Animation implementations, which can be used for animShow and animHide attribute
75 * @property ANIMATIONS
78 * @default "pluginWidgetAnim"
80 WidgetAnim.ANIMATIONS = {
84 var widget = this.get(HOST),
85 boundingBox = widget.get(BOUNDING_BOX),
90 duration: this.get(DURATION)
93 // Set initial opacity, to avoid initial flicker
94 if (!widget.get(VISIBLE)) {
95 boundingBox.setStyle(OPACITY, 0);
98 // Clean up, on destroy. Where supported, remove
99 // opacity set using style. Else make 100% opaque
100 anim.on(DESTROY, function() {
101 this.get(NODE).setStyle(OPACITY, (Y.UA.ie) ? 1 : EMPTY_STR);
107 fadeOut : function() {
109 node: this.get(HOST).get(BOUNDING_BOX),
111 duration: this.get(DURATION)
117 * Static property used to define the default attribute
118 * configuration for the plugin.
127 * Default duration in seconds. Used as the default duration for the default animation implementations
129 * @attribute duration
131 * @default 0.2 (seconds
138 * Default animation instance used for showing the widget (opacity fade-in)
140 * @attribute animShow
142 * @default WidgetAnim.ANIMATIONS.fadeIn
145 valueFn: WidgetAnim.ANIMATIONS.fadeIn
149 * Default animation instance used for hiding the widget (opacity fade-out)
151 * @attribute animHide
153 * @default WidgetAnim.ANIMATIONS.fadeOut
156 valueFn: WidgetAnim.ANIMATIONS.fadeOut
160 Y.extend(WidgetAnim, Y.Plugin.Base, {
163 * The initializer lifecycle implementation. Modifies the host widget's
164 * visibililty implementation to add animation.
166 * @method initializer
167 * @param {Object} config The user configuration for the plugin
169 initializer : function(config) {
170 this._bindAnimShow();
171 this._bindAnimHide();
173 this.after(ANIM_SHOW_CHANGE, this._bindAnimShow);
174 this.after(ANIM_HIDE_CHANGE, this._bindAnimHide);
176 // Override default _uiSetVisible method, with custom animated method
177 this.beforeHostMethod(_UI_SET_VISIBLE, this._uiAnimSetVisible);
181 * The initializer destructor implementation. Responsible for destroying the configured
182 * animation instances.
186 destructor : function() {
187 this.get(ANIM_SHOW).destroy();
188 this.get(ANIM_HIDE).destroy();
192 * The injected method used to override the host widget's _uiSetVisible implementation with
193 * an animated version of the same.
195 * <p>This method replaces the default _uiSetVisible handler
196 * Widget provides, by injecting itself before _uiSetVisible,
197 * and preventing the default behavior. </p>
199 * @method _uiAnimSetVisible
201 * @param {boolean} val true, if making the widget visible. false, if hiding it.
203 _uiAnimSetVisible : function(val) {
204 if (this.get(HOST).get(RENDERED)) {
206 this.get(ANIM_HIDE).stop();
207 this.get(ANIM_SHOW).run();
209 this.get(ANIM_SHOW).stop();
210 this.get(ANIM_HIDE).run();
212 return new Y.Do.Prevent();
217 * The original Widget _uiSetVisible implementation. This currently needs to be replicated,
218 * so it can be invoked before or after the animation starts or stops, since the original
219 * methods is not available to the AOP implementation.
221 * @method _uiSetVisible
222 * @param {boolean} val true, if making the widget visible. false, if hiding it.
225 _uiSetVisible : function(val) {
226 var host = this.get(HOST),
227 hiddenClass = host.getClassName(HIDDEN);
229 host.get(BOUNDING_BOX).toggleClass(hiddenClass, !val);
233 * Binds a listener to invoke the original visibility handling when the animShow animation is started
235 * @method _bindAnimShow
238 _bindAnimShow : function() {
239 // Setup original visibility handling (for show) before starting to animate
240 this.get(ANIM_SHOW).on(START,
242 this._uiSetVisible(true);
247 * Binds a listener to invoke the original visibility handling when the animHide animation is complete
249 * @method _bindAnimHide
252 _bindAnimHide : function() {
253 // Setup original visibility handling (for hide) after completing animation
254 this.get(ANIM_HIDE).after(END,
256 this._uiSetVisible(false);
261 Y.namespace("Plugin").WidgetAnim = WidgetAnim;
264 }, '3.13.0', {"requires": ["anim-base", "plugin", "widget"]});