summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Häggqvist <[email protected]>2014-06-09 16:15:23 +0200
committerVictor Häggqvist <[email protected]>2014-06-09 16:15:23 +0200
commit411adde03dce1da3d60eb6634fe0cf761f0dff93 (patch)
tree7908383aa9a1c82d416e359d8f95eb649f6ce7e6
parent05c23edfc0105ad539f1c3efdf18880d2ecb8b76 (diff)
stuff
-rw-r--r--.gitignore2
-rw-r--r--Gruntfile.js116
-rw-r--r--README.md9
-rw-r--r--dist/image-lightbox.js311
-rw-r--r--dist/image-lightbox.min.js5
-rw-r--r--js/image-lightbox.js307
6 files changed, 126 insertions, 624 deletions
diff --git a/.gitignore b/.gitignore
index a4af05a..756a42d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,3 +26,5 @@ node_modules
# Users Environment Variables
.lock-wscript
+
+bower_components
diff --git a/Gruntfile.js b/Gruntfile.js
new file mode 100644
index 0000000..8d74cc1
--- /dev/null
+++ b/Gruntfile.js
@@ -0,0 +1,116 @@
+/*global module:false*/
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ // Metadata.
+ pkg: grunt.file.readJSON('package.json'),
+ banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
+ '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
+ '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
+ '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>;' +
+ ' Licensed <%= pkg.license %>\n' +
+ '* Contributors <%= pkg.contributors.join(", ") %> */\n',
+ // Task configuration.
+ concat: {
+ options: {
+ banner: '<%= banner %>',
+ stripBanners: true
+ },
+ dist: {
+ src: ['js/<%= pkg.name %>.js'],
+ dest: 'dist/<%= pkg.name %>.js'
+ }
+ },
+ uglify: {
+ options: {
+ banner: '<%= banner %>'
+ },
+ dist: {
+ src: '<%= concat.dist.dest %>',
+ dest: 'dist/<%= pkg.name %>.min.js'
+ }
+ },
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ unused: true,
+ boss: true,
+ eqnull: true,
+ globals: {}
+ },
+ gruntfile: {
+ src: 'Gruntfile.js'
+ },
+ main: {
+ src: 'js/*.js'
+ }
+ },
+ watch: {
+ gruntfile: {
+ files: '<%= jshint.gruntfile.src %>',
+ tasks: ['jshint:gruntfile']
+ },
+ main: {
+ options: {
+ reload: true
+ },
+ files: ['<%= jshint.main.src %>', 'sass/*.scss'],
+ // tasks: ['jshint:main', 'sass:dist_exp']
+ tasks: ['sass', 'copy']
+ }
+ },
+ sass: {
+ dist: {
+ options: {
+ style: 'compressed',
+ compass: true
+ },
+ files: {
+ 'dist/<%= pkg.name %>.min.css': 'sass/*.scss'
+ }
+ },
+ dist_exp: {
+ options: {
+ style: 'expanded',
+ sourcemap: true,
+ compass: true
+ },
+ files: {
+ 'dist/<%= pkg.name %>.css': 'sass/*.scss'
+ }
+ }
+ },
+ copy: {
+ demo: {
+ expand: true,
+ flatten: true,
+ src: 'dist/*.min.*',
+ dest: 'demo/',
+ filter: 'isFile'
+ }
+ }
+ });
+
+ // These plugins provide necessary tasks.
+ grunt.loadNpmTasks('grunt-contrib-concat');
+ grunt.loadNpmTasks('grunt-contrib-copy');
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-contrib-sass');
+ grunt.loadNpmTasks('grunt-contrib-uglify');
+ grunt.loadNpmTasks('grunt-contrib-watch');
+
+ // Default task.
+ // grunt.registerTask('default', ['jshint', 'concat', 'uglify', 'sass']);
+ grunt.registerTask('default', ['concat', 'uglify', 'sass']);
+
+ grunt.registerTask('demo', ['copy']);
+
+};
diff --git a/README.md b/README.md
index 20b2cdf..ce81b24 100644
--- a/README.md
+++ b/README.md
@@ -2,8 +2,15 @@ Image Lightbox
==============
Image lightbox was originly created by [Osvaldas Valutis](http://osvaldas.info/image-lightbox-responsive-touch-friendly), though it was only accesible through his site. I put it here to make it more maintainable.
-#Install
+#How to get it
ATM, `git clone` the repo or download the zip.
+#How to build
+Image Lightbox is buildt using the Grunt toolchain. The CSS is build using SASS and Compass, which means you need Ruby, the SASS and Compass gems to make it.
+
+Note: Remenber that you ATM need a alpha build of Compass to make it work with Grunt (`gem install compass --pre`).
+
+If you have Node just do a `npm install` then `grunt` and you will have a new build in the `dist/` directory.
+
#License
MIT
diff --git a/dist/image-lightbox.js b/dist/image-lightbox.js
deleted file mode 100644
index 56b095c..0000000
--- a/dist/image-lightbox.js
+++ /dev/null
@@ -1,311 +0,0 @@
-/*! image-lightbox - v0.1.0 - 2014-06-07
-* https://github.com/victorhaggqvist/image-lightbox
-* Copyright (c) 2014 Osvaldas Valutis (www.osvaldas.info); Licensed MIT
-* Contributors Victor Häggqvist (victorhaggqvist.com) */
-;( function( $, window, document, undefined )
-{
- 'use strict';
-
- var cssTransitionSupport = function()
- {
- var s = document.body || document.documentElement, s = s.style;
- if( s.WebkitTransition == '' ) return '-webkit-';
- if( s.MozTransition == '' ) return '-moz-';
- if( s.OTransition == '' ) return '-o-';
- if( s.transition == '' ) return '';
- return false;
- },
-
- isCssTransitionSupport = cssTransitionSupport() === false ? false : true,
-
- cssTransitionTranslateX = function( element, positionX, speed )
- {
- var options = {}, prefix = cssTransitionSupport();
- options[ prefix + 'transform' ] = 'translateX(' + positionX + ')';
- options[ prefix + 'transition' ] = prefix + 'transform ' + speed + 's linear';
- element.css( options );
- },
-
- hasTouch = ( 'ontouchstart' in window ),
- hasPointers = window.navigator.pointerEnabled || window.navigator.msPointerEnabled,
- wasTouched = function( event )
- {
- if( hasTouch )
- return true;
-
- if( !hasPointers || typeof event === 'undefined' || typeof event.pointerType === 'undefined' )
- return false;
-
- if( typeof event.MSPOINTER_TYPE_MOUSE !== 'undefined' )
- {
- if( event.MSPOINTER_TYPE_MOUSE != event.pointerType )
- return true;
- }
- else
- if( event.pointerType != 'mouse' )
- return true;
-
- return false;
- };
-
- $.fn.imageLightbox = function( options )
- {
- var options = $.extend(
- {
- selector: 'id="imagelightbox"',
- allowedTypes: 'png|jpg|jpeg|gif',
- animationSpeed: 250,
- preloadNext: true,
- enableKeyboard: true,
- quitOnEnd: false,
- quitOnImgClick: false,
- quitOnDocClick: true,
- onStart: false,
- onEnd: false,
- onLoadStart: false,
- onLoadEnd: false
- },
- options ),
-
- targets = $([]),
- target = $(),
- image = $(),
- imageWidth = 0,
- imageHeight = 0,
- swipeDiff = 0,
- inProgress = false,
-
- isTargetValid = function( element )
- {
- return $( element ).prop( 'tagName' ).toLowerCase() == 'a' && ( new RegExp( '\.(' + options.allowedTypes + ')$', 'i' ) ).test( $( element ).attr( 'href' ) );
- },
-
- setImage = function()
- {
- if( !image.length ) return false;
-
- var screenWidth = $( window ).width() * 0.8,
- screenHeight = $( window ).height() * 0.9,
- tmpImage = new Image();
-
- tmpImage.src = image.attr( 'src' );
- tmpImage.onload = function()
- {
- imageWidth = tmpImage.width;
- imageHeight = tmpImage.height;
-
- if( imageWidth > screenWidth || imageHeight > screenHeight )
- {
- var ratio = imageWidth / imageHeight > screenWidth / screenHeight ? imageWidth / screenWidth : imageHeight / screenHeight;
- imageWidth /= ratio;
- imageHeight /= ratio;
- }
-
- image.css(
- {
- 'width': imageWidth + 'px',
- 'height': imageHeight + 'px',
- 'top': ( $( window ).height() - imageHeight ) / 2 + 'px',
- 'left': ( $( window ).width() - imageWidth ) / 2 + 'px'
- });
- };
- },
-
- loadImage = function( direction )
- {
- if( inProgress ) return false;
-
- direction = typeof direction === 'undefined' ? false : direction == 'left' ? 1 : -1;
-
- if( image.length )
- {
- if( direction !== false && ( targets.length < 2 || ( options.quitOnEnd === true && ( ( direction === -1 && targets.index( target ) == 0 ) || ( direction === 1 && targets.index( target ) == targets.length - 1 ) ) ) ) )
- {
- quitLightbox();
- return false;
- }
- var params = { 'opacity': 0 };
- if( isCssTransitionSupport ) cssTransitionTranslateX( image, ( 100 * direction ) - swipeDiff + 'px', options.animationSpeed / 1000 );
- else params.left = parseInt( image.css( 'left' ) ) + 100 * direction + 'px';
- image.animate( params, options.animationSpeed, function(){ removeImage(); });
- swipeDiff = 0;
- }
-
- inProgress = true;
- if( options.onLoadStart !== false ) options.onLoadStart();
-
- setTimeout( function()
- {
- image = $( '<img ' + options.selector + ' />' )
- .attr( 'src', target.attr( 'href' ) )
- .load( function()
- {
- image.appendTo( 'body' );
- setImage();
-
- var params = { 'opacity': 1 };
-
- image.css( 'opacity', 0 );
- if( isCssTransitionSupport )
- {
- cssTransitionTranslateX( image, -100 * direction + 'px', 0 );
- setTimeout( function(){ cssTransitionTranslateX( image, 0 + 'px', options.animationSpeed / 1000 ) }, 50 );
- }
- else
- {
- var imagePosLeft = parseInt( image.css( 'left' ) );
- params.left = imagePosLeft + 'px';
- image.css( 'left', imagePosLeft - 100 * direction + 'px' );
- }
-
- image.animate( params, options.animationSpeed, function()
- {
- inProgress = false;
- if( options.onLoadEnd !== false ) options.onLoadEnd();
- });
- if( options.preloadNext )
- {
- var nextTarget = targets.eq( targets.index( target ) + 1 );
- if( !nextTarget.length ) nextTarget = targets.eq( 0 );
- $( '<img />' ).attr( 'src', nextTarget.attr( 'href' ) ).load();
- }
- })
- .error( function()
- {
- if( options.onLoadEnd !== false ) options.onLoadEnd();
- })
-
- var swipeStart = 0,
- swipeEnd = 0,
- imagePosLeft = 0;
-
- image.on( hasPointers ? 'pointerup MSPointerUp' : 'click', function( e )
- {
- e.preventDefault();
- if( options.quitOnImgClick )
- {
- quitLightbox();
- return false;
- }
- if( wasTouched( e.originalEvent ) ) return true;
- var posX = ( e.pageX || e.originalEvent.pageX ) - e.target.offsetLeft;
- target = targets.eq( targets.index( target ) - ( imageWidth / 2 > posX ? 1 : -1 ) );
- if( !target.length ) target = targets.eq( imageWidth / 2 > posX ? targets.length : 0 );
- loadImage( imageWidth / 2 > posX ? 'left' : 'right' );
- })
- .on( 'touchstart pointerdown MSPointerDown', function( e )
- {
- if( !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
- if( isCssTransitionSupport ) imagePosLeft = parseInt( image.css( 'left' ) );
- swipeStart = e.originalEvent.pageX || e.originalEvent.touches[ 0 ].pageX;
- })
- .on( 'touchmove pointermove MSPointerMove', function( e )
- {
- if( !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
- e.preventDefault();
- swipeEnd = e.originalEvent.pageX || e.originalEvent.touches[ 0 ].pageX;
- swipeDiff = swipeStart - swipeEnd;
- if( isCssTransitionSupport ) cssTransitionTranslateX( image, -swipeDiff + 'px', 0 );
- else image.css( 'left', imagePosLeft - swipeDiff + 'px' );
- })
- .on( 'touchend touchcancel pointerup MSPointerUp', function( e )
- {
- if( !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
- if( Math.abs( swipeDiff ) > 50 )
- {
- target = targets.eq( targets.index( target ) - ( swipeDiff < 0 ? 1 : -1 ) );
- if( !target.length ) target = targets.eq( swipeDiff < 0 ? targets.length : 0 );
- loadImage( swipeDiff > 0 ? 'right' : 'left' );
- }
- else
- {
- if( isCssTransitionSupport ) cssTransitionTranslateX( image, 0 + 'px', options.animationSpeed / 1000 );
- else image.animate({ 'left': imagePosLeft + 'px' }, options.animationSpeed / 2 );
- }
- });
-
- }, options.animationSpeed + 100 );
- },
-
- removeImage = function()
- {
- if( !image.length ) return false;
- image.remove();
- image = $();
- },
-
- quitLightbox = function()
- {
- if( !image.length ) return false;
- image.animate({ 'opacity': 0 }, options.animationSpeed, function()
- {
- removeImage();
- inProgress = false;
- if( options.onEnd !== false ) options.onEnd();
- });
- };
-
- $( window ).on( 'resize', setImage );
-
- if( options.quitOnDocClick )
- {
- $( document ).on( hasTouch ? 'touchend' : 'click', function( e )
- {
- if( image.length && !$( e.target ).is( image ) ) quitLightbox();
- })
- }
-
- if( options.enableKeyboard )
- {
- $( document ).on( 'keyup', function( e )
- {
- if( !image.length ) return true;
- e.preventDefault();
- if( e.keyCode == 27 ) quitLightbox();
- if( e.keyCode == 37 || e.keyCode == 39 )
- {
- target = targets.eq( targets.index( target ) - ( e.keyCode == 37 ? 1 : -1 ) );
- if( !target.length ) target = targets.eq( e.keyCode == 37 ? targets.length : 0 );
- loadImage( e.keyCode == 37 ? 'left' : 'right' );
- }
- });
- }
-
- $( document ).on( 'click', this.selector, function( e )
- {
- if( !isTargetValid( this ) ) return true;
- e.preventDefault();
- if( inProgress ) return false;
- inProgress = false;
- if( options.onStart !== false ) options.onStart();
- target = $( this );
- loadImage();
- });
-
- this.each( function()
- {
- if( !isTargetValid( this ) ) return true;
- targets = targets.add( $( this ) );
- });
-
- this.switchImageLightbox = function( index )
- {
- var tmpTarget = targets.eq( index );
- if( tmpTarget.length )
- {
- var currentIndex = targets.index( target );
- target = tmpTarget;
- loadImage( index < currentIndex ? 'left' : 'right' );
- }
- return this;
- };
-
- this.quitImageLightbox = function()
- {
- quitLightbox();
- return this;
- };
-
- return this;
- };
-})( jQuery, window, document );
diff --git a/dist/image-lightbox.min.js b/dist/image-lightbox.min.js
deleted file mode 100644
index 3fb0bbc..0000000
--- a/dist/image-lightbox.min.js
+++ /dev/null
@@ -1,5 +0,0 @@
-/*! image-lightbox - v0.1.0 - 2014-06-07
-* https://github.com/victorhaggqvist/image-lightbox
-* Copyright (c) 2014 Osvaldas Valutis (www.osvaldas.info); Licensed MIT
-* Contributors Victor Häggqvist (victorhaggqvist.com) */
-!function(a,b,c){"use strict";var d=function(){var a=c.body||c.documentElement,a=a.style;return""==a.WebkitTransition?"-webkit-":""==a.MozTransition?"-moz-":""==a.OTransition?"-o-":""==a.transition?"":!1},e=d()===!1?!1:!0,f=function(a,b,c){var e={},f=d();e[f+"transform"]="translateX("+b+")",e[f+"transition"]=f+"transform "+c+"s linear",a.css(e)},g="ontouchstart"in b,h=b.navigator.pointerEnabled||b.navigator.msPointerEnabled,i=function(a){if(g)return!0;if(!h||"undefined"==typeof a||"undefined"==typeof a.pointerType)return!1;if("undefined"!=typeof a.MSPOINTER_TYPE_MOUSE){if(a.MSPOINTER_TYPE_MOUSE!=a.pointerType)return!0}else if("mouse"!=a.pointerType)return!0;return!1};a.fn.imageLightbox=function(d){var d=a.extend({selector:'id="imagelightbox"',allowedTypes:"png|jpg|jpeg|gif",animationSpeed:250,preloadNext:!0,enableKeyboard:!0,quitOnEnd:!1,quitOnImgClick:!1,quitOnDocClick:!0,onStart:!1,onEnd:!1,onLoadStart:!1,onLoadEnd:!1},d),j=a([]),k=a(),l=a(),m=0,n=0,o=0,p=!1,q=function(b){return"a"==a(b).prop("tagName").toLowerCase()&&new RegExp(".("+d.allowedTypes+")$","i").test(a(b).attr("href"))},r=function(){if(!l.length)return!1;var c=.8*a(b).width(),d=.9*a(b).height(),e=new Image;e.src=l.attr("src"),e.onload=function(){if(m=e.width,n=e.height,m>c||n>d){var f=m/n>c/d?m/c:n/d;m/=f,n/=f}l.css({width:m+"px",height:n+"px",top:(a(b).height()-n)/2+"px",left:(a(b).width()-m)/2+"px"})}},s=function(b){if(p)return!1;if(b="undefined"==typeof b?!1:"left"==b?1:-1,l.length){if(b!==!1&&(j.length<2||d.quitOnEnd===!0&&(-1===b&&0==j.index(k)||1===b&&j.index(k)==j.length-1)))return u(),!1;var c={opacity:0};e?f(l,100*b-o+"px",d.animationSpeed/1e3):c.left=parseInt(l.css("left"))+100*b+"px",l.animate(c,d.animationSpeed,function(){t()}),o=0}p=!0,d.onLoadStart!==!1&&d.onLoadStart(),setTimeout(function(){l=a("<img "+d.selector+" />").attr("src",k.attr("href")).load(function(){l.appendTo("body"),r();var c={opacity:1};if(l.css("opacity",0),e)f(l,-100*b+"px",0),setTimeout(function(){f(l,"0px",d.animationSpeed/1e3)},50);else{var g=parseInt(l.css("left"));c.left=g+"px",l.css("left",g-100*b+"px")}if(l.animate(c,d.animationSpeed,function(){p=!1,d.onLoadEnd!==!1&&d.onLoadEnd()}),d.preloadNext){var h=j.eq(j.index(k)+1);h.length||(h=j.eq(0)),a("<img />").attr("src",h.attr("href")).load()}}).error(function(){d.onLoadEnd!==!1&&d.onLoadEnd()});var c=0,g=0,n=0;l.on(h?"pointerup MSPointerUp":"click",function(a){if(a.preventDefault(),d.quitOnImgClick)return u(),!1;if(i(a.originalEvent))return!0;var b=(a.pageX||a.originalEvent.pageX)-a.target.offsetLeft;k=j.eq(j.index(k)-(m/2>b?1:-1)),k.length||(k=j.eq(m/2>b?j.length:0)),s(m/2>b?"left":"right")}).on("touchstart pointerdown MSPointerDown",function(a){return!i(a.originalEvent)||d.quitOnImgClick?!0:(e&&(n=parseInt(l.css("left"))),void(c=a.originalEvent.pageX||a.originalEvent.touches[0].pageX))}).on("touchmove pointermove MSPointerMove",function(a){return!i(a.originalEvent)||d.quitOnImgClick?!0:(a.preventDefault(),g=a.originalEvent.pageX||a.originalEvent.touches[0].pageX,o=c-g,void(e?f(l,-o+"px",0):l.css("left",n-o+"px")))}).on("touchend touchcancel pointerup MSPointerUp",function(a){return!i(a.originalEvent)||d.quitOnImgClick?!0:void(Math.abs(o)>50?(k=j.eq(j.index(k)-(0>o?1:-1)),k.length||(k=j.eq(0>o?j.length:0)),s(o>0?"right":"left")):e?f(l,"0px",d.animationSpeed/1e3):l.animate({left:n+"px"},d.animationSpeed/2))})},d.animationSpeed+100)},t=function(){return l.length?(l.remove(),void(l=a())):!1},u=function(){return l.length?void l.animate({opacity:0},d.animationSpeed,function(){t(),p=!1,d.onEnd!==!1&&d.onEnd()}):!1};return a(b).on("resize",r),d.quitOnDocClick&&a(c).on(g?"touchend":"click",function(b){l.length&&!a(b.target).is(l)&&u()}),d.enableKeyboard&&a(c).on("keyup",function(a){return l.length?(a.preventDefault(),27==a.keyCode&&u(),void((37==a.keyCode||39==a.keyCode)&&(k=j.eq(j.index(k)-(37==a.keyCode?1:-1)),k.length||(k=j.eq(37==a.keyCode?j.length:0)),s(37==a.keyCode?"left":"right")))):!0}),a(c).on("click",this.selector,function(b){return q(this)?(b.preventDefault(),p?!1:(p=!1,d.onStart!==!1&&d.onStart(),k=a(this),void s())):!0}),this.each(function(){return q(this)?void(j=j.add(a(this))):!0}),this.switchImageLightbox=function(a){var b=j.eq(a);if(b.length){var c=j.index(k);k=b,s(c>a?"left":"right")}return this},this.quitImageLightbox=function(){return u(),this},this}}(jQuery,window,document); \ No newline at end of file
diff --git a/js/image-lightbox.js b/js/image-lightbox.js
deleted file mode 100644
index ad46eb1..0000000
--- a/js/image-lightbox.js
+++ /dev/null
@@ -1,307 +0,0 @@
-;( function( $, window, document, undefined )
-{
- 'use strict';
-
- var cssTransitionSupport = function()
- {
- var s = document.body || document.documentElement, s = s.style;
- if( s.WebkitTransition == '' ) return '-webkit-';
- if( s.MozTransition == '' ) return '-moz-';
- if( s.OTransition == '' ) return '-o-';
- if( s.transition == '' ) return '';
- return false;
- },
-
- isCssTransitionSupport = cssTransitionSupport() === false ? false : true,
-
- cssTransitionTranslateX = function( element, positionX, speed )
- {
- var options = {}, prefix = cssTransitionSupport();
- options[ prefix + 'transform' ] = 'translateX(' + positionX + ')';
- options[ prefix + 'transition' ] = prefix + 'transform ' + speed + 's linear';
- element.css( options );
- },
-
- hasTouch = ( 'ontouchstart' in window ),
- hasPointers = window.navigator.pointerEnabled || window.navigator.msPointerEnabled,
- wasTouched = function( event )
- {
- if( hasTouch )
- return true;
-
- if( !hasPointers || typeof event === 'undefined' || typeof event.pointerType === 'undefined' )
- return false;
-
- if( typeof event.MSPOINTER_TYPE_MOUSE !== 'undefined' )
- {
- if( event.MSPOINTER_TYPE_MOUSE != event.pointerType )
- return true;
- }
- else
- if( event.pointerType != 'mouse' )
- return true;
-
- return false;
- };
-
- $.fn.imageLightbox = function( options )
- {
- var options = $.extend(
- {
- selector: 'id="imagelightbox"',
- allowedTypes: 'png|jpg|jpeg|gif',
- animationSpeed: 250,
- preloadNext: true,
- enableKeyboard: true,
- quitOnEnd: false,
- quitOnImgClick: false,
- quitOnDocClick: true,
- onStart: false,
- onEnd: false,
- onLoadStart: false,
- onLoadEnd: false
- },
- options ),
-
- targets = $([]),
- target = $(),
- image = $(),
- imageWidth = 0,
- imageHeight = 0,
- swipeDiff = 0,
- inProgress = false,
-
- isTargetValid = function( element )
- {
- return $( element ).prop( 'tagName' ).toLowerCase() == 'a' && ( new RegExp( '\.(' + options.allowedTypes + ')$', 'i' ) ).test( $( element ).attr( 'href' ) );
- },
-
- setImage = function()
- {
- if( !image.length ) return false;
-
- var screenWidth = $( window ).width() * 0.8,
- screenHeight = $( window ).height() * 0.9,
- tmpImage = new Image();
-
- tmpImage.src = image.attr( 'src' );
- tmpImage.onload = function()
- {
- imageWidth = tmpImage.width;
- imageHeight = tmpImage.height;
-
- if( imageWidth > screenWidth || imageHeight > screenHeight )
- {
- var ratio = imageWidth / imageHeight > screenWidth / screenHeight ? imageWidth / screenWidth : imageHeight / screenHeight;
- imageWidth /= ratio;
- imageHeight /= ratio;
- }
-
- image.css(
- {
- 'width': imageWidth + 'px',
- 'height': imageHeight + 'px',
- 'top': ( $( window ).height() - imageHeight ) / 2 + 'px',
- 'left': ( $( window ).width() - imageWidth ) / 2 + 'px'
- });
- };
- },
-
- loadImage = function( direction )
- {
- if( inProgress ) return false;
-
- direction = typeof direction === 'undefined' ? false : direction == 'left' ? 1 : -1;
-
- if( image.length )
- {
- if( direction !== false && ( targets.length < 2 || ( options.quitOnEnd === true && ( ( direction === -1 && targets.index( target ) == 0 ) || ( direction === 1 && targets.index( target ) == targets.length - 1 ) ) ) ) )
- {
- quitLightbox();
- return false;
- }
- var params = { 'opacity': 0 };
- if( isCssTransitionSupport ) cssTransitionTranslateX( image, ( 100 * direction ) - swipeDiff + 'px', options.animationSpeed / 1000 );
- else params.left = parseInt( image.css( 'left' ) ) + 100 * direction + 'px';
- image.animate( params, options.animationSpeed, function(){ removeImage(); });
- swipeDiff = 0;
- }
-
- inProgress = true;
- if( options.onLoadStart !== false ) options.onLoadStart();
-
- setTimeout( function()
- {
- image = $( '<img ' + options.selector + ' />' )
- .attr( 'src', target.attr( 'href' ) )
- .load( function()
- {
- image.appendTo( 'body' );
- setImage();
-
- var params = { 'opacity': 1 };
-
- image.css( 'opacity', 0 );
- if( isCssTransitionSupport )
- {
- cssTransitionTranslateX( image, -100 * direction + 'px', 0 );
- setTimeout( function(){ cssTransitionTranslateX( image, 0 + 'px', options.animationSpeed / 1000 ) }, 50 );
- }
- else
- {
- var imagePosLeft = parseInt( image.css( 'left' ) );
- params.left = imagePosLeft + 'px';
- image.css( 'left', imagePosLeft - 100 * direction + 'px' );
- }
-
- image.animate( params, options.animationSpeed, function()
- {
- inProgress = false;
- if( options.onLoadEnd !== false ) options.onLoadEnd();
- });
- if( options.preloadNext )
- {
- var nextTarget = targets.eq( targets.index( target ) + 1 );
- if( !nextTarget.length ) nextTarget = targets.eq( 0 );
- $( '<img />' ).attr( 'src', nextTarget.attr( 'href' ) ).load();
- }
- })
- .error( function()
- {
- if( options.onLoadEnd !== false ) options.onLoadEnd();
- })
-
- var swipeStart = 0,
- swipeEnd = 0,
- imagePosLeft = 0;
-
- image.on( hasPointers ? 'pointerup MSPointerUp' : 'click', function( e )
- {
- e.preventDefault();
- if( options.quitOnImgClick )
- {
- quitLightbox();
- return false;
- }
- if( wasTouched( e.originalEvent ) ) return true;
- var posX = ( e.pageX || e.originalEvent.pageX ) - e.target.offsetLeft;
- target = targets.eq( targets.index( target ) - ( imageWidth / 2 > posX ? 1 : -1 ) );
- if( !target.length ) target = targets.eq( imageWidth / 2 > posX ? targets.length : 0 );
- loadImage( imageWidth / 2 > posX ? 'left' : 'right' );
- })
- .on( 'touchstart pointerdown MSPointerDown', function( e )
- {
- if( !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
- if( isCssTransitionSupport ) imagePosLeft = parseInt( image.css( 'left' ) );
- swipeStart = e.originalEvent.pageX || e.originalEvent.touches[ 0 ].pageX;
- })
- .on( 'touchmove pointermove MSPointerMove', function( e )
- {
- if( !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
- e.preventDefault();
- swipeEnd = e.originalEvent.pageX || e.originalEvent.touches[ 0 ].pageX;
- swipeDiff = swipeStart - swipeEnd;
- if( isCssTransitionSupport ) cssTransitionTranslateX( image, -swipeDiff + 'px', 0 );
- else image.css( 'left', imagePosLeft - swipeDiff + 'px' );
- })
- .on( 'touchend touchcancel pointerup MSPointerUp', function( e )
- {
- if( !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
- if( Math.abs( swipeDiff ) > 50 )
- {
- target = targets.eq( targets.index( target ) - ( swipeDiff < 0 ? 1 : -1 ) );
- if( !target.length ) target = targets.eq( swipeDiff < 0 ? targets.length : 0 );
- loadImage( swipeDiff > 0 ? 'right' : 'left' );
- }
- else
- {
- if( isCssTransitionSupport ) cssTransitionTranslateX( image, 0 + 'px', options.animationSpeed / 1000 );
- else image.animate({ 'left': imagePosLeft + 'px' }, options.animationSpeed / 2 );
- }
- });
-
- }, options.animationSpeed + 100 );
- },
-
- removeImage = function()
- {
- if( !image.length ) return false;
- image.remove();
- image = $();
- },
-
- quitLightbox = function()
- {
- if( !image.length ) return false;
- image.animate({ 'opacity': 0 }, options.animationSpeed, function()
- {
- removeImage();
- inProgress = false;
- if( options.onEnd !== false ) options.onEnd();
- });
- };
-
- $( window ).on( 'resize', setImage );
-
- if( options.quitOnDocClick )
- {
- $( document ).on( hasTouch ? 'touchend' : 'click', function( e )
- {
- if( image.length && !$( e.target ).is( image ) ) quitLightbox();
- })
- }
-
- if( options.enableKeyboard )
- {
- $( document ).on( 'keyup', function( e )
- {
- if( !image.length ) return true;
- e.preventDefault();
- if( e.keyCode == 27 ) quitLightbox();
- if( e.keyCode == 37 || e.keyCode == 39 )
- {
- target = targets.eq( targets.index( target ) - ( e.keyCode == 37 ? 1 : -1 ) );
- if( !target.length ) target = targets.eq( e.keyCode == 37 ? targets.length : 0 );
- loadImage( e.keyCode == 37 ? 'left' : 'right' );
- }
- });
- }
-
- $( document ).on( 'click', this.selector, function( e )
- {
- if( !isTargetValid( this ) ) return true;
- e.preventDefault();
- if( inProgress ) return false;
- inProgress = false;
- if( options.onStart !== false ) options.onStart();
- target = $( this );
- loadImage();
- });
-
- this.each( function()
- {
- if( !isTargetValid( this ) ) return true;
- targets = targets.add( $( this ) );
- });
-
- this.switchImageLightbox = function( index )
- {
- var tmpTarget = targets.eq( index );
- if( tmpTarget.length )
- {
- var currentIndex = targets.index( target );
- target = tmpTarget;
- loadImage( index < currentIndex ? 'left' : 'right' );
- }
- return this;
- };
-
- this.quitImageLightbox = function()
- {
- quitLightbox();
- return this;
- };
-
- return this;
- };
-})( jQuery, window, document );