source: Dev/branches/rest-dojo-ui/client/dojox/widget/Roller.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: 6.6 KB
Line 
1define(["dojo", "dijit", "dijit/_Widget"], function(dojo, dijit){
2
3        dojo.declare("dojox.widget.Roller", dijit._Widget, {
4                // summary: A simple widget to take an unordered-list of Text and roll through them
5                //
6                // description:
7                //              The Roller widget takes an unordered-list of items, and converts
8                //              them to a single-area (the size of one list-item, however you so choose
9                //              to style it) and loops continually, fading between items.
10                //
11                //              In it's current state, it requires it be created from an unordered (or ordered)
12                //              list, though can contain complex markup.
13                //
14                //              You can manipulate the `items` array at any point during the cycle with
15                //              standard array manipulation techniques.
16                //
17                //              The class "dojoxRoller" is added to the UL element for styling purposes.
18                //
19                //      example:
20                //      |       // create a scroller from a unordered list with id="lister"
21                //  |   var thinger = new dojox.widget.Roller.Roller({},"lister");
22                //
23                //      example:
24                //      |       // create a scroller from a fixed array, and place in the DOM:
25                //      |       new dojox.widget.Roller({ items:["one","two","three"] }).placeAt(dojo.body());
26                //
27                //      example:
28                //      |       // add an item:
29                //      |       dijit.byId("roller").items.push("I am a new Label");
30                //
31                //  example:
32                //      |       // stop a roller from rolling:
33                //      |       dijit.byId("roller").stop();
34                //
35                // delay: Integer
36                //              Interval between rolls
37                delay: 2000,
38
39                // autoStart: Boolean
40                //              Toggle to control starup behavior. Call .start() manually
41                //              if set to `false`
42                autoStart: true,
43
44                // itemSelector: String
45                //              A CSS selector to be used by `dojo.query` to find the children
46                //              items in this widget. Defaults to "> li", finding only first-children
47                //              list-items in the list, allowing for embedded lists to occur.
48                itemSelector: "> li",
49
50                // durationIn: Integer
51                //              Speed (in ms) to apply to the "in" animation (show the node)
52                durationIn: 400,
53
54                // durationOut: Integer
55                //              Speed (in ms) to apply to the "out" animation (hide the showing node)
56                durationOut: 275,
57        /*=====
58                // items: Array
59                //              If populated prior to instantiation, is used as the Items over the children
60                items: [],
61        =====*/
62
63                // _idx: Integer
64                //              Index of the the currently visible item in the list of items[]
65                _idx: -1,
66
67                postCreate: function(){
68
69                        // add some instance vars:
70                        if(!this["items"]){
71                                this.items = [];
72                        }
73
74                        dojo.addClass(this.domNode,"dojoxRoller");
75
76                        // find all the items in this list, and popuplate
77                        dojo.query(this.itemSelector, this.domNode).forEach(function(item, i){
78                                this.items.push(item.innerHTML);
79                                // reuse the first match, destroy the rest
80                                if(i == 0){
81                                        this._roller = item;
82                                        this._idx = 0;
83                                }else{ dojo.destroy(item); }
84                        }, this);
85
86                        // handle the case where items[] were passed, and no srcNodeRef exists
87                        if(!this._roller){
88                                this._roller = dojo.create('li', null, this.domNode);
89                        }
90                        // stub out animation creation (for overloading maybe later)
91                        this.makeAnims();
92
93                        // and start, if true:
94                        if(this.autoStart){ this.start(); }
95
96                },
97
98                makeAnims: function(){
99                        // summary: Animation creator function. Need to create an 'in' and 'out'
100                        //              Animation stored in _anim Object, which the rest of the widget
101                        //              will reuse.
102                        var n = this.domNode;
103                        dojo.mixin(this, {
104                                _anim: {
105                                        "in": dojo.fadeIn({ node:n, duration: this.durationIn }),
106                                        "out": dojo.fadeOut({ node:n, duration: this.durationOut })
107                                }
108                        });
109                        this._setupConnects();
110
111                },
112
113                _setupConnects: function(){
114                        // summary: setup the loop connection logic
115                        var anim = this._anim;
116
117                        this.connect(anim["out"], "onEnd", function(){
118                                // onEnd of the `out` animation, select the next items and play `in` animation
119                                this._setIndex(this._idx + 1);
120                                anim["in"].play(15);
121                        });
122
123                        this.connect(anim["in"], "onEnd", function(){
124                                // onEnd of the `in` animation, call `start` again after some delay:
125                                this._timeout = setTimeout(dojo.hitch(this, "_run"), this.delay);
126                        });
127                },
128
129                start: function(){
130                        // summary: Starts to Roller looping
131                        if(!this.rolling){
132                                this.rolling = true;
133                                this._run();
134                        }
135                },
136
137                _run: function(){
138                        this._anim["out"].gotoPercent(0, true);
139                },
140
141                stop: function(){
142                        // summary: Stops the Roller from looping anymore.
143                        this.rolling = false;
144
145                        var m = this._anim,
146                                t = this._timeout;
147
148                        if(t){ clearTimeout(t); }
149                        m["in"].stop();
150                        m["out"].stop();
151                },
152
153                _setIndex: function(i){
154                        // summary: Set the Roller to some passed index. If beyond range, go to first.
155                        var l = this.items.length - 1;
156                        if(i < 0){ i = l; }
157                        if(i > l){ i = 0; }
158                        this._roller.innerHTML = this.items[i] || "error!";
159                        this._idx = i;
160                }
161
162        });
163
164        dojo.declare("dojox.widget.RollerSlide", dojox.widget.Roller, {
165                // summary: An add-on to the Roller to modify animations. This produces
166                //              a slide-from-bottom like effect. See `dojox.widget.Roller` for
167                //              full API information.
168
169                durationOut: 175, // slightly faster than default
170
171                makeAnims: function(){
172                        // summary: Animation creator function. Need to create an 'in' and 'out'
173                        //              Animation stored in _anim Object, which the rest of the widget
174                        //              will reuse.
175
176                        var n = this.domNode, pos = "position",
177                                props = {
178                                        top: { end: 0, start: 25 },
179                                        opacity: 1
180                                }
181                        ;
182
183                        dojo.style(n, pos, "relative");
184                        dojo.style(this._roller, pos, "absolute");
185
186                        dojo.mixin(this, {
187                                _anim: {
188
189                                        "in": dojo.animateProperty({
190                                                node: n,
191                                                duration: this.durationIn,
192                                                properties: props
193                                        }),
194
195                                        "out": dojo.fadeOut({ node: n, duration: this.durationOut })
196                                }
197                        });
198                        // don't forget to do this in the class. override if necessary.
199                        this._setupConnects();
200                }
201
202        });
203
204        dojo.declare("dojox.widget._RollerHover", null, {
205                // summary: A mixin class to provide a way to automate the "stop on hover" functionality.
206                //
207                // description:
208                //              A mixin class used to provide a way to automate a "stop on hover" behavior,
209                //              while still allowing for ambigious subclassing for custom animations.
210                //              Simply mix this class into a `dojox.widget.Roller` variant, and instantiate
211                //              as you would. The hover connection is done automatically.
212                //
213                //              The "hover" functionality is as such: Stop rotation while the mouse is over the
214                //              instance, and resume again once leaving. Even if autoStart is disabled, the widget
215                //              will start if a mouse enters and leaves the node in this case.
216                //
217                // example:
218                // | dojo.declare("my.Roller", [dojox.widget.RollerSlide, dojox.widget._RollerHover], {});
219                // | new my.Roller({}, "myList");
220
221                postCreate: function(){
222                        this.inherited(arguments);
223                        this.connect(this.domNode, "onmouseenter", "stop");
224                        this.connect(this.domNode, "onmouseleave", "start");
225                }
226
227        });
228       
229        return dojox.widget.Roller;
230       
231});
Note: See TracBrowser for help on using the repository browser.