-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.imgutil.js
6 lines (6 loc) · 8.51 KB
/
jquery.imgutil.js
1
2
3
4
5
6
/*! jQuery.imgUtil (https://github.com/Takazudo/jQuery.imgUtil)
* lastupdate: 2014-12-09
* version: 0.7.0
* author: 'Takazudo' Takeshi Takatsudo <takazudo@gmail.com>
* License: MIT */
(function(){var t={}.hasOwnProperty,e=function(e,i){function r(){this.constructor=e}for(var n in i)t.call(i,n)&&(e[n]=i[n]);return r.prototype=i.prototype,e.prototype=new r,e.__super__=i.prototype,e};(function(t){var i;return i={},i.createCachedFunction=function(e){var i;return i={},function(r,n){return i[r]||(i[r]=t.Deferred(function(t){return e(t,r,n)}).promise()),i[r]}},i.loadImg=function(e){var i,r,n;return r=t.Deferred(),n=new Image,i=function(){return n.onload=n.onerror=null},n.onload=function(){return i(),r.resolve(t(n))},n.onerror=function(){return i(),r.reject(t(n))},n.src=e,r.promise()},function(){var e,r,n,h,o;return n={},e=null,r=function(){return t.Deferred(function(i){return t(function(){return e=t('<div id="calcNaturalWH-tempholder"></div>').css({position:"absolute",left:"-9999px",top:"-9999px"}),t("body").append(e),i.resolve()})}).promise()},h=function(t){return null==t.naturalWidth||0===t.naturalWidth||null==t.naturalHeight||0===t.naturalHeight?!1:!0},o=function(i,r){var h,o,g,c,u,a;return i=i.clone(),c=i[0],g=t.Deferred(),a={},i.css({width:"auto",height:"auto"}),h=t("<div></div>").append(i),e.append(h),o=0,u=function(){return a.width=c.naturalWidth||i.width(),a.height=c.naturalHeight||i.height(),o>10?(h.remove(),g.reject()):a.width&&a.height?(n[r],h.remove(),g.resolve(a)):(o++,setTimeout(function(){return u()},100))},u(),g.promise()},i.calcNaturalWH=i.createCachedFunction(function(t,e){return i.loadImg(e).then(function(i){var g,c;return g=i[0],h(g)?(c={width:g.naturalWidth,height:g.naturalHeight},n[e]=c,t.resolve(c,i)):r().done(function(){return o(i,e).then(function(e){return t.resolve(e,i)},function(){return t.reject()})})},function(){return t.reject()})})}(),i.calcStylesToBeContainedInRect=function(){var e;return e={imgWidth:null,imgHeight:null,rectWidth:null,rectHeight:null},function(i){var r,n;return r=t.extend({},e,i),n={},n.left=r.imgWidth<r.rectWidth?Math.floor((r.rectWidth-r.imgWidth)/2):0,n.top=r.imgHeight<r.rectHeight?Math.floor((r.rectHeight-r.imgHeight)/2):0,n}}(),i.calcRectContainImgWH=function(){var e,i;return i=function(t){return t.imgWidth=100*t.imgWidth,t.imgHeight=100*t.imgHeight,t},e={imgWidth:null,imgHeight:null,rectWidth:null,rectHeight:null,enlargeSmallImg:!0},function(r){var n,h,o;return n=t.extend({},e,r),n.enlargeSmallImg&&(n=i(n)),n.imgWidth<n.rectWidth&&n.imgHeight<n.rectHeight?{width:n.imgWidth,height:n.imgHeight}:(o=n.rectWidth/n.imgWidth,h=n.rectHeight/n.imgHeight,h>o?{width:n.rectWidth,height:Math.ceil(n.imgHeight*o)}:o>h?{width:Math.ceil(n.imgWidth*h),height:n.rectHeight}:o===h?{width:n.imgWidth*o,height:n.imgHeight*h}:void 0)}}(),i.calcStylesToCoverRect=function(){var e;return e={imgWidth:null,imgHeight:null,rectWidth:null,rectHeight:null},function(i){var r,n;return r=t.extend({},e,i),n={},n.left=r.imgWidth>r.rectWidth?-1*Math.floor((r.imgWidth-r.rectWidth)/2):0,n.top=r.imgHeight>r.rectHeight?-1*Math.floor((r.imgHeight-r.rectHeight)/2):0,n}}(),i.calcRectCoverImgWH=function(){var e;return e={imgWidth:null,imgHeight:null,rectWidth:null,rectHeight:null},function(i){var r,n,h,o;return r=t.extend({},e,i),o=function(){var t,e;return e=r.rectWidth/r.imgWidth,t=Math.floor(e*r.imgHeight),r.rectHeight>t?!1:{width:r.rectWidth,height:t}},h=function(){var t,e;return e=r.rectHeight/r.imgHeight,t=Math.floor(e*r.imgWidth),r.rectWidth>t?!1:{width:t,height:r.rectHeight}},n=o(),n===!1&&(n=h(),n===!1&&(n.width=r.rectWidth,n.height=r.rectHeight)),n}}(),i.calcEdgeFitImgWH=function(){var e;return e={edge:null,imgWidth:null,imgHeight:null,rectWidth:null,rectHeight:null},function(i){var r,n,h,o;switch(r=t.extend({},e,i),n={},r.edge){case"right":case"left":h=r.rectHeight/r.imgHeight,n.width=Math.round(r.imgWidth*h),n.height=r.rectHeight;break;case"top":case"bottom":o=r.rectWidth/r.imgWidth,n.width=r.rectWidth,n.height=Math.round(r.imgHeight*o)}switch(r.edge){case"left":n.top=0,n.right="auto",n.bottom="auto",n.left=0;break;case"right":n.top=0,n.right=0,n.bottom="auto",n.left="auto";break;case"top":n.top=0,n.right="auto",n.bottom="auto",n.left=0;break;case"bottom":n.top="auto",n.right="auto",n.bottom=0,n.left=0}return n}}(),i.AbstractImgRectFitter=function(){function e(){var t,e;e=this.$el.attr(this.options.attr_src),e&&(this.options.src=e),this.options.oninit&&(t={el:this.$el},this.options.oninit(t)),this._doFirstRefresh()}return e.prototype._doFirstRefresh=function(){var t=this;return this._stillLoadingImg=!0,this._calcNaturalImgWH().done(function(){return t._stillLoadingImg=!1,t.refresh()}),this},e.prototype._calcNaturalImgWH=function(){var e,r,n,h=this;return e=t.Deferred(),n=function(t,i){h.originalImgWidth=t.width,h.originalImgHeight=t.height,h.options.cloneImg&&(i=i.clone()),h.$img=i,e.resolve()},r=function(){h.options.onfail&&h.options.onfail(),e.reject()},i.calcNaturalWH(this.options.src).then(n,r),e.promise()},e.prototype._putImg=function(t){return this.options.overrideImgPut?this.options.overrideImgPut(this.$el,t):this.$el.empty().append(t),this},e.prototype._finalizeImg=function(t){var e,i;return this.options.useNewImgElOnRefresh?(e=this.$img.clone(),e.css(t),this._putImg(e)):(this.$img.css(t),i=this.$el.find("img"),0===i.length&&this._putImg(this.$img)),this},e}(),i.ImgCoverRect=function(r){function n(e,r){this.$el=e,this.options=t.extend({},i.ImgCoverRect.defaults,r),n.__super__.constructor.apply(this,arguments)}return e(n,r),n.defaults={src:null,oninit:null,onfail:null,cloneImg:!0,useNewImgElOnRefresh:!1,attr_src:"data-imgcoverrect-src",overrideImgPut:null},n.prototype.refresh=function(){var e,r,n;if(this._stillLoadingImg!==!0)return this.rectWidth=this.$el.width(),this.rectHeight=this.$el.height(),e=i.calcRectCoverImgWH({imgWidth:this.originalImgWidth,imgHeight:this.originalImgHeight,rectWidth:this.rectWidth,rectHeight:this.rectHeight}),n={width:e.width,height:e.height},r=i.calcStylesToCoverRect({imgWidth:e.width,imgHeight:e.height,rectWidth:this.rectWidth,rectHeight:this.rectHeight}),n=t.extend(n,r),this._finalizeImg(n),this},n}(i.AbstractImgRectFitter),function(){var e;return e="imgcoverrect",t.fn.imgCoverRect=function(r){return this.each(function(n,h){var o;return o=t(h),o.data(e,new i.ImgCoverRect(o,r))})},t.fn.refreshImgCoverRect=function(){return this.each(function(i,r){var n,h;return n=t(r),(h=n.data(e))?h.refresh():void 0})}}(),i.ImgContainRect=function(r){function n(e,r){this.$el=e,this.options=t.extend({},i.ImgContainRect.defaults,r),n.__super__.constructor.apply(this,arguments)}return e(n,r),n.defaults={src:null,oninit:null,onfail:null,cloneImg:!0,enlargeSmallImg:!0,useNewImgElOnRefresh:!1,attr_src:"data-imgcontainrect-src",overrideImgPut:null},n.prototype.refresh=function(){var e,r,n;if(this._stillLoadingImg!==!0)return this.rectWidth=this.$el.width(),this.rectHeight=this.$el.height(),e=i.calcRectContainImgWH({imgWidth:this.originalImgWidth,imgHeight:this.originalImgHeight,rectWidth:this.rectWidth,rectHeight:this.rectHeight}),n={width:e.width,height:e.height},r=i.calcStylesToBeContainedInRect({imgWidth:e.width,imgHeight:e.height,rectWidth:this.rectWidth,rectHeight:this.rectHeight}),n=t.extend(n,r),this._finalizeImg(n),this},n}(i.AbstractImgRectFitter),function(){var e;return e="imgcontainrect",t.fn.imgContainRect=function(r){return this.each(function(n,h){var o;return o=t(h),o.data(e,new i.ImgContainRect(o,r))})},t.fn.refreshImgContainRect=function(){return this.each(function(i,r){var n,h;return n=t(r),(h=n.data(e))?h.refresh():void 0})}}(),i.ImgFitToEdge=function(r){function n(e,r){this.$el=e,this.options=t.extend({},i.ImgFitToEdge.defaults,r),this.edge=this.$el.attr(this.options.attr_edge),n.__super__.constructor.apply(this,arguments)}return e(n,r),n.defaults={src:null,oninit:null,onfail:null,cloneImg:!0,enlargeSmallImg:!0,useNewImgElOnRefresh:!1,attr_src:"data-imgfittoedge-src",attr_edge:"data-imgfittoedge-edge",overrideImgPut:null},n.prototype.refresh=function(){var t;if(this._stillLoadingImg!==!0)return this.rectWidth=this.$el.width(),this.rectHeight=this.$el.height(),t=i.calcEdgeFitImgWH({edge:this.edge,imgWidth:this.originalImgWidth,imgHeight:this.originalImgHeight,rectWidth:this.rectWidth,rectHeight:this.rectHeight}),this._finalizeImg(t),this},n}(i.AbstractImgRectFitter),function(){var e;return e="imgfittoedge",t.fn.imgFitToEdge=function(r){return this.each(function(n,h){var o;return o=t(h),o.data(e,new i.ImgFitToEdge(o,r))})},t.fn.refreshImgFitToEdge=function(){return this.each(function(i,r){var n,h;return n=t(r),(h=n.data(e))?h.refresh():void 0})}}(),t.imgUtil=i})(jQuery,window,document)}).call(this);