Projet

Général

Profil

Paste
Télécharger (22,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_slideshow / contrib / views_slideshow_cycle / js / views_slideshow_cycle.js @ 27370441

1

    
2
/**
3
 *  @file
4
 *  A simple jQuery Cycle Div Slideshow Rotator.
5
 */
6

    
7
/**
8
 * This will set our initial behavior, by starting up each individual slideshow.
9
 */
10
(function ($) {
11
  Drupal.behaviors.viewsSlideshowCycle = {
12
    attach: function (context) {
13
      $('.views_slideshow_cycle_main:not(.viewsSlideshowCycle-processed)', context).addClass('viewsSlideshowCycle-processed').each(function() {
14
        var fullId = '#' + $(this).attr('id');
15
        var settings = Drupal.settings.viewsSlideshowCycle[fullId];
16
        settings.targetId = '#' + $(fullId + " :first").attr('id');
17

    
18
        settings.slideshowId = settings.targetId.replace('#views_slideshow_cycle_teaser_section_', '');
19
        // Pager after function.
20
        var pager_after_fn = function(curr, next, opts) {
21
          // Need to do some special handling on first load.
22
          var slideNum = opts.currSlide;
23
          if (typeof settings.processedAfter == 'undefined' || !settings.processedAfter) {
24
            settings.processedAfter = 1;
25
            slideNum = (typeof settings.opts.startingSlide == 'undefined') ? 0 : settings.opts.startingSlide;
26
          }
27
          Drupal.viewsSlideshow.action({ "action": 'transitionEnd', "slideshowID": settings.slideshowId, "slideNum": slideNum });
28
        }
29
        // Pager before function.
30
        var pager_before_fn = function(curr, next, opts) {
31
          var slideNum = opts.nextSlide;
32

    
33
          // Remember last slide.
34
          if (settings.remember_slide) {
35
            createCookie(settings.vss_id, slideNum, settings.remember_slide_days);
36
          }
37

    
38
          // Make variable height.
39
          if (!settings.fixed_height) {
40
            //get the height of the current slide
41
            var $ht = $(next).height();
42
            //set the container's height to that of the current slide
43
            $(next).parent().animate({height: $ht});
44
          }
45

    
46
          // Need to do some special handling on first load.
47
          if (typeof settings.processedBefore == 'undefined' || !settings.processedBefore) {
48
            settings.processedBefore = 1;
49
            slideNum = (typeof opts.startingSlide == 'undefined') ? 0 : opts.startingSlide;
50
          }
51

    
52
          Drupal.viewsSlideshow.action({ "action": 'transitionBegin', "slideshowID": settings.slideshowId, "slideNum": slideNum });
53
        }
54
        settings.loaded = false;
55

    
56
        settings.opts = {
57
          speed:settings.speed,
58
          timeout:settings.timeout,
59
          delay:settings.delay,
60
          sync:settings.sync,
61
          random:settings.random,
62
          nowrap:settings.nowrap,
63
          after:pager_after_fn,
64
          before:pager_before_fn,
65
          cleartype:(settings.cleartype)? true : false,
66
          cleartypeNoBg:(settings.cleartypenobg)? true : false
67
        }
68

    
69
        // Set the starting slide if we are supposed to remember the slide
70
        if (settings.remember_slide) {
71
          var startSlide = readCookie(settings.vss_id);
72
          if (startSlide == null) {
73
            startSlide = 0;
74
          }
75
          settings.opts.startingSlide = parseInt(startSlide);
76
        }
77

    
78
        if (settings.effect == 'none') {
79
          settings.opts.speed = 1;
80
        }
81
        else {
82
          settings.opts.fx = settings.effect;
83
        }
84

    
85
        // Take starting item from fragment.
86
        var hash = location.hash;
87
        if (hash) {
88
          var hash = hash.replace('#', '');
89
          var aHash = hash.split(';');
90
          var aHashLen = aHash.length;
91

    
92
          // Loop through all the possible starting points.
93
          for (var i = 0; i < aHashLen; i++) {
94
            // Split the hash into two parts. One part is the slideshow id the
95
            // other is the slide number.
96
            var initialInfo = aHash[i].split(':');
97
            // The id in the hash should match our slideshow.
98
            // The slide number chosen shouldn't be larger than the number of
99
            // slides we have.
100
            if (settings.slideshowId == initialInfo[0] && settings.num_divs > initialInfo[1]) {
101
              settings.opts.startingSlide = parseInt(initialInfo[1]);
102
            }
103
          }
104
        }
105

    
106
        // Pause on hover.
107
        if (settings.pause) {
108
          var mouseIn = function() {
109
            Drupal.viewsSlideshow.action({ "action": 'pause', "slideshowID": settings.slideshowId });
110
          }
111

    
112
          var mouseOut = function() {
113
            Drupal.viewsSlideshow.action({ "action": 'play', "slideshowID": settings.slideshowId });
114
          }
115

    
116
          if (jQuery.fn.hoverIntent) {
117
            $('#views_slideshow_cycle_teaser_section_' + settings.vss_id).hoverIntent(mouseIn, mouseOut);
118
          }
119
          else {
120
            $('#views_slideshow_cycle_teaser_section_' + settings.vss_id).hover(mouseIn, mouseOut);
121
          }
122
        }
123

    
124
        // Pause on clicking of the slide.
125
        if (settings.pause_on_click) {
126
          $('#views_slideshow_cycle_teaser_section_' + settings.vss_id).click(function() {
127
            Drupal.viewsSlideshow.action({ "action": 'pause', "slideshowID": settings.slideshowId, "force": true });
128
          });
129
        }
130

    
131
        if (typeof JSON != 'undefined') {
132
          var advancedOptions = JSON.parse(settings.advanced_options);
133
          for (var option in advancedOptions) {
134
            switch(option) {
135

    
136
              // Standard Options
137
              case "activePagerClass":
138
              case "allowPagerClickBubble":
139
              case "autostop":
140
              case "autostopCount":
141
              case "backwards":
142
              case "bounce":
143
              case "cleartype":
144
              case "cleartypeNoBg":
145
              case "containerResize":
146
              case "continuous":
147
              case "delay":
148
              case "easeIn":
149
              case "easeOut":
150
              case "easing":
151
              case "fastOnEvent":
152
              case "fit":
153
              case "fx":
154
              case "height":
155
              case "manualTrump":
156
              case "metaAttr":
157
              case "next":
158
              case "nowrap":
159
              case "pager":
160
              case "pagerEvent":
161
              case "pause":
162
              case "pauseOnPagerHover":
163
              case "prev":
164
              case "prevNextEvent":
165
              case "random":
166
              case "randomizeEffects":
167
              case "requeueOnImageNotLoaded":
168
              case "requeueTimeout":
169
              case "rev":
170
              case "slideExpr":
171
              case "slideResize":
172
              case "speed":
173
              case "speedIn":
174
              case "speedOut":
175
              case "startingSlide":
176
              case "sync":
177
              case "timeout":
178
              case "width":
179
                var optionValue = advancedOptions[option];
180
                optionValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(optionValue);
181
                settings.opts[option] = optionValue;
182
                break;
183

    
184
              // These process options that look like {top:50, bottom:20}
185
              case "animIn":
186
              case "animOut":
187
              case "cssBefore":
188
              case "cssAfter":
189
              case "shuffle":
190
                var cssValue = advancedOptions[option];
191
                cssValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(cssValue);
192
                settings.opts[option] = eval('(' + cssValue + ')');
193
                break;
194

    
195
              // These options have their own functions.
196
              case "after":
197
                var afterValue = advancedOptions[option];
198
                afterValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(afterValue);
199
                // transition callback (scope set to element that was shown): function(currSlideElement, nextSlideElement, options, forwardFlag)
200
                settings.opts[option] = function(currSlideElement, nextSlideElement, options, forwardFlag) {
201
                  pager_after_fn(currSlideElement, nextSlideElement, options);
202
                  eval(afterValue);
203
                }
204
                break;
205

    
206
              case "before":
207
                var beforeValue = advancedOptions[option];
208
                beforeValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(beforeValue);
209
                // transition callback (scope set to element to be shown):     function(currSlideElement, nextSlideElement, options, forwardFlag)
210
                settings.opts[option] = function(currSlideElement, nextSlideElement, options, forwardFlag) {
211
                  pager_before_fn(currSlideElement, nextSlideElement, options);
212
                  eval(beforeValue);
213
                }
214
                break;
215

    
216
              case "end":
217
                var endValue = advancedOptions[option];
218
                endValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(endValue);
219
                // callback invoked when the slideshow terminates (use with autostop or nowrap options): function(options)
220
                settings.opts[option] = function(options) {
221
                  eval(endValue);
222
                }
223
                break;
224

    
225
              case "fxFn":
226
                var fxFnValue = advancedOptions[option];
227
                fxFnValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(fxFnValue);
228
                // function used to control the transition: function(currSlideElement, nextSlideElement, options, afterCalback, forwardFlag)
229
                settings.opts[option] = function(currSlideElement, nextSlideElement, options, afterCalback, forwardFlag) {
230
                  eval(fxFnValue);
231
                }
232
                break;
233

    
234
              case "onPagerEvent":
235
                var onPagerEventValue = advancedOptions[option];
236
                onPagerEventValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(onPagerEventValue);
237
                settings.opts[option] = function(zeroBasedSlideIndex, slideElement) {
238
                  eval(onPagerEventValue);
239
                }
240
                break;
241

    
242
              case "onPrevNextEvent":
243
                var onPrevNextEventValue = advancedOptions[option];
244
                onPrevNextEventValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(onPrevNextEventValue);
245
                settings.opts[option] = function(isNext, zeroBasedSlideIndex, slideElement) {
246
                  eval(onPrevNextEventValue);
247
                }
248
                break;
249

    
250
              case "pagerAnchorBuilder":
251
                var pagerAnchorBuilderValue = advancedOptions[option];
252
                pagerAnchorBuilderValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(pagerAnchorBuilderValue);
253
                // callback fn for building anchor links:  function(index, DOMelement)
254
                settings.opts[option] = function(index, DOMelement) {
255
                  var returnVal = '';
256
                  eval(pagerAnchorBuilderValue);
257
                  return returnVal;
258
                }
259
                break;
260

    
261
              case "pagerClick":
262
                var pagerClickValue = advancedOptions[option];
263
                pagerClickValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(pagerClickValue);
264
                // callback fn for pager clicks:    function(zeroBasedSlideIndex, slideElement)
265
                settings.opts[option] = function(zeroBasedSlideIndex, slideElement) {
266
                  eval(pagerClickValue);
267
                }
268
                break;
269

    
270
              case "paused":
271
                var pausedValue = advancedOptions[option];
272
                pausedValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(pausedValue);
273
                // undocumented callback when slideshow is paused:    function(cont, opts, byHover)
274
                settings.opts[option] = function(cont, opts, byHover) {
275
                  eval(pausedValue);
276
                }
277
                break;
278

    
279
              case "resumed":
280
                var resumedValue = advancedOptions[option];
281
                resumedValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(resumedValue);
282
                // undocumented callback when slideshow is resumed:    function(cont, opts, byHover)
283
                settings.opts[option] = function(cont, opts, byHover) {
284
                  eval(resumedValue);
285
                }
286
                break;
287

    
288
              case "timeoutFn":
289
                var timeoutFnValue = advancedOptions[option];
290
                timeoutFnValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(timeoutFnValue);
291
                settings.opts[option] = function(currSlideElement, nextSlideElement, options, forwardFlag) {
292
                  eval(timeoutFnValue);
293
                }
294
                break;
295

    
296
              case "updateActivePagerLink":
297
                var updateActivePagerLinkValue = advancedOptions[option];
298
                updateActivePagerLinkValue = Drupal.viewsSlideshowCycle.advancedOptionCleanup(updateActivePagerLinkValue);
299
                // callback fn invoked to update the active pager link (adds/removes activePagerClass style)
300
                settings.opts[option] = function(pager, currSlideIndex) {
301
                  eval(updateActivePagerLinkValue);
302
                }
303
                break;
304
            }
305
          }
306
        }
307

    
308
        // If selected wait for the images to be loaded.
309
        // otherwise just load the slideshow.
310
        if (settings.wait_for_image_load) {
311
          // For IE/Chrome/Opera we if there are images then we need to make
312
          // sure the images are loaded before starting the slideshow.
313
          settings.totalImages = $(settings.targetId + ' img').length;
314
          if (settings.totalImages) {
315
            settings.loadedImages = 0;
316

    
317
            // Add a load event for each image.
318
            $(settings.targetId + ' img').each(function() {
319
              var $imageElement = $(this);
320
              $imageElement.bind('load', function () {
321
                Drupal.viewsSlideshowCycle.imageWait(fullId);
322
              });
323

    
324
              // Removing the source and adding it again will fire the load event.
325
              var imgSrc = $imageElement.attr('src');
326
              $imageElement.attr('src', '');
327
              $imageElement.attr('src', imgSrc);
328
            });
329

    
330
            // We need to set a timeout so that the slideshow doesn't wait
331
            // indefinitely for all images to load.
332
            setTimeout("Drupal.viewsSlideshowCycle.load('" + fullId + "')", settings.wait_for_image_load_timeout);
333
          }
334
          else {
335
            Drupal.viewsSlideshowCycle.load(fullId);
336
          }
337
        }
338
        else {
339
          Drupal.viewsSlideshowCycle.load(fullId);
340
        }
341
      });
342
    }
343
  };
344

    
345
  Drupal.viewsSlideshowCycle = Drupal.viewsSlideshowCycle || {};
346

    
347
  // Cleanup the values of advanced options.
348
  Drupal.viewsSlideshowCycle.advancedOptionCleanup = function(value) {
349
    value = $.trim(value);
350
    value = value.replace(/\n/g, '');
351
    if (!isNaN(parseInt(value))) {
352
      value = parseInt(value);
353
    }
354
    else if (value.toLowerCase() == 'true') {
355
      value = true;
356
    }
357
    else if (value.toLowerCase() == 'false') {
358
      value = false;
359
    }
360

    
361
    return value;
362
  }
363

    
364
  // This checks to see if all the images have been loaded.
365
  // If they have then it starts the slideshow.
366
  Drupal.viewsSlideshowCycle.imageWait = function(fullId) {
367
    if (++Drupal.settings.viewsSlideshowCycle[fullId].loadedImages == Drupal.settings.viewsSlideshowCycle[fullId].totalImages) {
368
      Drupal.viewsSlideshowCycle.load(fullId);
369
    }
370
  };
371

    
372
  // Start the slideshow.
373
  Drupal.viewsSlideshowCycle.load = function (fullId) {
374
    var settings = Drupal.settings.viewsSlideshowCycle[fullId];
375

    
376
    // Make sure the slideshow isn't already loaded.
377
    if (!settings.loaded) {
378
      $(settings.targetId).cycle(settings.opts);
379
      settings.loaded = true;
380

    
381
      // Start Paused
382
      if (settings.start_paused) {
383
        Drupal.viewsSlideshow.action({ "action": 'pause', "slideshowID": settings.slideshowId, "force": true });
384
      }
385

    
386
      // Pause if hidden.
387
      if (settings.pause_when_hidden) {
388
        var checkPause = function(settings) {
389
          // If the slideshow is visible and it is paused then resume.
390
          // otherwise if the slideshow is not visible and it is not paused then
391
          // pause it.
392
          var visible = viewsSlideshowCycleIsVisible(settings.targetId, settings.pause_when_hidden_type, settings.amount_allowed_visible);
393
          if (visible) {
394
            Drupal.viewsSlideshow.action({ "action": 'play', "slideshowID": settings.slideshowId });
395
          }
396
          else {
397
            Drupal.viewsSlideshow.action({ "action": 'pause', "slideshowID": settings.slideshowId });
398
          }
399
        }
400

    
401
        // Check when scrolled.
402
        $(window).scroll(function() {
403
         checkPause(settings);
404
        });
405

    
406
        // Check when the window is resized.
407
        $(window).resize(function() {
408
          checkPause(settings);
409
        });
410
      }
411
    }
412
  };
413

    
414
  Drupal.viewsSlideshowCycle.pause = function (options) {
415
    //Eat TypeError, cycle doesn't handle pause well if options isn't defined.
416
    try{
417
      if (options.pause_in_middle && $.fn.pause) {
418
        $('#views_slideshow_cycle_teaser_section_' + options.slideshowID).pause();
419
      }
420
      else {
421
        $('#views_slideshow_cycle_teaser_section_' + options.slideshowID).cycle('pause');
422
      }
423
    }
424
    catch(e){
425
      if(!e instanceof TypeError){
426
        throw e;
427
      }
428
    }
429
  };
430

    
431
  Drupal.viewsSlideshowCycle.play = function (options) {
432
    Drupal.settings.viewsSlideshowCycle['#views_slideshow_cycle_main_' + options.slideshowID].paused = false;
433
    if (options.pause_in_middle && $.fn.resume) {
434
      $('#views_slideshow_cycle_teaser_section_' + options.slideshowID).resume();
435
    }
436
    else {
437
      $('#views_slideshow_cycle_teaser_section_' + options.slideshowID).cycle('resume');
438
    }
439
  };
440

    
441
  Drupal.viewsSlideshowCycle.previousSlide = function (options) {
442
    $('#views_slideshow_cycle_teaser_section_' + options.slideshowID).cycle('prev');
443
  };
444

    
445
  Drupal.viewsSlideshowCycle.nextSlide = function (options) {
446
    $('#views_slideshow_cycle_teaser_section_' + options.slideshowID).cycle('next');
447
  };
448

    
449
  Drupal.viewsSlideshowCycle.goToSlide = function (options) {
450
    $('#views_slideshow_cycle_teaser_section_' + options.slideshowID).cycle(options.slideNum);
451
  };
452

    
453
  // Verify that the value is a number.
454
  function IsNumeric(sText) {
455
    var ValidChars = "0123456789";
456
    var IsNumber=true;
457
    var Char;
458

    
459
    for (var i=0; i < sText.length && IsNumber == true; i++) {
460
      Char = sText.charAt(i);
461
      if (ValidChars.indexOf(Char) == -1) {
462
        IsNumber = false;
463
      }
464
    }
465
    return IsNumber;
466
  }
467

    
468
  /**
469
   * Cookie Handling Functions
470
   */
471
  function createCookie(name,value,days) {
472
    if (days) {
473
      var date = new Date();
474
      date.setTime(date.getTime()+(days*24*60*60*1000));
475
      var expires = "; expires="+date.toGMTString();
476
    }
477
    else {
478
      var expires = "";
479
    }
480
    document.cookie = name+"="+value+expires+"; path=/";
481
  }
482

    
483
  function readCookie(name) {
484
    var nameEQ = name + "=";
485
    var ca = document.cookie.split(';');
486
    for(var i=0;i < ca.length;i++) {
487
      var c = ca[i];
488
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
489
      if (c.indexOf(nameEQ) == 0) {
490
        return c.substring(nameEQ.length,c.length);
491
      }
492
    }
493
    return null;
494
  }
495

    
496
  function eraseCookie(name) {
497
    createCookie(name,"",-1);
498
  }
499

    
500
  /**
501
   * Checks to see if the slide is visible enough.
502
   * elem = element to check.
503
   * type = The way to calculate how much is visible.
504
   * amountVisible = amount that should be visible. Either in percent or px. If
505
   *                it's not defined then all of the slide must be visible.
506
   *
507
   * Returns true or false
508
   */
509
  function viewsSlideshowCycleIsVisible(elem, type, amountVisible) {
510
    // Get the top and bottom of the window;
511
    var docViewTop = $(window).scrollTop();
512
    var docViewBottom = docViewTop + $(window).height();
513
    var docViewLeft = $(window).scrollLeft();
514
    var docViewRight = docViewLeft + $(window).width();
515

    
516
    // Get the top, bottom, and height of the slide;
517
    var elemTop = $(elem).offset().top;
518
    var elemHeight = $(elem).height();
519
    var elemBottom = elemTop + elemHeight;
520
    var elemLeft = $(elem).offset().left;
521
    var elemWidth = $(elem).width();
522
    var elemRight = elemLeft + elemWidth;
523
    var elemArea = elemHeight * elemWidth;
524

    
525
    // Calculate what's hiding in the slide.
526
    var missingLeft = 0;
527
    var missingRight = 0;
528
    var missingTop = 0;
529
    var missingBottom = 0;
530

    
531
    // Find out how much of the slide is missing from the left.
532
    if (elemLeft < docViewLeft) {
533
      missingLeft = docViewLeft - elemLeft;
534
    }
535

    
536
    // Find out how much of the slide is missing from the right.
537
    if (elemRight > docViewRight) {
538
      missingRight = elemRight - docViewRight;
539
    }
540

    
541
    // Find out how much of the slide is missing from the top.
542
    if (elemTop < docViewTop) {
543
      missingTop = docViewTop - elemTop;
544
    }
545

    
546
    // Find out how much of the slide is missing from the bottom.
547
    if (elemBottom > docViewBottom) {
548
      missingBottom = elemBottom - docViewBottom;
549
    }
550

    
551
    // If there is no amountVisible defined then check to see if the whole slide
552
    // is visible.
553
    if (type == 'full') {
554
      return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
555
      && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop)
556
      && (elemLeft >= docViewLeft) && (elemRight <= docViewRight)
557
      && (elemLeft <= docViewRight) && (elemRight >= docViewLeft));
558
    }
559
    else if(type == 'vertical') {
560
      var verticalShowing = elemHeight - missingTop - missingBottom;
561

    
562
      // If user specified a percentage then find out if the current shown percent
563
      // is larger than the allowed percent.
564
      // Otherwise check to see if the amount of px shown is larger than the
565
      // allotted amount.
566
      if (amountVisible.indexOf('%')) {
567
        return (((verticalShowing/elemHeight)*100) >= parseInt(amountVisible));
568
      }
569
      else {
570
        return (verticalShowing >= parseInt(amountVisible));
571
      }
572
    }
573
    else if(type == 'horizontal') {
574
      var horizontalShowing = elemWidth - missingLeft - missingRight;
575

    
576
      // If user specified a percentage then find out if the current shown percent
577
      // is larger than the allowed percent.
578
      // Otherwise check to see if the amount of px shown is larger than the
579
      // allotted amount.
580
      if (amountVisible.indexOf('%')) {
581
        return (((horizontalShowing/elemWidth)*100) >= parseInt(amountVisible));
582
      }
583
      else {
584
        return (horizontalShowing >= parseInt(amountVisible));
585
      }
586
    }
587
    else if(type == 'area') {
588
      var areaShowing = (elemWidth - missingLeft - missingRight) * (elemHeight - missingTop - missingBottom);
589

    
590
      // If user specified a percentage then find out if the current shown percent
591
      // is larger than the allowed percent.
592
      // Otherwise check to see if the amount of px shown is larger than the
593
      // allotted amount.
594
      if (amountVisible.indexOf('%')) {
595
        return (((areaShowing/elemArea)*100) >= parseInt(amountVisible));
596
      }
597
      else {
598
        return (areaShowing >= parseInt(amountVisible));
599
      }
600
    }
601
  }
602
})(jQuery);