source: Dev/branches/rest-dojo-ui/client/dojox/widget/AutoRotator.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: 5.5 KB
Line 
1dojo.provide("dojox.widget.AutoRotator");
2dojo.require("dojox.widget.Rotator");
3
4(function(d){
5
6        d.declare("dojox.widget.AutoRotator", dojox.widget.Rotator, {
7                //      summary:
8                //              A rotator that automatically transitions between child nodes.
9                //
10                //      description:
11                //              Adds automatic rotating to the dojox.widget.Rotator.  The
12                //              AutoRotator has parameters that control how user input can
13                //              affect the rotator including a suspend when hovering over the
14                //              rotator and pausing when the user manually advances to another
15                //              pane.
16                //
17                //      example:
18                //      |       <div dojoType="dojox.widget.AutoRotator" duration="3000">
19                //      |               <div>
20                //      |                       Pane 1!
21                //      |               </div>
22                //      |               <div duration="5000">
23                //      |                       Pane 2 with an overrided duration!
24                //      |               </div>
25                //      |       </div>
26
27                //      suspendOnHover: boolean
28                //              Pause the rotator when the mouse hovers over it.
29                suspendOnHover: false,
30
31                //      duration: int
32                //              The time in milliseconds before transitioning to the next pane.  The
33                //              default value is 4000 (4 seconds).
34                duration: 4000,
35
36                //      autoStart: boolean
37                //              Starts the timer to transition children upon creation.
38                autoStart: true,
39
40                //      pauseOnManualChange: boolean
41                //              Pause the rotator when the pane is changed or a controller's next or
42                //              previous buttons are clicked.
43                pauseOnManualChange: false,
44
45                //      cycles: int
46                //              Number of cycles before pausing.
47                cycles: -1,
48
49                //      random: boolean
50                //              Determines if the panes should cycle randomly.
51                random: false,
52
53                //      reverse: boolean
54                //              Causes the rotator to rotate in reverse order.
55                reverse: false,
56
57                constructor: function(){
58                        //      summary:
59                        //              Initializes the timer and connect to the rotator.
60
61                        var _t = this;
62
63                        // validate the cycles counter
64                        if(_t.cycles-0 == _t.cycles && _t.cycles > 0){
65                                // we need to add 1 because we decrement cycles before the animation starts
66                                _t.cycles++;
67                        }else{
68                                _t.cycles = _t.cycles ? -1 : 0;
69                        }
70
71                        // wire up the mouse hover events
72                        _t._connects = [
73                                d.connect(_t._domNode, "onmouseover", function(){
74                                        // temporarily suspend the cycling, but don't officially pause
75                                        // it and don't allow suspending if we're transitioning
76                                        if(_t.suspendOnHover && !_t.anim && !_t.wfe){
77                                                var t = _t._endTime,
78                                                        n = _t._now();
79                                                _t._suspended = true;
80                                                _t._resetTimer();
81                                                _t._resumeDuration = t > n ? t - n : 0.01;
82                                        }
83                                }),
84
85                                d.connect(_t._domNode, "onmouseout", function(){
86                                        // if we were playing, resume playback unless were in the
87                                        // middle of a transition
88                                        if(_t.suspendOnHover && !_t.anim){
89                                                _t._suspended = false;
90                                                if(_t.playing && !_t.wfe){
91                                                        _t.play(true);
92                                                }
93                                        }
94                                })
95                        ];
96
97                        // everything is ready, so start
98                        if(_t.autoStart && _t.panes.length > 1){
99                                // start playing
100                                _t.play();
101                        }else{
102                                // since we're not playing, lets pause
103                                _t.pause();
104                        }
105                },
106
107                destroy: function(){
108                        //      summary:
109                        //              Disconnect the AutoRotator's events.
110                        d.forEach(this._connects, d.disconnect);
111                        this.inherited(arguments);
112                },
113
114                play: function(/*boolean?*/skipCycleDecrement, /*boolean?*/skipDuration){
115                        //      summary:
116                        //              Sets the state to "playing" and schedules the next cycle to run.
117                        this.playing = true;
118                        this._resetTimer();
119
120                        // don't decrement the count if we're resuming play
121                        if(skipCycleDecrement !== true && this.cycles > 0){
122                                this.cycles--;
123                        }
124
125                        if(this.cycles == 0){
126                                // we have reached the number of cycles, so pause
127                                this.pause();
128                        }else if(!this._suspended){
129                                this.onUpdate("play");
130                                // if we haven't been suspended, then grab the duration for this pane and
131                                // schedule a cycle to be run
132                                if(skipDuration){
133                                        this._cycle();
134                                }else{
135                                        var r = (this._resumeDuration || 0)-0,
136                                                u = (r > 0 ? r : (this.panes[this.idx].duration || this.duration))-0;
137                                        // call _cycle() after a duration and pass in false so it isn't manual
138                                        this._resumeDuration = 0;
139                                        this._endTime = this._now() + u;
140                                        this._timer = setTimeout(d.hitch(this, "_cycle", false), u);
141                                }
142                        }
143                },
144
145                pause: function(){
146                        //      summary:
147                        //              Sets the state to "not playing" and clears the cycle timer.
148                        this.playing = this._suspended = false;
149                        this.cycles = -1;
150                        this._resetTimer();
151
152                        // notify the controllers we're paused
153                        this.onUpdate("pause");
154                },
155
156                _now: function(){
157                        //      summary:
158                        //              Helper function to return the current system time in milliseconds.
159                        return (new Date()).getTime(); /*int*/
160                },
161
162                _resetTimer: function(){
163                        //      summary:
164                        //              Resets the timer used to schedule the next transition.
165                        clearTimeout(this._timer);
166                },
167
168                _cycle: function(/*boolean|int?*/manual){
169                        //      summary:
170                        //              Cycles the rotator to the next/previous pane.
171                        var _t = this,
172                                i = _t.idx,
173                                j;
174
175                        if(_t.random){
176                                // make sure we don't randomly pick the pane we're already on
177                                do{
178                                        j = Math.floor(Math.random() * _t.panes.length + 1);
179                                }while(j == i);
180                        }else{
181                                j = i + (_t.reverse ? -1 : 1)
182                        }
183
184                        // rotate!
185                        var def = _t.go(j);
186
187                        if(def){
188                                def.addCallback(function(/*boolean?*/skipDuration){
189                                        _t.onUpdate("cycle");
190                                        if(_t.playing){
191                                                _t.play(false, skipDuration);
192                                        }
193                                });
194                        }
195                },
196
197                onManualChange: function(/*string*/action){
198                        //      summary:
199                        //              Override the Rotator's onManualChange so we can pause.
200
201                        this.cycles = -1;
202
203                        // obviously we don't want to pause if play was just clicked
204                        if(action != "play"){
205                                this._resetTimer();
206                                if(this.pauseOnManualChange){
207                                        this.pause();
208                                }
209                        }
210
211                        if(this.playing){
212                                this.play();
213                        }
214                }
215        });
216
217})(dojo);
Note: See TracBrowser for help on using the repository browser.