source: Dev/branches/rest-dojo-ui/client/dojox/fx/_base.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 8.9 KB
Line 
1define(["dojo/_base/array","dojo/_base/lang", "dojo/_base/fx", "dojo/fx", "dojo/dom", "dojo/dom-style",
2            "dojo/dom-geometry", "dojo/_base/connect", "dojo/_base/html"],
3        function(arrayUtil, lang, baseFx, coreFx, dom, domStyle, domGeom, connectUtil, htmlUtil){
4// summary: Experimental and extended Animations beyond Dojo Core / Base functionality.
5//      Provides advanced Lines, Animations, and convenience aliases.
6var dojoxFx = lang.getObject("dojox.fx", true);
7/*
8lang.mixin(dojox.fx, {
9
10        // anim: Function
11        //      Alias of `dojo.anim` - the shorthand `dojo.animateProperty` with auto-play
12        anim: dojo.fx.anim,
13
14        // animateProperty: Function
15        //      Alias of `dojo.animateProperty` - animate any CSS property
16        animateProperty: dojox.fx.animateProperty,
17
18        // fadeTo: Function
19        //              Fade an element from an opacity to an opacity.
20        //              Omit `start:` property to detect. `end:` property is required.
21        //              Ultimately an alias to `dojo._fade`
22        fadeTo: dojo._fade,
23
24        // fadeIn: Function
25        //      Alias of `dojo.fadeIn` - Fade a node in.
26        fadeIn: dojo.fadeIn,
27       
28        // fadeOut: Function
29        //      Alias of `dojo.fadeOut` - Fades a node out.
30        fadeOut: dojo.fadeOut,
31
32        // combine: Function
33        //      Alias of `dojo.fx.combine` - Run an array of animations in parallel
34        combine: dojo.fx.combine,
35
36        // chain: Function
37        //      Alias of `dojo.fx.chain` - Run an array of animations in sequence
38        chain: dojo.fx.chain,
39
40        // slideTo: Function
41        //      Alias of `dojo.fx.slideTo` - Slide a node to a defined top/left coordinate
42        slideTo: dojo.fx.slideTo,
43
44        // wipeIn: Function
45        //      Alias of `dojo.fx.wipeIn` - Wipe a node to visible
46        wipeIn: dojo.fx.wipeIn,
47
48        // wipeOut: Function
49        //      Alias of `dojo.fx.wipeOut` - Wipe a node to non-visible
50        wipeOut: dojo.fx.wipeOut
51
52});
53*/
54
55dojoxFx.sizeTo = function(/* Object */args){
56        // summary:
57        //              Creates an animation that will size a node
58        //
59        // description:
60        //              Returns an animation that will size the target node
61        //              defined in args Object about it's center to
62        //              a width and height defined by (args.width, args.height),
63        //              supporting an optional method: chain||combine mixin
64        //              (defaults to chain).
65        //
66        //      - works best on absolutely or relatively positioned elements
67        //
68        // example:
69        //      |       // size #myNode to 400px x 200px over 1 second
70        //      |       dojo.fx.sizeTo({
71        //      |               node:'myNode',
72        //      |               duration: 1000,
73        //      |               width: 400,
74        //      |               height: 200,
75        //      |               method: "combine"
76        //      |       }).play();
77        //
78
79        var node = args.node = dom.byId(args.node),
80                abs = "absolute";
81
82        var method = args.method || "chain";
83        if(!args.duration){ args.duration = 500; } // default duration needed
84        if(method == "chain"){ args.duration = Math.floor(args.duration / 2); }
85       
86        var top, newTop, left, newLeft, width, height = null;
87
88        var init = (function(n){
89                return function(){
90                        var cs = domStyle.getComputedStyle(n),
91                                pos = cs.position,
92                                w = cs.width,
93                                h = cs.height
94                        ;
95                       
96                        top = (pos == abs ? n.offsetTop : parseInt(cs.top) || 0);
97                        left = (pos == abs ? n.offsetLeft : parseInt(cs.left) || 0);
98                        width = (w == "auto" ? 0 : parseInt(w));
99                        height = (h == "auto" ? 0 : parseInt(h));
100                       
101                        newLeft = left - Math.floor((args.width - width) / 2);
102                        newTop = top - Math.floor((args.height - height) / 2);
103
104                        if(pos != abs && pos != 'relative'){
105                                var ret = domStyle.coords(n, true);
106                                top = ret.y;
107                                left = ret.x;
108                                n.style.position = abs;
109                                n.style.top = top + "px";
110                                n.style.left = left + "px";
111                        }
112                }
113        })(node);
114
115        var anim1 = baseFx.animateProperty(lang.mixin({
116                properties: {
117                        height: function(){
118                                init();
119                                return { end: args.height || 0, start: height };
120                        },
121                        top: function(){
122                                return { start: top, end: newTop };
123                        }
124                }
125        }, args));
126        var anim2 = baseFx.animateProperty(lang.mixin({
127                properties: {
128                        width: function(){
129                                return { start: width, end: args.width || 0 }
130                        },
131                        left: function(){
132                                return { start: left, end: newLeft }
133                        }
134                }
135        }, args));
136
137        var anim = coreFx[(args.method == "combine" ? "combine" : "chain")]([anim1, anim2]);
138        return anim; // dojo.Animation
139
140};
141
142dojoxFx.slideBy = function(/* Object */args){
143        // summary:
144        //              Returns an animation to slide a node by a defined offset.
145        //
146        // description:
147        //              Returns an animation that will slide a node (args.node) from it's
148        //              current position to it's current posision plus the numbers defined
149        //              in args.top and args.left. standard dojo.fx mixin's apply.
150        //
151        // example:
152        //      |       // slide domNode 50px down, and 22px left
153        //      |       dojox.fx.slideBy({
154        //      |               node: domNode, duration:400,
155        //      |               top: 50, left: -22
156        //      |       }).play();
157
158        var node = args.node = dom.byId(args.node),
159                top, left;
160
161        var init = (function(n){
162                return function(){
163                        var cs = domStyle.getComputedStyle(n);
164                        var pos = cs.position;
165                        top = (pos == 'absolute' ? n.offsetTop : parseInt(cs.top) || 0);
166                        left = (pos == 'absolute' ? n.offsetLeft : parseInt(cs.left) || 0);
167                        if(pos != 'absolute' && pos != 'relative'){
168                                var ret = domGeom.coords(n, true);
169                                top = ret.y;
170                                left = ret.x;
171                                n.style.position = "absolute";
172                                n.style.top = top + "px";
173                                n.style.left = left + "px";
174                        }
175                }
176        })(node);
177        init();
178       
179        var _anim = baseFx.animateProperty(lang.mixin({
180                properties: {
181                        // FIXME: is there a way to update the _Line after creation?
182                        // null start values allow chaining to work, animateProperty will
183                        // determine them for us (except in ie6? -- ugh)
184                        top: top + (args.top || 0),
185                        left: left + (args.left || 0)
186                }
187        }, args));
188        connectUtil.connect(_anim, "beforeBegin", _anim, init);
189        return _anim; // dojo.Animation
190};
191
192dojoxFx.crossFade = function(/* Object */args){
193        // summary:
194        //              Returns an animation cross fading two element simultaneously
195        //
196        // args:
197        //      args.nodes: Array - two element array of domNodes, or id's
198        //
199        //      all other standard animation args mixins apply. args.node ignored.
200        //
201
202        // simple check for which node is visible, maybe too simple?
203        var node1 = args.nodes[0] = dom.byId(args.nodes[0]),
204                op1 = htmlUtil.style(node1,"opacity"),
205                node2 = args.nodes[1] = dom.byId(args.nodes[1]),
206                op2 = htmlUtil.style(node2, "opacity")
207        ;
208       
209        var _anim = coreFx.combine([
210                baseFx[(op1 == 0 ? "fadeIn" : "fadeOut")](lang.mixin({
211                        node: node1
212                },args)),
213                baseFx[(op1 == 0 ? "fadeOut" : "fadeIn")](lang.mixin({
214                        node: node2
215                },args))
216        ]);
217        return _anim; // dojo.Animation
218};
219
220dojoxFx.highlight = function(/*Object*/ args){
221        // summary:
222        //              Highlight a node
223        //
224        // description:
225        //              Returns an animation that sets the node background to args.color
226        //              then gradually fades back the original node background color
227        //
228        // example:
229        //      |       dojox.fx.highlight({ node:"foo" }).play();
230
231        var node = args.node = dom.byId(args.node);
232
233        args.duration = args.duration || 400;
234       
235        // Assign default color light yellow
236        var startColor = args.color || '#ffff99',
237                endColor = htmlUtil.style(node, "backgroundColor")
238        ;
239
240        // safari "fix"
241        // safari reports rgba(0, 0, 0, 0) (black) as transparent color, while
242        // other browsers return "transparent", rendered as white by default by
243        // dojo.Color; now dojo.Color maps "transparent" to
244        // djConfig.transparentColor ([r, g, b]), if present; so we can use
245        // the color behind the effect node
246        if(endColor == "rgba(0, 0, 0, 0)"){
247                endColor = "transparent";
248        }
249
250        var anim = baseFx.animateProperty(lang.mixin({
251                properties: {
252                        backgroundColor: { start: startColor, end: endColor }
253                }
254        }, args));
255
256        if(endColor == "transparent"){
257                connectUtil.connect(anim, "onEnd", anim, function(){
258                        node.style.backgroundColor = endColor;
259                });
260        }
261
262        return anim; // dojo.Animation
263};
264
265 
266dojoxFx.wipeTo = function(/*Object*/ args){
267        // summary:
268        //              Animate a node wiping to a specific width or height
269        //
270        // description:
271        //              Returns an animation that will expand the
272        //              node defined in 'args' object from it's current to
273        //              the height or width value given by the args object.
274        //
275        //              default to height:, so leave height null and specify width:
276        //              to wipeTo a width. note: this may be deprecated by a
277        //
278        //              Note that the final value should not include
279        //              units and should be an integer.  Thus a valid args object
280        //              would look something like this:
281        //
282        //              |       dojox.fx.wipeTo({ node: "nodeId", height: 200 }).play();
283        //
284        //              Node must have no margin/border/padding, so put another
285        //              node inside your target node for additional styling.
286
287        args.node = dom.byId(args.node);
288        var node = args.node, s = node.style;
289
290        var dir = (args.width ? "width" : "height"),
291                endVal = args[dir],
292                props = {}
293        ;
294
295        props[dir] = {
296                // wrapped in functions so we wait till the last second to query (in case value has changed)
297                start: function(){
298                        // start at current [computed] height, but use 1px rather than 0
299                        // because 0 causes IE to display the whole panel
300                        s.overflow = "hidden";
301                        if(s.visibility == "hidden" || s.display == "none"){
302                                s[dir] = "1px";
303                                s.display = "";
304                                s.visibility = "";
305                                return 1;
306                        }else{
307                                var now = htmlUtil.style(node,dir);
308                                return Math.max(now, 1);
309                        }
310                },
311                end: endVal
312        };
313
314        var anim = baseFx.animateProperty(lang.mixin({ properties: props }, args));
315        return anim; // dojo.Animation
316};
317
318return dojoxFx;
319});
Note: See TracBrowser for help on using the repository browser.