PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 3.37
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v3.37
3.37 3.36 3.35 3.34 3.33 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 2.28 2.29 2.30 2.31 2.32 2.33 2.34 2.40 2.41 2.42 2.43 All 142 releases
photonic / include / ext / imagelightbox / imagelightbox.js

imagelightbox.js in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 3.37, at include/ext/imagelightbox/imagelightbox.js

391 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1
2 /*
3 By Osvaldas Valutis, www.osvaldas.info
4 Available for use under the MIT License
5 */
6
7 ;( function( $, window, document, undefined )
8 {
9 'use strict';
10
11 var cssTransitionSupport = function()
12 {
13 var s = document.body || document.documentElement, s = s.style;
14 if( s.WebkitTransition == '' ) return '-webkit-';
15 if( s.MozTransition == '' ) return '-moz-';
16 if( s.OTransition == '' ) return '-o-';
17 if( s.transition == '' ) return '';
18 return false;
19 },
20
21 isCssTransitionSupport = cssTransitionSupport() === false ? false : true,
22
23 cssTransitionTranslateX = function( element, positionX, speed )
24 {
25 var options = {}, prefix = cssTransitionSupport();
26 options[ prefix + 'transform' ] = 'translateX(' + positionX + ')';
27 options[ prefix + 'transition' ] = prefix + 'transform ' + speed + 's linear';
28 element.css( options );
29 },
30
31 hasTouch = ( 'ontouchstart' in window ),
32 hasPointers = window.navigator.pointerEnabled || window.navigator.msPointerEnabled,
33 wasTouched = function( event )
34 {
35 if( hasTouch )
36 return true;
37
38 if( !hasPointers || typeof event === 'undefined' || typeof event.pointerType === 'undefined' )
39 return false;
40
41 if( typeof event.MSPOINTER_TYPE_MOUSE !== 'undefined' )
42 {
43 if( event.MSPOINTER_TYPE_MOUSE != event.pointerType )
44 return true;
45 }
46 else
47 if( event.pointerType != 'mouse' )
48 return true;
49
50 return false;
51 };
52
53 $.fn.imageLightbox = function(selector, opts )
54 {
55 var options = $.extend(
56 {
57 selector: 'id="imagelightbox"',
58 animationSpeed: 250,
59 preloadNext: true,
60 enableKeyboard: true,
61 quitOnEnd: false,
62 quitOnImgClick: false,
63 quitOnDocClick: true,
64 onStart: false,
65 onEnd: false,
66 onLoadStart: false,
67 onLoadEnd: false
68 },
69 opts ),
70
71 targets = $([]),
72 target = $(),
73 image = $(),
74 imageWidth = 0,
75 imageHeight = 0,
76 swipeDiff = 0,
77 inProgress = false,
78
79 getTargetIndex = function() {
80 var match = -1;
81 $(targets).each(function (idx, array_target) {
82 if (array_target.href == target[0].href) {
83 match = idx;
84 return false;
85 }
86 });
87 return match;
88 },
89
90 setImage = function()
91 {
92 if( !image.length ) return true;
93
94 var screenWidth = $( window ).width() * 0.8,
95 screenHeight = $( window ).height() * 0.9,
96 tmpImage = new Image();
97
98 tmpImage.src = image.attr( 'src' );
99 tmpImage.onload = function()
100 {
101 imageWidth = tmpImage.width;
102 imageHeight = tmpImage.height;
103
104 if( imageWidth > screenWidth || imageHeight > screenHeight )
105 {
106 var ratio = imageWidth / imageHeight > screenWidth / screenHeight ? imageWidth / screenWidth : imageHeight / screenHeight;
107 imageWidth /= ratio;
108 imageHeight /= ratio;
109 }
110
111 image.css(
112 {
113 'width': imageWidth + 'px',
114 'height': imageHeight + 'px',
115 'top': ( $( window ).height() - imageHeight ) / 2 + 'px',
116 'left': ( $( window ).width() - imageWidth ) / 2 + 'px'
117 });
118 };
119 },
120
121 loadImage = function( direction )
122 {
123 if( inProgress ) return false;
124
125 direction = typeof direction === 'undefined' ? false : direction == 'left' ? 1 : -1;
126
127 if( image.length )
128 {
129 if( direction !== false && ( targets.length < 2 || ( options.quitOnEnd === true && ( ( direction === -1 && targets.index( target ) == 0 ) || ( direction === 1 && targets.index( target ) == targets.length - 1 ) ) ) ) )
130 {
131 quitLightbox();
132 return false;
133 }
134 var params = { 'opacity': 0 };
135 if( isCssTransitionSupport ) cssTransitionTranslateX( image, ( 100 * direction ) - swipeDiff + 'px', options.animationSpeed / 1000 );
136 else params.left = parseInt( image.css( 'left' ) ) + 100 * direction + 'px';
137 image.animate( params, options.animationSpeed, function(){ removeImage(); });
138 swipeDiff = 0;
139 }
140
141 inProgress = true;
142 if( options.onLoadStart !== false ) options.onLoadStart();
143
144 setTimeout( function()
145 {
146 image = $( '<img ' + options.selector + ' />' )
147 .attr( 'src', target.attr( 'href' ) )
148 .on('load', function()
149 {
150 image.appendTo( 'body' );
151 setImage();
152
153 var params = { 'opacity': 1 };
154
155 image.css( 'opacity', 0 );
156 if( isCssTransitionSupport )
157 {
158 cssTransitionTranslateX( image, -100 * direction + 'px', 0 );
159 setTimeout( function(){ cssTransitionTranslateX( image, 0 + 'px', options.animationSpeed / 1000 ) }, 50 );
160 }
161 else
162 {
163 var imagePosLeft = parseInt( image.css( 'left' ) );
164 params.left = imagePosLeft + 'px';
165 image.css( 'left', imagePosLeft - 100 * direction + 'px' );
166 }
167
168 image.animate( params, options.animationSpeed, function()
169 {
170 inProgress = false;
171 if( options.onLoadEnd !== false ) options.onLoadEnd();
172 });
173 if( options.preloadNext )
174 {
175 var nextTarget = targets.eq( getTargetIndex() + 1 );
176 if( !nextTarget.length ) nextTarget = targets.eq( 0 );
177 $( '<img />' ).attr( 'src', nextTarget.attr( 'href' ) ).trigger('load');
178 }
179 })
180 .on('error', function()
181 {
182 if( options.onLoadEnd !== false ) options.onLoadEnd();
183 })
184
185 var swipeStart = 0,
186 swipeEnd = 0,
187 imagePosLeft = 0;
188
189 image.on( hasPointers ? 'pointerup MSPointerUp' : 'click', function( e )
190 {
191 e.preventDefault();
192 if( options.quitOnImgClick )
193 {
194 quitLightbox();
195 return false;
196 }
197 if( wasTouched( e.originalEvent ) ) return true;
198 var posX = ( e.pageX || e.originalEvent.pageX ) - e.target.offsetLeft;
199 target = targets.eq( getTargetIndex() - ( imageWidth / 2 > posX ? 1 : -1 ) );
200 if( !target.length ) target = targets.eq( imageWidth / 2 > posX ? targets.length : 0 );
201 loadImage( imageWidth / 2 > posX ? 'left' : 'right' );
202 })
203 .on( 'touchstart pointerdown MSPointerDown', function( e )
204 {
205 if( !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
206 if( isCssTransitionSupport ) imagePosLeft = parseInt( image.css( 'left' ) );
207 swipeStart = e.originalEvent.pageX || e.originalEvent.touches[ 0 ].pageX;
208 })
209 .on( 'touchmove pointermove MSPointerMove', function( e )
210 {
211 if( (!hasPointers && e.type == 'pointermove') || !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
212 e.preventDefault();
213 swipeEnd = e.originalEvent.pageX || e.originalEvent.touches[ 0 ].pageX;
214 swipeDiff = swipeStart - swipeEnd;
215 if( isCssTransitionSupport ) cssTransitionTranslateX( image, -swipeDiff + 'px', 0 );
216 else image.css( 'left', imagePosLeft - swipeDiff + 'px' );
217 })
218 .on( 'touchend touchcancel pointerup pointercancel MSPointerUp MSPointerCancel', function( e )
219 {
220 if( !wasTouched( e.originalEvent ) || options.quitOnImgClick ) return true;
221 if( Math.abs( swipeDiff ) > 50 )
222 {
223 target = targets.eq( getTargetIndex() - ( swipeDiff < 0 ? 1 : -1 ) );
224 if( !target.length ) target = targets.eq( swipeDiff < 0 ? targets.length : 0 );
225 loadImage( swipeDiff > 0 ? 'right' : 'left' );
226 }
227 else
228 {
229 if( isCssTransitionSupport ) cssTransitionTranslateX( image, 0 + 'px', options.animationSpeed / 1000 );
230 else image.animate({ 'left': imagePosLeft + 'px' }, options.animationSpeed / 2 );
231 }
232 });
233
234 }, options.animationSpeed + 100 );
235 },
236
237 removeImage = function()
238 {
239 if( !image.length ) return false;
240 image.remove();
241 image = $();
242 },
243
244 quitLightbox = function()
245 {
246 if( !image.length ) return false;
247 image.animate({ 'opacity': 0 }, options.animationSpeed, function()
248 {
249 removeImage();
250 inProgress = false;
251 if( options.onEnd !== false ) options.onEnd();
252 });
253 },
254
255 addTargets = function( newTargets )
256 {
257 $(newTargets).each( function()
258 {
259 targets = targets.add( $( this ) );
260 });
261 /*
262
263 newTargets.on( 'click.imageLightbox', function( e )
264 {
265 e.preventDefault();
266 if( inProgress ) return false;
267 inProgress = false;
268 if( options.onStart !== false ) options.onStart();
269 target = $( this );
270 loadImage();
271 });
272 */
273 };
274
275 $( window ).on( 'resize', setImage );
276
277 if( options.quitOnDocClick )
278 {
279 $( document ).on( hasTouch ? 'touchend' : 'click', function( e )
280 {
281 if( image.length && !$( e.target ).is( image ) ) quitLightbox();
282 })
283 }
284
285 if( options.enableKeyboard )
286 {
287 $( document ).on( 'keyup', function( e )
288 {
289 if( !image.length ) return true;
290 e.preventDefault();
291 if( e.keyCode == 27 ) quitLightbox();
292 if( e.keyCode == 37 || e.keyCode == 39 )
293 {
294 target = targets.eq( getTargetIndex() - ( e.keyCode == 37 ? 1 : -1 ) );
295 if( !target.length ) target = targets.eq( e.keyCode == 37 ? targets.length : 0 );
296 loadImage( e.keyCode == 37 ? 'left' : 'right' );
297 }
298 });
299 }
300
301 $( document ).on( 'click', selector, function( e )
302 {
303 e.preventDefault();
304 if( inProgress ) return false;
305 inProgress = false;
306 if( options.onStart !== false ) options.onStart();
307 target = $( this );
308 loadImage();
309 });
310
311 this.each( function()
312 {
313 targets = targets.add( $( this ) );
314 });
315
316 this.switchImageLightbox = function( index )
317 {
318 var tmpTarget = targets.eq( index );
319 if( tmpTarget.length )
320 {
321 var currentIndex = targets.index( target );
322 target = tmpTarget;
323 loadImage( index < currentIndex ? 'left' : 'right' );
324 }
325 return this;
326 };
327
328 this.quitImageLightbox = function()
329 {
330 quitLightbox();
331 return this;
332 };
333
334 this.addToImageLightbox = function( newTargets ) {
335 addTargets( newTargets );
336 };
337
338 return this;
339 };
340 })( jQuery, window, document );
341
342 jQuery(document).ready(function($) {
343 window.imageLightboxCaptionOn = function imageLightboxCaptionOn() {
344 var title = $('a[href="' + $('#imagelightbox').attr('src') + '"]').data('title');
345 if (title != undefined && title.length > 0) {
346 $('<div id="imagelightbox-caption">' + title + '</div>').appendTo( 'body' );
347 }
348 };
349 window.imageLightboxCaptionOff = function imageLightboxCaptionOff() { $( '#imagelightbox-caption' ).remove(); };
350 window.imageLightboxArrowsOn = function imageLightboxArrowsOn(instance, selector ) {
351 var $arrows = $( '<button type="button" class="imagelightbox-arrow imagelightbox-arrow-left"></button><button type="button" class="imagelightbox-arrow imagelightbox-arrow-right"></button>' );
352 $arrows.appendTo( 'body' );
353 $arrows.on( 'click touchend', function( e ) {
354 e.preventDefault();
355
356 var $this = $( this ),
357 $target = $( selector + '[href="' + $( '#imagelightbox' ).attr( 'src' ) + '"]' ),
358 index = $target.index( selector );
359
360 if( $this.hasClass( 'imagelightbox-arrow-left' ) ) {
361 index = index - 1;
362 if( !$( selector ).eq( index ).length )
363 index = $( selector ).length;
364 }
365 else {
366 index = index + 1;
367 if( !$( selector ).eq( index ).length )
368 index = 0;
369 }
370
371 instance.switchImageLightbox( index );
372 return false;
373 });
374 };
375 window.imageLightboxArrowsOff = function imageLightboxArrowsOff() {
376 $( '.imagelightbox-arrow' ).remove();
377 };
378
379 window.imageLightboxCloseButtonOn = function imageLightboxCloseButtonOn(instance) {
380 $( '<button type="button" id="imagelightbox-close" title="Close"></button>' ).appendTo( 'body' ).on( 'click touchend', function(){ $( this ).remove(); instance.quitImageLightbox(); return false; });
381 };
382
383 window.imageLightboxCloseButtonOff = function imageLightboxCloseButtonOff() {
384 $( '#imagelightbox-close' ).remove();
385 };
386
387 window.imageLightboxLoadingOn = function() {
388 $('<div id="imagelightbox-loading"><div></div></div>').appendTo('body');
389 }
390 });
391