comparison sites/all/modules/views_slideshow/js/views_slideshow.js @ 2:b74b41bb73f0

-- Google analytics module
author danieleb <danielebarchiesi@me.com>
date Thu, 22 Aug 2013 17:22:54 +0100
parents
children
comparison
equal deleted inserted replaced
1:67ce89da90df 2:b74b41bb73f0
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 };
113
114 /**
115 * Implement the play hook for text controls.
116 */
117 Drupal.viewsSlideshowControlsText.play = function (options) {
118 var playText = Drupal.theme.prototype['viewsSlideshowControlsPlay'] ? Drupal.theme('viewsSlideshowControlsPlay') : '';
119 $('#views_slideshow_controls_text_pause_' + options.slideshowID + ' a').text(playText);
120 };
121
122 // Theme the resume control.
123 Drupal.theme.prototype.viewsSlideshowControlsPause = function () {
124 return Drupal.t('Resume');
125 };
126
127 // Theme the pause control.
128 Drupal.theme.prototype.viewsSlideshowControlsPlay = function () {
129 return Drupal.t('Pause');
130 };
131
132 /**
133 * Views Slideshow Pager
134 */
135 Drupal.viewsSlideshowPager = Drupal.viewsSlideshowPager || {};
136
137 /**
138 * Implement the transitionBegin hook for pagers.
139 */
140 Drupal.viewsSlideshowPager.transitionBegin = function (options) {
141 // Route the pager call to the correct pager type.
142 // Need to use try catch so we don't have to check to make sure every part
143 // of the object is defined.
144 try {
145 if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].transitionBegin == 'function') {
146 Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].transitionBegin(options);
147 }
148 }
149 catch(err) {
150 // Don't need to do anything on error.
151 }
152
153 try {
154 if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].transitionBegin == 'function') {
155 Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].transitionBegin(options);
156 }
157 }
158 catch(err) {
159 // Don't need to do anything on error.
160 }
161 };
162
163 /**
164 * Implement the goToSlide hook for pagers.
165 */
166 Drupal.viewsSlideshowPager.goToSlide = function (options) {
167 // Route the pager call to the correct pager type.
168 // Need to use try catch so we don't have to check to make sure every part
169 // of the object is defined.
170 try {
171 if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].goToSlide == 'function') {
172 Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].goToSlide(options);
173 }
174 }
175 catch(err) {
176 // Don't need to do anything on error.
177 }
178
179 try {
180 if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].goToSlide == 'function') {
181 Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].goToSlide(options);
182 }
183 }
184 catch(err) {
185 // Don't need to do anything on error.
186 }
187 };
188
189 /**
190 * Implement the previousSlide hook for pagers.
191 */
192 Drupal.viewsSlideshowPager.previousSlide = function (options) {
193 // Route the pager call to the correct pager type.
194 // Need to use try catch so we don't have to check to make sure every part
195 // of the object is defined.
196 try {
197 if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].previousSlide == 'function') {
198 Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].previousSlide(options);
199 }
200 }
201 catch(err) {
202 // Don't need to do anything on error.
203 }
204
205 try {
206 if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].previousSlide == 'function') {
207 Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].previousSlide(options);
208 }
209 }
210 catch(err) {
211 // Don't need to do anything on error.
212 }
213 };
214
215 /**
216 * Implement the nextSlide hook for pagers.
217 */
218 Drupal.viewsSlideshowPager.nextSlide = function (options) {
219 // Route the pager call to the correct pager type.
220 // Need to use try catch so we don't have to check to make sure every part
221 // of the object is defined.
222 try {
223 if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].nextSlide == 'function') {
224 Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].top.type].nextSlide(options);
225 }
226 }
227 catch(err) {
228 // Don't need to do anything on error.
229 }
230
231 try {
232 if (typeof Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type != "undefined" && typeof Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].nextSlide == 'function') {
233 Drupal[Drupal.settings.viewsSlideshowPager[options.slideshowID].bottom.type].nextSlide(options);
234 }
235 }
236 catch(err) {
237 // Don't need to do anything on error.
238 }
239 };
240
241
242 /**
243 * Views Slideshow Pager Fields
244 */
245
246 // Add views slieshow api calls for views slideshow pager fields.
247 Drupal.behaviors.viewsSlideshowPagerFields = {
248 attach: function (context) {
249 // Process pause on hover.
250 $('.views_slideshow_pager_field:not(.views-slideshow-pager-field-processed)', context).addClass('views-slideshow-pager-field-processed').each(function() {
251 // Parse out the location and unique id from the full id.
252 var pagerInfo = $(this).attr('id').split('_');
253 var location = pagerInfo[2];
254 pagerInfo.splice(0, 3);
255 var uniqueID = pagerInfo.join('_');
256
257 // Add the activate and pause on pager hover event to each pager item.
258 if (Drupal.settings.viewsSlideshowPagerFields[uniqueID][location].activatePauseOnHover) {
259 $(this).children().each(function(index, pagerItem) {
260 var mouseIn = function() {
261 Drupal.viewsSlideshow.action({ "action": 'goToSlide', "slideshowID": uniqueID, "slideNum": index });
262 Drupal.viewsSlideshow.action({ "action": 'pause', "slideshowID": uniqueID });
263 }
264
265 var mouseOut = function() {
266 Drupal.viewsSlideshow.action({ "action": 'play', "slideshowID": uniqueID });
267 }
268
269 if (jQuery.fn.hoverIntent) {
270 $(pagerItem).hoverIntent(mouseIn, mouseOut);
271 }
272 else {
273 $(pagerItem).hover(mouseIn, mouseOut);
274 }
275
276 });
277 }
278 else {
279 $(this).children().each(function(index, pagerItem) {
280 $(pagerItem).click(function() {
281 Drupal.viewsSlideshow.action({ "action": 'goToSlide', "slideshowID": uniqueID, "slideNum": index });
282 });
283 });
284 }
285 });
286 }
287 };
288
289 Drupal.viewsSlideshowPagerFields = Drupal.viewsSlideshowPagerFields || {};
290
291 /**
292 * Implement the transitionBegin hook for pager fields pager.
293 */
294 Drupal.viewsSlideshowPagerFields.transitionBegin = function (options) {
295 for (pagerLocation in Drupal.settings.viewsSlideshowPager[options.slideshowID]) {
296 // Remove active class from pagers
297 $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
298
299 // Add active class to active pager.
300 $('#views_slideshow_pager_field_item_'+ pagerLocation + '_' + options.slideshowID + '_' + options.slideNum).addClass('active');
301 }
302
303 };
304
305 /**
306 * Implement the goToSlide hook for pager fields pager.
307 */
308 Drupal.viewsSlideshowPagerFields.goToSlide = function (options) {
309 for (pagerLocation in Drupal.settings.viewsSlideshowPager[options.slideshowID]) {
310 // Remove active class from pagers
311 $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
312
313 // Add active class to active pager.
314 $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + options.slideNum).addClass('active');
315 }
316 };
317
318 /**
319 * Implement the previousSlide hook for pager fields pager.
320 */
321 Drupal.viewsSlideshowPagerFields.previousSlide = function (options) {
322 for (pagerLocation in Drupal.settings.viewsSlideshowPager[options.slideshowID]) {
323 // Get the current active pager.
324 var pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"].active').attr('id').replace('views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_', '');
325
326 // If we are on the first pager then activate the last pager.
327 // Otherwise activate the previous pager.
328 if (pagerNum == 0) {
329 pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').length() - 1;
330 }
331 else {
332 pagerNum--;
333 }
334
335 // Remove active class from pagers
336 $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
337
338 // Add active class to active pager.
339 $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + pagerNum).addClass('active');
340 }
341 };
342
343 /**
344 * Implement the nextSlide hook for pager fields pager.
345 */
346 Drupal.viewsSlideshowPagerFields.nextSlide = function (options) {
347 for (pagerLocation in Drupal.settings.viewsSlideshowPager[options.slideshowID]) {
348 // Get the current active pager.
349 var pagerNum = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"].active').attr('id').replace('views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_', '');
350 var totalPagers = $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').length();
351
352 // If we are on the last pager then activate the first pager.
353 // Otherwise activate the next pager.
354 pagerNum++;
355 if (pagerNum == totalPagers) {
356 pagerNum = 0;
357 }
358
359 // Remove active class from pagers
360 $('[id^="views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '"]').removeClass('active');
361
362 // Add active class to active pager.
363 $('#views_slideshow_pager_field_item_' + pagerLocation + '_' + options.slideshowID + '_' + slideNum).addClass('active');
364 }
365 };
366
367
368 /**
369 * Views Slideshow Slide Counter
370 */
371
372 Drupal.viewsSlideshowSlideCounter = Drupal.viewsSlideshowSlideCounter || {};
373
374 /**
375 * Implement the transitionBegin for the slide counter.
376 */
377 Drupal.viewsSlideshowSlideCounter.transitionBegin = function (options) {
378 $('#views_slideshow_slide_counter_' + options.slideshowID + ' .num').text(options.slideNum + 1);
379 };
380
381 /**
382 * This is used as a router to process actions for the slideshow.
383 */
384 Drupal.viewsSlideshow.action = function (options) {
385 // Set default values for our return status.
386 var status = {
387 'value': true,
388 'text': ''
389 }
390
391 // If an action isn't specified return false.
392 if (typeof options.action == 'undefined' || options.action == '') {
393 status.value = false;
394 status.text = Drupal.t('There was no action specified.');
395 return error;
396 }
397
398 // If we are using pause or play switch paused state accordingly.
399 if (options.action == 'pause') {
400 Drupal.settings.viewsSlideshow[options.slideshowID].paused = 1;
401 // If the calling method is forcing a pause then mark it as such.
402 if (options.force) {
403 Drupal.settings.viewsSlideshow[options.slideshowID].pausedForce = 1;
404 }
405 }
406 else if (options.action == 'play') {
407 // If the slideshow isn't forced pause or we are forcing a play then play
408 // the slideshow.
409 // Otherwise return telling the calling method that it was forced paused.
410 if (!Drupal.settings.viewsSlideshow[options.slideshowID].pausedForce || options.force) {
411 Drupal.settings.viewsSlideshow[options.slideshowID].paused = 0;
412 Drupal.settings.viewsSlideshow[options.slideshowID].pausedForce = 0;
413 }
414 else {
415 status.value = false;
416 status.text += ' ' + Drupal.t('This slideshow is forced paused.');
417 return status;
418 }
419 }
420
421 // We use a switch statement here mainly just to limit the type of actions
422 // that are available.
423 switch (options.action) {
424 case "goToSlide":
425 case "transitionBegin":
426 case "transitionEnd":
427 // The three methods above require a slide number. Checking if it is
428 // defined and it is a number that is an integer.
429 if (typeof options.slideNum == 'undefined' || typeof options.slideNum !== 'number' || parseInt(options.slideNum) != (options.slideNum - 0)) {
430 status.value = false;
431 status.text = Drupal.t('An invalid integer was specified for slideNum.');
432 }
433 case "pause":
434 case "play":
435 case "nextSlide":
436 case "previousSlide":
437 // Grab our list of methods.
438 var methods = Drupal.settings.viewsSlideshow[options.slideshowID]['methods'];
439
440 // if the calling method specified methods that shouldn't be called then
441 // exclude calling them.
442 var excludeMethodsObj = {};
443 if (typeof options.excludeMethods !== 'undefined') {
444 // We need to turn the excludeMethods array into an object so we can use the in
445 // function.
446 for (var i=0; i < excludeMethods.length; i++) {
447 excludeMethodsObj[excludeMethods[i]] = '';
448 }
449 }
450
451 // Call every registered method and don't call excluded ones.
452 for (i = 0; i < methods[options.action].length; i++) {
453 if (Drupal[methods[options.action][i]] != undefined && typeof Drupal[methods[options.action][i]][options.action] == 'function' && !(methods[options.action][i] in excludeMethodsObj)) {
454 Drupal[methods[options.action][i]][options.action](options);
455 }
456 }
457 break;
458
459 // If it gets here it's because it's an invalid action.
460 default:
461 status.value = false;
462 status.text = Drupal.t('An invalid action "!action" was specified.', { "!action": options.action });
463 }
464 return status;
465 };
466 })(jQuery);