comparison modules/contrib/views_slideshow/js/views_slideshow.js @ 5:c69a71b4f40f

Add slideshow module
author Chris Cannam
date Thu, 07 Dec 2017 14:46:23 +0000
parents
children
comparison
equal deleted inserted replaced
4:8948ab6c87d2 5:c69a71b4f40f
1 (function ($, Drupal, drupalSettings) {
2 'use strict';
3 Drupal.viewsSlideshow = Drupal.viewsSlideshow || {};
4 var pagerLocation;
5 var slideNum;
6 var error;
7 var excludeMethods;
8 /**
9 * Views Slideshow Controls
10 */
11 Drupal.viewsSlideshowControls = Drupal.viewsSlideshowControls || {};
12
13 /**
14 * Implement the play hook for controls.
15 */
16 Drupal.viewsSlideshowControls.play = function (options) {
17 // Route the control call to the correct control type.
18 // Need to use try catch so we don't have to check to make sure every part
19 // of the object is defined.
20 try {
21 if (typeof drupalSettings.viewsSlideshowControls[options.slideshowID].top.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowControls[options.slideshowID].top.type].play == 'function') {
22 Drupal[drupalSettings.viewsSlideshowControls[options.slideshowID].top.type].play(options);
23 }
24 }
25 catch(err) {
26 // Don't need to do anything on error.
27 }
28
29 try {
30 if (typeof drupalSettings.viewsSlideshowControls[options.slideshowID].bottom.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowControls[options.slideshowID].bottom.type].play == 'function') {
31 Drupal[drupalSettings.viewsSlideshowControls[options.slideshowID].bottom.type].play(options);
32 }
33 }
34 catch(err) {
35 // Don't need to do anything on error.
36 }
37 };
38
39 /**
40 * Implement the pause hook for controls.
41 */
42 Drupal.viewsSlideshowControls.pause = function (options) {
43 // Route the control call to the correct control type.
44 // Need to use try catch so we don't have to check to make sure every part
45 // of the object is defined.
46 try {
47 if (typeof drupalSettings.viewsSlideshowControls[options.slideshowID].top.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowControls[options.slideshowID].top.type].pause == 'function') {
48 Drupal[drupalSettings.viewsSlideshowControls[options.slideshowID].top.type].pause(options);
49 }
50 }
51 catch(err) {
52 // Don't need to do anything on error.
53 }
54
55 try {
56 if (typeof drupalSettings.viewsSlideshowControls[options.slideshowID].bottom.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowControls[options.slideshowID].bottom.type].pause == 'function') {
57 Drupal[drupalSettings.viewsSlideshowControls[options.slideshowID].bottom.type].pause(options);
58 }
59 }
60 catch(err) {
61 // Don't need to do anything on error.
62 }
63 };
64
65
66 /**
67 * Views Slideshow Text Controls
68 */
69
70 // Add views slieshow api calls for views slideshow text controls.
71 Drupal.behaviors.viewsSlideshowControlsText = {
72 attach: function (context) {
73
74 // Process previous link
75 $('.views_slideshow_controls_text_previous:not(.views-slideshow-controls-text-previous-processed)', context).addClass('views-slideshow-controls-text-previous-processed').each(function () {
76 var uniqueID = $(this).attr('id').replace('views_slideshow_controls_text_previous_', '');
77 $(this).click(function () {
78 Drupal.viewsSlideshow.action({"action": 'previousSlide', "slideshowID": uniqueID});
79 return false;
80 });
81 });
82
83 // Process next link
84 $('.views_slideshow_controls_text_next:not(.views-slideshow-controls-text-next-processed)', context).addClass('views-slideshow-controls-text-next-processed').each(function () {
85 var uniqueID = $(this).attr('id').replace('views_slideshow_controls_text_next_', '');
86 $(this).click(function () {
87 Drupal.viewsSlideshow.action({"action": 'nextSlide', "slideshowID": uniqueID});
88 return false;
89 });
90 });
91
92 // Process pause link
93 $('.views_slideshow_controls_text_pause:not(.views-slideshow-controls-text-pause-processed)', context).addClass('views-slideshow-controls-text-pause-processed').each(function () {
94 var uniqueID = $(this).attr('id').replace('views_slideshow_controls_text_pause_', '');
95 $(this).click(function () {
96 if (drupalSettings.viewsSlideshow[uniqueID].paused) {
97 Drupal.viewsSlideshow.action({"action": 'play', "slideshowID": uniqueID, "force": true});
98 }
99 else {
100 Drupal.viewsSlideshow.action({"action": 'pause', "slideshowID": uniqueID, "force": true});
101 }
102 return false;
103 });
104 });
105 }
106 };
107
108 Drupal.viewsSlideshowControlsText = Drupal.viewsSlideshowControlsText || {};
109
110 /**
111 * Implement the pause hook for text controls.
112 */
113 Drupal.viewsSlideshowControlsText.pause = function (options) {
114 var pauseText = Drupal.theme.viewsSlideshowControlsPause ? Drupal.theme('viewsSlideshowControlsPause') : '';
115 var $element = $('#views_slideshow_controls_text_pause_' + options.slideshowID);
116 $element.find('a').text(pauseText);
117 $element.removeClass('views-slideshow-controls-text-status-play');
118 $element.addClass('views-slideshow-controls-text-status-pause');
119 };
120
121 /**
122 * Implement the play hook for text controls.
123 */
124 Drupal.viewsSlideshowControlsText.play = function (options) {
125 var playText = Drupal.theme.viewsSlideshowControlsPlay ? Drupal.theme('viewsSlideshowControlsPlay') : '';
126 var $element = $('#views_slideshow_controls_text_pause_' + options.slideshowID);
127 $element.find('a').text(playText);
128 $element.removeClass('views-slideshow-controls-text-status-pause');
129 $element.addClass('views-slideshow-controls-text-status-play');
130 };
131
132 // Theme the resume control.
133 Drupal.theme.viewsSlideshowControlsPause = function () {
134 return Drupal.t('Resume');
135 };
136
137 // Theme the pause control.
138 Drupal.theme.viewsSlideshowControlsPlay = function () {
139 return Drupal.t('Pause');
140 };
141
142 /**
143 * Views Slideshow Pager
144 */
145 Drupal.viewsSlideshowPager = Drupal.viewsSlideshowPager || {};
146
147 /**
148 * Implement the transitionBegin hook for pagers.
149 */
150 Drupal.viewsSlideshowPager.transitionBegin = function (options) {
151 // Route the pager call to the correct pager type.
152 // Need to use try catch so we don't have to check to make sure every part
153 // of the object is defined.
154 try {
155 if (typeof drupalSettings.viewsSlideshowPager != "undefined" && typeof drupalSettings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].top.type].transitionBegin == 'function') {
156 Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].top.type].transitionBegin(options);
157 }
158 }
159 catch(err) {
160 // Don't need to do anything on error.
161 }
162
163 try {
164 if (typeof drupalSettings.viewsSlideshowPager != "undefined" && typeof drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type].transitionBegin == 'function') {
165 Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type].transitionBegin(options);
166 }
167 }
168 catch(err) {
169 // Don't need to do anything on error.
170 }
171 };
172
173 /**
174 * Implement the goToSlide hook for pagers.
175 */
176 Drupal.viewsSlideshowPager.goToSlide = function (options) {
177 // Route the pager call to the correct pager type.
178 // Need to use try catch so we don't have to check to make sure every part
179 // of the object is defined.
180 try {
181 if (typeof drupalSettings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].top.type].goToSlide == 'function') {
182 Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].top.type].goToSlide(options);
183 }
184 }
185 catch(err) {
186 // Don't need to do anything on error.
187 }
188
189 try {
190 if (typeof drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type].goToSlide == 'function') {
191 Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type].goToSlide(options);
192 }
193 }
194 catch(err) {
195 // Don't need to do anything on error.
196 }
197 };
198
199 /**
200 * Implement the previousSlide hook for pagers.
201 */
202 Drupal.viewsSlideshowPager.previousSlide = function (options) {
203 // Route the pager call to the correct pager type.
204 // Need to use try catch so we don't have to check to make sure every part
205 // of the object is defined.
206 try {
207 if (typeof drupalSettings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].top.type].previousSlide == 'function') {
208 Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].top.type].previousSlide(options);
209 }
210 }
211 catch(err) {
212 // Don't need to do anything on error.
213 }
214
215 try {
216 if (typeof drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type].previousSlide == 'function') {
217 Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type].previousSlide(options);
218 }
219 }
220 catch(err) {
221 // Don't need to do anything on error.
222 }
223 };
224
225 /**
226 * Implement the nextSlide hook for pagers.
227 */
228 Drupal.viewsSlideshowPager.nextSlide = function (options) {
229 // Route the pager call to the correct pager type.
230 // Need to use try catch so we don't have to check to make sure every part
231 // of the object is defined.
232 try {
233 if (typeof drupalSettings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].top.type].nextSlide == 'function') {
234 Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].top.type].nextSlide(options);
235 }
236 }
237 catch(err) {
238 // Don't need to do anything on error.
239 }
240
241 try {
242 if (typeof drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type].nextSlide == 'function') {
243 Drupal[drupalSettings.viewsSlideshowPager[options.slideshowID].bottom.type].nextSlide(options);
244 }
245 }
246 catch(err) {
247 // Don't need to do anything on error.
248 }
249 };
250
251
252 /**
253 * Views Slideshow Pager Fields
254 */
255
256 // Add views slieshow api calls for views slideshow pager fields.
257 Drupal.behaviors.viewsSlideshowPagerFields = {
258 attach: function (context) {
259 // Process pause on hover.
260 $('.views_slideshow_pager_field:not(.views-slideshow-pager-field-processed)', context).addClass('views-slideshow-pager-field-processed').each(function () {
261 // Parse out the location and unique id from the full id.
262 var pagerInfo = $(this).attr('id').split('_');
263 var location = pagerInfo[2];
264 pagerInfo.splice(0, 3);
265 var uniqueID = pagerInfo.join('_');
266
267 // Add the activate and pause on pager hover event to each pager item.
268 if (drupalSettings.viewsSlideshowPagerFields[uniqueID][location].activatePauseOnHover) {
269 $(this).children().each(function (index, pagerItem) {
270 var mouseIn = function () {
271 Drupal.viewsSlideshow.action({"action": 'goToSlide', "slideshowID": uniqueID, "slideNum": index});
272 Drupal.viewsSlideshow.action({"action": 'pause', "slideshowID": uniqueID});
273 };
274
275 var mouseOut = function () {
276 Drupal.viewsSlideshow.action({"action": 'play', "slideshowID": uniqueID});
277 };
278
279 if (jQuery.fn.hoverIntent) {
280 $(pagerItem).hoverIntent(mouseIn, mouseOut);
281 }
282 else {
283 $(pagerItem).hover(mouseIn, mouseOut);
284 }
285 });
286 }
287 else {
288 $(this).children().each(function (index, pagerItem) {
289 $(pagerItem).click(function () {
290 Drupal.viewsSlideshow.action({"action": 'goToSlide', "slideshowID": uniqueID, "slideNum": index});
291 });
292 });
293 }
294 });
295 }
296 };
297
298 Drupal.viewsSlideshowPagerFields = Drupal.viewsSlideshowPagerFields || {};
299
300 /**
301 * Implement the transitionBegin hook for pager fields pager.
302 */
303 Drupal.viewsSlideshowPagerFields.transitionBegin = function (options) {
304 for (pagerLocation in drupalSettings.viewsSlideshowPager[options.slideshowID]) {
305 if (drupalSettings.viewsSlideshowPager[options.slideshowID]) {
306 // Remove active class from pagers
307 $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
308
309 // Add active class to active pager.
310 $('#views_slideshow_pager_field_item_'+ pagerLocation + '_' + options.slideshowID + '_' + options.slideNum).addClass('active');
311 }
312 }
313 };
314
315 /**
316 * Implement the goToSlide hook for pager fields pager.
317 */
318 Drupal.viewsSlideshowPagerFields.goToSlide = function (options) {
319 for (pagerLocation in drupalSettings.viewsSlideshowPager[options.slideshowID]) {
320 if (drupalSettings.viewsSlideshowPager[options.slideshowID]) {
321 // Remove active class from pagers
322 $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
323
324 // Add active class to active pager.
325 $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + options.slideNum).addClass('active');
326 }
327 }
328 };
329
330 /**
331 * Implement the previousSlide hook for pager fields pager.
332 */
333 Drupal.viewsSlideshowPagerFields.previousSlide = function (options) {
334 for (pagerLocation in drupalSettings.viewsSlideshowPager[options.slideshowID]) {
335 if (drupalSettings.viewsSlideshowPager[options.slideshowID]) {
336 // Get the current active pager.
337 var pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"].active').attr('id').replace('views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_', '');
338
339 // If we are on the first pager then activate the last pager.
340 // Otherwise activate the previous pager.
341 if (pagerNum === 0) {
342 pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').length() - 1;
343 }
344 else {
345 pagerNum--;
346 }
347
348 // Remove active class from pagers
349 $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
350
351 // Add active class to active pager.
352 $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + pagerNum).addClass('active');
353 }
354 }
355 };
356
357 /**
358 * Implement the nextSlide hook for pager fields pager.
359 */
360 Drupal.viewsSlideshowPagerFields.nextSlide = function (options) {
361 for (pagerLocation in drupalSettings.viewsSlideshowPager[options.slideshowID]) {
362 if (drupalSettings.viewsSlideshowPager[options.slideshowID]) {
363 // Get the current active pager.
364 var pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"].active').attr('id').replace('views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_', '');
365 var totalPagers = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').length();
366
367 // If we are on the last pager then activate the first pager.
368 // Otherwise activate the next pager.
369 pagerNum++;
370 if (pagerNum === totalPagers) {
371 pagerNum = 0;
372 }
373
374 // Remove active class from pagers
375 $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
376
377 // Add active class to active pager.
378 $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + slideNum).addClass('active');
379 }
380 }
381 };
382
383 // Copy the pager hooks from fields pager to the bullets one.
384 Drupal.viewsSlideshowPagerBullets = Drupal.viewsSlideshowPagerFields || {};
385
386 /**
387 * Views Slideshow Slide Counter
388 */
389
390 Drupal.viewsSlideshowSlideCounter = Drupal.viewsSlideshowSlideCounter || {};
391
392 /**
393 * Implement the transitionBegin for the slide counter.
394 */
395 Drupal.viewsSlideshowSlideCounter.transitionBegin = function (options) {
396 $('#views_slideshow_slide_counter_' + options.slideshowID + ' .num').text(options.slideNum + 1);
397 };
398
399 /**
400 * This is used as a router to process actions for the slideshow.
401 */
402 Drupal.viewsSlideshow.action = function (options) {
403 // Set default values for our return status.
404 var status = {
405 'value': true,
406 'text': ''
407 };
408
409 // If an action isn't specified return false.
410 if (typeof options.action == 'undefined' || options.action === '') {
411 status.value = false;
412 status.text = Drupal.t('There was no action specified.');
413 return error;
414 }
415
416 // If we are using pause or play switch paused state accordingly.
417 if (options.action === 'pause') {
418 drupalSettings.viewsSlideshow[options.slideshowID].paused = 1;
419 // If the calling method is forcing a pause then mark it as such.
420 if (options.force) {
421 drupalSettings.viewsSlideshow[options.slideshowID].pausedForce = 1;
422 }
423 }
424 else if (options.action === 'play') {
425 // If the slideshow isn't forced pause or we are forcing a play then play
426 // the slideshow.
427 // Otherwise return telling the calling method that it was forced paused.
428 if (!drupalSettings.viewsSlideshow[options.slideshowID].pausedForce || options.force) {
429 drupalSettings.viewsSlideshow[options.slideshowID].paused = 0;
430 drupalSettings.viewsSlideshow[options.slideshowID].pausedForce = 0;
431 }
432 else {
433 status.value = false;
434 status.text += ' ' + Drupal.t('This slideshow is forced paused.');
435 return status;
436 }
437 }
438
439 // We use a switch statement here mainly just to limit the type of actions
440 // that are available.
441 switch (options.action) {
442 case "goToSlide":
443 case "transitionBegin":
444 case "transitionEnd":
445 // The three methods above require a slide number. Checking if it is
446 // defined and it is a number that is an integer.
447 if (typeof options.slideNum == 'undefined' || typeof options.slideNum !== 'number' || parseInt(options.slideNum) !== (options.slideNum - 0)) {
448 status.value = false;
449 status.text = Drupal.t('An invalid integer was specified for slideNum.');
450 }
451 case "pause":
452 case "play":
453 case "nextSlide":
454 case "previousSlide":
455 // Grab our list of methods.
456 var methods = drupalSettings.viewsSlideshow[options.slideshowID]['methods'];
457
458 // if the calling method specified methods that shouldn't be called then
459 // exclude calling them.
460 var excludeMethodsObj = {};
461 if (typeof options.excludeMethods !== 'undefined') {
462 // We need to turn the excludeMethods array into an object so we can use the in
463 // function.
464 for (var i=0; i < excludeMethods.length; i++) {
465 excludeMethodsObj[excludeMethods[i]] = '';
466 }
467 }
468
469 // Call every registered method and don't call excluded ones.
470 for (var i = 0; i < methods[options.action].length; i++) {
471 if (Drupal[methods[options.action][i]] !== 'undefined' && typeof Drupal[methods[options.action][i]][options.action] == 'function' && !(methods[options.action][i] in excludeMethodsObj)) {
472 Drupal[methods[options.action][i]][options.action](options);
473 }
474 }
475 break;
476
477 // If it gets here it's because it's an invalid action.
478 default:
479 status.value = false;
480 status.text = Drupal.t('An invalid action "@action" was specified.', {"@action": options.action});
481 }
482 return status;
483 };
484 })(jQuery, Drupal, drupalSettings);