-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mriv.min.js
3 lines (3 loc) · 2.42 KB
/
jquery.mriv.min.js
1
2
3
/**
* Matrix Responsive Image Varieties
*/(function(e,t,n){function r(e){this.options.set(e),(!e.hasOwnProperty("resize")||e.resize!==!1)&&this.bindResize(),this.outputs=[],this.lastVariety="",this.options.get().run&&this.run()}(function(t){function r(t,n,r,i){var s=e('<img src="'+n+'" alt="'+i+'" class="variety variety-'+r+'" />');return e(t).before(s),s}function i(e,t){var n=/\.(jpeg|gif|jpg|png)$/i,r=n.exec(e),i=new RegExp("(.*)__data/assets/image/[0-9]{4}/[0-9]{4}/","i"),s=i.exec(e),o=s!==null;if(r===null)return e;var u=r[1].toLowerCase();return r.length>2&&!o?e:o&&/\/varieties\//.exec(e)!==null?e:o?s[0]+"varieties/"+t+"."+u:e+"/"+t+"."+u}t.options={_opt:{},defaults:{output:{},tagName:"noscript",urlAttribute:"data-url",altAttribute:"data-alt",allowedNodes:"body",removeOriginal:!1,resize:!0,force:!1,run:!0,getWidth:function(){return e(window).width()}},set:function(t){this._opt=e.extend({},this.defaults,t)},get:function(e){return e===n||!this._opt.hasOwnProperty(e)?this._opt:this._opt[e]}};var s=null;t.bindResize=function(){var t=this;e(window).bind("resize.mriv",function(){clearTimeout(s),s=setTimeout(function(){t.run.call(t)},200)})},t.unbindResize=function(){e(window).unbind("resize.mriv")},t.scan=function(){var t=this.options.get(),n=e(t.tagName+"["+t.urlAttribute+"]",t.allowedNodes);return n.length?n:[]},t.each=function(e,t){t=t===n?this:t;var r=this.options.get(),i=[];for(var s in r.output)r.output.hasOwnProperty(s)&&i.push(e.call(t,s,r.output[s]));return i},t.findVariety=function(e){var t=null;return matches=this.each(function(t,n){var r=0,i=9001;n.hasOwnProperty("width")&&(n.width.hasOwnProperty("length")?(r=parseFloat(n.width[0]),i=n.width.length===2?parseFloat(n.width[1]):i):r=parseFloat(n.width));if(e>=r&&e<=i)return{name:t,output:n}}),matches.length?matches[0]:null},t.run=function(){var t=this.scan(),s=this.options.get(),o=this,u=this.findVariety(s.getWidth());if(!s.resize)return!1;if(this.lastVariety===u&&!s.force)return!1;this.lastVariety=u,jQuery.each(o.outputs,function(e,t){t&&t.remove()}),o.outputs=[],t.length&&(t.each(function(){var t=e(this),a=r,f="default",l=t.attr(s.altAttribute),c=t.attr(s.urlAttribute);u!==null&&u!==n&&(f=u.name,c=i(t.attr(s.urlAttribute),f),u.output.hasOwnProperty("outputter")&&typeof u.output.outputter=="function"&&(a=u.output.outputter));var h=a.call(o,t.get(0),c,f,l);o.outputs.push(h)}),s.removeOriginal&&t.remove())}})(r.prototype),t.MRIV=r})(jQuery,typeof window!="undefined"?window:this);