1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
//>>built
define("dojox/layout/RotatorContainer", ["dojo/_base/declare","dojo/_base/html","dojo/_base/connect","dojo/_base/lang","dojo/_base/array",
"dojo/_base/fx","dojo/fx","dijit/_base/manager","dijit/layout/StackContainer","dijit/layout/StackController","dijit/_Widget",
"dijit/_Templated","dijit/_Contained"
],function(declare,html,connect,lang,array,baseFx,coreFx,manager,
StackContainer,StackController,Widget,Templated,Contained){
/*=====
var Widget = dijit._Widget,
Templated = dijit._Templated,
Contained = dijit._Contained,
StackContainer = dijit.layout.StackContainer,
StackController = dijit.layout.StackController;
=====*/
var RotatorContainer = declare("dojox.layout.RotatorContainer",[StackContainer, Templated], {
// summary:
// Extends a StackContainer to automatically transition between children
// and display navigation in the form of tabs or a pager.
//
// description:
// The RotatorContainer cycles through the children with a transition.
//
// published topics:
// [widgetId]-update - Notifies pager(s) that a child has changed.
// Parameters:
// /*boolean*/ playing - true if playing, false if paused
// /*int*/ current - current selected child
// /*int*/ total - total number of children
//
// example:
// | <div dojoType="dojox.layout.RotatorContainer" id="myRotator" showTabs="true" autoStart="true" transitionDelay="5000">
// | <div id="pane1" dojoType="dijit.layout.ContentPane" title="1">
// | Pane 1!
// | </div>
// | <div id="pane2" dojoType="dijit.layout.ContentPane" title="2">
// | Pane 2!
// | </div>
// | <div id="pane3" dojoType="dijit.layout.ContentPane" title="3" transitionDelay="10000">
// | Pane 3 with overrided transitionDelay!
// | </div>
// | </div>
templateString: '<div class="dojoxRotatorContainer"><div dojoAttachPoint="tabNode"></div><div class="dojoxRotatorPager" dojoAttachPoint="pagerNode"></div><div class="dojoxRotatorContent" dojoAttachPoint="containerNode"></div></div>',
// showTabs: Boolean
// Sets the display of the tabs. The tabs are actually a StackController.
// The child's title is used for the tab's label.
showTabs: true,
// transitionDelay: int
// The delay in milliseconds before transitioning to the next child.
transitionDelay: 5000,
// transition: String
// The type of transition to perform when switching children.
// A null transition will transition instantly.
transition: "fade",
// transitionDuration: int
// The duration of the transition in milliseconds.
transitionDuration: 1000,
// autoStart: Boolean
// Starts the timer to transition children upon creation.
autoStart: true,
// suspendOnHover: Boolean
// Pause the rotator when the mouse hovers over it.
suspendOnHover: false,
// pauseOnManualChange: Boolean
// Pause the rotator when the tab is changed or the pager's next/previous
// buttons are clicked.
pauseOnManualChange: null,
// reverse: Boolean
// Causes the rotator to rotate in reverse order.
reverse: false,
// pagerId: String
// ID the pager widget.
pagerId: "",
// cycles: int
// Number of cycles before pausing.
cycles: -1,
// pagerClass: String
// The declared Class of the Pager used for this Widget
pagerClass: "dojox.layout.RotatorPager",
postCreate: function(){
// summary: Initializes the DOM nodes, tabs, and transition stuff.
this.inherited(arguments);
// force this DOM node to a relative position and make sure the children are absolute positioned
html.style(this.domNode, "position", "relative");
// validate the cycles counter
if(this.cycles-0 == this.cycles && this.cycles != -1){
// we need to add 1 because we decrement cycles before the animation starts
this.cycles++;
}else{
this.cycles = -1;
}
// if they didn't specify the pauseOnManualChange, then we want it to be the opposite of
// the suspendOnHover since it doesn't make sense to do both, unless you really want to
if(this.pauseOnManualChange === null){
this.pauseOnManualChange = !this.suspendOnHover;
}
// create the stack controller if we are using tabs
var id = this.id || "rotator"+(new Date()).getTime(),
sc = new StackController({ containerId:id }, this.tabNode);
this.tabNode = sc.domNode;
this._stackController = sc;
html.style(this.tabNode, "display", this.showTabs ? "" : "none");
// if the controller's tabs are clicked, check if we should pause and reset the cycle counter
this.connect(sc, "onButtonClick","_manualChange");
// set up our topic listeners
this._subscriptions = [
connect.subscribe(this.id+"-cycle", this, "_cycle"),
connect.subscribe(this.id+"-state", this, "_state")
];
// make sure the transition duration isn't less than the transition delay
var d = Math.round(this.transitionDelay * 0.75);
if(d < this.transitionDuration){
this.transitionDuration = d;
}
// wire up the mouse hover events
if(this.suspendOnHover){
this.connect(this.domNode, "onmouseover", "_onMouseOver");
this.connect(this.domNode, "onmouseout", "_onMouseOut");
}
},
startup: function(){
// summary: Initializes the pagers.
if(this._started){ return; }
// check if the pager is defined within the rotator container
var c = this.getChildren();
for(var i=0, len=c.length; i<len; i++){
if(c[i].declaredClass == this.pagerClass){
this.pagerNode.appendChild(c[i].domNode);
break;
}
}
// process the child widgets
this.inherited(arguments);
// check if we should start automatically
if(this.autoStart){
// start playing
setTimeout(lang.hitch(this, "_play"), 10);
}else{
// update the pagers with the initial state
this._updatePager();
}
},
destroy: function(){
// summary: Unsubscribe to all of our topics
array.forEach(this._subscriptions, connect.unsubscribe);
this.inherited(arguments);
},
_setShowTabsAttr: function(/*anything*/value){
this.showTabs = value;
html.style(this.tabNode, "display", value ? "" : "none");
},
_updatePager: function(){
// summary: Notify the pager's current and total numbers.
var c = this.getChildren();
connect.publish(this.id+"-update", [this._playing, array.indexOf(c, this.selectedChildWidget)+1, c.length]);
},
_onMouseOver: function(){
// summary: Triggered when the mouse is moved over the rotator container.
// temporarily suspend the cycling, but don't officially pause it
this._resetTimer();
this._over = true;
},
_onMouseOut: function(){
// summary: Triggered when the mouse is moved off the rotator container.
this._over = false;
// if we were playing, resume playback in 200ms
// we need to wait because we may be moused over again right away
if(this._playing){
clearTimeout(this._timer);
this._timer = setTimeout(lang.hitch(this, "_play", true), 200);
}
},
_resetTimer: function(){
// summary: Resets the timer used to start the next transition.
clearTimeout(this._timer);
this._timer = null;
},
_cycle: function(/*boolean or int*/next){
// summary: Cycles to the next/previous child.
// if next is an int, then _cycle() was called via a timer
// if next is a boolean, then _cycle() was called via the next/prev buttons, stop playing and reset cycles
if(next instanceof Boolean || typeof next == "boolean"){
this._manualChange();
}
var c = this.getChildren(),
len = c.length,
i = array.indexOf(c, this.selectedChildWidget) + (next === false || (next !== true && this.reverse) ? -1 : 1);
this.selectChild(c[(i < len ? (i < 0 ? len-1 : i) : 0)]);
this._updatePager();
},
_manualChange: function(){
// summary: This function is only called when a manual change occurs in which
// case we may need to stop playing and we need to reset the cycle counter
if(this.pauseOnManualChange){
this._playing = false;
}
this.cycles = -1;
},
_play: function(skip){
// summary: Schedules the next transition.
this._playing = true;
this._resetTimer();
if(skip !== true && this.cycles>0){
this.cycles--;
}
if(this.cycles==0){
this._pause();
}else if((!this.suspendOnHover || !this._over) && this.transitionDelay){
// check if current pane has a delay
this._timer = setTimeout(lang.hitch(this, "_cycle"), this.selectedChildWidget.domNode.getAttribute("transitionDelay") || this.transitionDelay);
}
this._updatePager();
},
_pause: function(){
// summary: Clears the transition timer and pauses the rotator.
this._playing = false;
this._resetTimer();
},
_state: function(playing){
// summary: Fired when the play/pause pager button is toggled.
if(playing){
// since we were manually changed, disable the cycle counter
this.cycles = -1;
this._play();
}else{
this._pause();
}
},
_transition: function(/*dijit._Widget*/ next, /*dijit._Widget*/ prev){
// summary: Dispatches the appropriate transition.
this._resetTimer();
// check if we have anything to transition
if(prev && this.transitionDuration){
switch(this.transition){
case "fade": this._fade(next, prev); return;
}
}
this._transitionEnd();
this.inherited(arguments);
},
_transitionEnd: function(){
if(this._playing){
this._play();
}else{
this._updatePager();
}
},
_fade: function(/*dijit._Widget*/ next, /*dijit._Widget*/ prev){
// summary: Crossfades two children.
this._styleNode(prev.domNode, 1, 1);
this._styleNode(next.domNode, 0, 2);
// show the next child and make sure it's sized properly
this._showChild(next);
if(this.doLayout && next.resize){
next.resize(this._containerContentBox || this._contentBox);
}
// create the crossfade animation
var args = { duration:this.transitionDuration },
anim = coreFx.combine([
baseFx["fadeOut"](lang.mixin({node:prev.domNode}, args)),
baseFx["fadeIn"](lang.mixin({node:next.domNode}, args))
]);
this.connect(anim, "onEnd", lang.hitch(this,function(){
this._hideChild(prev);
this._transitionEnd();
}));
anim.play();
},
_styleNode: function(/*DOMnode*/node, /*number*/opacity, /*int*/zIndex){
// summary: Helper function to style the children.
html.style(node, "opacity", opacity);
html.style(node, "zIndex", zIndex);
html.style(node, "position", "absolute");
}
});
declare("dojox.layout.RotatorPager", [Widget, Templated, Contained], {
// summary:
// Defines controls used to manipulate a RotatorContainer
//
// description:
// A pager can be defined one of two ways:
// * Externally of the RotatorContainer's template and tell the
// RotatorPager the rotatorId of the RotatorContainer
// * As a direct descendant of the RotatorContainer (i.e. inside the
// RotatorContainer's template)
//
// The pager can contain the following components:
// * Previous button
// - Must be a dijit.form.Button
// - dojoAttachPoint must be named "previous"
// * Next button
// - Must be a dijit.form.Button
// - dojoAttachPoint must be named "next"
// * Play/Pause toggle button
// - Must be a dijit.form.ToggleButton
// - dojoAttachPoint must be named "playPause"
// - Use iconClass to specify toggled state
// * Current child #
// - dojoAttachPoint must be named "current"
// * Total # of children
// - dojoAttachPoint must be named "total"
//
// You can choose to exclude specific controls as well as add elements
// for styling.
//
// Should you need a pager, but don't want to use Dijit buttons, you can
// write your own pager widget and just wire it into the topics. The
// topic names are prefixed with the widget ID of the RotatorContainer.
// Notifications are received from and sent to the RotatorContainer as
// well as other RotatorPagers.
//
// published topics:
// [widgetId]-cycle - Notify that the next or previous button was pressed.
// Parameters:
// /*boolean*/ next - true if next, false if previous
// [widgetId]-state - Notify that the play/pause button was toggled.
// Parameters:
// /*boolean*/ playing - true if playing, false if paused
//
// example:
// A pager with the current/total children and previous/next buttons.
// | <div dojoType="dojox.layout.RotatorPager" rotatorId="myRotator">
// | <button dojoType="dijit.form.Button" dojoAttachPoint="previous">Prev</button>
// | <span dojoAttachPoint="current"></span> / <span dojoAttachPoint="total"></span>
// | <button dojoType="dijit.form.Button" dojoAttachPoint="next">Next</button>
// | </div>
//
// example:
// A pager with only a play/pause toggle button.
// | <div dojoType="dojox.layout.RotatorPager" rotatorId="myRotator">
// | <button dojoType="dijit.form.ToggleButton" dojoAttachPoint="playPause"></button>
// | </div>
//
// example:
// A pager styled with iconClass.
// | <div dojoType="dojox.layout.RotatorPager" class="rotatorIcons" rotatorId="myRotator">
// | <button dojoType="dijit.form.Button" iconClass="previous" dojoAttachPoint="previous">Prev</button>
// | <button dojoType="dijit.form.ToggleButton" iconClass="playPause" dojoAttachPoint="playPause"></button>
// | <button dojoType="dijit.form.Button" iconClass="next" dojoAttachPoint="next">Next</button>
// | <span dojoAttachPoint="current"></span> / <span dojoAttachPoint="total"></span>
// | </div>
widgetsInTemplate: true,
// rotatorId: int
// The ID of the rotator this pager is tied to.
// Only required if defined outside of the RotatorContainer's container.
rotatorId: "",
postMixInProperties: function(){
this.templateString = "<div>" + this.srcNodeRef.innerHTML + "</div>";
},
postCreate: function(){
var p = manager.byId(this.rotatorId) || this.getParent();
if(p && p.declaredClass == "dojox.layout.RotatorContainer"){
if(this.previous){
connect.connect(this.previous, "onClick", function(){
connect.publish(p.id+"-cycle", [false]);
});
}
if(this.next){
connect.connect(this.next, "onClick", function(){
connect.publish(p.id+"-cycle", [true]);
});
}
if(this.playPause){
connect.connect(this.playPause, "onClick", function(){
this.set('label', this.checked ? "Pause" : "Play");
connect.publish(p.id+"-state", [this.checked]);
});
}
this._subscriptions = [
connect.subscribe(p.id+"-state", this, "_state"),
connect.subscribe(p.id+"-update", this, "_update")
];
}
},
destroy: function(){
// summary: Unsubscribe to all of our topics
array.forEach(this._subscriptions, connect.unsubscribe);
this.inherited(arguments);
},
_state: function(/*boolean*/playing){
// summary: Updates the display of the play/pause button
if(this.playPause && this.playPause.checked != playing){
this.playPause.set("label", playing ? "Pause" : "Play");
this.playPause.set("checked", playing);
}
},
_update: function(/*boolean*/playing, /*int*/current, /*int*/total){
// summary: Updates the pager's play/pause button, current child, and total number of children.
this._state(playing);
if(this.current && current){
this.current.innerHTML = current;
}
if(this.total && total){
this.total.innerHTML = total;
}
}
});
return RotatorContainer;
});
|