-
Notifications
You must be signed in to change notification settings - Fork 4
/
image-zoom.js
1357 lines (1141 loc) · 27 KB
/
image-zoom.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
(function umd(_require){
if ('object' == typeof exports) {
module.exports = _require('1');
} else if ('function' == typeof define && define.amd) {
define(function(){ return _require('1'); });
} else {
this['image-zoom'] = _require('1');
}
})((function outer(modules, cache, entries){
/**
* Global
*/
var global = (function(){ return this; })();
/**
* Require `name`.
*
* @param {String} name
* @param {Boolean} jumped
* @api public
*/
function _require(name, jumped){
if (cache[name]) return cache[name].exports;
if (modules[name]) return call(name, _require);
throw new Error('cannot find module "' + name + '"');
}
/**
* Call module `id` and cache it.
*
* @param {Number} id
* @param {Function} require
* @return {Function}
* @api private
*/
function call(id, _require){
var m = cache[id] = { exports: {} };
var mod = modules[id];
var name = mod[2];
var fn = mod[0];
fn.call(m.exports, function(req){
var dep = modules[id][1][req];
return _require(dep ? dep : req);
}, m, m.exports, outer, modules, cache, entries);
// expose as `name`.
if (name) cache[name] = cache[id];
return cache[id].exports;
}
/**
* Require all entries exposing them on global if needed.
*/
for (var id in entries) {
if (entries[id]) {
global[entries[id]] = _require(id);
} else {
_require(id);
}
}
/**
* Duo flag.
*/
_require.duo = true;
/**
* Expose cache.
*/
_require.cache = cache;
/**
* Expose modules
*/
_require.modules = modules;
/**
* Return newest require.
*/
return _require;
})({
1: [function(_require, module, exports) {
/**
* Module dependencies
*/
var emitter = _require('emitter');
var transform = _require('transform-property');
var redraw = _require('redraw');
var afterTransition = _require('after-transition');
var scale = _require('scale-to-bounds');
var viewport = _require('viewport');
var has3d = _require('has-translate3d');
var Overlay = _require('overlay');
var delegate = _require('delegate');
var events = _require('events');
var nextTick = _require('next-tick');
/**
* Create the supported translate string.
* @param {Int} x coordinate
* @param {Int} y coordinate
* @return {String}
*/
function translateString(x, y){
return has3d
? 'translate3d('+ x +'px, '+ y +'px, 0)'
: 'translate('+ x +'px, '+ y + 'px)';
}
/**
* Bootstrap-style API that allows designers to invoke zooming
* the element using markup
*/
var zoomListener = delegate.bind(document, '[data-zoom-url]', 'click', function(e){
new Zoom(e.target).show();
});
/**
* Javascript API.
* @param {Element} el
* @param {String} url
* @return {Zoom}
*/
module.exports = exports = Zoom;
/**
* Zoom Constructor
* @param {Element} el
* @param {String} url
*/
function Zoom(el, url){
if (!(this instanceof Zoom)) return new Zoom(el, url);
this.thumb = el;
if (this.thumb.getAttribute('data-zoom-overlay')) this.overlay();
this.padding();
this.backgroundURL = url;
this.viewport = {};
}
emitter(Zoom.prototype);
/**
* Enable overlay.
* @return {Zoom}
*/
Zoom.prototype.overlay = function(){
this._overlay = new Overlay('image-zoom-overlay');
return this;
};
/**
* Set padding (or should this be margin?) around the zoomed
* image.
*
* @param {Number} num in pixels
* @return {Zoom}
*/
Zoom.prototype.padding = function(num){
this._padding = num || this.thumb.getAttribute('data-zoom-padding') || 0;
return this;
};
/**
* While our image is loading, we add a loading
* class to our target element.
*
* @param {Function} fn
*/
Zoom.prototype.loadImage = function(fn){
if (this.hasLoaded) return fn();
var img = this.clone = new Image();
this.loaderTimer = setTimeout(function(){
if (!this.hasLoaded) this.loading();
}.bind(this), 50);
this.clone.onload = function(){
this.hasLoaded = true;
this.finishLoading();
this.imageWidth = img.width;
this.imageHeight = img.height;
fn();
}.bind(this);
img.src = this.src;
};
/**
* Add loading class
*
* @return {Zoom}
*/
Zoom.prototype.loading = function(){
this.emit('loading', this.thumb);
this.thumb.classList.add('loading');
return this;
};
/**
* Remove loading class
*
* @return {Zoom}
*/
Zoom.prototype.finishLoading = function(){
this.emit('end-loading', this.thumb);
window.clearTimeout(this.loaderTimer);
this.thumb.classList.remove('loading');
return this;
};
/**
* Get image dimensions
*
* @return {Zoom}
*/
Zoom.prototype.getDimensions = function(){
var pos = this.thumb.getBoundingClientRect();
this.origin = {
x : pos.left,
y : pos.top,
w : this.thumb.clientWidth,
h : this.thumb.clientHeight
};
this.src = this.thumb.getAttribute('data-zoom-url')
|| this.backgroundURL
|| this.thumb.src;
return this;
};
/**
* Append a clone of the image to the DOM in
* prep for our zoom animation.
*
* @return {Zoom}
*/
Zoom.prototype.appendClone = function(){
this.clone.classList.add('zoom-image-clone');
this.windowEvents = events(window, this);
this.windowEvents.bind('resize');
document.body.appendChild(this.clone);
return this;
};
/**
* On resize handler - recalc position of zoomed
* image.
*/
Zoom.prototype.onresize = function(){
this.determineZoomedSize();
this.updateStyles();
};
/**
* Determine size of zoomed image
*
* @return {Zoom}
*/
Zoom.prototype.determineZoomedSize = function(){
// image size
var iw = this.imageWidth;
var ih = this.imageHeight;
// viewport size
var vp = viewport();
// zoomed image max size
var target = scale(iw, ih, vp.width - this._padding, vp.height - this._padding);
// determine left & top position of zoomed image
var left = (vp.width / 2) - (target.width / 2);
var top = (vp.height / 2) - (target.height / 2);
this.target = {
x : left,
y: top,
w: target.width,
h: target.height
};
return this;
};
/**
* Yodate zoom styles
*
* @return {Zoom}
*/
Zoom.prototype.updateStyles = function(){
var t = this.target;
var s = this.clone.style;
s.width = t.w + 'px';
s.height = t.h + 'px';
s.left = t.x + 'px';
s.top = t.y + 'px';
this.emit('position updated', t);
return this;
};
/**
* Set original dimensions
*
* @return {Zoom}
*/
Zoom.prototype.setOriginalDeminsions = function(){
var o = this.origin;
var t = this.target;
this.updateStyles();
if (transform){
var scale = o.w / t.w;
var translateX = (o.x + (o.w / 2)) - (t.x + (t.w / 2));
var translateY = (o.y + (o.h / 2)) - (t.y + (t.h / 2));
var translate3d = translateString(translateX, translateY);
var scaleProp = ' scale('+ scale +')';
this.clone.style[transform] = translate3d + scaleProp;
}
return this;
};
/**
* Set transition Position
* @return {Zoom}
*/
Zoom.prototype.setTargetPosition = function(){
if (transform){
this.clone.style[transform] = translateString(0, 0) + ' scale(1)';
}
return this;
};
/**
* Show our zoomed image
*
* @param {Event} e
*/
Zoom.prototype.show = function(e){
if (e) e.preventDefault();
if (this.isZoomed || this.isShowing) return;
this.getDimensions();
this.cancelZoom = false;
this.isShowing = true;
function onImageLoad() {
if (this.cancelZoom) return;
this.emit('showing');
if (this._overlay) this._overlay.show();
this.determineZoomedSize()
.setOriginalDeminsions()
.appendClone();
this.thumb.style.opacity = 0;
redraw(this.clone);
this.setTargetPosition();
this.isZoomed = true;
afterTransition.once(this.clone, function(){
this.emit('shown');
}.bind(this));
}
// bind these events before the image has loaded, so that
// it in effect 'cancels' the load if the user clicks
// outside the image while it is loading
nextTick(function(){
this.docEvents = events(document, this);
this.docEvents.bind('touchstart', 'hide');
this.docEvents.bind('click', 'hide');
}.bind(this));
this.loadImage(onImageLoad.bind(this));
return this;
};
/**
* Hide our zoomed image
* @param {Event} e
* @return {Zoom}
*/
Zoom.prototype.hide = function(){
this.isShowing = false;
// cancel a loading state
if (!this.isZoomed) {
this.cancelZoom = true;
if (this.docEvents) this.docEvents.unbind();
this.emit('cancel');
this.finishLoading();
if (this._overlay) this._overlay.hide();
this.emit('hiding');
this.emit('hidden');
this.isZoomed = false;
return;
}
// hide our zoomed image
this.windowEvents.unbind();
this.docEvents.unbind();
this.setOriginalDeminsions();
this.emit('hiding');
if (this._overlay) this._overlay.hide();
this.isZoomed = false;
afterTransition.once(this.clone, function(){
this.thumb.style.opacity = 1;
this.clone.parentNode.removeChild(this.clone);
this.emit('hidden');
}.bind(this));
return this;
};
/**
* Enable plugin usage
*
* @param {function} plugin
* @param {Object} options
* @return {Zoom}
*/
Zoom.prototype.use = function(plugin, options){
plugin(this, options);
return this;
};
/**
* Unbind our event listener
*/
exports.stopListening = function(){
delegate.unbind(document, 'click', zoomListener, false);
};
}, {"emitter":2,"transform-property":3,"redraw":4,"after-transition":5,"scale-to-bounds":6,"viewport":7,"has-translate3d":8,"overlay":9,"delegate":10,"events":11,"next-tick":12}],
2: [function(_require, module, exports) {
/**
* Expose `Emitter`.
*/
module.exports = Emitter;
/**
* Initialize a new `Emitter`.
*
* @api public
*/
function Emitter(obj) {
if (obj) return mixin(obj);
};
/**
* Mixin the emitter properties.
*
* @param {Object} obj
* @return {Object}
* @api private
*/
function mixin(obj) {
for (var key in Emitter.prototype) {
obj[key] = Emitter.prototype[key];
}
return obj;
}
/**
* Listen on the given `event` with `fn`.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.on =
Emitter.prototype.addEventListener = function(event, fn){
this._callbacks = this._callbacks || {};
(this._callbacks[event] = this._callbacks[event] || [])
.push(fn);
return this;
};
/**
* Adds an `event` listener that will be invoked a single
* time then automatically removed.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.once = function(event, fn){
var self = this;
this._callbacks = this._callbacks || {};
function on() {
self.off(event, on);
fn.apply(this, arguments);
}
on.fn = fn;
this.on(event, on);
return this;
};
/**
* Remove the given callback for `event` or all
* registered callbacks.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners =
Emitter.prototype.removeEventListener = function(event, fn){
this._callbacks = this._callbacks || {};
// all
if (0 == arguments.length) {
this._callbacks = {};
return this;
}
// specific event
var callbacks = this._callbacks[event];
if (!callbacks) return this;
// remove all handlers
if (1 == arguments.length) {
delete this._callbacks[event];
return this;
}
// remove specific handler
var cb;
for (var i = 0; i < callbacks.length; i++) {
cb = callbacks[i];
if (cb === fn || cb.fn === fn) {
callbacks.splice(i, 1);
break;
}
}
return this;
};
/**
* Emit `event` with the given args.
*
* @param {String} event
* @param {Mixed} ...
* @return {Emitter}
*/
Emitter.prototype.emit = function(event){
this._callbacks = this._callbacks || {};
var args = [].slice.call(arguments, 1)
, callbacks = this._callbacks[event];
if (callbacks) {
callbacks = callbacks.slice(0);
for (var i = 0, len = callbacks.length; i < len; ++i) {
callbacks[i].apply(this, args);
}
}
return this;
};
/**
* Return array of callbacks for `event`.
*
* @param {String} event
* @return {Array}
* @api public
*/
Emitter.prototype.listeners = function(event){
this._callbacks = this._callbacks || {};
return this._callbacks[event] || [];
};
/**
* Check if this emitter has `event` handlers.
*
* @param {String} event
* @return {Boolean}
* @api public
*/
Emitter.prototype.hasListeners = function(event){
return !! this.listeners(event).length;
};
}, {}],
3: [function(_require, module, exports) {
var styles = [
'webkitTransform',
'MozTransform',
'msTransform',
'OTransform',
'transform'
];
var el = document.createElement('p');
var style;
for (var i = 0; i < styles.length; i++) {
style = styles[i];
if (null != el.style[style]) {
module.exports = style;
break;
}
}
}, {}],
4: [function(_require, module, exports) {
/**
* Expose `redraw`.
*/
module.exports = redraw;
/**
* Force a redraw on an `el`.
*
* @param {Element} el
*/
function redraw (el) {
el.offsetHeight;
}
}, {}],
5: [function(_require, module, exports) {
var hasTransitions = _require('has-transitions');
var emitter = _require('css-emitter');
function afterTransition(el, callback) {
if(hasTransitions(el)) {
return emitter(el).bind(callback);
}
return callback.apply(el);
};
afterTransition.once = function(el, callback) {
afterTransition(el, function fn(){
callback.apply(el);
emitter(el).unbind(fn);
});
};
module.exports = afterTransition;
}, {"has-transitions":13,"css-emitter":14}],
13: [function(_require, module, exports) {
/**
* This will store the property that the current
* browser uses for transitionDuration
*/
var property;
/**
* The properties we'll check on an element
* to determine if it actually has transitions
* We use duration as this is the only property
* needed to technically have transitions
* @type {Array}
*/
var types = [
"transitionDuration",
"MozTransitionDuration",
"webkitTransitionDuration"
];
/**
* Determine the correct property for this browser
* just once so we done need to check every time
*/
while(types.length) {
var type = types.shift();
if(type in document.body.style) {
property = type;
}
}
/**
* Determine if the browser supports transitions or
* if an element has transitions at all.
* @param {Element} el Optional. Returns browser support if not included
* @return {Boolean}
*/
function hasTransitions(el){
if(!property) {
return false; // No browser support for transitions
}
if(!el) {
return property != null; // We just want to know if browsers support it
}
var duration = getComputedStyle(el)[property];
return duration !== "" && parseFloat(duration) !== 0; // Does this element have transitions?
}
module.exports = hasTransitions;
}, {}],
14: [function(_require, module, exports) {
/**
* Module Dependencies
*/
var events = _require('event');
// CSS events
var watch = [
'transitionend'
, 'webkitTransitionEnd'
, 'oTransitionEnd'
, 'MSTransitionEnd'
, 'animationend'
, 'webkitAnimationEnd'
, 'oAnimationEnd'
, 'MSAnimationEnd'
];
/**
* Expose `CSSnext`
*/
module.exports = CssEmitter;
/**
* Initialize a new `CssEmitter`
*
*/
function CssEmitter(element){
if (!(this instanceof CssEmitter)) return new CssEmitter(element);
this.el = element;
}
/**
* Bind CSS events.
*
* @api public
*/
CssEmitter.prototype.bind = function(fn){
for (var i=0; i < watch.length; i++) {
events.bind(this.el, watch[i], fn);
}
return this;
};
/**
* Unbind CSS events
*
* @api public
*/
CssEmitter.prototype.unbind = function(fn){
for (var i=0; i < watch.length; i++) {
events.unbind(this.el, watch[i], fn);
}
return this;
};
/**
* Fire callback only once
*
* @api public
*/
CssEmitter.prototype.once = function(fn){
var self = this;
function on(){
self.unbind(on);
fn.apply(self.el, arguments);
}
self.bind(on);
return this;
};
}, {"event":15}],
15: [function(_require, module, exports) {
var bind = window.addEventListener ? 'addEventListener' : 'attachEvent',
unbind = window.removeEventListener ? 'removeEventListener' : 'detachEvent',
prefix = bind !== 'addEventListener' ? 'on' : '';
/**
* Bind `el` event `type` to `fn`.
*
* @param {Element} el
* @param {String} type
* @param {Function} fn
* @param {Boolean} capture
* @return {Function}
* @api public
*/
exports.bind = function(el, type, fn, capture){
el[bind](prefix + type, fn, capture || false);
return fn;
};
/**
* Unbind `el` event `type`'s callback `fn`.
*
* @param {Element} el
* @param {String} type
* @param {Function} fn
* @param {Boolean} capture
* @return {Function}
* @api public
*/
exports.unbind = function(el, type, fn, capture){
el[unbind](prefix + type, fn, capture || false);
return fn;
};
}, {}],
6: [function(_require, module, exports) {
/**
* Return the maximum size given a set of bounds
* while maintaining the original aspect ratio.
* @param {Number} ow original width
* @param {Number} oh original height
* @param {Number} mw max width
* @param {Number} mh max height
* @return {Object}
*/
module.exports = function(ow, oh, mw, mh){
var scale = Math.min(mw / ow, mh / oh);
if (scale > 1) scale = 1;
return {
width : ow * scale,
height : oh * scale
};
};
}, {}],
7: [function(_require, module, exports) {
/**
* get the current viewport size
* credit goes here: http://stackoverflow.com/a/11744120/1198166
* @return {Object} containing width and height
*/
module.exports = function(){
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0];
return {
width: w.innerWidth || e.clientWidth || g.clientWidth,
height: w.innerHeight|| e.clientHeight|| g.clientHeight
};
}
}, {}],
8: [function(_require, module, exports) {
var prop = _require('transform-property');
// IE <=8 doesn't have `getComputedStyle`
if (!prop || !window.getComputedStyle) {
module.exports = false;
} else {
var map = {
webkitTransform: '-webkit-transform',
OTransform: '-o-transform',
msTransform: '-ms-transform',
MozTransform: '-moz-transform',
transform: 'transform'
};
// from: https://gist.github.com/lorenzopolidori/3794226
var el = document.createElement('div');
el.style[prop] = 'translate3d(1px,1px,1px)';
document.body.insertBefore(el, null);
var val = getComputedStyle(el).getPropertyValue(map[prop]);
document.body.removeChild(el);
module.exports = null != val && val.length && 'none' != val;
}
}, {"transform-property":3}],
9: [function(_require, module, exports) {
/**
* Module dependencies.
*/
var emitter = _require('emitter');
var redraw = _require('redraw');
var afterTransition = _require('after-transition');
/**
* Expose `Overlay`.
*/
module.exports = Overlay;
/**
* Initialize a new `Overlay`.
*
* @param {Object} options
* @api public
*/
function Overlay(className) {
if (!(this instanceof Overlay)) return new Overlay();
this.el = document.createElement('div');
this.el.className = 'Overlay';
if (className) {
this.el.classList.add(className);
}
}
/**
* Mixin 'Emitter'
*/
emitter(Overlay.prototype);
/**
* Show the overlay.
*
* Emits "show" event.
*
* @return {Overlay}
* @api public
*/
Overlay.prototype.show = function(){
document.body.appendChild(this.el);
this.emit('show');
redraw(this.el);
afterTransition.once(this.el, function(){
this.emit('shown');
}.bind(this));
this.el.classList.add('show');
return this;
};
/**
* Hide the overlay.
*
* Emits "hide" event, and "hidden" when finished.
*
* @return {Overlay}
* @api public
*/
Overlay.prototype.hide = function(){
if (!this.el) return;
this.emit('hide');
afterTransition.once(this.el, function(){
this.emit('hidden');
this.el.parentNode.removeChild(this.el);
}.bind(this));
this.el.classList.remove('show');
return this;
};