|
1 | 1 | /* |
2 | | - * Nivo Lightbox v1.0 |
| 2 | + * Nivo Lightbox v1.1 |
3 | 3 | * http://dev7studios.com/nivo-lightbox |
4 | 4 | * |
5 | 5 | * Copyright 2013, Dev7studios |
|
14 | 14 | effect: 'fade', |
15 | 15 | theme: 'default', |
16 | 16 | keyboardNav: true, |
| 17 | + clickOverlayToClose: true, |
17 | 18 | onInit: function(){}, |
18 | 19 | beforeShowLightbox: function(){}, |
19 | 20 | afterShowLightbox: function(lightbox){}, |
|
37 | 38 | } |
38 | 39 |
|
39 | 40 | NivoLightbox.prototype = { |
40 | | - |
| 41 | + |
41 | 42 | init: function(){ |
42 | 43 | var $this = this; |
43 | | - |
| 44 | + |
| 45 | + // Need this so we don't use CSS transitions in mobile |
| 46 | + if(!$('html').hasClass('nivo-lightbox-notouch')) $('html').addClass('nivo-lightbox-notouch'); |
| 47 | + if('ontouchstart' in document) $('html').removeClass('nivo-lightbox-notouch'); |
| 48 | + |
| 49 | + // Setup the click |
44 | 50 | this.$el.on('click', function(e){ |
45 | 51 | e.preventDefault(); |
46 | 52 | $this.showLightbox(); |
47 | 53 | }); |
48 | | - |
| 54 | + |
49 | 55 | // keyboardNav |
50 | 56 | if(this.options.keyboardNav){ |
51 | 57 | $('body').off('keyup').on('keyup', function(e){ |
|
66 | 72 | showLightbox: function(){ |
67 | 73 | var $this = this; |
68 | 74 | this.options.beforeShowLightbox.call(this); |
69 | | - |
| 75 | + |
70 | 76 | var lightbox = this.constructLightbox(); |
71 | 77 | if(!lightbox) return; |
72 | 78 | var content = lightbox.find('.nivo-lightbox-content'); |
|
76 | 82 |
|
77 | 83 | // Add content |
78 | 84 | this.processContent(content, currentLink); |
79 | | - |
| 85 | + |
80 | 86 | // Nav |
81 | 87 | if(this.$el.attr('data-lightbox-gallery')){ |
82 | 88 | var $this = this, |
83 | 89 | galleryItems = $('[data-lightbox-gallery="'+ this.$el.attr('data-lightbox-gallery') +'"]'); |
84 | | - |
| 90 | + |
85 | 91 | $('.nivo-lightbox-nav').show(); |
86 | 92 |
|
87 | 93 | // Prev |
|
93 | 99 | $this.processContent(content, currentLink); |
94 | 100 | $this.options.onPrev.call(this, [ currentLink ]); |
95 | 101 | }); |
96 | | - |
| 102 | + |
97 | 103 | // Next |
98 | 104 | $('.nivo-lightbox-next').off('click').on('click', function(e){ |
99 | 105 | e.preventDefault(); |
|
104 | 110 | $this.options.onNext.call(this, [ currentLink ]); |
105 | 111 | }); |
106 | 112 | } |
107 | | - |
| 113 | + |
108 | 114 | setTimeout(function(){ |
109 | 115 | lightbox.addClass('nivo-lightbox-open'); |
110 | 116 | $this.options.afterShowLightbox.call(this, [ lightbox ]); |
111 | 117 | }, 1); // For CSS transitions |
112 | 118 | }, |
113 | | - |
| 119 | + |
114 | 120 | processContent: function(content, link){ |
115 | 121 | var $this = this; |
116 | 122 | var href = link.attr('href'); |
117 | 123 | content.html('').addClass('nivo-lightbox-loading'); |
118 | | - |
| 124 | + |
119 | 125 | // Is HiDPI? |
120 | 126 | if(this.isHidpi() && link.attr('data-lightbox-hidpi')){ |
121 | 127 | href = link.attr('data-lightbox-hidpi'); |
122 | 128 | } |
123 | | - |
| 129 | + |
124 | 130 | // Image |
125 | | - if(href.match(/\.(jpeg|jpg|gif|png)$/) != null){ |
| 131 | + if(href.match(/\.(jpeg|jpg|gif|png)$/i) != null){ |
126 | 132 | var img = $('<img>', { src: href }); |
127 | 133 | img.one('load', function() { |
128 | 134 | var wrap = $('<div class="nivo-lightbox-image" />'); |
129 | 135 | wrap.append(img); |
130 | 136 | content.html(wrap).removeClass('nivo-lightbox-loading'); |
131 | | - |
| 137 | + |
132 | 138 | // Vertically center images |
133 | 139 | wrap.css({ |
134 | 140 | 'line-height': $('.nivo-lightbox-content').height() +'px', |
|
143 | 149 | }).each(function() { |
144 | 150 | if(this.complete) $(this).load(); |
145 | 151 | }); |
146 | | - |
| 152 | + |
147 | 153 | img.error(function() { |
148 | 154 | var wrap = $('<div class="nivo-lightbox-error"><p>'+ $this.options.errorMessage +'</p></div>'); |
149 | 155 | content.html(wrap).removeClass('nivo-lightbox-loading'); |
150 | 156 | }); |
151 | 157 | } |
152 | 158 | // Video (Youtube/Vimeo) |
153 | | - else if(video = href.match(/(youtube|youtu|vimeo)\.(com|be)\/(watch\?v=(\w+)|(\w+))/)){ |
| 159 | + else if(video = href.match(/(youtube|youtu|vimeo)\.(com|be)\/(watch\?v=([\w-]+)|([\w-]+))/)){ |
154 | 160 | var src = '', |
155 | 161 | classTerm = 'nivo-lightbox-video'; |
156 | | - |
| 162 | + |
157 | 163 | if(video[1] == 'youtube'){ |
158 | 164 | src = 'http://www.youtube.com/v/'+ video[4]; |
159 | 165 | classTerm = 'nivo-lightbox-youtube'; |
|
166 | 172 | src = 'http://player.vimeo.com/video/'+ video[3]; |
167 | 173 | classTerm = 'nivo-lightbox-vimeo'; |
168 | 174 | } |
169 | | - |
| 175 | + |
170 | 176 | if(src){ |
171 | | - var iframe = $('<iframe>', { |
172 | | - src: src, |
| 177 | + var iframe = $('<iframe>', { |
| 178 | + src: src, |
173 | 179 | 'class': classTerm, |
174 | 180 | frameborder: 0, |
175 | 181 | vspace: 0, |
|
190 | 196 | var wrap = $('<div class="nivo-lightbox-ajax" />'); |
191 | 197 | wrap.append(data); |
192 | 198 | content.html(wrap).removeClass('nivo-lightbox-loading'); |
193 | | - |
| 199 | + |
194 | 200 | // Vertically center html |
195 | 201 | if(wrap.outerHeight() < content.height()){ |
196 | 202 | wrap.css({ |
|
221 | 227 | var wrap = $('<div class="nivo-lightbox-inline" />'); |
222 | 228 | wrap.append($(href).clone().show()); |
223 | 229 | content.html(wrap).removeClass('nivo-lightbox-loading'); |
224 | | - |
| 230 | + |
225 | 231 | // Vertically center html |
226 | 232 | if(wrap.outerHeight() < content.height()){ |
227 | 233 | wrap.css({ |
|
246 | 252 | } |
247 | 253 | // iFrame (default) |
248 | 254 | else { |
249 | | - var iframe = $('<iframe>', { |
250 | | - src: href, |
| 255 | + var iframe = $('<iframe>', { |
| 256 | + src: href, |
251 | 257 | 'class': 'nivo-lightbox-item', |
252 | 258 | frameborder: 0, |
253 | 259 | vspace: 0, |
|
257 | 263 | content.html(iframe); |
258 | 264 | iframe.load(function(){ content.removeClass('nivo-lightbox-loading'); }); |
259 | 265 | } |
260 | | - |
| 266 | + |
261 | 267 | // Set the title |
262 | 268 | if(link.attr('title')){ |
263 | 269 | var titleWrap = $('<span>', { 'class': 'nivo-lightbox-title' }); |
|
267 | 273 | $('.nivo-lightbox-title-wrap').html(''); |
268 | 274 | } |
269 | 275 | }, |
270 | | - |
| 276 | + |
271 | 277 | constructLightbox: function(){ |
272 | 278 | if($('.nivo-lightbox-overlay').length) return $('.nivo-lightbox-overlay'); |
273 | | - |
| 279 | + |
274 | 280 | var overlay = $('<div>', { 'class': 'nivo-lightbox-overlay nivo-lightbox-theme-'+ this.options.theme +' nivo-lightbox-effect-'+ this.options.effect }); |
275 | 281 | var wrap = $('<div>', { 'class': 'nivo-lightbox-wrap' }); |
276 | 282 | var content = $('<div>', { 'class': 'nivo-lightbox-content' }); |
277 | 283 | var nav = $('<a href="#" class="nivo-lightbox-nav nivo-lightbox-prev">Previous</a><a href="#" class="nivo-lightbox-nav nivo-lightbox-next">Next</a>'); |
278 | 284 | var close = $('<a href="#" class="nivo-lightbox-close" title="Close">Close</a>'); |
279 | 285 | var title = $('<div>', { 'class': 'nivo-lightbox-title-wrap' }); |
280 | | - |
| 286 | + |
281 | 287 | var isMSIE = /*@cc_on!@*/0; |
282 | 288 | if(isMSIE) overlay.addClass('nivo-lightbox-ie'); |
283 | | - |
| 289 | + |
284 | 290 | wrap.append(content); |
285 | 291 | wrap.append(title); |
286 | 292 | overlay.append(wrap); |
287 | 293 | overlay.append(nav); |
288 | 294 | overlay.append(close); |
289 | 295 | $('body').append(overlay); |
290 | | - |
| 296 | + |
291 | 297 | var $this = this; |
292 | | - overlay.on('click', function(e){ |
293 | | - if(e.target === this || |
294 | | - $(e.target).hasClass('nivo-lightbox-content') || |
295 | | - $(e.target).hasClass('nivo-lightbox-image')) $this.destructLightbox(); |
296 | | - }); |
| 298 | + if($this.options.clickOverlayToClose){ |
| 299 | + overlay.on('click', function(e){ |
| 300 | + if(e.target === this || |
| 301 | + $(e.target).hasClass('nivo-lightbox-content') || |
| 302 | + $(e.target).hasClass('nivo-lightbox-image')) $this.destructLightbox(); |
| 303 | + }); |
| 304 | + } |
| 305 | + |
297 | 306 | close.on('click', function(e){ |
298 | 307 | e.preventDefault(); |
299 | 308 | $this.destructLightbox(); |
300 | 309 | }); |
301 | | - |
| 310 | + |
302 | 311 | return overlay; |
303 | 312 | }, |
304 | | - |
| 313 | + |
305 | 314 | destructLightbox: function(){ |
306 | 315 | var $this = this; |
307 | 316 | this.options.beforeHideLightbox.call(this); |
308 | | - |
| 317 | + |
309 | 318 | $('.nivo-lightbox-overlay').removeClass('nivo-lightbox-open'); |
310 | 319 | $('.nivo-lightbox-nav').hide(); |
311 | 320 | $('body').removeClass('nivo-lightbox-body-effect-'+ $this.options.effect); |
312 | | - |
| 321 | + |
313 | 322 | // For IE |
314 | 323 | var isMSIE = /*@cc_on!@*/0; |
315 | 324 | if(isMSIE){ |
316 | 325 | $('.nivo-lightbox-overlay iframe').attr("src", " "); |
317 | 326 | $('.nivo-lightbox-overlay iframe').remove(); |
318 | 327 | } |
319 | | - |
| 328 | + |
320 | 329 | // Remove click handlers |
321 | 330 | $('.nivo-lightbox-prev').off('click'); |
322 | 331 | $('.nivo-lightbox-next').off('click'); |
323 | | - |
| 332 | + |
| 333 | + // Empty content (for videos) |
| 334 | + $('.nivo-lightbox-content').empty(); |
| 335 | + |
324 | 336 | this.options.afterHideLightbox.call(this); |
325 | 337 | }, |
326 | | - |
| 338 | + |
327 | 339 | isHidpi: function(){ |
328 | 340 | var mediaQuery = "(-webkit-min-device-pixel-ratio: 1.5),\ |
329 | 341 | (min--moz-device-pixel-ratio: 1.5),\ |
|
333 | 345 | if(window.matchMedia && window.matchMedia(mediaQuery).matches) return true; |
334 | 346 | return false; |
335 | 347 | } |
336 | | - |
| 348 | + |
337 | 349 | }; |
338 | 350 |
|
339 | 351 | $.fn[pluginName] = function(options){ |
|
0 commit comments