This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
ReactImageLightbox.js
1061 lines (874 loc) · 39.3 KB
/
ReactImageLightbox.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
/*
* react-image-lightbox 2.0.0
* Copyright 2016 Chris Fritz All rights reserved.
* @license Open source under the MIT License
*/
'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _radium = require('radium');
var _radium2 = _interopRequireDefault(_radium);
var _Styles = require('./Styles');
var _Styles2 = _interopRequireDefault(_Styles);
var _Portal = require('./Portal');
var _Portal2 = _interopRequireDefault(_Portal);
var _Constant = require('./Constant');
var _Constant2 = _interopRequireDefault(_Constant);
function _getWindowWidth() {
return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}
function _getWindowHeight() {
return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
// Returns true if this window is rendered as an iframe inside another window
function _isInIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
var ReactImageLightbox = _react2['default'].createClass({
displayName: 'ReactImageLightbox',
propTypes: {
///////////////////////////////
// Image sources
///////////////////////////////
// Main display image url
mainSrc: _react.PropTypes.string.isRequired,
// Previous display image url (displayed to the left)
// If left undefined, movePrev actions will not be performed, and the button not displayed
prevSrc: _react.PropTypes.string,
// Next display image url (displayed to the right)
// If left undefined, moveNext actions will not be performed, and the button not displayed
nextSrc: _react.PropTypes.string,
///////////////////////////////
// Image thumbnail sources
///////////////////////////////
// Thumbnail image url corresponding to props.mainSrc
mainSrcThumbnail: _react.PropTypes.string,
// Thumbnail image url corresponding to props.prevSrc
prevSrcThumbnail: _react.PropTypes.string,
// Thumbnail image url corresponding to props.nextSrc
nextSrcThumbnail: _react.PropTypes.string,
///////////////////////////////
// Event Handlers
///////////////////////////////
// Close window event
// Should change the parent state such that the lightbox is not rendered
onCloseRequest: _react.PropTypes.func.isRequired,
// Move to previous image event
// Should change the parent state such that props.prevSrc becomes props.mainSrc,
// props.mainSrc becomes props.nextSrc, etc.
onMovePrevRequest: _react.PropTypes.func,
// Move to next image event
// Should change the parent state such that props.nextSrc becomes props.mainSrc,
// props.mainSrc becomes props.prevSrc, etc.
onMoveNextRequest: _react.PropTypes.func,
///////////////////////////////
// Download discouragement settings
///////////////////////////////
// Enable download discouragement (prevents [right-click -> Save Image As...])
discourageDownloads: _react.PropTypes.bool,
///////////////////////////////
// Animation settings
///////////////////////////////
// Disable all animation
animationDisabled: _react.PropTypes.bool,
// Disable animation on actions performed with keyboard shortcuts
animationOnKeyInput: _react.PropTypes.bool,
// Animation duration (ms)
animationDuration: _react.PropTypes.number,
///////////////////////////////
// Keyboard shortcut settings
///////////////////////////////
// Required interval of time (ms) between key actions
// (prevents excessively fast navigation of images)
keyRepeatLimit: _react.PropTypes.number,
// Amount of time (ms) restored after each keyup
// (makes rapid key presses slightly faster than holding down the key to navigate images)
keyRepeatKeyupBonus: _react.PropTypes.number,
///////////////////////////////
// Image info
///////////////////////////////
// Image title
imageTitle: _react.PropTypes.node,
///////////////////////////////
// Other
///////////////////////////////
// Array of custom toolbar buttons
toolbarButtons: _react.PropTypes.arrayOf(_react.PropTypes.node),
// Padding (px) between the edge of the window and the lightbox
imagePadding: _react.PropTypes.number,
// When true, clicks outside of the image close the lightbox
clickOutsideToClose: _react.PropTypes.bool
},
getDefaultProps: function getDefaultProps() {
return {
onMovePrevRequest: function onMovePrevRequest() {},
onMoveNextRequest: function onMoveNextRequest() {},
discourageDownloads: false,
animationDisabled: false,
animationOnKeyInput: false,
animationDuration: 300,
keyRepeatLimit: 180,
keyRepeatKeyupBonus: 40,
imagePadding: 10,
clickOutsideToClose: true
};
},
getInitialState: function getInitialState() {
return {
///////////////////////////////
// Animation
///////////////////////////////
// Lightbox is closing
// When Lightbox is mounted, if animation is enabled it will open with the reverse of the closing animation
isClosing: !this.props.animationDisabled,
// Component parts should animate (e.g., when images are moving, or image is being zoomed)
shouldAnimate: false,
///////////////////////////////
// Zoom settings
///////////////////////////////
// Zoom level of image
zoomLevel: _Constant2['default'].MIN_ZOOM_LEVEL,
///////////////////////////////
// Image position settings
///////////////////////////////
// Horizontal offset from center
offsetX: 0,
// Vertical offset from center
offsetY: 0
};
},
componentWillMount: function componentWillMount() {
// Whether event listeners for keyboard and mouse input have been attached or not
this.listenersAttached = false;
// Used to disable animation when changing props.mainSrc|nextSrc|prevSrc
this.keyPressed = false;
// Used to store load state / dimensions of images
this.imageCache = {};
// Time the last keydown event was called (used in keyboard action rate limiting)
this.lastKeyDownTime = 0;
// Used for debouncing window resize event
this.resizeTimeout = null;
// Used to determine when actions are triggered by the scroll wheel
this.wheelActionTimeout = null;
this.resetScrollTimeout = null;
this.scrollX = 0;
this.scrollY = 0;
// Used in panning zoomed images
this.isDragging = false;
this.dragStartX = 0;
this.dragStartY = 0;
this.dragStartOffsetX = 0;
this.dragStartOffsetY = 0;
// Used to differentiate between images with identical src
this.keyCounter = 0;
// Used to detect a move when all src's remain unchanged (four or more of the same image in a row)
this.moveRequested = false;
},
componentDidMount: function componentDidMount() {
this.attachListeners();
if (!this.props.animationDisabled) {
// Make opening animation play
this.setState({ isClosing: false });
}
this.loadAllImages();
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
var _this = this;
var sourcesChanged = this.getSrcTypes().some(function (srcType) {
return _this.props[srcType.name] != nextProps[srcType.name];
});
if (sourcesChanged || this.moveRequested) {
this.moveRequested = false;
// Load any new images
this.loadAllImages(nextProps);
}
},
componentWillUnmount: function componentWillUnmount() {
this.detachListeners();
},
// Handle user keyboard actions
handleKeyInput: function handleKeyInput(event) {
event.stopPropagation();
// Ignore key input during animations
if (this.isAnimating()) {
return;
}
// Allow slightly faster navigation through the images when user presses keys repeatedly
if (event.type === 'keyup') {
this.lastKeyDownTime -= this.props.keyRepeatKeyupBonus;
return;
}
var keyCode = event.which || event.keyCode;
var key = {
esc: 27,
leftArrow: 37,
rightArrow: 39
};
// Ignore key presses that happen too close to each other (when rapid fire key pressing or holding down the key)
// But allow it if it's a lightbox closing action
var currentTime = new Date();
if (currentTime.getTime() - this.lastKeyDownTime < this.props.keyRepeatLimit && keyCode != key.esc) {
return;
}
this.lastKeyDownTime = currentTime.getTime();
switch (keyCode) {
// ESC key closes the lightbox
case key.esc:
event.preventDefault();
this.requestClose(event);
break;
// Left arrow key moves to previous image
case key.leftArrow:
if (!this.props.prevSrc) {
return;
}
event.preventDefault();
this.keyPressed = true;
this.requestMovePrev(event);
break;
// Right arrow key moves to next image
case key.rightArrow:
if (!this.props.nextSrc) {
return;
}
event.preventDefault();
this.keyPressed = true;
this.requestMoveNext(event);
break;
default:
}
},
// Handle the window resize event
handleWindowResize: function handleWindowResize() {
clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(this.forceUpdate.bind(this), 100);
},
// Handle a mouse wheel event over the lightbox container
handleOuterMousewheel: function handleOuterMousewheel(event) {
var _this2 = this;
// Prevent scrolling of the background
event.preventDefault();
event.stopPropagation();
var xThreshold = _Constant2['default'].WHEEL_MOVE_X_THRESHOLD;
var actionDelay = 0;
var imageMoveDelay = 500;
clearTimeout(this.resetScrollTimeout);
this.resetScrollTimeout = setTimeout(function () {
_this2.scrollX = 0;
_this2.scrollY = 0;
}, 300);
// Prevent rapid-fire zoom behavior
if (this.wheelActionTimeout !== null || this.isAnimating()) {
return;
}
if (Math.abs(event.deltaY) < Math.abs(event.deltaX)) {
// handle horizontal scrolls with image moves
this.scrollY = 0;
this.scrollX += event.deltaX;
var bigLeapX = xThreshold / 2;
// If the scroll amount has accumulated sufficiently, or a large leap was taken
if (this.scrollX >= xThreshold || event.deltaX >= bigLeapX) {
// Scroll right moves to next
this.requestMoveNext(event);
actionDelay = imageMoveDelay;
this.scrollX = 0;
} else if (this.scrollX <= -1 * xThreshold || event.deltaX <= -1 * bigLeapX) {
// Scroll left moves to previous
this.requestMovePrev(event);
actionDelay = imageMoveDelay;
this.scrollX = 0;
}
}
// Allow successive actions after the set delay
if (actionDelay !== 0) {
this.wheelActionTimeout = setTimeout(function () {
_this2.wheelActionTimeout = null;
}, actionDelay);
}
},
handleImageMouseWheel: function handleImageMouseWheel(event) {
event.preventDefault();
var yThreshold = _Constant2['default'].WHEEL_MOVE_Y_THRESHOLD;
if (Math.abs(event.deltaY) >= Math.abs(event.deltaX)) {
event.stopPropagation();
// If the vertical scroll amount was large enough, perform a zoom
if (Math.abs(event.deltaY) < yThreshold) {
return;
}
this.scrollX = 0;
this.scrollY += event.deltaY;
this.changeZoom(this.state.zoomLevel - event.deltaY, event.clientX, event.clientY);
}
},
getOffsetXFromWindowCenter: function getOffsetXFromWindowCenter(x) {
var windowWidth = _getWindowWidth();
return windowWidth / 2 - x;
},
getOffsetYFromWindowCenter: function getOffsetYFromWindowCenter(y) {
var windowHeight = _getWindowHeight();
return windowHeight / 2 - y;
},
// Handle a double click on the current image
handleImageDoubleClick: function handleImageDoubleClick(event) {
if (this.state.zoomLevel > _Constant2['default'].MIN_ZOOM_LEVEL) {
// A double click when zoomed in zooms all the way out
this.changeZoom(_Constant2['default'].MIN_ZOOM_LEVEL, event.clientX, event.clientY);
} else {
// A double click when zoomed all the way out zooms in
this.changeZoom(this.state.zoomLevel + _Constant2['default'].ZOOM_BUTTON_INCREMENT_SIZE, event.clientX, event.clientY);
}
},
// Handle the mouse clicking down in the lightbox container
handleOuterMouseDown: function handleOuterMouseDown(event) {
event.preventDefault();
// Allow dragging when zoomed
if (this.state.zoomLevel > _Constant2['default'].MIN_ZOOM_LEVEL) {
this.isDragging = true;
this.dragStartX = event.clientX;
this.dragStartY = event.clientY;
this.dragStartOffsetX = this.state.offsetX;
this.dragStartOffsetY = this.state.offsetY;
}
},
// Handle the mouse dragging over the lightbox container
// (after a mouseDown and before a mouseUp event)
handleOuterMouseMove: function handleOuterMouseMove(event) {
if (!this.isDragging) {
return;
}
var zoomMultiplier = this.getZoomMultiplier();
var newOffsetX = (this.dragStartX - event.clientX) / zoomMultiplier + this.dragStartOffsetX;
var newOffsetY = (this.dragStartY - event.clientY) / zoomMultiplier + this.dragStartOffsetY;
if (this.state.offsetX !== newOffsetX || this.state.offsetY !== newOffsetY) {
this.setState({
offsetX: newOffsetX,
offsetY: newOffsetY
});
}
},
// Handle a mouse click ending in the lightbox container
handleMouseUp: function handleMouseUp() {
var _this3 = this;
if (!this.isDragging) {
return;
}
this.isDragging = false;
// Snap image back into frame if outside max offset range
var maxOffsets = this.getMaxOffsets();
var nextOffsetX = Math.max(maxOffsets.minX, Math.min(maxOffsets.maxX, this.state.offsetX));
var nextOffsetY = Math.max(maxOffsets.minY, Math.min(maxOffsets.maxY, this.state.offsetY));
if (nextOffsetX !== this.state.offsetX || nextOffsetY !== this.state.offsetY) {
this.setState({
offsetX: nextOffsetX,
offsetY: nextOffsetY,
shouldAnimate: true
});
setTimeout(function () {
_this3.setState({ shouldAnimate: false });
}, this.props.animationDuration);
}
},
handleZoomInButtonClick: function handleZoomInButtonClick() {
this.changeZoom(this.state.zoomLevel + _Constant2['default'].ZOOM_BUTTON_INCREMENT_SIZE);
},
handleZoomOutButtonClick: function handleZoomOutButtonClick() {
this.changeZoom(this.state.zoomLevel - _Constant2['default'].ZOOM_BUTTON_INCREMENT_SIZE);
},
// Change zoom level
changeZoom: function changeZoom(zoomLevel, clientX, clientY) {
var windowWidth = _getWindowWidth();
var windowHeight = _getWindowHeight();
// Default to the center of the screen to zoom when no mouse position specified
clientX = typeof clientX !== 'undefined' ? clientX : windowWidth / 2;
clientY = typeof clientY !== 'undefined' ? clientY : windowHeight / 2;
// Constrain zoom level to the set bounds
var nextZoomLevel = Math.max(_Constant2['default'].MIN_ZOOM_LEVEL, Math.min(_Constant2['default'].MAX_ZOOM_LEVEL, zoomLevel));
// Ignore requests that don't change the zoom level
if (nextZoomLevel === this.state.zoomLevel) {
return;
} else if (nextZoomLevel === _Constant2['default'].MIN_ZOOM_LEVEL) {
// Snap back to center if zoomed all the way out
return this.setState({
zoomLevel: nextZoomLevel,
offsetX: 0,
offsetY: 0
});
}
var currentZoomMultiplier = this.getZoomMultiplier();
var nextZoomMultiplier = this.getZoomMultiplier(nextZoomLevel);
var percentXInCurrentBox = clientX / windowWidth;
var percentYInCurrentBox = clientY / windowHeight;
var currentBoxWidth = windowWidth / currentZoomMultiplier;
var currentBoxHeight = windowHeight / currentZoomMultiplier;
var nextBoxWidth = windowWidth / nextZoomMultiplier;
var nextBoxHeight = windowHeight / nextZoomMultiplier;
var deltaX = (nextBoxWidth - currentBoxWidth) * (percentXInCurrentBox - 0.5);
var deltaY = (nextBoxHeight - currentBoxHeight) * (percentYInCurrentBox - 0.5);
var nextOffsetX = this.state.offsetX - deltaX;
var nextOffsetY = this.state.offsetY - deltaY;
// When zooming out, limit the offset so things don't get left askew
var maxOffsets = this.getMaxOffsets();
if (this.state.zoomLevel > nextZoomLevel) {
nextOffsetX = Math.max(maxOffsets.minX, Math.min(maxOffsets.maxX, nextOffsetX));
nextOffsetY = Math.max(maxOffsets.minY, Math.min(maxOffsets.maxY, nextOffsetY));
}
this.setState({
zoomLevel: nextZoomLevel,
offsetX: nextOffsetX,
offsetY: nextOffsetY
});
},
// Request that the lightbox be closed
requestClose: function requestClose(event) {
var _this4 = this;
// Call the parent close request
var closeLightbox = function closeLightbox() {
return _this4.props.onCloseRequest(event);
};
if (this.props.animationDisabled || event.type === 'keydown' && !this.props.animationOnKeyInput) {
// No animation
return closeLightbox();
} else {
// With animation
// Start closing animation
this.setState({ isClosing: true });
// Perform the actual closing at the end of the animation
setTimeout(closeLightbox, this.props.animationDuration);
}
},
requestMove: function requestMove(direction, event) {
var _this5 = this;
// Reset the zoom level on image move
var nextState = {
zoomLevel: _Constant2['default'].MIN_ZOOM_LEVEL,
offsetX: 0,
offsetY: 0
};
// Enable animated states
if (!this.props.animationDisabled && (!this.keyPressed || this.props.animationOnKeyInput)) {
nextState.shouldAnimate = true;
setTimeout(function () {
return _this5.setState({ shouldAnimate: false });
}, this.props.animationDuration);
}
this.keyPressed = false;
this.moveRequested = true;
if (direction === 'prev') {
this.keyCounter--;
this.setState(nextState);
this.props.onMovePrevRequest(event);
} else {
this.keyCounter++;
this.setState(nextState);
this.props.onMoveNextRequest(event);
}
},
// Request to transition to the previous image
requestMovePrev: function requestMovePrev(event) {
this.requestMove('prev', event);
},
// Request to transition to the next image
requestMoveNext: function requestMoveNext(event) {
this.requestMove('next', event);
},
closeIfClickInner: function closeIfClickInner(event) {
if (event.target.className.search(/\binner\b/) > -1) {
this.requestClose(event);
}
},
// Attach key and mouse input events
attachListeners: function attachListeners() {
if (!this.listenersAttached) {
document.addEventListener('keydown', this.handleKeyInput);
document.addEventListener('keyup', this.handleKeyInput);
window.addEventListener('resize', this.handleWindowResize);
window.addEventListener('mouseup', this.handleMouseUp);
// Have to add an extra mouseup handler to catch mouseup events outside of the window
// if the page containing the lightbox is displayed in an iframe
if (_isInIframe()) {
window.top.addEventListener('mouseup', this.handleMouseUp);
}
this.listenersAttached = true;
}
},
// Detach key and mouse input events
detachListeners: function detachListeners() {
if (this.listenersAttached) {
document.removeEventListener('keydown', this.handleKeyInput);
document.removeEventListener('keyup', this.handleKeyInput);
window.removeEventListener('resize', this.handleWindowResize);
window.removeEventListener('mouseup', this.handleMouseUp);
if (_isInIframe()) {
window.top.removeEventListener('mouseup', this.handleMouseUp);
}
this.listenersAttached = false;
}
},
// Get image src types
getSrcTypes: function getSrcTypes() {
return [{
name: 'mainSrc',
keyEnding: 'i' + this.keyCounter
}, {
name: 'mainSrcThumbnail',
keyEnding: 't' + this.keyCounter
}, {
name: 'nextSrc',
keyEnding: 'i' + (this.keyCounter + 1)
}, {
name: 'nextSrcThumbnail',
keyEnding: 't' + (this.keyCounter + 1)
}, {
name: 'prevSrc',
keyEnding: 'i' + (this.keyCounter - 1)
}, {
name: 'prevSrcThumbnail',
keyEnding: 't' + (this.keyCounter - 1)
}];
},
// Get sizing for when an image is larger than the window
getFitSizes: function getFitSizes(width, height, stretch) {
var windowHeight = _getWindowHeight();
var windowWidth = _getWindowWidth();
var maxHeight = windowHeight - this.props.imagePadding * 2;
var maxWidth = windowWidth - this.props.imagePadding * 2;
if (!stretch) {
maxHeight = Math.min(maxHeight, height);
maxWidth = Math.min(maxWidth, width);
}
var maxRatio = maxWidth / maxHeight;
var srcRatio = width / height;
var fitSizes = {};
if (maxRatio > srcRatio) {
// height is the constraining dimension of the photo
fitSizes.width = width * maxHeight / height;
fitSizes.height = maxHeight;
} else {
fitSizes.width = maxWidth;
fitSizes.height = height * maxWidth / width;
}
return fitSizes;
},
// Get sizing when the image is scaled
getZoomMultiplier: function getZoomMultiplier(zoomLevel) {
zoomLevel = typeof zoomLevel !== 'undefined' ? zoomLevel : this.state.zoomLevel;
return Math.pow(_Constant2['default'].ZOOM_RATIO, zoomLevel);
},
getMaxOffsets: function getMaxOffsets(zoomLevel) {
zoomLevel = typeof zoomLevel !== 'undefined' ? zoomLevel : this.state.zoomLevel;
var currentImageInfo = this.getBestImageForType('mainSrc');
if (currentImageInfo === null) {
return { maxX: 0, minX: 0, maxY: 0, minY: 0 };
}
var windowWidth = _getWindowWidth();
var windowHeight = _getWindowHeight();
var zoomMultiplier = this.getZoomMultiplier(zoomLevel);
var maxX = 0;
if (currentImageInfo.width - windowWidth / zoomMultiplier < 0) {
// if there is still blank space in the X dimension, don't limit except to the opposite edge
maxX = (windowWidth / zoomMultiplier - currentImageInfo.width) / 2;
} else {
maxX = (currentImageInfo.width - windowWidth / zoomMultiplier) / 2;
}
var maxY = 0;
if (currentImageInfo.height - windowHeight / zoomMultiplier < 0) {
// if there is still blank space in the Y dimension, don't limit except to the opposite edge
maxY = (windowHeight / zoomMultiplier - currentImageInfo.height) / 2;
} else {
maxY = (currentImageInfo.height - windowHeight / zoomMultiplier) / 2;
}
return {
maxX: maxX,
minX: -1 * maxX,
maxY: maxY,
minY: -1 * maxY
};
},
// Detach key and mouse input events
isAnimating: function isAnimating() {
return this.state.shouldAnimate || this.state.isClosing;
},
// Load image from src and call callback with image width and height on load
loadImage: function loadImage(imageSrc, callback) {
var _this6 = this;
// Return the image info if it is already cached
if (this.isImageLoaded(imageSrc)) {
setTimeout(function () {
callback(null, _this6.imageCache[imageSrc].width, _this6.imageCache[imageSrc].height);
}, 1);
return;
}
var that = this;
var inMemoryImage = new Image();
inMemoryImage.onerror = function () {
callback('image load error');
};
inMemoryImage.onload = function () {
that.imageCache[imageSrc] = {
loaded: true,
width: this.width,
height: this.height
};
callback(null, this.width, this.height);
};
inMemoryImage.src = imageSrc;
},
// Load all images and their thumbnails
loadAllImages: function loadAllImages(props) {
var _this7 = this;
props = props || this.props;
var generateImageLoadedCallback = function generateImageLoadedCallback(srcType, imageSrc) {
return function (err) {
// Give up showing image on error
if (err) {
if (window.console) {
window.console.warn(err);
}
return;
}
// Don't rerender if the src is not the same as when the load started
if (_this7.props[srcType] != imageSrc) {
return;
}
// Force rerender with the new image
_this7.forceUpdate();
};
};
// Load the images
this.getSrcTypes().forEach(function (srcType) {
var type = srcType.name;
// Load unloaded images
if (props[type] && !_this7.isImageLoaded(props[type])) {
_this7.loadImage(props[type], generateImageLoadedCallback(type, props[type]));
}
});
},
// Load image from src and call callback with image width and height on load
isImageLoaded: function isImageLoaded(imageSrc) {
return imageSrc && imageSrc in this.imageCache && this.imageCache[imageSrc].loaded;
},
// Get info for the best suited image to display with the given srcType
getBestImageForType: function getBestImageForType(srcType) {
var imageSrc = this.props[srcType];
var fitSizes = {};
if (this.isImageLoaded(imageSrc)) {
// Use full-size image if available
fitSizes = this.getFitSizes(this.imageCache[imageSrc].width, this.imageCache[imageSrc].height);
} else if (this.isImageLoaded(this.props[srcType + 'Thumbnail'])) {
// Fall back to using thumbnail if the image has not been loaded
imageSrc = this.props[srcType + 'Thumbnail'];
fitSizes = this.getFitSizes(this.imageCache[imageSrc].width, this.imageCache[imageSrc].height, true);
} else {
return null;
}
return {
src: imageSrc,
height: fitSizes.height,
width: fitSizes.width
};
},
render: function render() {
var _this8 = this;
// Transition settings for sliding animations
var transitionStyle = {};
if (!this.props.animationDisabled && this.isAnimating()) {
transitionStyle = _Styles2['default'].imageAnimating(this.props.animationDuration);
}
// Key endings to differentiate between images with the same src
var keyEndings = {};
this.getSrcTypes().forEach(function (srcType) {
keyEndings[srcType.name] = srcType.keyEnding;
});
// Images to be displayed
var images = [];
var addImage = function addImage(srcType, imageClass, baseStyle) {
// Ignore types that have no source defined for their full size image
if (!_this8.props[srcType]) {
return;
}
var imageStyle = [_Styles2['default'].image, baseStyle, transitionStyle];
if (_this8.state.zoomLevel > _Constant2['default'].MIN_ZOOM_LEVEL) {
imageStyle.push({ cursor: 'move' });
}
var bestImageInfo = _this8.getBestImageForType(srcType);
if (bestImageInfo === null) {
// Fall back to loading icon if the thumbnail has not been loaded
images.push(_react2['default'].createElement('div', {
className: imageClass + ' not-loaded',
style: imageStyle,
key: _this8.props[srcType] + keyEndings[srcType]
}));
return;
}
imageStyle.push({
width: bestImageInfo.width,
height: bestImageInfo.height
});
var imageSrc = bestImageInfo.src;
if (_this8.props.discourageDownloads) {
imageStyle.push({ backgroundImage: 'url(\'' + imageSrc + '\')' });
imageStyle.push(_Styles2['default'].imageDiscourager);
images.push(_react2['default'].createElement(
'div',
{
className: imageClass,
onDoubleClick: _this8.handleImageDoubleClick,
onWheel: _this8.handleImageMouseWheel,
style: imageStyle,
key: imageSrc + keyEndings[srcType]
},
_react2['default'].createElement('div', { className: 'download-blocker', style: [_Styles2['default'].downloadBlocker] })
));
} else {
images.push(_react2['default'].createElement('img', {
className: imageClass,
onDoubleClick: _this8.handleImageDoubleClick,
onWheel: _this8.handleImageMouseWheel,
style: imageStyle,
src: imageSrc,
key: imageSrc + keyEndings[srcType]
}));
}
};
var zoomMultiplier = this.getZoomMultiplier();
// Next Image (displayed on the right)
addImage('nextSrc', 'image-next', _Styles2['default'].imageNext);
// Main Image
addImage('mainSrc', 'image-current', _Styles2['default'].imageCurrent(zoomMultiplier, zoomMultiplier * this.state.offsetX, zoomMultiplier * this.state.offsetY));
// Previous Image (displayed on the left)
addImage('prevSrc', 'image-prev', _Styles2['default'].imagePrev);
var noop = function noop() {};
// Prepare styles and handlers for the zoom in/out buttons
var zoomInButtonStyle = [_Styles2['default'].toolbarItemChild, _Styles2['default'].builtinButton, _Styles2['default'].zoomInButton];
var zoomOutButtonStyle = [_Styles2['default'].toolbarItemChild, _Styles2['default'].builtinButton, _Styles2['default'].zoomOutButton];
var zoomInButtonHandler = this.handleZoomInButtonClick;
var zoomOutButtonHandler = this.handleZoomOutButtonClick;
// Disable zooming in when zoomed all the way in
if (this.state.zoomLevel === _Constant2['default'].MAX_ZOOM_LEVEL) {
zoomInButtonStyle.push(_Styles2['default'].builtinButtonDisabled);
zoomInButtonHandler = noop;
}
// Disable zooming out when zoomed all the way out
if (this.state.zoomLevel === _Constant2['default'].MIN_ZOOM_LEVEL) {
zoomOutButtonStyle.push(_Styles2['default'].builtinButtonDisabled);
zoomOutButtonHandler = noop;
}
// Ignore clicks during animation
if (this.isAnimating()) {
zoomInButtonHandler = noop;
zoomOutButtonHandler = noop;
}
return _react2['default'].createElement(
_Portal2['default'],
null,
_react2['default'].createElement(
_radium.StyleRoot,
null,
_react2['default'].createElement(
'div',
{ // Floating modal with closing animations
className: 'outer' + (this.state.isClosing ? ' closing' : ''),
onWheel: this.handleOuterMousewheel,
onMouseMove: this.handleOuterMouseMove,
onMouseDown: this.handleOuterMouseDown,
onMouseUp: this.handleOuterMouseUp,
style: [_Styles2['default'].outer, _Styles2['default'].outerAnimating(this.props.animationDuration, this.state.isClosing), this.state.isClosing ? _Styles2['default'].outerClosing : {}]
},
_react2['default'].createElement(
'div',
{ // Image holder
className: 'inner',
onClick: this.props.clickOutsideToClose ? this.closeIfClickInner : noop,
style: [_Styles2['default'].inner]
},
images
),
!this.props.prevSrc ? '' : _react2['default'].createElement('button', { // Move to previous image button
type: 'button',
className: 'prev-button',
key: 'prev',
style: [_Styles2['default'].navButtons, _Styles2['default'].navButtonPrev],
onClick: !this.isAnimating() ? this.requestMovePrev : noop // Ignore clicks during animation
}),
!this.props.nextSrc ? '' : _react2['default'].createElement('button', { // Move to next image button
type: 'button',
className: 'next-button',
key: 'next',
style: [_Styles2['default'].navButtons, _Styles2['default'].navButtonNext],
onClick: !this.isAnimating() ? this.requestMoveNext : noop // Ignore clicks during animation
}),
_react2['default'].createElement(
'div',
{ // Lightbox toolbar
className: 'toolbar',
style: [_Styles2['default'].toolbar]
},
_react2['default'].createElement(
'ul',
{ className: 'toolbar-left', style: [_Styles2['default'].toolbarSide, _Styles2['default'].toolbarLeftSide] },
_react2['default'].createElement(