-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.ui.rlightbox.js
2151 lines (1840 loc) · 66.1 KB
/
jquery.ui.rlightbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* jQuery UI rlightbox2
*
* Copyright 2011 Wojciech ‘rrh’ Ryrych
* licensed under the MIT license
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
*/
(function( $, undefined ) {
$.widget( "ui.rlightbox", {
options: {
animationSpeed: "fast",
setPrefix: "lb",
showMap: true,
counterDelimiter: " / ",
videoWidth: 640,
videoHeight: 385,
errorMessage: "Oh dear! Something went wrong! If the problem still appears let the page’s admin know. Would you like to try again or reject the content?",
againButtonLabel: "Try again",
rejectButtonLabel: "Reject this content",
overwriteTitle: false,
keys: {
next: [78, 39],
previous: [80, 37],
close: [67, 27],
panorama: [90, null]
},
loop: false
},
_create: function() {
var _setElement,
global = $.ui.rlightbox.global;
// init the lightbox – do in only once
global.getLightbox();
// which type content belongs to: youtube, vimeo, flash, image, etc.
// what is its url, title (for images), etc…
_setElement = global.extractAnchor( this );
// add type, url, jQuery element and title of content to a set if content is supported by the lightbox
// otherwise fall silently
if ( _setElement.type !== undefined ) {
global.addToSet( _setElement );
// open the lightbox when clicking
this.element.on( "click", {setElement: _setElement}, function(event) {
global.open( event.data.setElement );
event.preventDefault();
});
}
},
_setOption: function( key, value ) {
}
});
// In almost every jQuery UI plugin, in an element the plugin is initialised on
// its initial DOM structure it transformed into the plugin's one. Therefore
// ‘this’ always refers to the plugin instance.
// rlightbox takes another approach: there are many elements (anchors) the plugin
// is installed on, but there is only one UI – one DOM structure.
// If we put all these methods below in the widget factory's scope, (the same as
// options and _create) and created only one UI, the UI would have ‘this’ scope
// of the first matched element – the first element in the DOM that rlightbox
// is initialised on. Since different instances can have different
// set of options, ‘this.options’ would always refer to the first instance.
// Since in rlightbox we don’t operate on elements directly but only get their
// urls, all methods are placed in the extended scope of the plugin:
// ‘$.ui.rlightbox.global’. An instance ‘this’ scope is merely used when we
// use options.
$.extend($.ui.rlightbox, {
global: {
addToSet: function( setElement ) {
// set structure is following:
// sets: {
// setName: [
// {
// url: "http://www.youtube.com?v=u408408598,
// type: "youtube"
// },
// {…}
// ],
// setName2: […]
//
var _setName = setElement.setName,
_sets = this.sets,
_options = setElement.options,
_setPrefix = _options.setPrefix,
_class = "." + _setPrefix + "_" + _setName,
_setElementIndex = $( _class ).index( setElement.$anchor );
if ( !_sets[_setName] ) {
// first time - such set has not been created before
_sets[_setName] = [];
_sets[_setName].push( setElement );
} else {
// set exists already - just add element to it
// added element respects changes in DOM
_sets[_setName].splice( _setElementIndex, 0 , setElement );
}
},
checkButtonsState: function() {
var data = this.data,
$lb = this.$lightbox,
_currentSet = data.currentSet,
_currentSetName = _currentSet.name,
_totalElements = _currentSet.totalElements,
_currentIndex = _currentSet.currentIndex,
_isLoop = _currentSet.currentElement.options.loop;
// if lightbox is opened and there is only one element
// single element or one element in named set
if ( _currentSetName === "single" || _totalElements === 1 ) {
this.setButtonState( "disabled" );
} else if ( _currentIndex === 1 && _isLoop === false ) {
// in case of 1st element when loop is disabled
this.setButtonState( "disabled", $lb.prev );
// when there are only two elements in a set
this.setButtonState( "default", $lb.next );
} else if ( _currentIndex === _totalElements && _isLoop === false ) {
// in case of last element
this.setButtonState( "disabled", $lb.next );
// when there are only two elements in a set
this.setButtonState( "default", $lb.prev );
} else {
// between first and last elements or when the loop is enabled
this.setButtonState( "default" );
}
},
closeLightbox: function() {
var data = this.data,
$lb = this.$lightbox,
_currentSet = data.currentSet;
if ( data.ready ) {
$lb.overlay
.add( $lb.root )
.add ( $lb.header )
.hide();
$( "body" ).css( "overflow", "visible" );
// remove content and restore its initial size
$lb.content
.empty()
.width( 20 )
.height( 20 );
// hide arrow cue
this.hideArrow();
// reset control buttons states to default
this.setButtonState( "default" );
// reset panorama
this.panoramaHideIcon();
// reset the counter
_currentSet.currentIndex = null;
_currentSet.totalElements = null;
// remove old title
$lb.title.empty();
// hide the map
this.panoramaHideMap();
// lightbox is not ready again
data.ready = false;
}
},
createStructure: function() {
var data= this.data;
// append the structure
$( data.htmlLightbox )
.appendTo( "body" )
.after( data.htmlMap )
.after( data.htmlOverlay );
},
destroy: function() {
// unwrap $currentElement from jQuery wrapped object and
// prevents it from being acted upon, unbinding event handlers
var data = this.data,
$rlbInstance = data.currentSet.currentElement.$anchor;
// code taken from jqury.ui.widget.js – it is the default behaviour
// from the widget factory but we can’t call it because it acts upon
// this.element – we have to act on a arbitrary one
$rlbInstance
.unbind( "." + "rlightbox" )
.unbind( "click" )
.removeData( "rlightbox" );
},
dequeue: function( object, name, data ) {
var _queue = object[name],
_fn = _queue.shift(),
self = this;
if ( _fn ) {
_fn.call(this, function( d ) {
self.dequeue( object, name, d );
}, data);
}
},
extractAnchor: function( jQElement ) {
// _extractAnchor gets information from an anchor element (DOM A element)
// @url is used for loading content such as images, youtube videos, etc
// @type is needed to choose suitable loading method in _queueLoadContent
// @title is used to dispay title of an image or flash content (.flv) –
// since vimeo content is got via oembed, title is got later after loading the content
// @setName is a name of a set, an element belongs to; it is used for the first time in open method
// @options is a handy shortcut to options of an element rlightbox is initiated on
// @anchor is a direct access to an anchor rlightbox is initiated on
var _result = {type: undefined},
$anchor = jQElement.element,
_url = $anchor.attr( "href" ),
_setName = this.getSetName( jQElement );
_service = {
youtube: {
urls: [/(http:\/\/www\.youtube\.com\/watch\?v=([\w-_]+))&?/],
type: "youtube"
},
image: {
urls: [/.jpg$|.jpeg$|.png$|.gif$/i],
type: "image"
},
vimeo: {
urls: [/(http:\/\/vimeo\.com\/groups\/\w+\/videos\/(\w+))&?/, /(http:\/\/vimeo\.com\/(\w+))&?/],
type: "vimeo"
},
flash: {
urls: [/.swf/i],
type: "flash"
}
};
$.each(_service,
function( name, content ) {
// outer loop: _service.youtube, _service.image, etc.
var _found = false;
$.each(content.urls,
function( index, regExp ) {
// inner loop: urls array
var _res = regExp.exec( _url );
if ( _res !== null ) {
if ( content.type === "image" || content.type === "flash" ) {
// image and flash urls are not normalised; in case of flash content
// there may be &with and &height parameters
_result = {
url: _url,
type: content.type,
title: $anchor.attr( "title" ),
setName: _setName,
options: jQElement.options,
$anchor: $anchor
};
} else if ( content.type === "youtube" || content.type === "vimeo" ) {
// for Youtube, Vimeo we return a normalised url
// without additional parameters
_result = {
url: _res[1],
videoId: _res[2],
type: content.type,
setName: _setName,
options: jQElement.options,
$anchor: $anchor
};
if ( jQElement.options.overwriteTitle ) {
_result.title = $anchor.attr( "title" );
}
}
_found = true;
// break the loop
return false;
}
});
if ( _found ) {
// no need to loop
return false;
}
});
return _result;
},
getAvailableScreenSize: function() {
var data = this.data,
_padding = data.lightboxPadding;
return {
width: this.getWindowSize( "width" ) - _padding,
height: this.getWindowSize( "height" ) - data.headerHeight - _padding
};
},
getCurrentElementNumber: function( element ) {
var _currentNumber,
data = this.data,
sets = this.sets,
_currentSet = sets[data.currentSet.name];
// returns a 1 based ordinal number of an image in a set
$.each(_currentSet, function(i, v) {
// compare DOM elements
if ( $(element).get(0) === v.$anchor.get(0) ) {
_currentNumber = i + 1;
// exit $.each()
return false;
}
});
return _currentNumber;
},
getImageStatus: function( width, height ) {
// statuses (concern both sides):
// 1 - content fits the window and is larger than minimal lightbox size
// -1 - content fits the window but is smaller or equal to minimal lightbox size
// 2 - content is larger than the window
var _statusWidth, _statusHeight,
data = this.data,
_currentElement = data.currentSet.currentElement,
_windowWidth = this.getWindowSize( "width" ),
_windowHeight = this.getWindowSize( "height" ),
_minimalLightboxWidth = data.minimalLightboxSize.width,
_minimalLightboxHeight = data.minimalLightboxSize.height,
_imageWidth = _currentElement.width,
_imageHeight = _currentElement.height,
_lightboxPadding = data.lightboxPadding,
_headerHeight = data.headerHeight;
if ( width <= _minimalLightboxWidth ) {
_statusWidth = -1;
} else if ( width > _minimalLightboxWidth && width + _lightboxPadding <= _windowWidth ) {
_statusWidth = 1;
} else {
_statusWidth = 2;
}
if ( height <= _minimalLightboxHeight ) {
_statusHeight = -1;
} else if ( height > _minimalLightboxHeight && _windowHeight >= height + _lightboxPadding + _headerHeight ) {
_statusHeight = 1;
} else {
_statusHeight = 2;
}
return {
statusWidth: _statusWidth,
statusHeight: _statusHeight
};
},
getLightbox: function() {
var data = this.data,
$lb = this.$lightbox,
self = this;
// if the structure has not been created
if ( !$lb.root ) {
// create the DOM structure of the lightbox
this.createStructure();
// remember references to lightbox structure
this.setReferences();
// close the lightbox when clicking on the close button and the overlay
$lb.close.add( $lb.overlay ).click( $.proxy(this.closeLightbox, this) );
// goes to the next element when button is clicked
$lb.next.click( $.proxy(this.next, this) );
// and goes to the prev element when prev button is clicked
$lb.prev.click( $.proxy(this.prev, this) );
// highlight buttons when mouse hovers over them
$lb.next
.add( $lb.prev )
//.add( $lb.play )
.add( $lb.close )
.hover(
function() {
if ( $(this).is(":not(.ui-state-disabled)") ) {
self.setButtonState( "highlight", $(this) );
}
},
function() {
if ( $(this).is(":not(.ui-state-disabled)") ) {
self.setButtonState( "default", $(this) );
}
}
);
// add handlers to the content container
$lb.contentContainer
.mousemove( $.proxy(this.showArrow, this) )
.mousemove( $.proxy(this.checkSide, this) )
.mousemove( $.proxy(this.setCursor, this) )
.click(
function() {
if ( data.side === "left" ) {
self.prev.apply( self );
} else if ( data.side === "right" ) {
self.next.apply( self );
}
}
)
.mousedown( $.proxy(this.panoramaStart, this) )
.mouseup( $.proxy(this.panoramaStop, this ) )
.mouseleave(
function() {
self.hideArrow.apply( self );
data.side = "";
}
);
// zoom in or zoom out an image
$lb.panoramaIcon
.click( $.proxy(this.panoramaToggle, this) )
.hover( $.proxy(this.panoramaHighlight, this) );
// resize lightbox when window size changes
$( window ).bind( "resize.rlightbox", $.proxy(this.liveResize, this) );
// keyboard navigation
$( document ).keyup( $.proxy(this.handleKeyboard, this) );
}
},
getOptimalSize: function( sizeName, size ) {
// returns size not smaller than the minimal size and not bigger than
// the window size
var data = this.data,
_minimalLightboxSize = data.minimalLightboxSize,
_minimalLightboxWidth = _minimalLightboxSize.width,
_minimalLightboxHeight = _minimalLightboxSize.height,
_screenSize = this.getAvailableScreenSize(),
_screenWidth = _screenSize.width,
_screenHeight = _screenSize.height;
if ( sizeName === "width" ) {
if ( size < _minimalLightboxWidth ) {
return _minimalLightboxWidth;
} else if ( size > _screenWidth ) {
return _screenWidth;
} else {
return size;
}
} else {
if ( size < _minimalLightboxHeight ) {
return _minimalLightboxHeight;
} else if ( size > _screenHeight ) {
return _screenHeight;
} else {
return size;
}
}
},
getParam: function( param, url ) {
// with param ‘with’ and url ‘foo.flv?width=100" it returns ‘100’
var _result,
_regExpString = "[\\?&]" + param + "=(\\w+)",
_regExp = new RegExp( _regExpString );
_result = _regExp.exec( url );
if ( _result !== null ) {
return _result[1];
} else {
return null;
}
},
getSetName: function( jQElement ) {
// if an anchor has class of e.g. ‘lb_gallery’ getSetName() returns ‘gallery’ string as a set name
// otherwise it returns ‘single’ - single content is placed under ‘single’ set
var _classNames = jQElement.element.attr( "class" ),
_classPrefix = jQElement.options.setPrefix + "_",
_classPattern = new RegExp( _classPrefix + "([\\w-_]+)" ),
_name = _classPattern.exec( _classNames );
return _name ? _name[1] : "single";
},
checkSide: function( event ) {
var data = this.data,
$container = this.$lightbox.contentContainer,
_pos = event.pageX - $container.offset().left,
_center = Math.round( $container.width() / 2 );
if ( _pos <= _center ) {
data.side = "left";
} else if ( _pos > _center ) {
data.side = "right";
}
// for Panorama to work in IE7 & IE8
event.preventDefault();
},
getSizes: function() {
var _statuses, _statusWidth, _statusHeight, _imageTargetWidth, _imageTargetHeight, _lightboxTargetWidth, _lightboxTargetHeight,
_heightRatio, _widthRatio,
$lb = this.$lightbox,
data = this.data,
self = this,
_currentElement = data.currentSet.currentElement,
_windowWidth = this.getWindowSize( "width" ),
_windowHeight = this.getWindowSize( "height" ),
_minimalLightboxWidth = data.minimalLightboxSize.width,
_minimalLightboxHeight = data.minimalLightboxSize.height,
_imageWidth = _currentElement.width,
_imageHeight = _currentElement.height,
_lightboxPadding = data.lightboxPadding,
_headerHeight = data.headerHeight;
function _calculateSizes( w, h ) {
_statuses = self.getImageStatus( w, h );
_statusWidth = _statuses.statusWidth;
_statusHeight = _statuses.statusHeight;
// if image fits the window
if ( ((_statusWidth === 1 || _statusWidth === -1) && _statusHeight !== 2) && ((_statusHeight === 1 || _statusHeight === -1) && _statusWidth !== 2) ) {
if ( _statusWidth === 1 ) {
_lightboxTargetWidth = w;
} else if ( _statusWidth === -1 ) {
_lightboxTargetWidth = _minimalLightboxWidth;
}
_imageTargetWidth = w;
if ( _statusHeight === 1 ) {
_lightboxTargetHeight = h;
} else if ( _statusHeight === -1 ) {
_lightboxTargetHeight = _minimalLightboxHeight;
}
_imageTargetHeight = h;
} else if ( _statusWidth === 2 || _statusHeight === 2 ) {
// height is larger than window, width fits the window
if ( _statusWidth === 1 || _statusWidth === -1 ) {
_lightboxTargetHeight = _windowHeight - _headerHeight - _lightboxPadding;
_imageTargetHeight = _lightboxTargetHeight;
_heightRatio = _lightboxTargetHeight / h;
if (_statusWidth === -1) {
_lightboxTargetWidth = _minimalLightboxWidth;
_imageTargetWidth = Math.ceil( w * _heightRatio );
} else {
_lightboxTargetWidth = Math.ceil( w * _heightRatio ) - _lightboxPadding;
_imageTargetWidth = _lightboxTargetWidth;
if ( _imageTargetWidth <= _minimalLightboxWidth ) {
_calculateSizes( _imageTargetWidth, _imageTargetHeight );
}
}
} else if ( _statusHeight === 1 || _statusHeight === -1 ) {
// width is larger than window, height fit the window
_lightboxTargetWidth = _windowWidth - _lightboxPadding;
_imageTargetWidth = _lightboxTargetWidth;
_widthRatio = _lightboxTargetWidth / w;
if ( _statusHeight === -1 ) {
_lightboxTargetHeight = _minimalLightboxHeight;
_imageTargetHeight = Math.ceil( h * _widthRatio );
} else {
_lightboxTargetHeight = Math.ceil( h * _widthRatio ) - _headerHeight - _lightboxPadding;
_imageTargetHeight = _lightboxTargetHeight;
if ( _imageTargetHeight <= _minimalLightboxHeight ) {
_calculateSizes( _imageTargetWidth, _imageTargetHeight );
}
}
} else {
// both width and height are larger than window
if ( w > h ) {
_lightboxTargetWidth = _windowWidth - _lightboxPadding;
_imageTargetWidth = _lightboxTargetWidth;
_widthRatio = _lightboxTargetWidth / w;
_lightboxTargetHeight = Math.ceil( h * _widthRatio ) - _lightboxPadding - _headerHeight;
_imageTargetHeight = _lightboxTargetHeight;
// if after scaling an image is smaller or bigger
if ( _imageTargetHeight <= _minimalLightboxHeight || _lightboxTargetHeight + _lightboxPadding + _headerHeight > _windowHeight ) {
_calculateSizes( _imageTargetWidth, _imageTargetHeight );
}
} else {
_lightboxTargetHeight = _windowHeight - _headerHeight - _lightboxPadding;
_imageTargetHeight = _lightboxTargetHeight;
_heightRatio = _lightboxTargetHeight / h;
_lightboxTargetWidth = Math.ceil( w * _heightRatio ) - _lightboxPadding;
_imageTargetWidth = _lightboxTargetWidth;
if ( _imageTargetWidth <= _minimalLightboxWidth || _lightboxTargetWidth > _windowWidth ) {
_calculateSizes( _imageTargetWidth, _imageTargetHeight );
}
}
}
}
}
_calculateSizes( _imageWidth, _imageHeight );
return {
imageTargetWidth: _imageTargetWidth,
imageTargetHeight: _imageTargetHeight,
lightboxTargetWidth: _lightboxTargetWidth,
lightboxTargetHeight: _lightboxTargetHeight,
statusWidth: _statusWidth,
statusHeight: _statusHeight
};
},
getWindowSize: function( size ) {
var data = this.data,
_windowWidth = $( window ).width(),
_windowHeight = $( window ).height(),
_minimalLightboxSize = data.minimalLightboxSize,
_lightboxPadding = data.lightboxPadding,
_headerHeight = data.headerHeight,
_minimalLightboxWidth = _minimalLightboxSize.width + _lightboxPadding,
_minimalLightboxHeight = _minimalLightboxSize.height + _lightboxPadding + _headerHeight;
if ( size === "width" ) {
if ( _windowWidth < _minimalLightboxWidth ) {
return _minimalLightboxWidth;
} else {
return _windowWidth;
}
} else {
if ( _windowHeight < _minimalLightboxHeight ) {
return _minimalLightboxHeight;
} else {
return _windowHeight;
}
}
},
handleKeyboard: function( event ) {
var data = this.data,
_currentElement = data.currentSet.currentElement,
_options = _currentElement.options,
_keys = _options.keys,
_key = event.which;
if ( data.ready === false ) {
return;
}
// handle pressing keys
if ( _key === _keys.next[0] || _key === _keys.next[1] ) {
// next keys: [N] & [→]
// load next content if possible
this.next();
} else if ( _key === _keys.previous[0] || _key === _keys.previous[1] ) {
// prev keys: [P] & [←]
// load previous content if possible
this.prev();
} else if ( _key === _keys.close[0] || _key === _keys.close[1] ) {
// close keys: [C] & [ESC]
this.closeLightbox();
} else if ( (_key === _keys.panorama[0] || _key === _keys.panorama[1]) && _currentElement.type === "image" ) {
// panorama keys: [Z]
this.panoramaToggle( event );
}
},
hideArrow: function() {
var $lb = this.$lightbox,
$arrow = $lb.arrow;
$arrow.hide();
},
liveResize: function() {
var _elementType,
data = this.data;
if ( data.ready ) {
_elementType = data.currentSet.currentElement.type;
// resizes an image when size of the browser window resizes and when Panorama is turned off
if ( data.ready && data.panoramaOn === false && _elementType === "image" ) {
this.updateImageSize();
this.updateLightboxSize();
this.updateTitleWidth();
this.queueCenterContent();
this.panoramaCheckAvailability();
} else if ( data.ready && data.panoramaOn && _elementType === "image" ) {
// otherwise keep the lightbox centered especially when window is bigger than the lightbox
this.queueCenterLightbox();
this.panoramaShrink();
this.panoramaCheckAvailability();
} else if ( data.ready && _elementType !== "image" ) {
this.queueCenterLightbox();
}
}
},
loadContentFlash: function( setElement ) {
var _width, _height, $contentWrapper, _lightboxTargetWidth, _lightboxTargetHeight,
data = this.data,
self = this,
_loadingFlash = $.Deferred(),
_flashStructure = data.htmlFlash,
_url = setElement.url,
_options = setElement.options;
function _load() {
// get width and height from parameters: &with & &height
// if any exist; ‘inline’ width and height overwrite that of options
_width = self.getParam( "width", _url );
_height = self.getParam( "height", _url );
// if &width and &height are invalid, use a default one
if ( _width === null || isNaN(_width) ) {
_width = _options.videoWidth;
}
if ( _height === null || isNaN(_height) ) {
_height = _options.videoHeight;
}
// what size the lightbox should have
_lightboxTargetWidth = self.getOptimalSize( "width", _width );
_lightboxTargetHeight = self.getOptimalSize( "height", _height );
// use real data
_flashStructure = self.replaceHtmlPatterns(_flashStructure,
{
width: _width,
height: _height,
url: _url
}
);
// we have to add ‘width’ and ‘height’ to the $contentWrapper
// explicitly since browsers can’t inherit them
$contentWrapper = $( "<div></div>" );
$contentWrapper
.css(
{
display: "none",
width: _width,
height: _height
}
)
.append( _flashStructure );
// pass to queueLoadContent()
_loadingFlash.resolve(
{
width: _lightboxTargetWidth,
height: _lightboxTargetHeight,
structure: $contentWrapper
}
);
}
// delay ‘_load’ because we have to return promise
setTimeout( _load, 500 );
return _loadingFlash.promise();
},
loadContentImage: function( setElement ) {
var _imageTargetWidth, _imageTargetHeight, _lightboxTargetWidth, _lightboxTargetHeight,
data = this.data,
self = this,
_loadingImage = $.Deferred(),
$newImage = $( "<img />" ),
_url = setElement.url,
$structure = $([]);
$newImage
.attr( "src", _url )
.bind("load",
function() {
$( this ).unbind( "load" );
// keep original size of an image – needed when resizing
setElement.width = this.width;
setElement.height = this.height;
_sizes = self.getSizes();
_imageTargetWidth = _sizes.imageTargetWidth;
_imageTargetHeight = _sizes.imageTargetHeight;
_lightboxTargetWidth = _sizes.lightboxTargetWidth;
_lightboxTargetHeight = _sizes.lightboxTargetHeight;
// if scaled size is smaller than the original, show Panorama
setElement.currentWidth = _imageTargetWidth;
setElement.currentHeight = _imageTargetHeight;
// scale the image
$( this )
.width( _imageTargetWidth )
.height( _imageTargetHeight );
// add the loaded image and hide it
$structure = $structure.add( this ).hide();
// continue the animation queue
_loadingImage.resolve(
{
width: _lightboxTargetWidth,
height: _lightboxTargetHeight,
structure: $structure
}
);
}
)
.error(
function() {
_loadingImage.reject();
}
)
.each(
function() {
// the code comes from https://github.com/desandro/imagesloaded
// cached images don't fire load sometimes, so we reset src.
if ( this.complete || this.complete === undefined ){
var src = this.src;
// webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
// data uri bypasses webkit log warning (thx doug jones)
this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
this.src = src;
}
}
);
return _loadingImage.promise();
},
loadContentVimeo: function( setElement ) {
var _width, _height, _lightboxTargetWidth, _lightboxTargetHeight, $structure,
data = this.data,
self = this,
_loadingVimeo = $.Deferred(),
_apiEnd = data.providers.vimeo,
_options = setElement.options,
_minimalLightboxSize = data.minimalLightboxSize,
_url = setElement.url;
$.ajax(
{
url: _apiEnd,
data: {
url: _url,
maxwidth: _options.videoWidth,
maxheight: _options.videoHeight
},
dataType: "jsonp",
timeout: 5000
}
)
.success(
function( data ) {
// we have to add ‘width’ and ‘height’ to the $contentWrapper
// explicitly since browsers can’t inherit them
$structure = $( "<div></div>" )
.css( "display", "none" )
.width( data.width )
.height( data.height )
.append( data.html );
// remember video title
if ( _options.overwriteTitle === false ) {
setElement.title = data.title;
}
// what size the lightbox should have
_lightboxTargetWidth = self.getOptimalSize( "width", data.width );
_lightboxTargetHeight = self.getOptimalSize( "height", data.height );
// continue the animation queue
_loadingVimeo.resolve(
{
width: _lightboxTargetWidth,
height: _lightboxTargetHeight,
structure: $structure
}
);
}
)
.error(function() {
_loadingVimeo.reject();
});
return _loadingVimeo.promise();
},
loadContentYoutube: function( setElement ) {
var $contentWrapper, _lightboxTargetWidth, _lightboxTargetHeight, $structure,
data = this.data,
self = this,
_loadingYoutube = $.Deferred(),
_apiEnd = data.providers.youtube,
_options = setElement.options,
_minimalLightboxSize = data.minimalLightboxSize,
_width = _options.videoWidth,
_height = _options.videoHeight,
_structure = data.htmlYoutube;
$.ajax(
{
url: _apiEnd + setElement.videoId + "?callback=?",
data: {
v: 2,
alt: "jsonc",
prettyprint: true
},
dataType: "jsonp",
timeout: 5000
}
)
.success(
function( json ) {
// if response is successful but there is an error
if ( json.error ) {//sprawdzić
_loadingYoutube.reject();
return;
}
// use real data
_structure = self.replaceHtmlPatterns(_structure,
{
width: _width,
height: _height,
url: setElement.videoId
}
);
// we have to add ‘width’ and ‘height’ to the $contentWrapper
// explicitly since browsers can’t inherit them
$structure = $( "<div></div>" )
.css( "display", "none" )
.width( _width )
.height( _height )
.append( _structure );
// remember video title
if ( _options.overwriteTitle === false ) {
setElement.title = json.data.title;
}
_lightboxTargetWidth = self.getOptimalSize( "width", _width );
_lightboxTargetHeight = self.getOptimalSize( "height", _height );
// continue the animation queue
_loadingYoutube.resolve(
{
width: _lightboxTargetWidth,
height: _lightboxTargetHeight,
structure: $structure
}
);
}
)
.error(function() {
_loadingYoutube.reject();
});
return _loadingYoutube.promise();
},
navigationGoToElement: function( number ) {
// goes to a custom element
var data = this.data,