/*
bttrlazyloading, responsive lazy loading plugin for jquery
by julien renaux http://bttrlazyloading.julienrenaux.fr
version: 1.0.8
full source at https://github.com/shprink/bttrlazyloading
mit license, https://github.com/shprink/bttrlazyloading/blob/master/license
*/
(function() {
"use strict";
var $, bttrlazyloading, bttrlazyloadingglobal,
__hasprop = {}.hasownproperty;
$ = jquery;
bttrlazyloading = (function() {
var _getimagesrc, _getimgobject, _getimgobjectperrange, _getlargestimgobject, _getrangefromscreensize, _isupdatable, _iswithinviewport, _setoptionsfromdata, _setupevents, _update, _updatecanvassize;
bttrlazyloading.dpr = 1;
function bttrlazyloading(img, options) {
var defaultoptions;
if (options == null) {
options = {};
}
this.$img = $(img);
this.loaded = false;
this.loading = false;
defaultoptions = $.extend(true, {}, $.bttrlazyloading.constructor.options);
this.options = $.extend(true, defaultoptions, options);
this.breakpoints = $.bttrlazyloading.constructor.breakpoints;
this.$container = $(this.options.container);
if (typeof window.devicepixelratio === 'number') {
this.constructor.dpr = window.devicepixelratio;
}
this.whitelist = ['lg', 'md', 'sm', 'xs'];
this.blacklist = [];
_setoptionsfromdata.call(this);
this.$wrapper = $('');
if (this.options.wrapperclasses && typeof this.options.wrapperclasses === 'string') {
this.$wrapper.addclass(this.options.wrapperclasses);
}
this.$img.before(this.$wrapper);
this.$clone = $('');
_updatecanvassize.call(this);
this.$wrapper.append(this.$clone);
this.$img.hide();
this.$wrapper.append(this.$img);
if (this.options.backgroundcolor) {
this.$wrapper.css('background-color', this.options.backgroundcolor);
}
_setupevents.call(this, 'on');
settimeout((function(_this) {
return function() {
return _update.call(_this);
};
})(this), 100);
}
/*
private functions
*/
_updatecanvassize = function() {
var imgobject;
imgobject = _getimgobject.call(this);
this.$clone.attr('width', imgobject.width);
return this.$clone.attr('height', imgobject.height);
};
_setoptionsfromdata = function() {
var i, v, _ref, _results;
_ref = this.$img.data();
_results = [];
for (i in _ref) {
if (!__hasprop.call(_ref, i)) continue;
v = _ref[i];
if ((v != null) && i.indexof('bttrlazyloading') !== 0) {
continue;
}
i = i.replace('bttrlazyloading', '').replace(/([a-z])([a-z])/g, '$1-$2').tolowercase().split('-');
if (i.length > 1) {
if (typeof this.options[i[0]][i[1]] !== 'undefined') {
_results.push(this.options[i[0]][i[1]] = v);
} else {
_results.push(void 0);
}
} else {
if (typeof v === 'object') {
_results.push($.extend(this.options[i[0]], v));
} else {
if (typeof this.options[i[0]] !== 'undefined') {
_results.push(this.options[i[0]] = v);
} else {
_results.push(void 0);
}
}
}
}
return _results;
};
_setupevents = function(onoroff) {
var onbttrload, onerror, onload, update;
onload = (function(_this) {
return function() {
_this.$clone.hide();
_this.$img.show();
_this.$wrapper.addclass('bttrlazyloading-loaded');
if (_this.options.animation) {
_this.$img.addclass('animated ' + _this.options.animation);
}
_this.loaded = _this.$img.attr('src');
return _this.$img.trigger('bttrlazyloading.afterload');
};
})(this);
this.$img[onoroff]('load', onload);
onbttrload = (function(_this) {
return function(e) {
var imgobject;
if (!_this.loading) {
_this.loading = true;
imgobject = _getimgobject.call(_this);
if (!_this.loaded) {
_this.$wrapper.css('background-image', "url('" + _this.options.placeholder + "')");
} else {
_this.$wrapper.removeclass('bttrlazyloading-loaded');
if (_this.options.animation) {
_this.$img.removeclass('animated ' + _this.options.animation);
}
_this.$img.removeattr('src');
_this.$img.hide();
_this.$clone.attr('width', imgobject.width);
_this.$clone.attr('height', imgobject.height);
_this.$clone.show();
}
return settimeout(function() {
_this.$img.trigger('bttrlazyloading.beforeload');
_this.$img.data('bttrlazyloading.range', imgobject.range);
_this.$img.attr('src', _getimagesrc.call(_this, imgobject.src, imgobject.range));
return _this.loading = false;
}, _this.options.delay);
}
};
})(this);
this.$img[onoroff]('bttrlazyloading.load', onbttrload);
onerror = (function(_this) {
return function(e) {
var range, src;
src = _this.$img.attr('src');
range = _this.$img.data('bttrlazyloading.range');
if (_this.constructor.dpr >= 2 && _this.options.retina && src.match(/@2x/gi)) {
_this.blacklist.push(range + '@2x');
} else {
_this.blacklist.push(range);
_this.whitelist.splice(_this.whitelist.indexof(range), 1);
if (_this.whitelist.length === 0) {
_this.$img.trigger('bttrlazyloading.error');
return false;
}
}
return _this.$img.trigger('bttrlazyloading.load');
};
})(this);
this.$img[onoroff]('error', onerror);
update = (function(_this) {
return function(e) {
return _update.call(_this);
};
})(this);
this.$container[onoroff](this.options.event, update);
if (this.options.container !== window) {
$(window)[onoroff](this.options.event, update);
}
return $(window)[onoroff]("resize", update);
};
_getrangefromscreensize = function() {
var ww;
ww = window.innerwidth;
if (ww <= this.breakpoints.xs) {
return 'xs';
} else if ((this.breakpoints.sm <= ww && ww < this.breakpoints.md)) {
return 'sm';
} else if ((this.breakpoints.md <= ww && ww < this.breakpoints.lg)) {
return 'md';
} else if (this.breakpoints.lg <= ww) {
return 'lg';
}
};
_getimgobject = function() {
this.range = _getrangefromscreensize.call(this);
return _getlargestimgobject.call(this);
};
_getimagesrc = function(src, range) {
if (this.constructor.dpr >= 2 && this.options.retina && this.blacklist.indexof(range + '@2x') === -1) {
return src.replace(/\.\w+$/, function(match) {
return '@2x' + match;
});
} else {
return src;
}
};
_getimgobjectperrange = function(range) {
if (typeof this.options[range].src !== 'undefined' && this.options[range].src !== null) {
return this.options[range];
}
return null;
};
_getlargestimgobject = function() {
var index, range, src, _i, _len, _ref;
index = this.whitelist.indexof(this.range);
if (index > -1) {
src = _getimgobjectperrange.call(this, this.range);
if (src) {
src.range = this.range;
return src;
}
}
_ref = this.whitelist;
for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
range = _ref[index];
src = _getimgobjectperrange.call(this, range);
if (src) {
src.range = range;
return src;
}
}
return '';
};
_isupdatable = function() {
var imgobject, iswithinwindowviewport, threshold;
if (!this.loaded && this.options.triggermanually) {
return false;
}
if (this.loaded && this.options.updatemanually) {
return false;
}
imgobject = _getimgobject.call(this);
if (!imgobject.src || this.loaded === _getimagesrc.call(this, imgobject.src, imgobject.range)) {
return false;
}
threshold = 0;
if (!this.loaded) {
threshold = this.options.threshold;
}
iswithinwindowviewport = _iswithinviewport.call(this, $(window), {
top: $(window).scrolltop() + threshold,
left: $(window).scrollleft()
});
if (this.options.container !== window) {
return iswithinwindowviewport && _iswithinviewport.call(this, this.$container, {
top: this.$container.offset().top + threshold,
left: this.$container.offset().left
});
}
return iswithinwindowviewport;
};
_iswithinviewport = function($container, viewport) {
var bounds;
if (viewport == null) {
viewport = {};
}
viewport.right = viewport.left + $container.width();
viewport.bottom = viewport.top + $container.height();
bounds = this.$wrapper.offset();
bounds.right = bounds.left + this.$wrapper.outerwidth();
bounds.bottom = bounds.top + this.$wrapper.outerheight();
return !(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom);
};
_update = function() {
if (this.range !== _getrangefromscreensize.call(this)) {
_updatecanvassize.call(this);
}
if (_isupdatable.call(this)) {
return this.$img.trigger('bttrlazyloading.load');
}
};
/*
public functions
*/
bttrlazyloading.prototype.get$img = function() {
return this.$img;
};
bttrlazyloading.prototype.get$clone = function() {
return this.$clone;
};
bttrlazyloading.prototype.get$wrapper = function() {
return this.$wrapper;
};
bttrlazyloading.prototype.destroy = function() {
this.$wrapper.before(this.$img);
this.$wrapper.remove();
_setupevents.call(this, 'off');
this.$img.off('bttrlazyloading');
this.$wrapper.removeclass('bttrlazyloading-loaded');
if (this.options.animation) {
this.$img.removeclass('animated ' + this.options.animation);
}
this.$img.removedata('bttrlazyloading');
return this.$img;
};
return bttrlazyloading;
})();
$.fn.extend({
bttrlazyloading: function(options) {
return this.each(function() {
var $this, data;
$this = $(this);
data = $this.data('bttrlazyloading');
if (typeof data === 'undefined') {
data = new bttrlazyloading(this, options);
$this.data('bttrlazyloading', data);
}
if (typeof options === 'string' && typeof data[options] !== 'undefined') {
return data[options].call(data);
}
});
}
});
$.fn.bttrlazyloading.constructor = bttrlazyloading;
bttrlazyloadingglobal = (function() {
function bttrlazyloadingglobal() {}
bttrlazyloadingglobal.prototype.version = '1.0.8';
bttrlazyloadingglobal.breakpoints = {
xs: 767,
sm: 768,
md: 992,
lg: 1200
};
bttrlazyloadingglobal.options = {
xs: {
src: null,
width: 100,
height: 100,
animation: null
},
sm: {
src: null,
width: 100,
height: 100,
animation: null
},
md: {
src: null,
width: 100,
height: 100,
animation: null
},
lg: {
src: null,
width: 100,
height: 100,
animation: null
},
retina: false,
animation: 'bouncein',
delay: 0,
event: 'scroll',
container: window,
threshold: 0,
triggermanually: false,
updatemanually: false,
wrapperclasses: null,
backgroundcolor: '#eee',
placeholder: 'data:image/gif;base64,r0lgodlheaalapqaap/391tbw+bf3+da2vhq6l5dxvtbw3h2dq6qqpivldlmzhbvb4qhh7ovr5uymntoznnxcv1cxi2kiu7n5+xf3/fw8h58fojh4fbv78/jycg8vnzw1vps7aaaaaaaaaaaach/c05fvfndqvbfmi4waweaaaah/hpdcmvhdgvkihdpdgggywphegxvywquaw5mbwah+qqjcwaaacwaaaaaeaalaaaflsagjmrpnqsgculkaq5aeim4zdvw03ve27ifdgfkeye04kdidc5zrtykra2wqgah+qqjcwaaacwaaaaaeaalaaafjgbhgavgnqhphiervsdawqns0qen5+y967tylyicbye7eykyagah+qqjcwaaacwaaaaaeaalaaafniagjothlooijakiggxjpgkikmkbz7sn6ziawjcdwik9w/hisxgbzdhtubnomcjvcyoulk7ceaah+qqjcwaaacwaaaaaeaalaaafnsagjqqirrfuao3jngiksdhqpi8tz3v55zuadacdyiq+yrbh+hwpzjfzoqqaeavwi7oqnvihach5baklaaaalaaaaaaqaasaaauyiccozgme1rjy5krrk7hi0mjsvuxjtf3iol7tltsbzsnfuegjay3i5sgfy55kqdx1ggiaifkecqsaaaasaaaaabaacwaabtcgii5kaz4kcv2eqljipmnzhwgxaooitm2axq4g7p2ct2er4amul00kj5g0al8tady2y6c+4fiiach5baklaaaalaaaaaaqaasaaauviccozgme5errk6iy7qpyhcvsta3gna/7txxwlwv2issacyuc+l4tadqgq1mvpbaaifkecqsaaaasaaaaabaacwaabs8gii5kaz7krfgtqlluqnicjvk0dea1r/u3hhcxc/akxjpxhrz6xi0anazdwa+keaa7aaaaaaaaaaaa'
};
bttrlazyloadingglobal.prototype.setoptions = function(object) {
if (object == null) {
object = {};
}
$.extend(true, this.constructor.options, object);
return this;
};
bttrlazyloadingglobal.prototype.setranges = function(object) {
if (object == null) {
object = {};
}
$.extend(true, this.constructor.breakpoints, object);
return this;
};
bttrlazyloadingglobal.prototype.setbreakpoints = function(object) {
if (object == null) {
object = {};
}
$.extend(true, this.constructor.breakpoints, object);
return this;
};
return bttrlazyloadingglobal;
})();
$.bttrlazyloading = new bttrlazyloadingglobal();
}).call(this);