forked from i-like-robots/EasyZoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyzoom.js
304 lines (240 loc) · 7.51 KB
/
easyzoom.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
(function ($) {
'use strict';
var dw, dh, rw, rh, lx, ly;
var defaults = {
// The text to display within the notice box while loading the zoom image.
loadingNotice: 'Loading image',
// The text to display within the notice box if an error occurs loading the zoom image.
errorNotice: 'The image could not be loaded',
// The time (in milliseconds) to display the error notice.
errorDuration: 2500,
// Prevent clicks on the zoom image link.
preventClicks: true,
// Callback function to execute when the flyout is displayed.
onShow: undefined,
// Callback function to execute when the flyout is removed.
onHide: undefined
};
/**
* EasyZoom
* @constructor
* @param {Object} target
* @param {Object} options
*/
function EasyZoom(target, options) {
this.$target = $(target);
this.opts = $.extend({}, defaults, options);
if ( this.isOpen === undefined ) {
this._init();
}
return this;
}
/**
* Init
* @private
*/
EasyZoom.prototype._init = function() {
var self = this;
this.$link = this.$target.find('a');
this.$image = this.$target.find('img');
this.$flyout = $('<div class="easyzoom-flyout" />');
this.$notice = $('<div class="easyzoom-notice" />');
this.$target
.on('mouseenter.easyzoom touchstart.easyzoom', function(e) {
self.isMouseOver = true;
if ( ! e.originalEvent.touches || e.originalEvent.touches.length === 1) {
e.preventDefault();
self.show(e, true);
}
})
.on('mousemove.easyzoom touchmove.easyzoom', function(e) {
if (self.isOpen) {
e.preventDefault();
self._move(e);
}
})
.on('mouseleave.easyzoom touchend.easyzoom', function() {
self.isMouseOver = false;
if (self.isOpen) {
self.hide();
}
});
if (this.opts.preventClicks) {
this.$target.on('click.easyzoom', 'a', function(e) {
e.preventDefault();
});
}
};
/**
* Show
* @param {MouseEvent|TouchEvent} e
* @param {Boolean} testMouseOver
*/
EasyZoom.prototype.show = function(e, testMouseOver) {
var w1, h1, w2, h2;
var self = this;
if (! this.isReady) {
this._load(this.$link.attr('href'), function() {
if (!testMouseOver || self.isMouseOver) {
self.show(e);
}
});
return;
}
this.$target.append(this.$flyout);
w1 = this.$target.width();
h1 = this.$target.height();
w2 = this.$flyout.width();
h2 = this.$flyout.height();
dw = this.$zoom.width() - w2;
dh = this.$zoom.height() - h2;
rw = dw / w1;
rh = dh / h1;
this.isOpen = true;
if (this.opts.onShow) {
this.opts.onShow.call(this);
}
if (e) {
this._move(e);
}
};
/**
* Load
* @private
* @param {String} href
* @param {Function} callback
*/
EasyZoom.prototype._load = function(href, callback) {
var zoom = new Image();
this.$target.addClass('is-loading').append(this.$notice.text(this.opts.loadingNotice));
this.$zoom = $(zoom);
zoom.onerror = $.proxy(function() {
var self = this;
this.$notice.text(this.opts.errorNotice);
this.$target.removeClass('is-loading').addClass('is-error');
this.detachNotice = setTimeout(function() {
self.$notice.detach();
self.detachNotice = null;
}, this.opts.errorDuration);
}, this);
zoom.onload = $.proxy(function() {
// IE may fire a load event even on error so check the image has dimensions
if (zoom.width === 0) {
return;
}
this.isReady = true;
this.$notice.detach();
this.$flyout.html(this.$zoom);
this.$target.removeClass('is-loading').addClass('is-ready');
callback();
}, this);
zoom.style.position = 'absolute';
zoom.src = href;
};
/**
* Move
* @private
* @param {Event} e
*/
EasyZoom.prototype._move = function(e) {
if (e.type.indexOf('touch') === 0) {
var touchlist = e.touches || e.originalEvent.touches;
lx = touchlist[0].pageX;
ly = touchlist[0].pageY;
}
else {
lx = e.pageX || lx;
ly = e.pageY || ly;
}
var offset = this.$target.offset();
var pt = ly - offset.top;
var pl = lx - offset.left;
var xt = Math.floor(pt * rh);
var xl = Math.floor(pl * rw);
// Close if outside
if (xl < 0 || xt < 0 || xl > dw || xt > dh) {
this.hide();
}
else {
this.$zoom.css({
top: '' + (xt * -1) + 'px',
left: '' + (xl * -1) + 'px'
});
}
};
/**
* Hide
*/
EasyZoom.prototype.hide = function() {
if (this.isOpen) {
this.$flyout.detach();
this.isOpen = false;
if (this.opts.onHide) {
this.opts.onHide.call(this);
}
}
};
/**
* Swap
* @param {String} standardSrc
* @param {String} zoomHref
* @param {String|Array} srcsetStringOrArray (Optional)
*/
EasyZoom.prototype.swap = function(standardSrc, zoomHref, srcsetStringOrArray) {
this.hide();
this.isReady = false;
if (this.detachNotice) {
clearTimeout(this.detachNotice);
}
if (this.$notice.parent().length) {
this.$notice.detach();
}
if ($.isArray(srcsetStringOrArray)) {
srcsetStringOrArray = srcsetStringOrArray.join();
}
this.$target.removeClass('is-loading is-ready is-error');
this.$image.attr({
src: standardSrc,
srcset: srcsetStringOrArray
});
this.$link.attr('href', zoomHref);
};
/**
* Teardown
*/
EasyZoom.prototype.teardown = function() {
this.hide();
this.$target.removeClass('is-loading is-ready is-error').off('.easyzoom');
if (this.detachNotice) {
clearTimeout(this.detachNotice);
}
delete this.$link;
delete this.$zoom;
delete this.$image;
delete this.$notice;
delete this.$flyout;
delete this.isOpen;
delete this.isReady;
};
// jQuery plugin wrapper
$.fn.easyZoom = function( options ) {
return this.each(function() {
var api = $.data(this, 'easyZoom');
if ( ! api) {
$.data(this, 'easyZoom', new EasyZoom(this, options));
}
else if ( api.isOpen === undefined ) {
api._init();
}
});
};
// AMD and CommonJS module compatibility
if ( typeof define === 'function' && define.amd ){
define(function() {
return EasyZoom;
});
}
else if ( typeof module !== 'undefined' && module.exports ) {
module.exports = EasyZoom;
}
})(jQuery);