source: Dev/branches/rest-dojo-ui/client/dojox/timing/Sequence.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: 4.6 KB
Line 
1define([
2        "dojo/_base/kernel",
3        "dojo/_base/array",
4        "dojo/_base/declare",
5        "dojo/_base/lang",
6        "./_base"
7], function(dojo){
8        dojo.experimental("dojox.timing.Sequence");
9        dojo.declare("dojox.timing.Sequence", null, {
10                // summary:
11                //      This class provides functionality to really sequentialize
12                //      function calls. You need to provide a list of functions and
13                //      some parameters for each (like: pauseBefore) and they will
14                //      be run one after another. This can be very useful for slideshows
15                //      or alike things.
16                //
17                // description:
18                //      This array will contain the sequence defines resolved, so that
19                //      ie. repeat:10 will result in 10 elements in the sequence, so
20                //      the repeat handling is easier and we don't need to handle that
21                //      many extra cases. Also the doneFunction, if given is added at the
22                //      end of the resolved-sequences.
23
24        /*=====
25                // _defsResolved: Array
26                //      The resolved sequence, for easier handling.
27                _defsResolved: [],
28        =====*/
29
30                // This is the time to wait before goOn() calls _go(), which
31                // mostly results from a pauseAfter for a function that returned
32                // false and is later continued by the external goOn() call.
33                // The time to wait needs to be waited in goOn() where the
34                // sequence is continued.
35
36                // _goOnPause: Integer
37                //      The pause to wait before really going on.
38                _goOnPause: 0,
39
40                _running: false,
41
42                constructor: function(){
43                        this._defsResolved = [];
44                },
45
46                go: function(/* Array */defs, /* Function|Array? */doneFunction){
47                        // summary: Run the passed sequence definition
48                        //
49                        // defs: Array
50                        //              The sequence of actions
51                        // doneFunction: Function|Array?
52                        //              The function to call when done
53                        this._running = true;
54                        dojo.forEach(defs, function(cur){
55                                if(cur.repeat > 1){
56                                        var repeat = cur.repeat;
57                                        for(var j = 0; j < repeat; j++){
58                                                cur.repeat = 1;
59                                                this._defsResolved.push(cur);
60                                        }
61                                }else{
62                                        this._defsResolved.push(cur);
63                                }
64                        }, this);
65                        var last = defs[defs.length - 1];
66                        if(doneFunction){
67                                this._defsResolved.push({ func: doneFunction });
68                        }
69                        // stop the sequence, this actually just sets this._running to false
70                        this._defsResolved.push({ func: [this.stop, this] });
71                        this._curId = 0;
72                        this._go();
73                },
74
75                _go: function(){
76                        // summary: Execute one task of this._defsResolved.
77
78                        // if _running was set to false stop the sequence, this is the
79                        // case when i.e. stop() was called.
80                        if(!this._running){
81                                return;
82                        }
83                        var cur = this._defsResolved[this._curId];
84                        this._curId += 1;
85                        // create the function to call, the func property might be an array, which means
86                        // [function, context, parameter1, parameter2, ...]
87                        function resolveAndCallFunc(func) {
88                                var ret = null;
89                                if(dojo.isArray(func)){
90                                        // Two elements might only be given when the function+context
91                                        // is given, this is nice for using this, ie: [this.func, this]
92                                        if(func.length>2){
93                                                ret = func[0].apply(func[1], func.slice(2));
94                                        }else{
95                                                ret = func[0].apply(func[1]);
96                                        }
97                                }else{
98                                        ret = func();
99                                }
100                                return ret;
101                        }
102
103                        if(this._curId >= this._defsResolved.length){
104                                resolveAndCallFunc(cur.func); // call the last function, since it is the doneFunction we dont need to handle pause stuff
105                                // don't go on and call this._go() again, we are done
106                                return;
107                        }
108
109                        if(cur.pauseAfter){
110                                if(resolveAndCallFunc(cur.func) !== false){
111                                        setTimeout(dojo.hitch(this, "_go"), cur.pauseAfter);
112                                }else{
113                                        this._goOnPause = cur.pauseAfter;
114                                }
115                        }else if(cur.pauseBefore){
116                                var x = dojo.hitch(this,function(){
117                                        if(resolveAndCallFunc(cur.func) !== false){
118                                                this._go()
119                                        }
120                                });
121                                setTimeout(x, cur.pauseBefore);
122                        }else{
123                                if(resolveAndCallFunc(cur.func) !== false){
124                                        this._go();
125                                }
126                        }
127                },
128
129                goOn: function(){
130                        // summary: This method just provides a hook from the outside, so that
131                        //              an interrupted sequence can be continued.
132                        if(this._goOnPause){
133                                setTimeout(dojo.hitch(this, "_go"), this._goOnPause);
134                                this._goOnPause = 0; // reset it, so if the next one doesnt set it we dont use the old pause
135                        }else{ this._go(); }
136                },
137
138                stop: function(){
139                        // summary:  Stop the currently running sequence.
140                        //
141                        // description:
142                        //              This can only interrupt the sequence not the last function that
143                        //              had been started. If the last function was i.e. a slideshow
144                        //              that is handled inside a function that you have given as
145                        //              one sequence item it cant be stopped, since it is not controlled
146                        //              by this object here. In this case it would be smarter to
147                        //              run the slideshow using a sequence object so you can also stop
148                        //              it using this method.
149                        this._running = false;
150                }
151
152        });
153        return dojox.timing.Sequence;
154});
Note: See TracBrowser for help on using the repository browser.