Projet

Général

Profil

Paste
Télécharger (19,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_slideshow / js / views_slideshow.js @ 13755f8d

1
(function ($) {
2
  Drupal.viewsSlideshow = Drupal.viewsSlideshow || {};
3

    
4
  /**
5
   * Views Slideshow Controls
6
   */
7
  Drupal.viewsSlideshowControls = Drupal.viewsSlideshowControls || {};
8

    
9
  /**
10
   * Implement the play hook for controls.
11
   */
12
  Drupal.viewsSlideshowControls.play = function (options) {
13
    // Route the control call to the correct control type.
14
    // Need to use try catch so we don't have to check to make sure every part
15
    // of the object is defined.
16
    try {
17
      if (typeof Drupal.settings.viewsSlideshowControls[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowControls[options.slideshowID].top.type].play == 'function') {
18
        Drupal[Drupal.settings.viewsSlideshowControls[options.slideshowID].top.type].play(options);
19
      }
20
    }
21
    catch(err) {
22
      // Don't need to do anything on error.
23
    }
24

    
25
    try {
26
      if (typeof Drupal.settings.viewsSlideshowControls[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowControls[options.slideshowID].bottom.type].play == 'function') {
27
        Drupal[Drupal.settings.viewsSlideshowControls[options.slideshowID].bottom.type].play(options);
28
      }
29
    }
30
    catch(err) {
31
      // Don't need to do anything on error.
32
    }
33
  };
34

    
35
  /**
36
   * Implement the pause hook for controls.
37
   */
38
  Drupal.viewsSlideshowControls.pause = function (options) {
39
    // Route the control call to the correct control type.
40
    // Need to use try catch so we don't have to check to make sure every part
41
    // of the object is defined.
42
    try {
43
      if (typeof Drupal.settings.viewsSlideshowControls[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowControls[options.slideshowID].top.type].pause == 'function') {
44
        Drupal[Drupal.settings.viewsSlideshowControls[options.slideshowID].top.type].pause(options);
45
      }
46
    }
47
    catch(err) {
48
      // Don't need to do anything on error.
49
    }
50

    
51
    try {
52
      if (typeof Drupal.settings.viewsSlideshowControls[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowControls[options.slideshowID].bottom.type].pause == 'function') {
53
        Drupal[Drupal.settings.viewsSlideshowControls[options.slideshowID].bottom.type].pause(options);
54
      }
55
    }
56
    catch(err) {
57
      // Don't need to do anything on error.
58
    }
59
  };
60

    
61

    
62
  /**
63
   * Views Slideshow Text Controls
64
   */
65

    
66
  // Add views slieshow api calls for views slideshow text controls.
67
  Drupal.behaviors.viewsSlideshowControlsText = {
68
    attach: function (context) {
69

    
70
      // Process previous link
71
      $('.views_slideshow_controls_text_previous:not(.views-slideshow-controls-text-previous-processed)', context).addClass('views-slideshow-controls-text-previous-processed').each(function() {
72
        var uniqueID = $(this).attr('id').replace('views_slideshow_controls_text_previous_', '');
73
        $(this).click(function() {
74
          Drupal.viewsSlideshow.action({ "action": 'previousSlide', "slideshowID": uniqueID });
75
          return false;
76
        });
77
      });
78

    
79
      // Process next link
80
      $('.views_slideshow_controls_text_next:not(.views-slideshow-controls-text-next-processed)', context).addClass('views-slideshow-controls-text-next-processed').each(function() {
81
        var uniqueID = $(this).attr('id').replace('views_slideshow_controls_text_next_', '');
82
        $(this).click(function() {
83
          Drupal.viewsSlideshow.action({ "action": 'nextSlide', "slideshowID": uniqueID });
84
          return false;
85
        });
86
      });
87

    
88
      // Process pause link
89
      $('.views_slideshow_controls_text_pause:not(.views-slideshow-controls-text-pause-processed)', context).addClass('views-slideshow-controls-text-pause-processed').each(function() {
90
        var uniqueID = $(this).attr('id').replace('views_slideshow_controls_text_pause_', '');
91
        $(this).click(function() {
92
          if (Drupal.settings.viewsSlideshow[uniqueID].paused) {
93
            Drupal.viewsSlideshow.action({ "action": 'play', "slideshowID": uniqueID, "force": true });
94
          }
95
          else {
96
            Drupal.viewsSlideshow.action({ "action": 'pause', "slideshowID": uniqueID, "force": true });
97
          }
98
          return false;
99
        });
100
      });
101
    }
102
  };
103

    
104
  Drupal.viewsSlideshowControlsText = Drupal.viewsSlideshowControlsText || {};
105

    
106
  /**
107
   * Implement the pause hook for text controls.
108
   */
109
  Drupal.viewsSlideshowControlsText.pause = function (options) {
110
    var pauseText = Drupal.theme.prototype['viewsSlideshowControlsPause'] ? Drupal.theme('viewsSlideshowControlsPause') : '';
111
    $('#views_slideshow_controls_text_pause_' + options.slideshowID + ' a').text(pauseText);
112
    $('#views_slideshow_controls_text_pause_' + options.slideshowID).removeClass('views-slideshow-controls-text-status-play');
113
    $('#views_slideshow_controls_text_pause_' + options.slideshowID).addClass('views-slideshow-controls-text-status-pause');
114
  };
115

    
116
  /**
117
   * Implement the play hook for text controls.
118
   */
119
  Drupal.viewsSlideshowControlsText.play = function (options) {
120
    var playText = Drupal.theme.prototype['viewsSlideshowControlsPlay'] ? Drupal.theme('viewsSlideshowControlsPlay') : '';
121
    $('#views_slideshow_controls_text_pause_' + options.slideshowID + ' a').text(playText);
122
    $('#views_slideshow_controls_text_pause_' + options.slideshowID).removeClass('views-slideshow-controls-text-status-pause');
123
    $('#views_slideshow_controls_text_pause_' + options.slideshowID).addClass('views-slideshow-controls-text-status-play');
124
  };
125

    
126
  // Theme the resume control.
127
  Drupal.theme.prototype.viewsSlideshowControlsPause = function () {
128
    return Drupal.t('Resume');
129
  };
130

    
131
  // Theme the pause control.
132
  Drupal.theme.prototype.viewsSlideshowControlsPlay = function () {
133
    return Drupal.t('Pause');
134
  };
135

    
136
  /**
137
   * Views Slideshow Pager
138
   */
139
  Drupal.viewsSlideshowPager = Drupal.viewsSlideshowPager || {};
140

    
141
  /**
142
   * Implement the transitionBegin hook for pagers.
143
   */
144
  Drupal.viewsSlideshowPager.transitionBegin = function (options) {
145
    // Route the pager call to the correct pager type.
146
    // Need to use try catch so we don't have to check to make sure every part
147
    // of the object is defined.
148
    try {
149
      if (typeof Drupal.settings.viewsSlideshowPager != "undefined" && typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].transitionBegin == 'function') {
150
        Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].transitionBegin(options);
151
      }
152
    }
153
    catch(err) {
154
      // Don't need to do anything on error.
155
    }
156

    
157
    try {
158
      if (typeof Drupal.settings.viewsSlideshowPager != "undefined" && typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].transitionBegin == 'function') {
159
        Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].transitionBegin(options);
160
      }
161
    }
162
    catch(err) {
163
      // Don't need to do anything on error.
164
    }
165
  };
166

    
167
  /**
168
   * Implement the goToSlide hook for pagers.
169
   */
170
  Drupal.viewsSlideshowPager.goToSlide = function (options) {
171
    // Route the pager call to the correct pager type.
172
    // Need to use try catch so we don't have to check to make sure every part
173
    // of the object is defined.
174
    try {
175
      if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].goToSlide == 'function') {
176
        Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].goToSlide(options);
177
      }
178
    }
179
    catch(err) {
180
      // Don't need to do anything on error.
181
    }
182

    
183
    try {
184
      if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].goToSlide == 'function') {
185
        Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].goToSlide(options);
186
      }
187
    }
188
    catch(err) {
189
      // Don't need to do anything on error.
190
    }
191
  };
192

    
193
  /**
194
   * Implement the previousSlide hook for pagers.
195
   */
196
  Drupal.viewsSlideshowPager.previousSlide = function (options) {
197
    // Route the pager call to the correct pager type.
198
    // Need to use try catch so we don't have to check to make sure every part
199
    // of the object is defined.
200
    try {
201
      if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].previousSlide == 'function') {
202
        Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].previousSlide(options);
203
      }
204
    }
205
    catch(err) {
206
      // Don't need to do anything on error.
207
    }
208

    
209
    try {
210
      if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].previousSlide == 'function') {
211
        Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].previousSlide(options);
212
      }
213
    }
214
    catch(err) {
215
      // Don't need to do anything on error.
216
    }
217
  };
218

    
219
  /**
220
   * Implement the nextSlide hook for pagers.
221
   */
222
  Drupal.viewsSlideshowPager.nextSlide = function (options) {
223
    // Route the pager call to the correct pager type.
224
    // Need to use try catch so we don't have to check to make sure every part
225
    // of the object is defined.
226
    try {
227
      if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].nextSlide == 'function') {
228
        Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].nextSlide(options);
229
      }
230
    }
231
    catch(err) {
232
      // Don't need to do anything on error.
233
    }
234

    
235
    try {
236
      if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].nextSlide == 'function') {
237
        Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].nextSlide(options);
238
      }
239
    }
240
    catch(err) {
241
      // Don't need to do anything on error.
242
    }
243
  };
244

    
245

    
246
  /**
247
   * Views Slideshow Pager Fields
248
   */
249

    
250
  // Add views slieshow api calls for views slideshow pager fields.
251
  Drupal.behaviors.viewsSlideshowPagerFields = {
252
    attach: function (context) {
253
      // Process pause on hover.
254
      $('.views_slideshow_pager_field:not(.views-slideshow-pager-field-processed)', context).addClass('views-slideshow-pager-field-processed').each(function() {
255
        // Parse out the location and unique id from the full id.
256
        var pagerInfo = $(this).attr('id').split('_');
257
        var location = pagerInfo[2];
258
        pagerInfo.splice(0, 3);
259
        var uniqueID = pagerInfo.join('_');
260

    
261
        // Add the activate and pause on pager hover event to each pager item.
262
        if (Drupal.settings.viewsSlideshowPagerFields[uniqueID][location].activatePauseOnHover) {
263
          $(this).children().each(function(index, pagerItem) {
264
            var mouseIn = function() {
265
              Drupal.viewsSlideshow.action({ "action": 'goToSlide', "slideshowID": uniqueID, "slideNum": index });
266
              Drupal.viewsSlideshow.action({ "action": 'pause', "slideshowID": uniqueID });
267
            };
268

    
269
            var mouseOut = function() {
270
              Drupal.viewsSlideshow.action({ "action": 'play', "slideshowID": uniqueID });
271
            };
272

    
273
            if (jQuery.fn.hoverIntent) {
274
              $(pagerItem).hoverIntent(mouseIn, mouseOut);
275
            }
276
            else {
277
              $(pagerItem).hover(mouseIn, mouseOut);
278
            }
279
          });
280
        }
281
        else {
282
          $(this).children().each(function(index, pagerItem) {
283
            $(pagerItem).click(function() {
284
              Drupal.viewsSlideshow.action({ "action": 'goToSlide', "slideshowID": uniqueID, "slideNum": index });
285
            });
286
          });
287
        }
288
      });
289
    }
290
  };
291

    
292
  Drupal.viewsSlideshowPagerFields = Drupal.viewsSlideshowPagerFields || {};
293

    
294
  /**
295
   * Implement the transitionBegin hook for pager fields pager.
296
   */
297
  Drupal.viewsSlideshowPagerFields.transitionBegin = function (options) {
298
    for (pagerLocation in Drupal.settings.viewsSlideshowPager[options.slideshowID]) {
299
      // Remove active class from pagers
300
      $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
301

    
302
      // Add active class to active pager.
303
      $('#views_slideshow_pager_field_item_'+ pagerLocation + '_' + options.slideshowID + '_' + options.slideNum).addClass('active');
304
    }
305
  };
306

    
307
  /**
308
   * Implement the goToSlide hook for pager fields pager.
309
   */
310
  Drupal.viewsSlideshowPagerFields.goToSlide = function (options) {
311
    for (pagerLocation in Drupal.settings.viewsSlideshowPager[options.slideshowID]) {
312
      // Remove active class from pagers
313
      $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
314

    
315
      // Add active class to active pager.
316
      $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + options.slideNum).addClass('active');
317
    }
318
  };
319

    
320
  /**
321
   * Implement the previousSlide hook for pager fields pager.
322
   */
323
  Drupal.viewsSlideshowPagerFields.previousSlide = function (options) {
324
    for (pagerLocation in Drupal.settings.viewsSlideshowPager[options.slideshowID]) {
325
      // Get the current active pager.
326
      var pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"].active').attr('id').replace('views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_', '');
327

    
328
      // If we are on the first pager then activate the last pager.
329
      // Otherwise activate the previous pager.
330
      if (pagerNum == 0) {
331
        pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').length() - 1;
332
      }
333
      else {
334
        pagerNum--;
335
      }
336

    
337
      // Remove active class from pagers
338
      $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
339

    
340
      // Add active class to active pager.
341
      $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + pagerNum).addClass('active');
342
    }
343
  };
344

    
345
  /**
346
   * Implement the nextSlide hook for pager fields pager.
347
   */
348
  Drupal.viewsSlideshowPagerFields.nextSlide = function (options) {
349
    for (pagerLocation in Drupal.settings.viewsSlideshowPager[options.slideshowID]) {
350
      // Get the current active pager.
351
      var pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"].active').attr('id').replace('views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_', '');
352
      var totalPagers = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').length();
353

    
354
      // If we are on the last pager then activate the first pager.
355
      // Otherwise activate the next pager.
356
      pagerNum++;
357
      if (pagerNum == totalPagers) {
358
        pagerNum = 0;
359
      }
360

    
361
      // Remove active class from pagers
362
      $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
363

    
364
      // Add active class to active pager.
365
      $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + slideNum).addClass('active');
366
    }
367
  };
368

    
369

    
370
  /**
371
   * Views Slideshow Slide Counter
372
   */
373

    
374
  Drupal.viewsSlideshowSlideCounter = Drupal.viewsSlideshowSlideCounter || {};
375

    
376
  /**
377
   * Implement the transitionBegin for the slide counter.
378
   */
379
  Drupal.viewsSlideshowSlideCounter.transitionBegin = function (options) {
380
    $('#views_slideshow_slide_counter_' + options.slideshowID + ' .num').text(options.slideNum + 1);
381
  };
382

    
383
  /**
384
   * This is used as a router to process actions for the slideshow.
385
   */
386
  Drupal.viewsSlideshow.action = function (options) {
387
    // Set default values for our return status.
388
    var status = {
389
      'value': true,
390
      'text': ''
391
    };
392

    
393
    // If an action isn't specified return false.
394
    if (typeof options.action == 'undefined' || options.action == '') {
395
      status.value = false;
396
      status.text =  Drupal.t('There was no action specified.');
397
      return error;
398
    }
399

    
400
    // If we are using pause or play switch paused state accordingly.
401
    if (options.action == 'pause') {
402
      Drupal.settings.viewsSlideshow[options.slideshowID].paused = 1;
403
      // If the calling method is forcing a pause then mark it as such.
404
      if (options.force) {
405
        Drupal.settings.viewsSlideshow[options.slideshowID].pausedForce = 1;
406
      }
407
    }
408
    else if (options.action == 'play') {
409
      // If the slideshow isn't forced pause or we are forcing a play then play
410
      // the slideshow.
411
      // Otherwise return telling the calling method that it was forced paused.
412
      if (!Drupal.settings.viewsSlideshow[options.slideshowID].pausedForce || options.force) {
413
        Drupal.settings.viewsSlideshow[options.slideshowID].paused = 0;
414
        Drupal.settings.viewsSlideshow[options.slideshowID].pausedForce = 0;
415
      }
416
      else {
417
        status.value = false;
418
        status.text += ' ' + Drupal.t('This slideshow is forced paused.');
419
        return status;
420
      }
421
    }
422

    
423
    // We use a switch statement here mainly just to limit the type of actions
424
    // that are available.
425
    switch (options.action) {
426
      case "goToSlide":
427
      case "transitionBegin":
428
      case "transitionEnd":
429
        // The three methods above require a slide number. Checking if it is
430
        // defined and it is a number that is an integer.
431
        if (typeof options.slideNum == 'undefined' || typeof options.slideNum !== 'number' || parseInt(options.slideNum) != (options.slideNum - 0)) {
432
          status.value = false;
433
          status.text = Drupal.t('An invalid integer was specified for slideNum.');
434
        }
435
      case "pause":
436
      case "play":
437
      case "nextSlide":
438
      case "previousSlide":
439
        // Grab our list of methods.
440
        var methods = Drupal.settings.viewsSlideshow[options.slideshowID]['methods'];
441

    
442
        // if the calling method specified methods that shouldn't be called then
443
        // exclude calling them.
444
        var excludeMethodsObj = {};
445
        if (typeof options.excludeMethods !== 'undefined') {
446
          // We need to turn the excludeMethods array into an object so we can use the in
447
          // function.
448
          for (var i=0; i < excludeMethods.length; i++) {
449
            excludeMethodsObj[excludeMethods[i]] = '';
450
          }
451
        }
452

    
453
        // Call every registered method and don't call excluded ones.
454
        for (i = 0; i < methods[options.action].length; i++) {
455
          if (Drupal[methods[options.action][i]] != undefined && typeof Drupal[methods[options.action][i]][options.action] == 'function' && !(methods[options.action][i] in excludeMethodsObj)) {
456
            Drupal[methods[options.action][i]][options.action](options);
457
          }
458
        }
459
        break;
460

    
461
      // If it gets here it's because it's an invalid action.
462
      default:
463
        status.value = false;
464
        status.text = Drupal.t('An invalid action "!action" was specified.', { "!action": options.action });
465
    }
466
    return status;
467
  };
468
})(jQuery);