|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>Animation Plugin</title> |
|
|
7 |
|
|
|
8 |
<style type="text/css"> |
|
|
9 |
/*margin and padding on body element |
|
|
10 |
can introduce errors in determining |
|
|
11 |
element position and are not recommended; |
|
|
12 |
we turn them off as a foundation for YUI |
|
|
13 |
CSS treatments. */ |
|
|
14 |
body { |
|
|
15 |
margin:0; |
|
|
16 |
padding:0; |
|
|
17 |
} |
|
|
18 |
</style> |
|
|
19 |
|
|
|
20 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
21 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<!--begin custom header content for this example--> |
|
|
25 |
<style type="text/css"> |
|
|
26 |
.yui-overlay-content { |
|
|
27 |
background-color:#ccc; |
|
|
28 |
border:1px solid #000; |
|
|
29 |
padding:5px; |
|
|
30 |
} |
|
|
31 |
|
|
|
32 |
.yui-overlay .yui-widget-hd, |
|
|
33 |
.yui-overlay .yui-widget-bd, |
|
|
34 |
.yui-overlay .yui-widget-ft { |
|
|
35 |
background-color:#eee; |
|
|
36 |
padding:2px; |
|
|
37 |
border:1px solid #999; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
.yui-overlay .yui-widget-bd { |
|
|
41 |
background-color:#fff; |
|
|
42 |
} |
|
|
43 |
</style> |
|
|
44 |
|
|
|
45 |
<!--end custom header content for this example--> |
|
|
46 |
|
|
|
47 |
</head> |
|
|
48 |
|
|
|
49 |
<body class=" yui-skin-sam"> |
|
|
50 |
|
|
|
51 |
<h1>Animation Plugin</h1> |
|
|
52 |
|
|
|
53 |
<div class="exampleIntro"> |
|
|
54 |
<p>This example shows how you can use Widget's plugin infrastructure to customize the existing behavior of a widget.</p> |
|
|
55 |
|
|
|
56 |
<p>We create an Animation plugin class for Overlay called <code>AnimPlugin</code> which changes the way Overlay instances are shown/hidden, by fading them in and out. The Overlay is initially constructed with the <code>AnimPlugin</code> plugged in <em>(with the duration set to 2 seconds)</em>. |
|
|
57 |
Clicking the "Unplug AnimPlugin" button, will restore the original non-Animated Overlay show/hide behavior. Clicking on the "Plug AnimPlugin" button will plug in the <code>AnimPlugin</code> again, but with a shorter duration.</p> |
|
|
58 |
|
|
|
59 |
</div> |
|
|
60 |
|
|
|
61 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
62 |
|
|
|
63 |
<div id="overlay"> |
|
|
64 |
<div class="yui-widget-hd">Overlay Header</div> |
|
|
65 |
<div class="yui-widget-bd">Overlay Body</div> |
|
|
66 |
<div class="yui-widget-ft">Overlay Footer</div> |
|
|
67 |
</div> |
|
|
68 |
|
|
|
69 |
<button type="button" id="show">Show</button> |
|
|
70 |
<button type="button" id="hide">Hide</button> |
|
|
71 |
<button type="button" id="unplug">Unplug AnimPlugin</button> |
|
|
72 |
<button type="button" id="plug">Plug AnimPlugin (duration:0.5)</button> |
|
|
73 |
|
|
|
74 |
<script type="text/javascript"> |
|
|
75 |
YUI({base:"../../build/", timeout: 10000}).use("overlay", "anim", "plugin", function(Y) { |
|
|
76 |
|
|
|
77 |
/* Animation Plugin Constructor */ |
|
|
78 |
function AnimPlugin(config) { |
|
|
79 |
AnimPlugin.superclass.constructor.apply(this, arguments); |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
/* |
|
|
83 |
* The namespace for the plugin. This will be the property on the widget, which will |
|
|
84 |
* reference the plugin instance, when it's plugged in |
|
|
85 |
*/ |
|
|
86 |
AnimPlugin.NS = "fx"; |
|
|
87 |
|
|
|
88 |
/* |
|
|
89 |
* The NAME of the AnimPlugin class. Used to prefix events generated |
|
|
90 |
* by the plugin class. |
|
|
91 |
*/ |
|
|
92 |
AnimPlugin.NAME = "animPlugin"; |
|
|
93 |
|
|
|
94 |
/* |
|
|
95 |
* The default set of attributes for the AnimPlugin class. |
|
|
96 |
*/ |
|
|
97 |
AnimPlugin.ATTRS = { |
|
|
98 |
|
|
|
99 |
/* |
|
|
100 |
* Default duration. Used by the default animation implementations |
|
|
101 |
*/ |
|
|
102 |
duration : { |
|
|
103 |
value: 0.2 |
|
|
104 |
}, |
|
|
105 |
|
|
|
106 |
/* |
|
|
107 |
* Default animation instance used for showing the widget (opacity fade-in) |
|
|
108 |
*/ |
|
|
109 |
animVisible : { |
|
|
110 |
valueFn : function() { |
|
|
111 |
|
|
|
112 |
var host = this.get("host"); |
|
|
113 |
var boundingBox = host.get("boundingBox"); |
|
|
114 |
|
|
|
115 |
var anim = new Y.Anim({ |
|
|
116 |
node: boundingBox, |
|
|
117 |
to: { opacity: 1 }, |
|
|
118 |
duration: this.get("duration") |
|
|
119 |
}); |
|
|
120 |
|
|
|
121 |
// Set initial opacity, to avoid initial flicker |
|
|
122 |
if (!host.get("visible")) { |
|
|
123 |
boundingBox.setStyle("opacity", 0); |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
// Clean up, on destroy. Where supported, remove |
|
|
127 |
// opacity set using style. Else make 100% opaque |
|
|
128 |
anim.on("destroy", function() { |
|
|
129 |
if (Y.UA.ie) { |
|
|
130 |
this.get("node").setStyle("opacity", 1); |
|
|
131 |
} else { |
|
|
132 |
this.get("node").setStyle("opacity", ""); |
|
|
133 |
} |
|
|
134 |
}); |
|
|
135 |
|
|
|
136 |
return anim; |
|
|
137 |
} |
|
|
138 |
}, |
|
|
139 |
|
|
|
140 |
/* |
|
|
141 |
* Default animation instance used for hiding the widget (opacity fade-out) |
|
|
142 |
*/ |
|
|
143 |
animHidden : { |
|
|
144 |
valueFn : function() { |
|
|
145 |
return new Y.Anim({ |
|
|
146 |
node: this.get("host").get("boundingBox"), |
|
|
147 |
to: { opacity: 0 }, |
|
|
148 |
duration: this.get("duration") |
|
|
149 |
}); |
|
|
150 |
} |
|
|
151 |
} |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
/* |
|
|
155 |
* Extend the base plugin class |
|
|
156 |
*/ |
|
|
157 |
Y.extend(AnimPlugin, Y.Plugin.Base, { |
|
|
158 |
|
|
|
159 |
/* |
|
|
160 |
* Initialization code. Called when the |
|
|
161 |
* plugin is instantiated (whenever it's |
|
|
162 |
* plugged into the host) |
|
|
163 |
*/ |
|
|
164 |
initializer : function(config) { |
|
|
165 |
this._bindAnimVisible(); |
|
|
166 |
this._bindAnimHidden(); |
|
|
167 |
|
|
|
168 |
this.after("animVisibleChange", this._bindAnimVisible); |
|
|
169 |
this.after("animHiddenChange", this._bindAnimHidden); |
|
|
170 |
|
|
|
171 |
// Override default _uiSetVisible method, with custom animated method |
|
|
172 |
this.doBefore("_uiSetVisible", this._uiAnimSetVisible); |
|
|
173 |
}, |
|
|
174 |
|
|
|
175 |
/* |
|
|
176 |
* Destruction code. Invokes destroy in the individual animation instances, |
|
|
177 |
* and lets them take care of cleaning up any state. |
|
|
178 |
*/ |
|
|
179 |
destructor : function() { |
|
|
180 |
this.get("animVisible").destroy(); |
|
|
181 |
this.get("animHidden").destroy(); |
|
|
182 |
}, |
|
|
183 |
|
|
|
184 |
/* |
|
|
185 |
* The custom animation method, added by the plugin. |
|
|
186 |
* |
|
|
187 |
* This method replaces the default _uiSetVisible handler |
|
|
188 |
* Widget provides, by injecting itself before _uiSetVisible, |
|
|
189 |
* (using Plugins before method) and preventing the default |
|
|
190 |
* behavior. |
|
|
191 |
*/ |
|
|
192 |
_uiAnimSetVisible : function(val) { |
|
|
193 |
if (this.get("host").get("rendered")) { |
|
|
194 |
if (val) { |
|
|
195 |
this.get("animHidden").stop(); |
|
|
196 |
this.get("animVisible").run(); |
|
|
197 |
} else { |
|
|
198 |
this.get("animVisible").stop(); |
|
|
199 |
this.get("animHidden").run(); |
|
|
200 |
} |
|
|
201 |
return new Y.Do.Prevent("AnimPlugin prevented default show/hide"); |
|
|
202 |
} |
|
|
203 |
}, |
|
|
204 |
|
|
|
205 |
/* |
|
|
206 |
* The original Widget _uiSetVisible implementation |
|
|
207 |
*/ |
|
|
208 |
_uiSetVisible : function(val) { |
|
|
209 |
var host = this.get("host"); |
|
|
210 |
var hiddenClass = host.getClassName("hidden"); |
|
|
211 |
if (!val) { |
|
|
212 |
host.get("boundingBox").addClass(hiddenClass); |
|
|
213 |
} else { |
|
|
214 |
host.get("boundingBox").removeClass(hiddenClass); |
|
|
215 |
} |
|
|
216 |
}, |
|
|
217 |
|
|
|
218 |
/* Sets up call to invoke original visibility handling when the animVisible animation is started */ |
|
|
219 |
_bindAnimVisible : function() { |
|
|
220 |
var animVisible = this.get("animVisible"); |
|
|
221 |
|
|
|
222 |
// Setup original visibility handling (for show) before starting to animate |
|
|
223 |
animVisible.on("start", Y.bind(function() { |
|
|
224 |
this._uiSetVisible(true); |
|
|
225 |
}, this)); |
|
|
226 |
}, |
|
|
227 |
|
|
|
228 |
/* Sets up call to invoke original visibility handling when the animHidden animation is complete */ |
|
|
229 |
_bindAnimHidden : function() { |
|
|
230 |
var animHidden = this.get("animHidden"); |
|
|
231 |
|
|
|
232 |
// Setup original visibility handling (for hide) after completing animation |
|
|
233 |
animHidden.after("end", Y.bind(function() { |
|
|
234 |
this._uiSetVisible(false); |
|
|
235 |
}, this)); |
|
|
236 |
} |
|
|
237 |
}); |
|
|
238 |
|
|
|
239 |
// Create a new Overlay instance, and add AnimPlugin with a default duration of 2 seconds |
|
|
240 |
var overlay = new Y.Overlay({ |
|
|
241 |
contentBox: "#overlay", |
|
|
242 |
width:"10em", |
|
|
243 |
height:"10em", |
|
|
244 |
visible:false, |
|
|
245 |
shim:false, |
|
|
246 |
align: { |
|
|
247 |
node: "#show", |
|
|
248 |
points: ["tl", "bl"] |
|
|
249 |
}, |
|
|
250 |
plugins : [{fn:AnimPlugin, cfg:{duration:2}}] |
|
|
251 |
}); |
|
|
252 |
overlay.render(); |
|
|
253 |
|
|
|
254 |
Y.on("click", function() { |
|
|
255 |
overlay.show(); |
|
|
256 |
}, "#show"); |
|
|
257 |
|
|
|
258 |
Y.on("click", function() { |
|
|
259 |
overlay.hide(); |
|
|
260 |
}, "#hide"); |
|
|
261 |
|
|
|
262 |
Y.on("click", function() { |
|
|
263 |
overlay.unplug("fx"); |
|
|
264 |
}, "#unplug"); |
|
|
265 |
|
|
|
266 |
Y.on("click", function() { |
|
|
267 |
overlay.unplug("fx"); |
|
|
268 |
overlay.plug(AnimPlugin, {duration:0.5}); |
|
|
269 |
}, "#plug"); |
|
|
270 |
|
|
|
271 |
}); |
|
|
272 |
</script> |
|
|
273 |
|
|
|
274 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
275 |
|
|
|
276 |
</body> |
|
|
277 |
</html> |