diff --git a/build/srcset.js b/build/srcset.js index c716112..51df4ac 100644 --- a/build/srcset.js +++ b/build/srcset.js @@ -803,6 +803,15 @@ var jsUri = Uri; var readyTimer = setInterval(function () { if (document.readyState === "complete") { main(); + + var resizeTimer; + window.onresize = function(e) { + clearTimeout(resizeTimer); // some browsers file resize continuously, so wait 500ms + resizeTimer = setTimeout(function() { + main(); + }, 500); + }; + clearInterval(readyTimer); } }, 10); diff --git a/build/srcset.min.js b/build/srcset.min.js index 010fe61..0fb0718 100644 --- a/build/srcset.min.js +++ b/build/srcset.min.js @@ -1 +1 @@ -var Query=function(t){"use strict";var e=function(t){var e,r,n,i,s=[];if(t===void 0||null===t||""===t)return s;for(0===t.indexOf("?")&&(t=t.substring(1)),r=(""+t).split(/[&;]/),e=0;r.length>e;e++)n=r[e],i=n.split("="),s.push([i[0],i[1]]);return s},r=e(t),n=function(){var t,e,n="";for(t=0;r.length>t;t++)e=r[t],n.length>0&&(n+="&"),n+=e.join("=");return n.length>0?"?"+n:n},i=function(t){return t=decodeURIComponent(t),t=t.replace("+"," ")},s=function(t){var e,n;for(n=0;r.length>n;n++)if(e=r[n],i(t)===i(e[0]))return e[1]},o=function(t){var e,n,s=[];for(e=0;r.length>e;e++)n=r[e],i(t)===i(n[0])&&s.push(n[1]);return s},a=function(t,e){var n,s,o,a,u=[];for(n=0;r.length>n;n++)s=r[n],o=i(s[0])===i(t),a=i(s[1])===i(e),(1===arguments.length&&!o||2===arguments.length&&!o&&!a)&&u.push(s);return r=u,this},u=function(t,e,n){return 3===arguments.length&&-1!==n?(n=Math.min(n,r.length),r.splice(n,0,[t,e])):arguments.length>0&&r.push([t,e]),this},h=function(t,e,n){var s,o,h=-1;if(3===arguments.length){for(s=0;r.length>s;s++)if(o=r[s],i(o[0])===i(t)&&decodeURIComponent(o[1])===i(n)){h=s;break}a(t,n).addParam(t,e,h)}else{for(s=0;r.length>s;s++)if(o=r[s],i(o[0])===i(t)){h=s;break}a(t),u(t,e,h)}return this};return{getParamValue:s,getParamValues:o,deleteParam:a,addParam:u,replaceParam:h,toString:n}},Uri=function(t){"use strict";var e=!1,r=function(t){for(var r={strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/},n=["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],i={name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},s=r[e?"strict":"loose"].exec(t),o={},a=14;a--;)o[n[a]]=s[a]||"";return o[i.name]={},o[n[12]].replace(i.parser,function(t,e,r){e&&(o[i.name][e]=r)}),o},n=r(t||""),i=new Query(n.query),s=function(t){return t!==void 0&&(n.protocol=t),n.protocol},o=null,a=function(t){return t!==void 0&&(o=t),null===o?-1!==n.source.indexOf("//"):o},u=function(t){return t!==void 0&&(n.userInfo=t),n.userInfo},h=function(t){return t!==void 0&&(n.host=t),n.host},c=function(t){return t!==void 0&&(n.port=t),n.port},f=function(t){return t!==void 0&&(n.path=t),n.path},l=function(t){return t!==void 0&&(i=new Query(t)),i},d=function(t){return t!==void 0&&(n.anchor=t),n.anchor},g=function(t){return s(t),this},p=function(t){return a(t),this},m=function(t){return u(t),this},v=function(t){return h(t),this},w=function(t){return c(t),this},x=function(t){return f(t),this},y=function(t){return l(t),this},I=function(t){return d(t),this},C=function(t){return l().getParamValue(t)},P=function(t){return l().getParamValues(t)},_=function(t,e){return 2===arguments.length?l().deleteParam(t,e):l().deleteParam(t),this},V=function(t,e,r){return 3===arguments.length?l().addParam(t,e,r):l().addParam(t,e),this},B=function(t,e,r){return 3===arguments.length?l().replaceParam(t,e,r):l().replaceParam(t,e),this},O=function(){var t="",e=function(t){return null!==t&&""!==t};return e(s())?(t+=s(),s().indexOf(":")!==s().length-1&&(t+=":"),t+="//"):a()&&e(h())&&(t+="//"),e(u())&&e(h())&&(t+=u(),u().indexOf("@")!==u().length-1&&(t+="@")),e(h())&&(t+=h(),e(c())&&(t+=":"+c())),e(f())?t+=f():e(h())&&(e(""+l())||e(d()))&&(t+="/"),e(""+l())&&(0!==(""+l()).indexOf("?")&&(t+="?"),t+=""+l()),e(d())&&(0!==d().indexOf("#")&&(t+="#"),t+=d()),t},Q=function(){return new Uri(O())};return{protocol:s,hasAuthorityPrefix:a,userInfo:u,host:h,port:c,path:f,query:l,anchor:d,setProtocol:g,setHasAuthorityPrefix:p,setUserInfo:m,setHost:v,setPort:w,setPath:x,setQuery:y,setAnchor:I,getQueryParamValue:C,getQueryParamValues:P,deleteQueryParam:_,addQueryParam:V,replaceQueryParam:B,toString:O,clone:Q}},jsUri=Uri;(function(t){function e(t){this.imageCandidates=[],this.srcValue=t.src,this.srcsetValue=t.srcset,this.isValid=!0,this.error="",this._parse(this.srcsetValue),this.isValid||console.error("Error: "+this.error)}function r(t){this.src=t.src,this.w=t.w||1/0,this.h=t.h||1/0,this.x=t.x||1}var n=/^[0-9]+$/;e.prototype._parse=function(){for(var t,e,n=this.srcsetValue,i=0,s=[];""!==n;){for(;" "===n.charAt(0);)n=n.slice(1);if(i=n.indexOf(" "),-1!==i){if(t=n.slice(0,i),""===t)break;n=n.slice(i+1),i=n.indexOf(","),-1===i?(e=n,n=""):(e=n.slice(0,i),n=n.slice(i+1)),s.push({url:t,descriptors:e})}else s.push({url:n,descriptors:""}),n=""}for(var o=0,a=s.length;a>o;o++){var u=s[o],h=this._parseDescriptors(u.descriptors);this._addCandidate(new r({src:u.url,x:h.x,w:h.w,h:h.h}))}this.srcValue&&this._addCandidate(new r({src:this.srcValue}))},e.prototype._addCandidate=function(t){for(var e=0;this.imageCandidates.length>e;e++){var r=this.imageCandidates[e];if(r.x==t.x&&r.w==t.w&&r.h==t.h)return}this.imageCandidates.push(t)},e.prototype._parseDescriptors=function(t){for(var e=t.split(/\s/),r={},i=0;e.length>i;i++){var s=e[i];if(s.length>0){var o=s[s.length-1],a=s.substring(0,s.length-1),u=parseInt(a,10),h=parseFloat(a);a.match(n)&&"w"===o?r[o]=u:a.match(n)&&"h"==o?r[o]=u:isNaN(h)||"x"!=o?(this.error='Invalid srcset descriptor found in "'+s+'".',this.isValid=!1):r[o]=h}}return r},t.SrcsetInfo=e})(window),function(t){function e(){this.w=null,this.h=null,this.x=null}e.prototype.compute=function(){this.w=window.innerWidth||document.documentElement.clientWidth,this.h=window.innerHeight||document.documentElement.clientHeight,this.x=window.devicePixelRatio},e.prototype.setForTesting=function(t){this.w=t.w,this.h=t.h,this.x=t.x},e.prototype.getBestImage=function(t){var e=t.imageCandidates.slice(0),r=this._getBestCandidateIf(e,function(t,e){return t.w>e.w});this._removeCandidatesIf(e,function(t){return function(e){return e.we.h});this._removeCandidatesIf(e,function(t){return function(e){return e.he.x});this._removeCandidatesIf(e,function(t){return function(e){return e.xs.w}),this._getBestCandidateIf(e,function(t,e){return t.hs.h});var o=this._getBestCandidateIf(e,function(t,e){return t.xo.x}),e[0]},e.prototype._getBestCandidateIf=function(t,e){for(var r=t[0],n=0;t.length>n;n++){var i=t[n];e(i,r)&&(r=i)}return r},e.prototype._removeCandidatesIf=function(t,e){for(var r=t.length-1;r>=0;r--){var n=t[r];e(n)&&t.splice(r,1)}return t},e.prototype.getBestImage2=function(t){for(var e=null,r=t.imageCandidates,n=0;r.length>n;n++){var i=r[n],s=e?e.x:0;if(i.x>=s&&i.x<=this.x){if(null===e){e=i;continue}this.w<=i.w&&i.w<=e.w&&(e=i)}}return e},t.ViewportInfo=e}(window),function(){function t(){var t=new Image;return"srcset"in t}function e(){if(!t()){var e=new ViewportInfo;e.compute();for(var r=document.querySelectorAll("img"),n=0;r.length>n;n++){var i=r[n],s=i.getAttribute("srcset");if(s){var o=new SrcsetInfo({src:i.src,srcset:s}),a=e.getBestImage(o);i.src=a.src,i.width||i.height||i.style.height||i.style.width||(i.style.webkitTransform="scale("+1/a.x+")",i.style.webkitTransformOrigin="0 0")}}}}var r=setInterval(function(){"complete"===document.readyState&&(e(),clearInterval(r))},10)}(window); \ No newline at end of file +var Query=function(t){"use strict";var e=function(t){var e,r,n,i,o=[];if(t===void 0||null===t||""===t)return o;for(0===t.indexOf("?")&&(t=t.substring(1)),r=(""+t).split(/[&;]/),e=0;r.length>e;e++)n=r[e],i=n.split("="),o.push([i[0],i[1]]);return o},r=e(t),n=function(){var t,e,n="";for(t=0;r.length>t;t++)e=r[t],n.length>0&&(n+="&"),n+=e.join("=");return n.length>0?"?"+n:n},i=function(t){return t=decodeURIComponent(t),t=t.replace("+"," ")},o=function(t){var e,n;for(n=0;r.length>n;n++)if(e=r[n],i(t)===i(e[0]))return e[1]},s=function(t){var e,n,o=[];for(e=0;r.length>e;e++)n=r[e],i(t)===i(n[0])&&o.push(n[1]);return o},a=function(t,e){var n,o,s,a,u=[];for(n=0;r.length>n;n++)o=r[n],s=i(o[0])===i(t),a=i(o[1])===i(e),(1===arguments.length&&!s||2===arguments.length&&!s&&!a)&&u.push(o);return r=u,this},u=function(t,e,n){return 3===arguments.length&&-1!==n?(n=Math.min(n,r.length),r.splice(n,0,[t,e])):arguments.length>0&&r.push([t,e]),this},c=function(t,e,n){var o,s,c=-1;if(3===arguments.length){for(o=0;r.length>o;o++)if(s=r[o],i(s[0])===i(t)&&decodeURIComponent(s[1])===i(n)){c=o;break}a(t,n).addParam(t,e,c)}else{for(o=0;r.length>o;o++)if(s=r[o],i(s[0])===i(t)){c=o;break}a(t),u(t,e,c)}return this};return{getParamValue:o,getParamValues:s,deleteParam:a,addParam:u,replaceParam:c,toString:n}},Uri=function(t){"use strict";var e=!1,r=function(t){for(var r={strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/},n=["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],i={name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},o=r[e?"strict":"loose"].exec(t),s={},a=14;a--;)s[n[a]]=o[a]||"";return s[i.name]={},s[n[12]].replace(i.parser,function(t,e,r){e&&(s[i.name][e]=r)}),s},n=r(t||""),i=new Query(n.query),o=function(t){return t!==void 0&&(n.protocol=t),n.protocol},s=null,a=function(t){return t!==void 0&&(s=t),null===s?-1!==n.source.indexOf("//"):s},u=function(t){return t!==void 0&&(n.userInfo=t),n.userInfo},c=function(t){return t!==void 0&&(n.host=t),n.host},h=function(t){return t!==void 0&&(n.port=t),n.port},f=function(t){return t!==void 0&&(n.path=t),n.path},l=function(t){return t!==void 0&&(i=new Query(t)),i},d=function(t){return t!==void 0&&(n.anchor=t),n.anchor},g=function(t){return o(t),this},p=function(t){return a(t),this},m=function(t){return u(t),this},v=function(t){return c(t),this},w=function(t){return h(t),this},x=function(t){return f(t),this},y=function(t){return l(t),this},I=function(t){return d(t),this},C=function(t){return l().getParamValue(t)},P=function(t){return l().getParamValues(t)},_=function(t,e){return 2===arguments.length?l().deleteParam(t,e):l().deleteParam(t),this},V=function(t,e,r){return 3===arguments.length?l().addParam(t,e,r):l().addParam(t,e),this},B=function(t,e,r){return 3===arguments.length?l().replaceParam(t,e,r):l().replaceParam(t,e),this},O=function(){var t="",e=function(t){return null!==t&&""!==t};return e(o())?(t+=o(),o().indexOf(":")!==o().length-1&&(t+=":"),t+="//"):a()&&e(c())&&(t+="//"),e(u())&&e(c())&&(t+=u(),u().indexOf("@")!==u().length-1&&(t+="@")),e(c())&&(t+=c(),e(h())&&(t+=":"+h())),e(f())?t+=f():e(c())&&(e(""+l())||e(d()))&&(t+="/"),e(""+l())&&(0!==(""+l()).indexOf("?")&&(t+="?"),t+=""+l()),e(d())&&(0!==d().indexOf("#")&&(t+="#"),t+=d()),t},Q=function(){return new Uri(O())};return{protocol:o,hasAuthorityPrefix:a,userInfo:u,host:c,port:h,path:f,query:l,anchor:d,setProtocol:g,setHasAuthorityPrefix:p,setUserInfo:m,setHost:v,setPort:w,setPath:x,setQuery:y,setAnchor:I,getQueryParamValue:C,getQueryParamValues:P,deleteQueryParam:_,addQueryParam:V,replaceQueryParam:B,toString:O,clone:Q}},jsUri=Uri;(function(t){function e(t){this.imageCandidates=[],this.srcValue=t.src,this.srcsetValue=t.srcset,this.isValid=!0,this.error="",this._parse(this.srcsetValue),this.isValid||console.error("Error: "+this.error)}function r(t){this.src=t.src,this.w=t.w||1/0,this.h=t.h||1/0,this.x=t.x||1}var n=/^[0-9]+$/;e.prototype._parse=function(){for(var t,e,n=this.srcsetValue,i=0,o=[];""!==n;){for(;" "===n.charAt(0);)n=n.slice(1);if(i=n.indexOf(" "),-1!==i){if(t=n.slice(0,i),""===t)break;n=n.slice(i+1),i=n.indexOf(","),-1===i?(e=n,n=""):(e=n.slice(0,i),n=n.slice(i+1)),o.push({url:t,descriptors:e})}else o.push({url:n,descriptors:""}),n=""}for(var s=0,a=o.length;a>s;s++){var u=o[s],c=this._parseDescriptors(u.descriptors);this._addCandidate(new r({src:u.url,x:c.x,w:c.w,h:c.h}))}this.srcValue&&this._addCandidate(new r({src:this.srcValue}))},e.prototype._addCandidate=function(t){for(var e=0;this.imageCandidates.length>e;e++){var r=this.imageCandidates[e];if(r.x==t.x&&r.w==t.w&&r.h==t.h)return}this.imageCandidates.push(t)},e.prototype._parseDescriptors=function(t){for(var e=t.split(/\s/),r={},i=0;e.length>i;i++){var o=e[i];if(o.length>0){var s=o[o.length-1],a=o.substring(0,o.length-1),u=parseInt(a,10),c=parseFloat(a);a.match(n)&&"w"===s?r[s]=u:a.match(n)&&"h"==s?r[s]=u:isNaN(c)||"x"!=s?(this.error='Invalid srcset descriptor found in "'+o+'".',this.isValid=!1):r[s]=c}}return r},t.SrcsetInfo=e})(window),function(t){function e(){this.w=null,this.h=null,this.x=null}e.prototype.compute=function(){this.w=window.innerWidth||document.documentElement.clientWidth,this.h=window.innerHeight||document.documentElement.clientHeight,this.x=window.devicePixelRatio},e.prototype.setForTesting=function(t){this.w=t.w,this.h=t.h,this.x=t.x},e.prototype.getBestImage=function(t){var e=t.imageCandidates.slice(0),r=this._getBestCandidateIf(e,function(t,e){return t.w>e.w});this._removeCandidatesIf(e,function(t){return function(e){return e.we.h});this._removeCandidatesIf(e,function(t){return function(e){return e.he.x});this._removeCandidatesIf(e,function(t){return function(e){return e.xo.w}),this._getBestCandidateIf(e,function(t,e){return t.ho.h});var s=this._getBestCandidateIf(e,function(t,e){return t.xs.x}),e[0]},e.prototype._getBestCandidateIf=function(t,e){for(var r=t[0],n=0;t.length>n;n++){var i=t[n];e(i,r)&&(r=i)}return r},e.prototype._removeCandidatesIf=function(t,e){for(var r=t.length-1;r>=0;r--){var n=t[r];e(n)&&t.splice(r,1)}return t},e.prototype.getBestImage2=function(t){for(var e=null,r=t.imageCandidates,n=0;r.length>n;n++){var i=r[n],o=e?e.x:0;if(i.x>=o&&i.x<=this.x){if(null===e){e=i;continue}this.w<=i.w&&i.w<=e.w&&(e=i)}}return e},t.ViewportInfo=e}(window),function(){function t(){var t=new Image;return"srcset"in t}function e(){if(!t()){var e=new ViewportInfo;e.compute();for(var r=document.querySelectorAll("img"),n=0;r.length>n;n++){var i=r[n],o=i.getAttribute("srcset");if(o){var s=new SrcsetInfo({src:i.src,srcset:o}),a=e.getBestImage(s);i.src=a.src,i.width||i.height||i.style.height||i.style.width||(i.style.webkitTransform="scale("+1/a.x+")",i.style.webkitTransformOrigin="0 0")}}}}var r=setInterval(function(){if("complete"===document.readyState){e();var t;window.onresize=function(){clearTimeout(t),t=setTimeout(function(){e()},500)},clearInterval(r)}},10)}(window); \ No newline at end of file diff --git a/js/main.js b/js/main.js index 2594d60..fed1395 100644 --- a/js/main.js +++ b/js/main.js @@ -43,6 +43,15 @@ var readyTimer = setInterval(function () { if (document.readyState === "complete") { main(); + + var resizeTimer; + window.onresize = function(e) { + clearTimeout(resizeTimer); // some browsers file resize continuously, so wait 500ms + resizeTimer = setTimeout(function() { + main(); + }, 500); + }; + clearInterval(readyTimer); } }, 10);