forked from adamdbradley/foresight.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
foresight.min.js
executable file
·1 lines (1 loc) · 7.97 KB
/
foresight.min.js
1
(function(a,b,c,d){"use strict",a.images=[],a.options=a.options||{};var e=a.options,f=e.testConn||!0,g=e.minKbpsForHighBandwidth||300,h=e.speedTestUri||"http://foresightjs.appspot.com/speed-test/50K",i=e.speedTestKB||50,j=e.speedTestExpireMinutes||30,k=e.hiResClassname||"fs-high-resolution",l=e.lowResClassname||"fs-standard-resolution",m="devicePixelRatio",n="devicePixelRatioRounded",o="bandwidth",p="connType",q="connTestResult",r="connKbps",s="requestChange",t="defaultSrc",u="highResolutionSrc",v="browserWidth",w="browserHeight",x="requestWidth",y="requestHeight",z="width",A="height",B="widthUnits",C="heightUnits",D="aspectRatio",E="appliedImageSetItem",F="scale",G="scaleRounded",H="uriTemplate",I="uriFind",J="uriReplace",K="srcModification",L="loading",M="complete",N="fsjs",O="auto",P="percent",Q="auto",R="pixel",S="auto",T=!0,U=!1,V=/url\((?:([a-zA-Z-_0-9{}\?=&\\/.:\s]+)|([a-zA-Z-_0-9{}\?=&\\/.:\s]+)\|([a-zA-Z-_0-9{}\?=&\\/.:\s]+))\)/g,W,X,Y=function(){if(W)return;W=L,$(),W=M,cb()},Z=function(a,b){if(c.createEvent){var d=c.createEvent("Event");d.initEvent("foresight-"+a,T,T),b.dispatchEvent(d)}},$=function(){var b,d,e;for(b=0;b<c.images.length;b++){d=c.images[b];if(d.ignore)continue;if(!d.initalized){d.initalized=T,Z("imageInitStart",d),d[t]=bb(d,"src"),d[B]=bb(d,z,T),d[C]=bb(d,A,T);var f=bb(d,"aspect-ratio",U);d[D]=f===O?f:isNaN(f)?0:parseInt(f,10);if(!d[t]||(!d[B]||!d[C])&&!d[D]){d.ignore=T;continue}d[u]=bb(d,"high-resolution-src"),d.orgClassName=d.className?d.className:"",d.onerror=pb,Z("imageInitEnd",d),a.images.push(d)}d.imageSetText=jb(d,"font-family","fontFamily"),d.imageSet=[],d.imageSetText.length>1&&_(d,d.imageSetText.split("image-set(")[1])}},_=function(a,b){var c,d=b.split(","),e,f;for(c=0;c<d.length;c++){e={text:d[c],weight:0},e.text.indexOf(" 1.5x")>-1?(e.weight++,e[F]=1.5):e.text.indexOf(" 2x")>-1?e[F]=2:e.text.indexOf(" 1x")>-1&&(e[F]=1),e.text.indexOf(" high-bandwidth")>-1?e[o]="high":e.text.indexOf(" low-bandwidth")>-1&&(e[o]="low");while(f=V.exec(e.text))f[1]!=null&&f[1]!==""?(e[H]=f[1],e.weight++):f[2]!=null&&f[2]!==""&&(e[I]=f[2],e[J]=f[3],e.weight++);e[F]&&e[o]?e.weight+=2:(e[F]||e[o])&&e.weight++,a.imageSet.push(e)}a.imageSet.sort(ab)},ab=function(a,b){return a.weight<b.weight?1:a.weight>b.weight?-1:0},bb=function(a,b,c,d){return d=a.getAttribute("data-"+b),c?isNaN(d)?0:parseInt(d,10):d},cb=function(){if(X!==M||W!==M)return;var b,c=a.images.length,d,e,f,g,h=[],i;for(b=0;b<c;b++){d=a.images[b];if(!gb(d))continue;Z("imageRebuildStart",d),f=d.orgClassName.split(" "),hb(d),d.unitType==R&&(g="fs-"+d[v]+"x"+d[w],f.push(g),h[g]==undefined&&(h[g]=T,h.push("."+g+"{width:"+d[v]+"px;height:"+d[w]+"px}"))),d.style.display!=="inline"&&(d.style.display="inline"),fb(d),db(d),a.hiResEnabled&&d.src!==d[t]?f.push(k):f.push(l),f.push("fs-"+d[K]),d.className=f.join(" "),Z("imageRebuildEnd",d)}h.length&&lb(h),a.updateComplete&&a.updateComplete(),tb=vb()},db=function(b){var c,d;b[E][F]>1&&b[E][o]==="high"?(c=b[v]===undefined?S:Math.round(b[v]*b[E][F]),d=b[w]===undefined?S:Math.round(b[w]*b[E][F]),a.hiResEnabled=T):(c=b[v]===undefined?S:b[v],d=b[w]===undefined?S:b[w],a.hiResEnabled=U),!b[x]||c>b[x]||b.activeImageSetText!==b.imageSetText?(b[x]=c,b[y]=d,b[u]&&a.hiResEnabled?(b.src=b[u],b[K]="src-hi-res"):b.src=eb(b),b[s]=T,b.activeImageSetText=b.imageSetText):b[s]=U},eb=function(a){return a[E][H]?(a[K]="src-uri-template",mb(a)):a[E][I]&&a[E][J]?(a[K]="src-find-replace",ob(a)):(a[K]="src-default",a[t])},fb=function(b){var c,d,e={};for(c=0;c<b.imageSet.length;c++){d=b.imageSet[c];if(d[F]&&d[o]){if(a[m]==d[F]&&a[o]===d[o]){e=d;break}if(Math.round(a[m])==d[F]&&a[o]===d[o]){e=d;break}}else if(d[F]){if(a[m]==d[F]){e=d;break}if(Math.round(a[m])==d[F]){e=d;break}}else if(d[o]){if(a[o]===d[o]){e=d;break}}else e=d}e[F]||(e[F]=a[m]),e[o]||(e[o]=a[o]),e[G]=Math.round(e[F]),b[E]=e},gb=function(a,b){return b=a.parentNode,b.clientWidth?T:jb(b,"display")==="inline"?gb(b):U},hb=function(a,b){a.unitType||(a[D]===O?a.unitType=Q:(b=jb(a,z),b.indexOf("%")>0?a.unitType=P:(a.unitType=R,a[D]?a[B]?(a[v]=a[B],a[w]=Math.round(a[B]/a[D])):a[C]&&(a[v]=Math.round(a[HEIGHT_UNTIS]/a[D]),a[w]=a[C]):(a[v]=a[B],a[w]=a[C]))));if(a.unitType===Q||a.unitType===P)a.computedWidth=ib(a),a[v]=a.computedWidth,a[D]!=O?a[w]=Math.round(a[v]/a[D]):a[C]&&(a[w]=Math.round(a[C]*(a.computedWidth/a[B]))),a[w]&&d.appVersion.indexOf("MSIE")>-1&&(a.style.height=a[w]+"px")},ib=function(a){if(a.offsetWidth!==0)return a.offsetWidth;var b,c,d={},e={position:"absolute",visibility:"hidden",display:"block"};for(c in e)d[c]=a.style[c],a.style[c]=e[c];b=a.offsetWidth;for(c in e)a.style[c]=d[c];return b},jb=function(a,b,d){return d||(d=b),a.currentStyle?a.currentStyle[d]:c.defaultView.getComputedStyle(a,null).getPropertyValue(b)},kb,lb=function(a,b){kb||(kb=c.createElement("style"),kb.setAttribute("type","text/css")),b=a.join("");try{kb.innerHTML=b}catch(d){kb.styleSheet.cssText=b}kb.parentElement==null&&c.getElementsByTagName("head")[0].appendChild(kb)},mb=function(a){var b,c=["src","protocol","host","port","directory","file","filename","ext","query",x,y,F,G],d=a[E][H];a.uri=nb(a[t]),a.uri.src=a[t],a.uri[x]=a[x],a.uri[y]=a[y],a.uri[F]=a[E][F],a.uri[G]=a[E][G];for(b=0;b<c.length;b++)d=d.replace("{"+c[b]+"}",a.uri[c[b]]);return d},nb=function(a){var b={key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/},c=b.parser.exec(a),d={},e=14;while(e--)d[b.key[e]]=c[e]||"";d[b.q.name]={},d[b.key[12]].replace(b.q.parser,function(a,c,e){c&&(d[b.q.name][c]=e)});var f=d.file.split(".");return d.filename=f[0],d.ext=f.length>1?f[f.length-1]:"",d},ob=function(a){var b=a[E][I].replace("{browserWidth}",a[B]).replace("{browserHeight}",a[C]),c,d=a[t].replace(b,a[E][J]),e=[x,y,F,G];for(c=0;c<e.length;c++)d=d.replace("{"+e[c]+"}",a[e[c]]);return d},pb=function(a){a=this,a.className=a.className.replace(k,l),a[K]="response-error";if(a.hasError||a.src===a[t])return;a.hasError=T,a.src=a[t]},qb=function(){if(X)return;if(e.forcedBandwidth){a[o]=e.forcedBandwidth,a[q]="forced",X=M;return}if(a[m]===1){a[q]="skip",X=M;return}var b=d.connection||{type:"unknown"},f=b.type==3||b.type==4||/^[23]g$/.test(b.type);a[p]=b.type;if(f){a[q]="connTypeSlow",X=M;return}try{var j=JSON.parse(localStorage.getItem(N));if(j!==null&&(new Date).getTime()<j.exp){a[o]=j.bw,a[r]=j.kbps,a[q]="localStorage",X=M;return}}catch(k){}var l=c.createElement("img"),n,s,t;l.onload=function(){n=(new Date).getTime();var b=(n-s)/1e3;b=b>1?b:1,a[r]=i*1024*8/b/1024,a[o]=a[r]>=g?"high":"low",rb("networkSuccess")},l.onerror=function(){rb("networkError",5)},l.onabort=function(){rb("networkAbort",5)},s=(new Date).getTime(),X=L,c.location.protocol==="https:"&&(h=h.replace("http:","https:")),l.src=h+"?r="+Math.random(),t=i*8/g*1e3+350,setTimeout(function(){rb("networkSlow")},t)},rb=function(b,c){if(X===M)return;X=M,a[q]=b;try{c||(c=j);var d={kbps:a[r],bw:a[o],exp:(new Date).getTime()+c*6e4};localStorage.setItem(N,JSON.stringify(d))}catch(e){}cb()},sb=function(){b.addEventListener?b.addEventListener("resize",ub,U):b.attachEvent&&b.attachEvent("onresize",ub)},tb=0,ub=function(){tb!==vb()&&a.reload()},vb=function(){return c.documentElement.clientWidth||c.body&&c.body.clientWidth||1024},wb,xb=function(){if(W!==M||X!==M)return;$(),cb()};a.resolve=function(a,b){b.imageSet=[],_(b,a),fb(b),db(b),b.src=eb(b)},a.reload=function(){b.clearTimeout(wb),wb=b.setTimeout(xb,250)},a.ready=function(){if(!c.body)return b.setTimeout(a.ready,1);Y()},c.readyState===M?setTimeout(a.ready,1):c.addEventListener?(c.addEventListener("DOMContentLoaded",a.ready,U),b.addEventListener("load",a.ready,U)):c.attachEvent&&(c.attachEvent("onreadystatechange",a.ready),b.attachEvent("onload",a.ready)),a[m]=b[m]?b[m]:1,e.forcedPixelRatio&&(a[m]=e.forcedPixelRatio),a[n]=Math.round(a[m]),qb(),sb()})(this.foresight=this.foresight||{},this,document,navigator)