source: Dev/trunk/src/client/dojox/widget/rotator/PanFade.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.8 KB
Line 
1define([
2        "dojo/_base/array",
3        "dojo/_base/connect",
4        "dojo/_base/lang",
5        "dojo/_base/fx",
6        "dojo/dom-style",
7        "dojo/fx"
8], function(array, connect, lang, baseFx, domStyle, fx) {
9
10        // Constants used to identify which edge the pane pans in from.
11        var DOWN = 0,
12                RIGHT = 1,
13                UP = 2,
14                LEFT = 3;
15
16        function _pan(/*int*/type, /*Object*/args){
17                // summary:
18                //              Handles the preparation of the dom node and creates the dojo.Animation object.
19                var j = {
20                                node: args.current.node,
21                                duration: args.duration,
22                                easing: args.easing
23                        },
24                        k = {
25                                node: args.next.node,
26                                duration: args.duration,
27                                easing: args.easing
28                        },
29                        r = args.rotatorBox,
30                        m = type % 2,
31                        a = m ? "left" : "top",
32                        s = (m ? r.w : r.h) * (type < 2 ? -1 : 1),
33                        p = {},
34                        q = {};
35
36                domStyle.set(k.node, {
37                        display: "",
38                        opacity: 0
39                });
40
41                p[a] = {
42                        start: 0,
43                        end: -s
44                };
45
46                q[a] = {
47                        start: s,
48                        end: 0
49                };
50
51                return fx.combine([ /*dojo.Animation*/
52                        baseFx.animateProperty(lang.mixin({ properties: p }, j)),
53                        baseFx.fadeOut(j),
54                        baseFx.animateProperty(lang.mixin({ properties: q }, k)),
55                        baseFx.fadeIn(k)
56                ]);
57        }
58
59        function _setZindex(/*DomNode*/n, /*int*/z){
60                // summary:
61                //              Helper function for continuously panning.
62                domStyle.set(n, "zIndex", z);
63        }
64
65        var exports = {
66                panFade: function(/*Object*/args){
67                        // summary:
68                        //              Returns a dojo.Animation that either pans left or right to the next pane.
69                        //              The actual direction depends on the order of the panes.
70                        //
71                        //              If panning forward from index 1 to 3, it will perform a pan left. If panning
72                        //              backwards from 5 to 1, then it will perform a pan right.
73                        //
74                        //              If the parameter "continuous" is set to true, it will return an animation
75                        //              chain of several pan animations of each intermediate pane panning. For
76                        //              example, if you pan forward from 1 to 3, it will return an animation panning
77                        //              left from 1 to 2 and then 2 to 3.
78                        //
79                        //              If an easing is specified, it will be applied to each pan transition.  For
80                        //              example, if you are panning from pane 1 to pane 5 and you set the easing to
81                        //              "dojo.fx.easing.elasticInOut", then it will "wobble" 5 times, once for each
82                        //              pan transition.
83                        //
84                        //              If the parameter "wrap" is set to true, it will pan to the next pane using
85                        //              the shortest distance in the array of panes. For example, if there are 6
86                        //              panes, then panning from 5 to 1 will pan forward (left) from pane 5 to 6 and
87                        //              6 to 1.  If the distance is the same either going forward or backwards, then
88                        //              it will always pan forward (left).
89                        //
90                        //              A continuous pan will use the target pane's duration to pan all intermediate
91                        //              panes.  To use the target's pane duration for each intermediate pane, then
92                        //              set the "quick" parameter to "false".
93
94                        var w = args.wrap,
95                                p = args.rotator.panes,
96                                len = p.length,
97                                z = len,
98                                j = args.current.idx,
99                                k = args.next.idx,
100                                nw = Math.abs(k - j),
101                                ww = Math.abs((len - Math.max(j, k)) + Math.min(j, k)) % len,
102                                _forward = j < k,
103                                _dir = LEFT,
104                                _pans = [],
105                                _nodes = [],
106                                _duration = args.duration;
107
108                        // default to pan left, but check if we should pan right.
109                        // need to take into account wrapping.
110                        if((!w && !_forward) || (w && (_forward && nw > ww || !_forward && nw < ww))){
111                                _dir = RIGHT;
112                        }
113
114                        if(args.continuous){
115                                // if continuous pans are quick, then divide the duration by the number of panes
116                                if(args.quick){
117                                        _duration = Math.round(_duration / (w ? Math.min(ww, nw) : nw));
118                                }
119
120                                // set the current pane's z-index
121                                _setZindex(p[j].node, z--);
122
123                                var f = (_dir == LEFT);
124
125                                // loop and set z-indexes and get all pan animations
126                                while(1){
127                                        // set the current pane
128                                        var i = j;
129
130                                        // increment/decrement the next pane's index
131                                        if(f){
132                                                if(++j >= len){
133                                                        j = 0;
134                                                }
135                                        }else{
136                                                if(--j < 0){
137                                                        j = len - 1;
138                                                }
139                                        }
140
141                                        var x = p[i],
142                                                y = p[j];
143
144                                        // set next pane's z-index
145                                        _setZindex(y.node, z--);
146
147                                        // build the pan animation
148                                        _pans.push(_pan(_dir, lang.mixin({
149                                                easing: function(m){ return m; } // continuous gets a linear easing by default
150                                        }, args, {
151                                                current: x,
152                                                next: y,
153                                                duration: _duration
154                                        })));
155
156                                        // if we're done, then break out of the loop
157                                        if((f && j == k) || (!f && j == k)){
158                                                break;
159                                        }
160
161                                        // this must come after the break... we don't want the last pane to get it's
162                                        // styles reset.
163                                        _nodes.push(y.node);
164                                }
165
166                                // build the chained animation of all pan animations
167                                var _anim = fx.chain(_pans);
168
169                                // clean up styles when the chained animation finishes
170                                var h = connect.connect(_anim, "onEnd", function(){
171                                        connect.disconnect(h);
172                                        array.forEach(_nodes, function(q){
173                                                domStyle.set(q, {
174                                                        display: "none",
175                                                        left: 0,
176                                                        opacity: 1,
177                                                        top: 0,
178                                                        zIndex: 0
179                                                });
180                                        });
181                                });
182
183                                return _anim;
184                        }
185
186                        // we're not continuous, so just return a normal pan animation
187                        return _pan(_dir, args); /*dojo.Animation*/
188                },
189
190                panFadeDown: function(/*Object*/args){
191                        // summary:
192                        //              Returns a dojo.Animation that pans in the next rotator pane from the top.
193                        return _pan(DOWN, args); /*dojo.Animation*/
194                },
195
196                panFadeRight: function(/*Object*/args){
197                        // summary:
198                        //              Returns a dojo.Animation that pans in the next rotator pane from the right.
199                        return _pan(RIGHT, args); /*dojo.Animation*/
200                },
201
202                panFadeUp: function(/*Object*/args){
203                        // summary:
204                        //              Returns a dojo.Animation that pans in the next rotator pane from the bottom.
205                        return _pan(UP, args); /*dojo.Animation*/
206                },
207
208                panFadeLeft: function(/*Object*/args){
209                        // summary:
210                        //              Returns a dojo.Animation that pans in the next rotator pane from the left.
211                        return _pan(LEFT, args); /*dojo.Animation*/
212                }
213        };
214
215        // back-compat, remove for 2.0
216        lang.mixin(lang.getObject("dojox.widget.rotator"), exports);
217
218        return exports;
219});
Note: See TracBrowser for help on using the repository browser.