1/*! Swipebox v1.4.1 | Constantin Saguin csag.co | MIT License | github.com/brutaldesign/swipebox */ 2/* 3 * Pinch-zoom added by Claudio Castelpietra - www.itala.net - 21/09/2015 4 * Code identified by: 5 * C.C. begin 6 * ... 7 * C.C. end 8 */ 9; 10(function (window, document, $, undefined) { 11 12 $.swipebox = function (elem, options) { 13 14 // Default options 15 var ui, 16 defaults = { 17 useCSS: true, 18 useSVG: true, 19 initialIndexOnArray: 0, 20 removeBarsOnMobile: true, 21 hideCloseButtonOnMobile: false, 22 hideBarsDelay: 3000, 23 videoMaxWidth: 1140, 24 vimeoColor: 'cccccc', 25 beforeOpen: null, 26 afterOpen: null, 27 afterClose: null, 28 nextSlide: null, 29 prevSlide: null, 30 loopAtEnd: false, 31 autoplayVideos: false, 32 queryStringData: {}, 33 toggleClassOnLoad: '' 34 }, 35 plugin = this, 36 elements = [], // slides array [ { href:'...', title:'...' }, ...], 37 $elem, 38 selector = elem.selector, 39 $selector = $(selector), 40 isMobile = navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(Android)|(PlayBook)|(BB10)|(BlackBerry)|(Opera Mini)|(IEMobile)|(webOS)|(MeeGo)/i), 41 isTouch = isMobile !== null || document.createTouch !== undefined || ('ontouchstart' in window) || ('onmsgesturechange' in window) || navigator.msMaxTouchPoints, 42 supportSVG = !!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGRect, 43 winWidth = window.innerWidth ? window.innerWidth : $(window).width(), 44 winHeight = window.innerHeight ? window.innerHeight : $(window).height(), 45 currentX = 0, 46 /* jshint multistr: true */ 47 html = '<div id="swipebox-overlay">\ 48 <div id="swipebox-container">\ 49 <div id="swipebox-slider"></div>\ 50 <div id="swipebox-top-bar">\ 51 <div id="swipebox-title"></div>\ 52 </div>\ 53 <div id="swipebox-bottom-bar">\ 54 <div id="swipebox-arrows">\ 55 <a id="swipebox-prev"></a>\ 56 <a id="swipebox-next"></a>\ 57 </div>\ 58 </div>\ 59 <a id="swipebox-close"></a>\ 60 </div>\ 61 </div>'; 62 63 plugin.settings = {}; 64 65 $.swipebox.close = function () { 66 ui.closeSlide(); 67 }; 68 69 $.swipebox.extend = function () { 70 return ui; 71 }; 72 73 plugin.init = function () { 74 75 plugin.settings = $.extend({}, defaults, options); 76 77 if ($.isArray(elem)) { 78 79 elements = elem; 80 ui.target = $(window); 81 ui.init(plugin.settings.initialIndexOnArray); 82 83 } else { 84 85 $(document).on('click', selector, function (event) { 86 87 // console.log( isTouch ); 88 89 if (event.target.parentNode.className === 'slide current') { 90 91 return false; 92 } 93 94 if (!$.isArray(elem)) { 95 ui.destroy(); 96 $elem = $(selector); 97 ui.actions(); 98 } 99 100 elements = []; 101 var index, relType, relVal; 102 103 // Allow for HTML5 compliant attribute before legacy use of rel 104 if (!relVal) { 105 relType = 'data-rel'; 106 relVal = $(this).attr(relType); 107 } 108 109 if (!relVal) { 110 relType = 'rel'; 111 relVal = $(this).attr(relType); 112 } 113 114 if (relVal && relVal !== '' && relVal !== 'nofollow') { 115 $elem = $selector.filter('[' + relType + '="' + relVal + '"]'); 116 } else { 117 $elem = $(selector); 118 } 119 120 $elem.each(function () { 121 122 var title = null, 123 href = null; 124 125 if ($(this).attr('title')) { 126 title = $(this).attr('title'); 127 } 128 129 130 if ($(this).attr('href')) { 131 href = $(this).attr('href'); 132 } 133 134 elements.push({ 135 href: href, 136 title: title 137 }); 138 }); 139 140 index = $elem.index($(this)); 141 event.preventDefault(); 142 event.stopPropagation(); 143 ui.target = $(event.target); 144 ui.init(index); 145 }); 146 } 147 }; 148 149 ui = { 150 /** 151 * Initiate Swipebox 152 */ 153 init: function (index) { 154 if (plugin.settings.beforeOpen) { 155 plugin.settings.beforeOpen(); 156 } 157 this.target.trigger('swipebox-start'); 158 $.swipebox.isOpen = true; 159 this.build(); 160 this.openSlide(index); 161 this.openMedia(index); 162 this.preloadMedia(index + 1); 163 this.preloadMedia(index - 1); 164 if (plugin.settings.afterOpen) { 165 plugin.settings.afterOpen(); 166 } 167 }, 168 /** 169 * Built HTML containers and fire main functions 170 */ 171 build: function () { 172 var $this = this, bg; 173 174 $('body').append(html); 175 176 if (supportSVG && plugin.settings.useSVG === true) { 177 bg = $('#swipebox-close').css('background-image'); 178 bg = bg.replace('png', 'svg'); 179 $('#swipebox-prev, #swipebox-next, #swipebox-close').css({ 180 'background-image': bg 181 }); 182 } 183 184 if (isMobile && plugin.settings.removeBarsOnMobile) { 185 $('#swipebox-bottom-bar, #swipebox-top-bar').remove(); 186 } 187 188 $.each(elements, function () { 189 $('#swipebox-slider').append('<div class="slide"></div>'); 190 }); 191 192 $this.setDim(); 193 $this.actions(); 194 195 if (isTouch) { 196 $this.gesture(); 197 } 198
199 // Devices can have both touch and keyboard input so always allow key events 200 $this.keyboard(); 201 202 $this.animBars(); 203 $this.resize(); 204 205 }, 206 /** 207 * Set dimensions depending on windows width and height 208 */ 209 setDim: function () { 210 211 var width, height, sliderCss = {}; 212 213 // Reset dimensions on mobile orientation change 214 if ('onorientationchange' in window) { 215 216 window.addEventListener('orientationchange', function () { 217 if (window.orientation === 0) { 218 width = winWidth; 219 height = winHeight; 220 } else if (window.orientation === 90 || window.orientation === -90) { 221 width = winHeight; 222 height = winWidth; 223 } 224 }, false); 225 226 227 } else { 228 229 width = window.innerWidth ? window.innerWidth : $(window).width(); 230 height = window.innerHeight ? window.innerHeight : $(window).height(); 231 } 232 233 sliderCss = { 234 width: width, 235 height: height 236 }; 237 238 $('#swipebox-overlay').css(sliderCss); 239 240 }, 241 /** 242 * Reset dimensions on window resize envent 243 */ 244 resize: function () { 245 var $this = this; 246 247 $(window).resize(function () { 248 $this.setDim(); 249 }).resize(); 250 }, 251 /** 252 * Check if device supports CSS transitions 253 */ 254 supportTransition: function () { 255 256 var prefixes = 'transition WebkitTransition MozTransition OTransition msTransition KhtmlTransition'.split(' '), 257 i; 258 259 for (i = 0; i < prefixes.length; i++) { 260 if (document.createElement('div').style[ prefixes[i] ] !== undefined) { 261 return prefixes[i]; 262 } 263 } 264 return false; 265 }, 266 /** 267 * Check if CSS transitions are allowed (options + devicesupport) 268 */ 269 doCssTrans: function () { 270 if (plugin.settings.useCSS && this.supportTransition()) { 271 return true; 272 } 273 }, 274 /** 275 * Touch navigation 276 */ 277 gesture: function () { 278 279 var $this = this, 280 index, 281 hDistance, 282 vDistance, 283 hDistanceLast, 284 vDistanceLast, 285 hDistancePercent, 286 vSwipe = false, 287 hSwipe = false, 288 hSwipMinDistance = 10, 289 vSwipMinDistance = 50, 290 startCoords = {}, 291 endCoords = {}, 292 // C.C. begin 293 pinchData = {inProgress: false, lastZoom: 100, overZoom: false}, 294 // C.C. end 295 bars = $('#swipebox-top-bar, #swipebox-bottom-bar'), 296 slider = $('#swipebox-slider'); 297 298 bars.addClass('visible-bars'); 299 $this.setTimeout(); 300 301 $('body').bind('touchstart', function (event) { 302 303 $(this).addClass('touching'); 304 index = $('#swipebox-slider .slide').index($('#swipebox-slider .slide.current')); 305 endCoords = event.originalEvent.targetTouches[0]; 306 startCoords.pageX = event.originalEvent.targetTouches[0].pageX; 307 startCoords.pageY = event.originalEvent.targetTouches[0].pageY; 308 309 // C.C. begin 310 if (event.originalEvent.targetTouches.length > 1) { 311 // pinch started 312 pinchData.inProgress = true; 313 pinchData.startZoom = pinchData.lastZoom; 314 pinchData.startDistance = Math.sqrt(Math.pow(event.originalEvent.targetTouches[0].pageX - event.originalEvent.targetTouches[1].pageX, 2) + Math.pow(event.originalEvent.targetTouches[0].pageY - event.originalEvent.targetTouches[1].pageY, 2)); 315 if (pinchData.lastZoom == 100) { 316 pinchData.marginTop = 0; 317 pinchData.marginLeft = 0; 318 pinchData.diffHeight = 0; 319 pinchData.diffWidth = 0; 320 } 321 } else { 322 pinchData.inProgress = false;
323 } 324 if (!pinchData.inProgress && !pinchData.overZoom) { 325 // C.C. end 326 327 $('#swipebox-slider').css({ 328 '-webkit-transform': 'translate3d(' + currentX + '%, 0, 0)', 329 'transform': 'translate3d(' + currentX + '%, 0, 0)' 330 }); 331 332 // C.C. begin 333 } 334 // C.C. end 335 336 $('.touching').bind('touchmove', function (event) { 337 event.preventDefault(); 338 event.stopPropagation(); 339 endCoords = event.originalEvent.targetTouches[0]; 340 341 // C.C. begin 342 if (pinchData.inProgress) { 343 var lastDistance = Math.sqrt(Math.pow(event.originalEvent.targetTouches[0].pageX - event.originalEvent.targetTouches[1].pageX, 2) + Math.pow(event.originalEvent.targetTouches[0].pageY - event.originalEvent.targetTouches[1].pageY, 2)); 344 var perc = pinchData.startZoom * (1 + (lastDistance - pinchData.startDistance) / pinchData.startDistance); 345 perc = Math.max(100, perc); 346 var nowZoom = pinchData.lastZoom; 347 var nowDiffHeight = pinchData.diffHeight; 348 var nowDiffWidth = pinchData.diffWidth; 349 pinchData.lastZoom = perc; 350 351 $("#swipebox-slider .current img").css({"max-width": perc + "%", "max-height": perc + "%"}); 352 pinchData.diffHeight = Math.max(0, $("#swipebox-slider .current img").height() - $("#swipebox-slider .current").height()); 353 pinchData.diffWidth = Math.max(0, $("#swipebox-slider .current img").width() - $("#swipebox-slider .current").width()); 354 355 pinchData.overZoom = false;
356 if (pinchData.diffHeight > 0) { 357 pinchData.marginTop += -(pinchData.diffHeight - nowDiffHeight) / 2; 358 pinchData.marginTop = Math.min(0, pinchData.marginTop); 359 pinchData.marginTop = Math.max(-pinchData.diffHeight, pinchData.marginTop); 360 pinchData.overZoom = true; 361 } else { 362 pinchData.marginTop = 0; 363 } 364 if (pinchData.diffWidth > 0) { 365 pinchData.marginLeft += -(pinchData.diffWidth - nowDiffWidth) / 2; 366 pinchData.marginLeft = Math.min(0, pinchData.marginLeft); 367 pinchData.marginLeft = Math.max(-pinchData.diffWidth, pinchData.marginLeft); 368 pinchData.overZoom = true; 369 } else { 370 pinchData.marginLeft = 0; 371 } 372 $("#swipebox-slider .current img").css({"margin-top": pinchData.marginTop + 'px', "margin-left": pinchData.marginLeft + 'px'}); 373 if (nowDiffHeight == pinchData.diffHeight && nowDiffWidth == pinchData.diffWidth) { 374 pinchData.lastZoom = nowZoom; 375 pinchData.diffHeight = nowDiffHeight; 376 pinchData.diffWidth = nowDiffWidth; 377 } 378 379 return; 380 } else if (pinchData.overZoom) { 381 // let's move the image 382 383 pinchData.lastMarginTop = pinchData.marginTop + endCoords.pageY - startCoords.pageY; 384 pinchData.lastMarginTop = Math.min(0, pinchData.lastMarginTop); 385 pinchData.lastMarginTop = Math.max(-pinchData.diffHeight, pinchData.lastMarginTop); 386 387 pinchData.lastMarginLeft = pinchData.marginLeft + endCoords.pageX
387- startCoords.pageX; 388 pinchData.lastMarginLeft = Math.min(0, pinchData.lastMarginLeft); 389 pinchData.lastMarginLeft = Math.max(-pinchData.diffWidth, pinchData.lastMarginLeft); 390 391 $("#swipebox-slider .current img").css({"margin-top": pinchData.lastMarginTop + 'px', "margin-left": pinchData.lastMarginLeft + 'px'}); 392 393 return; 394 } 395 // C.C. end 396 397 if (!hSwipe) { 398 vDistanceLast = vDistance; 399 vDistance = endCoords.pageY - startCoords.pageY; 400 if (Math.abs(vDistance) >= vSwipMinDistance || vSwipe) { 401 var opacity = 0.75 - Math.abs(vDistance) / slider.height(); 402 403 slider.css({'top': vDistance + 'px'}); 404 slider.css({'opacity': opacity}); 405 406 vSwipe = true; 407 } 408 } 409 410 hDistanceLast = hDistance;
411 hDistance = endCoords.pageX - startCoords.pageX; 412 hDistancePercent = hDistance * 100 / winWidth; 413 414 if (!hSwipe && !vSwipe && Math.abs(hDistance) >= hSwipMinDistance) { 415 $('#swipebox-slider').css({ 416 '-webkit-transition': '', 417 'transition': '' 418 }); 419 hSwipe = true; 420 } 421 422 if (hSwipe) { 423 424 // swipe left 425 if (0 < hDistance) { 426 427 // first slide 428 if (0 === index) { 429 // console.log( 'first' ); 430 $('#swipebox-overlay').addClass('leftSpringTouch'); 431 } else { 432 // Follow gesture 433 $('#swipebox-overlay').removeClass('leftSpringTouch').removeClass('rightSpringTouch'); 434 $('#swipebox-slider').css({ 435 '-webkit-transform': 'translate3d(' + (currentX + hDistancePercent) + '%, 0, 0)', 436 'transform': 'translate3d(' + (currentX + hDistancePercent) + '%, 0, 0)' 437 }); 438 } 439 440 // swipe rught 441 } else if (0 > hDistance) { 442 443 // last Slide 444 if (elements.length === index + 1) { 445 // console.log( 'last' ); 446 $('#swipebox-overlay').addClass('rightSpringTouch'); 447 } else { 448 $('#swipebox-overlay').removeClass('leftSpringTouch').removeClass('rightSpringTouch'); 449 $('#swipebox-slider').css({ 450 '-webkit-transform': 'translate3d(' + (currentX + hDistancePercent) + '%, 0, 0)', 451 'transform': 'translate3d(' + (currentX + hDistancePercent) + '%, 0, 0)' 452 }); 453 } 454 455 } 456 } 457 }); 458 459 return false; 460 461 }).bind('touchend', function (event) { 462 event.preventDefault(); 463 event.stopPropagation(); 464 465 // C.C. begin 466 if (!pinchData.inProgress && pinchData.overZoom) { 467 pinchData.marginTop = pinchData.lastMarginTop; 468 pinchData.marginLeft = pinchData.lastMarginLeft; 469 } 470 if (pinchData.inProgress || pinchData.overZoom) { 471 $('.touching').off('touchmove').removeClass('touching'); 472 return; 473 } 474 // C.C. end 475 476 $('#swipebox-slider').css({ 477 '-webkit-transition': '-webkit-transform 0.4s ease', 478 'transition': 'transform 0.4s ease' 479 }); 480 481 vDistance = endCoords.pageY - startCoords.pageY;
482 hDistance = endCoords.pageX - startCoords.pageX; 483 hDistancePercent = hDistance * 100 / winWidth; 484 485 // Swipe to bottom to close 486 if (vSwipe) { 487 vSwipe = false; 488 if (Math.abs(vDistance) >= 2 * vSwipMinDistance && Math.abs(vDistance) > Math.abs(vDistanceLast)) { 489 var vOffset = vDistance > 0 ? slider.height() : -slider.height(); 490 slider.animate({top: vOffset + 'px', 'opacity': 0}, 491 300, 492 function () { 493 $this.closeSlide(); 494 }); 495 } else { 496 slider.animate({top: 0, 'opacity': 1}, 300); 497 } 498 499 } else if (hSwipe) { 500 501 hSwipe = false; 502 503 // swipeLeft 504 if (hDistance >= hSwipMinDistance && hDistance >= hDistanceLast) { 505 506 // C.C. begin 507 if (pinchData.lastZoom > 100) { 508 // let's reset css props before swiping 509 $("#swipebox-slider .current img").css({"max-width": "100%", "max-height": "100%", "margin-top": '', "margin-left": ''}); 510 pinchData.lastZoom = 100; 511 } 512 // C.C. end 513 $this.getPrev(); 514 515 // swipeRight 516 } else if (hDistance <= -hSwipMinDistance && hDistance <= hDistanceLast) { 517 518 // C.C. begin 519 if (pinchData.lastZoom > 100) { 520 // let's reset css props before swiping 521 $("#swipebox-slider .current img").css({"max-width": "100%", "max-height": "100%", "margin-top": '', "margin-left": ''}); 522 pinchData.lastZoom = 100; 523 } 524 // C.C. end 525 $this.getNext(); 526 } 527 528 } else { // Top and bottom bars have been removed on touchable devices 529 // tap 530 if (!bars.hasClass('visible-bars')) { 531 $this.showBars(); 532 $this.setTimeout(); 533 } else { 534 $this.clearTimeout(); 535 $this.hideBars(); 536 } 537 } 538 539 $('#swipebox-slider').css({ 540 '-webkit-transform': 'translate3d(' + currentX + '%, 0, 0)', 541 'transform': 'translate3d(' + currentX + '%, 0, 0)' 542 }); 543 544 $('#swipebox-overlay').removeClass('leftSpringTouch').removeClass('rightSpringTouch'); 545 $('.touching').off('touchmove').removeClass('touching'); 546 547 }); 548 }, 549 /** 550 * Set timer to hide the action bars 551 */ 552 setTimeout: function () { 553 if (plugin.settings.hideBarsDelay > 0) { 554 var $this = this; 555 $this.clearTimeout(); 556 $this.timeout = window.setTimeout(function () { 557 $this.hideBars(); 558 }, 559 plugin.settings.hideBarsDelay 560 ); 561 } 562 }, 563 /** 564 * Clear timer 565 */ 566 clearTimeout: function () { 567 window.clearTimeout(this.timeout); 568 this.timeout = null; 569 }, 570 /** 571 * Show navigation and title bars 572 */ 573 showBars: function () { 574 var bars = $('#swipebox-top-bar, #swipebox-bottom-bar'); 575 if (this.doCssTrans()) { 576 bars.addClass('visible-bars'); 577 } else { 578 $('#swipebox-top-bar').animate({top: 0}, 500); 579 $('#swipebox-bottom-bar').animate({bottom: 0}, 500); 580 setTimeout(function () { 581 bars.addClass('visible-bars'); 582 }, 1000); 583 } 584 }, 585 /** 586 * Hide navigation and title bars 587 */ 588 hideBars: function () { 589 var bars = $('#swipebox-top-bar, #swipebox-bottom-bar'); 590 if (this.doCssTrans()) { 591 bars.removeClass('visible-bars'); 592 } else { 593 $('#swipebox-top-bar').animate({top: '-50px'}, 500); 594 $('#swipebox-bottom-bar').animate({bottom: '-50px'}, 500); 595 setTimeout(function () { 596 bars.removeClass('visible-bars'); 597 }, 1000); 598 } 599 }, 600 /** 601 * Animate navigation and top bars 602 */ 603 animBars: function () { 604 var $this = this, 605 bars = $('#swipebox-top-bar, #swipebox-bottom-bar'); 606 607 bars.addClass('visible-bars'); 608 $this.setTimeout(); 609 610 $('#swipebox-slider').click(function () { 611 if (!bars.hasClass('visible-bars')) { 612 $this.showBars(); 613 $this.setTimeout(); 614 } 615 }); 616 617 $('#swipebox-bottom-bar').hover(function () { 618 $this.showBars(); 619 bars.addClass('visible-bars'); 620 $this.clearTimeout(); 621 622 }, function () { 623 if (plugin.settings.hideBarsDelay > 0) { 624 bars.removeClass('visible-bars'); 625 $this.setTimeout(); 626 } 627 628 }); 629 }, 630 /** 631 * Keyboard navigation 632 */ 633 keyboard: function () { 634 var $this = this; 635 $(window).bind('keyup', function (event) { 636 event.preventDefault(); 637 event.stopPropagation(); 638 639 if (event.keyCode === 37) { 640 641 $this.getPrev(); 642 643 } else if (event.keyCode === 39) { 644 645 $this.getNext(); 646 647 } else if (event.keyCode === 27) { 648 649 $this.closeSlide(); 650 } 651 }); 652 }, 653 /**
654 * Navigation events : go to next slide, go to prevous slide and close 655 */ 656 actions: function () { 657 var $this = this, 658 action = 'touchend click'; // Just detect for both event types to allow for multi-input 659 660 if (elements.length < 2) { 661 662 $('#swipebox-bottom-bar').hide(); 663 664 if (undefined === elements[ 1 ]) { 665 $('#swipebox-top-bar').hide(); 666 } 667 668 } else { 669 $('#swipebox-prev').bind(action, function (event) { 670 event.preventDefault(); 671 event.stopPropagation(); 672 $this.getPrev(); 673 $this.setTimeout(); 674 }); 675 676 $('#swipebox-next').bind(action, function (event) { 677 event.preventDefault(); 678 event.stopPropagation(); 679 $this.getNext(); 680 $this.setTimeout(); 681 }); 682 } 683 684 $('#swipebox-close').bind(action, function () { 685 $this.closeSlide(); 686 }); 687 }, 688 /** 689 * Set current slide 690 */ 691 setSlide: function (index, isFirst) { 692 693 isFirst = isFirst || false; 694 695 var slider = $('#swipebox-slider'); 696 697 currentX = -index * 100; 698 699 if (this.doCssTrans()) { 700 slider.css({ 701 '-webkit-transform': 'translate3d(' + (-index * 100) + '%, 0, 0)', 702 'transform': 'translate3d(' + (-index * 100) + '%, 0, 0)' 703 }); 704 } else { 705 slider.animate({left: (-index * 100) + '%'}); 706 } 707 708 $('#swipebox-slider .slide').removeClass('current'); 709 $('#swipebox-slider .slide').eq(index).addClass('current'); 710 this.setTitle(index); 711 712 if (isFirst) { 713 slider.fadeIn(); 714 } 715 716 $('#swipebox-prev, #swipebox-next').removeClass('disabled'); 717 718 if (index === 0) { 719 $('#swipebox-prev').addClass('disabled'); 720 } else if (index === elements.length - 1 && plugin.settings.loopAtEnd !== true) { 721 $('#swipebox-next').addClass('disabled'); 722 } 723 }, 724 /** 725 * Open slide 726 */ 727 openSlide: function (index) { 728 $('html').addClass('swipebox-html'); 729 if (isTouch) { 730 $('html').addClass('swipebox-touch'); 731 732 if (plugin.settings.hideCloseButtonOnMobile) { 733 $('html').addClass('swipebox-no-close-button'); 734 } 735 } else { 736 $('html').addClass('swipebox-no-touch'); 737 } 738 $(window).trigger('resize'); // fix scroll bar visibility on desktop 739 this.setSlide(index, true); 740 }, 741 /** 742 * Set a time out if the media is a video 743 */ 744 preloadMedia: function (index) { 745 var $this = this, 746 src = null; 747 748 if (elements[ index ] !== undefined) { 749 src = elements[ index ].href; 750 } 751 752 if (!$this.isVideo(src)) { 753 setTimeout(function () { 754 $this.openMedia(index); 755 }, 1000); 756 } else { 757 $this.openMedia(index); 758 } 759 }, 760 /** 761 * Open 762 */ 763 openMedia: function (index) { 764 var $this = this, 765 src, 766 slide; 767 768 if (elements[ index ] !== undefined) { 769 src = elements[ index ].href; 770 } 771 772 if (index < 0 || index >= elements.length) { 773 return false;
774 } 775 776 slide = $('#swipebox-slider .slide').eq(index); 777 778 if (!$this.isVideo(src)) { 779 slide.addClass('slide-loading'); 780 $this.loadMedia(src, function () { 781 slide.removeClass('slide-loading'); 782 slide.html(this); 783 }); 784 } else { 785 slide.html($this.getVideo(src)); 786 } 787 788 }, 789 /** 790 * Set link title attribute as caption 791 */ 792 setTitle: function (index) { 793 var title = null; 794 795 $('#swipebox-title').empty(); 796 797 if (elements[ index ] !== undefined) { 798 title = elements[ index ].title; 799 } 800 801 if (title) { 802 $('#swipebox-top-bar').show(); 803 $('#swipebox-title').append(title); 804 } else { 805 $('#swipebox-top-bar').hide(); 806 } 807 }, 808 /** 809 * Check if the URL is a video 810 */ 811 isVideo: function (src) { 812 813 if (src) { 814 if (src.match(/(youtube\.com|youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/) || src.match(/vimeo\.com\/([0-9]*)/) || src.match(/youtu\.be\/([a-zA-Z0-9\-_]+)/)) { 815 return true; 816 } 817 818 if (src.toLowerCase().indexOf('swipeboxvideo=1') >= 0) { 819 820 return true; 821 } 822 } 823 824 }, 825 /** 826 * Parse URI querystring and: 827 * - overrides value provided via dictionary 828 * - rebuild it again returning a string 829 */ 830 parseUri: function (uri, customData) { 831 var a = document.createElement('a'), 832 qs = {}; 833 834 // Decode the URI 835 a.href = decodeURIComponent(uri); 836 837 // QueryString to Object 838 if (a.search) { 839 qs = JSON.parse('{"' + a.search.toLowerCase().replace('?', '').replace(/&/g, '","').replace(/=/g, '":"') + '"}'); 840 } 841 842 // Extend with custom data 843 if ($.isPlainObject(customData)) { 844 qs = $.extend(qs, customData, plugin.settings.queryStringData); // The dev has always the final word 845 } 846 847 // Return querystring as a string 848 return $ 849 .map(qs, function (val, key) { 850 if (val && val > '') { 851 return encodeURIComponent(key) + '=' + encodeURIComponent(val); 852 } 853 }) 854 .join('&'); 855 }, 856 /** 857 * Get video iframe code from URL 858 */ 859 getVideo: function (url) { 860 var iframe = '', 861 youtubeUrl = url.match(/((?:www\.)?youtube\.com|(?:www\.)?youtube-nocookie\.com)\/watch\?v=([a-zA-Z0-9\-_]+)/), 862 youtubeShortUrl = url.match(/(?:www\.)?youtu\.be\/([a-zA-Z0-9\-_]+)/), 863 vimeoUrl = url.match(/(?:www\.)?vimeo\.com\/([0-9]*)/), 864 qs = ''; 865 if (youtubeUrl || youtubeShortUrl) { 866 if (youtubeShortUrl) { 867 youtubeUrl = youtubeShortUrl; 868 } 869 qs = ui.parseUri(url, { 870 'autoplay': (plugin.settings.autoplayVideos ? '1' : '0'), 871 'v': '' 872 }); 873 iframe = '<iframe width="560" height="315" src="//' + youtubeUrl[1] + '/embed/' + youtubeUrl[2] + '?' + qs + '" frameborder="0" allowfullscreen></iframe>'; 874 875 } else if (vimeoUrl) { 876 qs = ui.parseUri(url, { 877 'autoplay': (plugin.settings.autoplayVideos ? '1' : '0'), 878 'byline': '0', 879 'portrait': '0', 880 'color': plugin.settings.vimeoColor 881 }); 882 iframe = '<iframe width="560" height="315" src="//player.vimeo.com/video/' + vimeoUrl[1] + '?' + qs + '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'; 883 884 } else { 885 iframe = '<iframe width="560" height="315" src="' + url + '" frameborder="0" allowfullscreen></iframe>'; 886 } 887 888 return '<div class="swipebox-video-container" style="max-width:' + plugin.settings.videoMaxWidth + 'px"><div class="swipebox-video">' + iframe + '</div></div>'; 889 }, 890 /** 891 * Load image 892 */ 893 loadMedia: function (src, callback) { 894 // Inline content 895 if (src.trim().indexOf('#') === 0) { 896 callback.call( 897 $('<div>', { 898 'class': 'swipebox-inline-container' 899 }) 900 .append( 901 $(src) 902 .clone() 903 .toggleClass(plugin.settings.toggleClassOnLoad) 904 ) 905 ); 906 } 907 // Everything else 908 else { 909 if (!this.isVideo(src)) { 910 var img = $('<img>').on('load', function () { 911 callback.call(img); 912 }); 913 914 img.attr('src', src); 915 } 916 } 917 }, 918 /** 919 * Get next slide 920 */ 921 getNext: function () { 922 var $this = this, 923 src, 924 index = $('#swipebox-slider .slide').index($('#swipebox-slider .slide.current')); 925 if (index + 1 < elements.length) { 926 927 src = $('#swipebox-slider .slide').eq(index).contents().find('iframe').attr('src'); 928 $('#swipebox-slider .slide').eq(index).contents().find('iframe').attr('src', src); 929 index++; 930 $this.setSlide(index); 931 $this.preloadMedia(index + 1); 932 if (plugin.settings.nextSlide) { 933 plugin.settings.nextSlide(); 934 } 935 } else { 936 937 if (plugin.settings.loopAtEnd === true) { 938 src = $('#swipebox-slider .slide').eq(index).contents().find('iframe').attr('src'); 939 $('#swipebox-slider .slide').eq(index).contents().find('iframe').attr('src', src); 940 index = 0; 941 $this.preloadMedia(index); 942 $this.setSlide(index); 943 $this.preloadMedia(index + 1); 944 if (plugin.settings.nextSlide) { 945 plugin.settings.nextSlide(); 946 } 947 } else { 948 $('#swipebox-overlay').addClass('rightSpring'); 949 setTimeout(function () { 950 $('#swipebox-overlay').removeClass('rightSpring'); 951 }, 500); 952 } 953 } 954 }, 955 /** 956 * Get previous slide 957 */ 958 getPrev: function () { 959 var index = $('#swipebox-slider .slide').index($('#swipebox-slider .slide.current')), 960 src; 961 if (index > 0) { 962 src = $('#swipebox-slider .slide').eq(index).contents().find('iframe').attr('src'); 963 $('#swipebox-slider .slide').eq(index).contents().find('iframe').attr('src', src); 964 index--; 965 this.setSlide(index); 966 this.preloadMedia(index - 1); 967 if (plugin.settings.prevSlide) { 968 plugin.settings.prevSlide(); 969 } 970 } else { 971 $('#swipebox-overlay').addClass('leftSpring'); 972 setTimeout(function () { 973 $('#swipebox-overlay').removeClass('leftSpring'); 974 }, 500); 975 } 976 }, 977 nextSlide: function () { 978 // Callback for next slide 979 }, 980 prevSlide: function () { 981 // Callback for prev slide 982 }, 983 /** 984 * Close 985 */ 986 closeSlide: function () { 987 $('html').removeClass('swipebox-html'); 988 $('html').removeClass('swipebox-touch'); 989 $(window).trigger('resize'); 990 this.destroy(); 991 }, 992 /** 993 * Destroy the whole thing 994 */ 995 destroy: function () { 996 $(window).unbind('keyup'); 997 $('body').unbind('touchstart'); 998 $('body').unbind('touchmove'); 999 $('body').unbind('touchend'); 1000 $('#swipebox-slider').unbind(); 1001 $('#swipebox-overlay').remove(); 1002 1003 if (!$.isArray(elem)) { 1004 elem.removeData('_swipebox'); 1005 } 1006 1007 if (this.target) { 1008 this.target.trigger('swipebox-destroy'); 1009 } 1010 1011 $.swipebox.isOpen = false;
1012 1013 if (plugin.settings.afterClose) { 1014 plugin.settings.afterClose(); 1015 } 1016 } 1017 }; 1018 1019 plugin.init(); 1020 }; 1021 1022 $.fn.swipebox = function (options) { 1023 1024 if (!$.data(this, '_swipebox')) { 1025 var swipebox = new $.swipebox(this, options); 1026 this.data('_swipebox', swipebox); 1027 } 1028 return this.data('_swipebox'); 1029 1030 }; 1031 1032}(window, document, jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.