-
Notifications
You must be signed in to change notification settings - Fork 2
/
voila.pkgd.js
419 lines (341 loc) · 9.53 KB
/
voila.pkgd.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
/*!
* Voilà - v1.5.2
* (c) 2015 Nick Stakenburg
*
* http://voila.nickstakenburg.com
*
* MIT License
*/
// UMD wrapper
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser global
root.Voila = factory(jQuery);
}
}(this, function($) {
function Voila(elements, opts, cb) {
if (!(this instanceof Voila)) {
return new Voila(elements, opts, cb);
}
var argTypeOne = $.type(arguments[1]),
options = (argTypeOne === 'object' ? arguments[1] : {}),
callback = argTypeOne === 'function' ? arguments[1] :
$.type(arguments[2]) === 'function' ? arguments[2] : false;
this.options = $.extend({
method: 'onload'
}, options);
this.deferred = new jQuery.Deferred();
// if there's a callback, push it onto the stack
if (callback) {
this.always(callback);
}
this._processed = 0;
this.images = [];
this._add(elements);
return this;
}
$.extend(Voila.prototype, {
_add: function(elements) {
// normalize to an array
var array = $.type(elements) == 'string' ? $(elements) : // selector
(elements instanceof jQuery) || elements.length > 0 ? elements : // jQuery obj, Array
[elements]; // element node
// subtract the images
$.each(array, $.proxy(function(i, element) {
var images = $(),
$element = $(element);
// single image
if ($element.is('img')) {
images = images.add($element);
} else {
// nested
images = images.add($element.find('img'));
}
images.each($.proxy(function(i, element) {
this.images.push(new ImageReady(element,
// success
$.proxy(function(image) {
this._progress(image);
}, this),
// error
$.proxy(function(image) {
this._progress(image);
}, this),
// options
this.options
));
}, this));
}, this));
// no images found
if (this.images.length < 1) {
setTimeout($.proxy(function() {
this._resolve();
}, this));
}
},
abort: function() {
// clear callbacks
this._progress = this._notify = this._reject = this._resolve = function() { };
// clear images
$.each(this.images, function(i, image) {
image.abort();
});
this.images = [];
},
_progress: function(image) {
this._processed++;
// when a broken image passes by keep track of it
if (!image.isLoaded) this._broken = true;
this._notify(image);
// completed
if (this._processed == this.images.length) {
this[this._broken ? '_reject' : '_resolve']();
}
},
_notify: function(image) { this.deferred.notify(this, image); },
_reject: function() { this.deferred.reject(this); },
_resolve: function() { this.deferred.resolve(this); },
always: function(callback) {
this.deferred.always(callback);
return this;
},
done: function(callback) {
this.deferred.done(callback);
return this;
},
fail: function(callback) {
this.deferred.fail(callback);
return this;
},
progress: function(callback) {
this.deferred.progress(callback);
return this;
}
});
// extend jQuery
$.fn.voila = function() {
return Voila.apply(Voila, [this].concat(Array.prototype.slice.call(arguments)));
};
/* ImageReady (standalone) - part of Voilà
* http://voila.nickstakenburg.com
* MIT License
*/
var ImageReady = (function($) {
var Poll = function() {
return this.initialize.apply(this, Array.prototype.slice.call(arguments));
};
$.extend(Poll.prototype, {
initialize: function() {
this.options = $.extend({
test: function() {},
success: function() {},
timeout: function() {},
callAt: false,
intervals: [
[0, 0],
[1 * 1000, 10],
[2 * 1000, 50],
[4 * 1000, 100],
[20 * 1000, 500]
]
}, arguments[0] || {});
this._test = this.options.test;
this._success = this.options.success;
this._timeout = this.options.timeout;
this._ipos = 0;
this._time = 0;
this._delay = this.options.intervals[this._ipos][1];
this._callTimeouts = [];
this.poll();
this._createCallsAt();
},
poll: function() {
this._polling = setTimeout($.proxy(function() {
if (this._test()) {
this.success();
return;
}
// update time
this._time += this._delay;
// next i within the interval
if (this._time >= this.options.intervals[this._ipos][0]) {
// timeout when no next interval
if (!this.options.intervals[this._ipos + 1]) {
if ($.type(this._timeout) == 'function') {
this._timeout();
}
return;
}
this._ipos++;
// update to the new bracket
this._delay = this.options.intervals[this._ipos][1];
}
this.poll();
}, this), this._delay);
},
success: function() {
this.abort();
this._success();
},
_createCallsAt: function() {
if (!this.options.callAt) return;
// start a timer for each call
$.each(this.options.callAt, $.proxy(function(i, at) {
var time = at[0], fn = at[1];
var timeout = setTimeout($.proxy(function() {
fn();
}, this), time);
this._callTimeouts.push(timeout);
}, this));
},
_stopCallTimeouts: function() {
$.each(this._callTimeouts, function(i, timeout) {
clearTimeout(timeout);
});
this._callTimeouts = [];
},
abort: function() {
this._stopCallTimeouts();
if (this._polling) {
clearTimeout(this._polling);
this._polling = null;
}
}
});
var ImageReady = function() {
return this.initialize.apply(this, Array.prototype.slice.call(arguments));
};
$.extend(ImageReady.prototype, {
supports: {
naturalWidth: (function() {
return ('naturalWidth' in new Image());
})()
},
// NOTE: setTimeouts allow callbacks to be attached
initialize: function(img, successCallback, errorCallback) {
this.img = $(img)[0];
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.isLoaded = false;
this.options = $.extend({
method: 'onload',
pollFallbackAfter: 1000
}, arguments[3] || {});
// onload and a fallback for no naturalWidth support (IE6-7)
if (this.options.method == 'onload' || !this.supports.naturalWidth) {
this.load();
return;
}
// start polling
this.poll();
},
// NOTE: Polling for naturalWidth is only reliable if the
// <img>.src never changes. naturalWidth isn't always reset
// to 0 after the src changes (depending on how the spec
// was implemented). The spec even seems to be against
// this, making polling unreliable in those cases.
poll: function() {
this._poll = new Poll({
test: $.proxy(function() {
return this.img.naturalWidth > 0;
}, this),
success: $.proxy(function() {
this.success();
}, this),
timeout: $.proxy(function() {
// error on timeout
this.error();
}, this),
callAt: [
[this.options.pollFallbackAfter, $.proxy(function() {
this.load();
}, this)]
]
});
},
load: function() {
this._loading = setTimeout($.proxy(function() {
var image = new Image();
this._onloadImage = image;
image.onload = $.proxy(function() {
image.onload = function() {};
if (!this.supports.naturalWidth) {
this.img.naturalWidth = image.width;
this.img.naturalHeight = image.height;
image.naturalWidth = image.width;
image.naturalHeight = image.height;
}
this.success();
}, this);
image.onerror = $.proxy(this.error, this);
image.src = this.img.src;
}, this));
},
success: function() {
if (this._calledSuccess) return;
this._calledSuccess = true;
// stop loading/polling
this.abort();
// some time to allow layout updates, IE requires this!
this.waitForRender($.proxy(function() {
this.isLoaded = true;
this.successCallback(this);
}, this));
},
error: function() {
if (this._calledError) return;
this._calledError = true;
// stop loading/polling
this.abort();
// don't wait for an actual render on error, just timeout
// to give the browser some time to render a broken image icon
this._errorRenderTimeout = setTimeout($.proxy(function() {
if (this.errorCallback) this.errorCallback(this);
}, this));
},
abort: function() {
this.stopLoading();
this.stopPolling();
this.stopWaitingForRender();
},
stopPolling: function() {
if (this._poll) {
this._poll.abort();
this._poll = null;
}
},
stopLoading: function() {
if (this._loading) {
clearTimeout(this._loading);
this._loading = null;
}
if (this._onloadImage) {
this._onloadImage.onload = function() { };
this._onloadImage.onerror = function() { };
}
},
// used by success() only
waitForRender: function(callback) {
this._renderTimeout = setTimeout(callback);
},
stopWaitingForRender: function() {
if (this._renderTimeout) {
clearTimeout(this._renderTimeout);
this._renderTimeout = null;
}
if (this._errorRenderTimeout) {
clearTimeout(this._errorRenderTimeout);
this._errorRenderTimeout = null;
}
}
});
return ImageReady;
})(jQuery);
return Voila;
}));