-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvListRotator.js
556 lines (516 loc) · 23.2 KB
/
advListRotator.js
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
(function($){
$.fn.extend({
advListRotator: function(options) {
if (this.length) {
return this.each(function() {
var obj = Object.create(AdvancedListRotatorClass);
obj.init(this, options);
$.data(this, 'advListRotator', AdvancedListRotatorClass);
});
}
return false;
}
});
})(jQuery);
/* Advanced List Rotator
* Copyright (c) 2010 Lars Boldt (larsboldt.com)
* E-mail: boldt.lars@gmail.com
* Version: 1.3
* Released: 07.15.2010
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
var AdvancedListRotatorClass = {
init: function(element, options) {
// Store "this" for reference
var c = this;
// Settings
c.settings = jQuery.extend({
rotationInterval: 5000,
effectTimer: 1000,
effect: false,
effectOptions: {},
itemControl: {},
shuffle: false,
randomStart: false,
autoStart: true,
disableRotationEngine: false,
helper: false,
activeItemClass: 'alrActiveItem',
helperActiveItemClass: 'alrHelperActiveItem',
helperInteraction: 'mouseover',
startIndex: 0,
nextItemElement: false,
nextItemElementInteraction: 'click',
previousItemElement: false,
previousItemElementInteraction: 'click',
randomEffect: false,
randomEffects: new Array('blind', 'clip', 'explode', 'fade', 'fold'),
debug: false,
debugLevel: new Array('debug', 'info', 'warn', 'error')
}, c.settings, options);
// Effect options
c.effectOptions = jQuery.extend({}, c.effectOptions, c.settings.effectOptions);
// Effect control
c.itemControl = jQuery.extend({}, c.itemControl, c.settings.itemControl);
// Store element for reference, both normal and jQuery
c.listRotator = element;
c.$listRotator = jQuery(element);
// Total items (li's in ul)
c.totalItems = c.$listRotator.find('li').length;
// Current item
c.currentItem = (c.settings.startIndex >= 0 && c.settings.startIndex < c.totalItems) ? c.settings.startIndex : 0;
// Current effect
c.currentEffect = false;
// Shuffle array
c.shuffledItems = new Array();
// Timer
c.tId = null;
// User interaction
c.userInteraction = false;
// Calculate next item index (we use this flag to skip calculating next item index when we move backwards)
c.calculateNextItem = true;
// Is animation running
c.animationRunning = false;
// Random start
if (c.settings.randomStart) {
c.currentItem = c.random(c.totalItems);
}
// Add currentItem to shuffle list to avoid showing item twice the first round
c.shuffledItems.push(c.currentItem);
// previousItem prevents shuffle ending and starting on the same item
c.previousItem = c.currentItem;
// Debug?
if (c.settings.debug && c.in_array('debug', c.settings.debugLevel)) {
console.debug('init: totalItems: ' + c.totalItems);
console.debug('init: startIndex: ' + c.currentItem);
if (c.settings.shuffle) {
console.debug('init: Pushed ' + c.currentItem + ' into shufflelist');
}
}
// Display first content
c.$listRotator.children().each(function(index) {
if (index == c.currentItem) {
if (c.settings.effect == 'slide') {
var pos = '-' + c.currentItem*c.effectOptions.slideBy;
c.$listRotator.css('left', pos);
}
jQuery(this).show();
jQuery(this).addClass(c.settings.activeItemClass);
c.setHelperClass(c, index, true);
}
});
// Loop all helper elements within the ul and bind mouseover/mouseout functionality to them
if (jQuery(c.settings.helper).length > 0) {
jQuery(c.settings.helper).children().each(function() {
jQuery(this).bind(c.settings.helperInteraction, function() {
// Set userInteraction true
c.userInteraction = true;
// Stop rotationEngine
c.stopRotationEngine(c);
// Make sure currentEffect is actually set (certain configs might not set this value)
c.currentEffect = c.getItemEffect(c);
// Hide active content
if (c.currentEffect != 'slide' && c.currentEffect !== false) {
c.$listRotator.children().hide();
}
// Remove active class from helper
c.$listRotator.children().removeClass(c.settings.activeItemClass);
jQuery(c.settings.helper).children().removeClass(c.settings.helperActiveItemClass);
// Find position of current item
c.currentItem = jQuery(this).index();
// Show current content
c.$listRotator.children().each(function (index) {
if (index == c.currentItem) {
// Stop animations on this element
jQuery(this).stop();
// Reset opacity incase animation was fade
jQuery(this).css('opacity', 1);
// Show element
jQuery(this).show();
if (c.settings.effect == 'slide') {
c.runEffect(c, jQuery(this), false);
}
// Add active class to helper
jQuery(this).addClass(c.settings.activeItemClass);
c.setHelperClass(c, index, true);
return false;
}
return true;
});
});
jQuery(this).bind('mouseout', function() {
// Start rotationEngine
c.startRotationEngine(c);
});
});
}
// Bind functionality to nextItemElement
if (jQuery(c.settings.nextItemElement).length > 0) {
jQuery(c.settings.nextItemElement).bind(c.settings.nextItemElementInteraction, function() {
c.moveToNextItem(c);
});
}
// Bind functionality to previousItemElement
if (jQuery(c.settings.previousItemElement).length > 0) {
jQuery(c.settings.previousItemElement).bind(c.settings.previousItemElementInteraction, function() {
c.moveToPreviousItem(c);
});
}
// Start rotationEngine if autoStart is true
if (c.settings.autoStart) {
c.startRotationEngine(c);
}
},
// rotationEngine
rotationEngine: function(c) {
// Stop rotationEngine
c.stopRotationEngine(c);
// Reset userInteraction
c.userInteraction = false;
// Hide open content
c.$listRotator.children().each(function(index) {
if (index == c.currentItem) {
// Should we calculate next item index?
if (c.calculateNextItem) {
// Is shuffle set?
if (c.settings.shuffle) {
// Get a random item, but make sure every item is shown in each rotation
c.currentItem = c.shuffleRotationEngine(c, c.totalItems);
} else {
// Activate next item
c.currentItem++;
// Make sure currentItem resets at the end of the itemlist
c.currentItem = (c.currentItem >= c.totalItems) ? 0 : c.currentItem;
}
} else {
// Calculate previous item index instead
// Going backwards is alittle more complicated because of shuffle
// Is shuffle enabled?
if (c.settings.shuffle) {
// Now we need to find the previous previous item based on the shuffleItems list
if (c.shuffledItems.length >= 1) {
for (var j=0; j < c.shuffledItems.length; j++) {
if (c.shuffledItems[j] == c.previousItem) {
// Moving backwards in the shuffledItems list will only hold true until we reach zero.
// At this point you will get a random item if you keep going backwards
// However, this is complicated by the fact that shuffledItems is reset at the last item.
// Moving backwards when you are at the last item in the current shuffle gives you a
// random item
do {
j--;
c.previousItem = (j < 0) ? c.shuffleRotationEngine(c, c.totalItems) : c.shuffledItems[j];
} while (c.currentItem == c.previousItem);
break;
}
}
} else {
// shuffledItems was reset.
c.previousItem = c.shuffleRotationEngine(c, c.totalItems);
}
// Set currentItem to the previous shuffled item
c.currentItem = c.previousItem;
} else {
// Move index back once
c.currentItem--;
// Once index goes below zero we simply move it to the last item in the list, creating an endless loop
c.currentItem = (c.currentItem < 0) ? (c.totalItems-1) : c.currentItem;
}
}
// Loop all li elements until you locate the new active item
c.$listRotator.children().each(function (idx) {
// Is next item found?
if (idx == c.currentItem) {
// Show item
jQuery(this).show();
// Give it the active class
c.setHelperClass(c, idx, true);
// Remove active class from current active item
c.setHelperClass(c, index, false);
return false;
}
return true;
});
// Reset item index calculation
c.calculateNextItem = true;
// Run effect
c.runEffect(c, jQuery(this), true);
return false;
}
return true;
});
},
continueRotation: function(c, e, h) {
// Check if user interacted with helper during effect duration
if (! c.userInteraction) {
// Make sure the element is hidden when effect is done, not all effects end with hide
if (h && c.currentEffect) {
e.hide();
}
// Remove active class on previous active item
e.removeClass(c.settings.activeItemClass);
// Loop all li elements until you locate the new active item
c.$listRotator.children().each(function (index) {
// Is next item found?
if (index == c.currentItem) {
// Give it the active class
jQuery(this).addClass(c.settings.activeItemClass);
return false;
}
return true;
});
// Start rotationEngine
this.startRotationEngine(c);
}
},
runEffect: function(c, e, runCallback) {
// Set animationRunning flag to true
c.animationRunning = true;
// Make sure jQuery UI is installed before running UI effects, if fade effect or UI isn't installed, run the standard fadeOut effect
c.currentEffect = c.getItemEffect(c);
if (c.currentEffect == 'slide') {
var pos = '-' + c.currentItem*c.effectOptions.slideBy;
c.$listRotator.animate({left: pos}, c.getItemEffectTimer(c), 'swing', function() {
// Animation done, reset animationRunning flag
c.animationRunning = false;
// Run callback?
if (runCallback) {
c.continueRotation(c, e, false);
}
});
} else if (e.effect && c.currentEffect != 'fade' && c.currentEffect !== false) {
e.effect(c.currentEffect, c.getItemEffectOptions(c), c.getItemEffectTimer(c), function() {
// Animation done, reset animationRunning flag
c.animationRunning = false;
// Run callback?
if (runCallback) {
c.continueRotation(c, e, true);
}
});
} else if (c.currentEffect == 'fade') {
e.fadeOut(c.getItemEffectTimer(c), function() {
// Animation done, reset animationRunning flag
c.animationRunning = false;
// Run callback?
if (runCallback) {
c.continueRotation(c, e, true);
}
});
} else {
// Debug?
if (c.settings.debug) {
if (c.currentEffect !== false) {
console.debug('runEffect: Missing jQuery UI effects; Cannot run ' + c.currentEffect + '; Using none');
}
}
// Animation done, reset animationRunning flag
c.animationRunning = false;
// Run callback?
if (runCallback) {
c.continueRotation(c, e, true);
}
}
},
stopRotationEngine: function(c) {
// Stop rotationEngine
if (c.tId) {
clearInterval(c.tId);
c.tId = null;
// Debug?
if (c.settings.debug && c.in_array('info', c.settings.debugLevel)) {
console.info('stopRotationEngine: Rotation Engine stopped');
}
}
},
startRotationEngine: function(c) {
// Stop rotationEngine
c.stopRotationEngine(c);
// Start rotationEngine as long as disableRotationEngine is false
if (! c.settings.disableRotationEngine) {
var rInt = c.getItemRotationInterval(c);
c.tId = setInterval(function() {c.rotationEngine(c)}, rInt);
// Debug?
if (c.settings.debug && c.in_array('info', c.settings.debugLevel)) {
console.info('startRotationEngine: Rotation Engine started; interval ' + rInt + '; currentItem ' + c.currentItem);
}
}
},
shuffleRotationEngine: function(c, max) {
// Number holds the random generated number
var number = c.random(max);
// Loop until we get a random number that has not been used in this rotation or was the last number of the previous rotation
while (c.in_array(number, c.shuffledItems) || number == c.previousItem) {
// Generate a new number until we get the one we want
number = c.random(max);
}
// Set generated number to previousItem
c.previousItem = number;
// Add generated number to shuffle list to avoid showing it twice or more each rotation
c.shuffledItems.push(number);
// Reset shuffle list if we've been through all the items
if (c.shuffledItems.length == c.totalItems) {
c.shuffledItems = new Array();
}
// Debug?
if (c.settings.debug && c.in_array('debug', c.settings.debugLevel)) {
console.debug('shuffleRotationEngine: Number ' + number + ' generated');
if (c.shuffledItems.length <= 0) {
console.debug('shuffleRotationEngine: Shufflelist was reset');
}
}
// Return generated number
return number;
},
in_array: function(needle, haystack) {
// Returns true if needle is in haystack
for (n in haystack) {
if (haystack[n] == needle) {
return true;
}
}
return false;
},
random: function(max) {
// Return a number between 0 and "max"
return Math.floor(Math.random()*max);
},
setHelperClass: function(c, n, status) {
// Make sure helper object is set
if (jQuery(c.settings.helper).length > 0) {
// Loop childs of helper object
jQuery(c.settings.helper).children().each(function(index) {
// Find the correct child
if (index == n) {
// Toggle class
if (status) {
jQuery(this).addClass(c.settings.helperActiveItemClass);
} else {
jQuery(this).removeClass(c.settings.helperActiveItemClass);
}
return false;
}
return true;
});
}
return false;
},
moveToNextItem: function(c) {
// Move can only be done when animation isn't running
if (! c.animationRunning) {
// Make sure we intercept any ongoing animations with the userInteraction flag
c.userInteraction = true;
// Call the next item, rotationEngine will automatically figure out which item is next
c.rotationEngine(c);
}
},
moveToPreviousItem: function(c) {
// Move can only be done when animation isn't running
if (! c.animationRunning) {
// Make sure we intercept any ongoing animations with the userInteraction flag
c.userInteraction = true;
// Setting calculate next item to false will tell rotationEngine to calculate the previous item instead
c.calculateNextItem = false;
c.rotationEngine(c);
}
},
getItemObj: function(c) {
//var actualItem = (c.currentItem > 0) ? c.currentItem-1 : c.totalItems-1;
var obj = eval("c.itemControl.listIndex_" + c.currentItem);
return (typeof(obj) == 'undefined') ? false : obj;
},
getItemEffect: function(c) {
var obj = c.getItemObj(c);
if (obj !== false) {
if (typeof(obj.randomEffect) != 'undefined' && obj.randomEffect) {
return c.getRandomItemEffect(c, obj);
} else if (typeof(obj.effect) != 'undefined') {
// Debug?
if (c.settings.debug) {
console.debug('getItemEffect: itemControl effect found; Using ' + obj.effect + ' for item ' + c.currentItem);
}
return obj.effect;
}
}
return c.getRandomEffect(c);
},
getItemEffectTimer: function(c) {
var obj = c.getItemObj(c);
if (obj !== false) {
return (typeof(obj.effectTimer) == 'undefined') ? c.settings.effectTimer : obj.effectTimer;
}
return c.settings.effectTimer;
},
getItemEffectOptions: function(c) {
var obj = c.getItemObj(c);
if (obj !== false) {
return (typeof(obj.effectOptions) == 'undefined') ? c.effectOptions : jQuery.extend({}, c.effectOptions, obj.effectOptions);
}
return c.effectOptions;
},
getItemRotationInterval: function(c) {
var obj = c.getItemObj(c);
if (obj !== false) {
return (typeof(obj.rotationInterval) == 'undefined') ? c.settings.rotationInterval : obj.rotationInterval;
}
return c.settings.rotationInterval;
},
getRandomEffect: function(c) {
if (c.settings.randomEffect) {
var randomEffectNumber = c.random(c.settings.randomEffects.length);
// Debug?
if (c.settings.debug) {
console.debug('getRandomEffect: true; Using ' + c.settings.randomEffects[randomEffectNumber] + ' for item ' + c.currentItem);
}
return c.settings.randomEffects[randomEffectNumber];
}
// Debug?
if (c.settings.debug) {
var effect = (c.settings.effect) ? c.settings.effect : 'none';
console.debug('getRandomEffect: false; Using ' + effect + ' for item ' + c.currentItem);
}
return c.settings.effect;
},
getRandomItemEffect: function(c, obj) {
var randomEffectNumber
if (typeof(obj.randomEffects) != 'undefined') {
randomEffectNumber = c.random(obj.randomEffects.length);
// Debug?
if (c.settings.debug) {
console.debug('getRandomItemEffect: found itemControl randomEffects; Using ' + obj.randomEffects[randomEffectNumber] + ' for item ' + c.currentItem);
}
return obj.randomEffects[randomEffectNumber];
}
randomEffectNumber = c.random(c.settings.randomEffects.length);
// Debug?
if (c.settings.debug) {
console.debug('getRandomItemEffect: no itemControl randomEffects, using standard; Using ' + c.settings.randomEffects[randomEffectNumber] + ' for item ' + c.currentItem);
}
return c.settings.randomEffects[randomEffectNumber];
}
}