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