UNPKG

emujs

Version:

A responsiveness testing tool on browsers

21 lines (20 loc) 9.64 kB
/*! emujs v1.0.3 - Zudd (c) 2015 */ var emu=function(h){var r,u,l,v,x,n={iPhone_6:"375x667",iPhone_5:"320x568",iPhone_4:"320x480",iPhone_3:"320x480",iPod_Touch:"320x568",LG_G4:"360x640",LG_G3:"360x640",LG_Optimus_G:"384x640",Galaxy_Note_2:"360x640",Galaxy_Note:"400x640",Galaxy_S5:"360x640",Galaxy_S4:"360x640",Galaxy_S4_mini:"360x640",Galaxy_S3:"360x640",Galaxy_S3_mini:"320x533",Galaxy_S2:"320x533",Galaxy_S:"320x533",Galaxy_Nexus:"360x600",LG_Nexus_5:"360x640",LG_Nexus_4:"384x640",Lumia_1020:"320x480",Lumia_925:"320x480",Lumia_920:"320x480", Lumia_900:"320x480",Lumia_830:"320x480",Lumia_620:"320x480",HTC_One:"360x640",HTC_8X:"320x480",HTC_Evo_3D:"360x640",Sony_Xperia_Z3:"360x598",Sony_Xperia_Z:"360x640",Sony_Xperia_S:"360x640",Sony_Xperia_P:"360x640",Xiaomi_Mi_4:"360x640",Xiaomi_Mi_3:"360x640",Lenovo_K900:"360x640","Pantech_Vega_n\u00b06":"360x640",Blackberry_Leap:"390x695",Blackberry_Passport:"504x504",Blackberry_Classic:"390x390",Blackberry_Q10:"346x346",Blackberry_Z30:"360x640",Blackberry_Z10:"384x640",Blackberry_Torch_9800:"360x480", ZTE_Grand_S:"360x640",ZTE_Open_FirefoxOS:"320x480",iPhone_6_Plus:"414x736",Motorola_Nexus_6:"412x690",Lumia_1520:"432x768",Galaxy_Note_4:"360x640",Galaxy_Note_3:"360x640",iPad_Pro:"1024x1366",iPad_3_4_Air:"768x1024",iPad_1_2:"768x1024",iPad_mini_2_3:"768x1024",iPad_mini:"768x1024",'Galaxy_Tab_3_10"':"800x1280",'Galaxy_Tab_2_10"':"800x1280",'Galaxy_Tab_8.9"':"800x1280",'Galaxy_Tab_2_7"':"600x1024",Samsung_Nexus_10:"800x1280",HTC_Nexus_9:"768x1024",Asus_Nexus_7_v2:"600x960",Asus_Nexus_7_v1:"604x966", "LG_G_Pad_8.3":"600x960","Kindle_Fire_HD_8.9":"800x1280",Kindle_Fire_HD_7:"480x800",Kindle_Fire:"600x1024",Surface_Pro_3:"1024x1440",Surface_Pro_2:"720x1280",Surface_Pro:"720x1280",Surface:"768x1366",Blackberry_Playbook:"600x1024",Google_Glass:"427x240"},q=function(){var a={},b,d,e,c,k,f;c=!1;document.addEventListener||(c=!0);f=function(){var a,y=/\+/g,m=/([^&=]+)=?([^&]*)/g,c=window.location.search.substring(1),b={};for(a=m.exec(c);a;)b[decodeURIComponent(a[1].replace(y," "))]=decodeURIComponent(a[2].replace(y, " ")),a=m.exec(c);a=function(a){return a?b[a.toString()]:b};a.has=function(a){return"undefined"!==typeof b[a]};a.toString=function(){var a=[],g;for(g in b)void 0!==b[g]&&(""===b[g]?a.push(encodeURIComponent(g)):a.push(encodeURIComponent(g)+"="+encodeURIComponent(b[g])));return a.length?"?"+a.join("&"):""};a["with"]=function(a){for(var g in a)b[g]=a[g];return b};a.rem=function(a){b[a]=void 0};a.add=function(a,g){b[a]=g};return a}();k=function(){function a(){this.url=[location.protocol,"//",location.host, location.pathname].join("");this.param=f;this.full=window.location.href}a.prototype={toString:function(){return this.url},to:function(a){window.location.href=a},previous:function(){return window.referrer},encode:function(a){return encodeURIComponent(a)},decode:function(a){return decodeURIComponent(a)},open:function(a){var g=document.createElement("a");g.href=a;g.target="_blank";g.click()},"with":function(a){var g=[],b;for(b in a)void 0!==a[b]&&(""===a[b]?g.push(this.encode(b)):g.push(this.encode(b)+ "="+this.encode(a[b])));return g.length?this.url+"?"+g.join("&"):this.url}};return new a}();b=function(a,b){for(var c in b)a[c]=b[c]};d=function(a){return a.offsetHeight};e=function(a){return a.offsetWidth};c=c?function(a,c){var m='"progid:DXImageTransform.Microsoft.Matrix(M11='+c+", M12=0, M21=0, M22="+c+", SizingMethod='auto expand')\"";b(a.style,{"-ms-filter":m,filter:m,"margin-left":"-"+e(a)+"px","margin-top":"-"+d(a)+"px"})}:function(a,c){var m="scale("+c+")";b(a.style,{"-moz-transform":m,"-o-transform":m, "-webkit-transform":m,transform:m})};a.each=function(a,b){for(var c=a.length-1;0<=c;c--)b.apply(a[c])};a.scale=c;a.center=function(a){b(a.style,{position:"absolute",top:"50%",left:"50%",margin:"-"+a.offsetHeight/2+"px 0 0 -"+a.offsetWidth/2+"px"})};a.first=function(a){for(var b in a)return b};a.url=k;a.isTop=window===window.top;a.isDev=0===navigator.plugins.length;return a}(),p=function(a){var b,d,e;b=function(){this.handlers=[]};b.prototype={add:function(a){this.handlers.push(a)},receive:function(a){for(var b= this.handlers.length-1;0<=b;b--)this.handlers[b](a)},reset:function(){this.handlers.length=0}};d=function(){this.boxes={}};d.prototype={open:function(a){this.boxes[a]=new b},close:function(a){this.boxes[a]=void 0},isOpened:function(a){return"undefined"!==typeof this.boxes[a]},pull:function(a,b){this.boxes[a].add(b)},push:function(b,e){parent.postMessage(a.stringify({name:b,data:e}),"*")},receive:function(a,b){this.isOpened(a)&&this.boxes[a].receive(b)}};e=new d;window.onmessage=function(b){if(b.data){var d= a.parse(b.data);b=d.name||'""';d=d.data||'""';b&&e.receive(b,d)}};return e}(JSON||function(){function a(a){return 10>a?"0"+a:a}"function"!==typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+a(this.getUTCMonth()+1)+"-"+a(this.getUTCDate())+"T"+a(this.getUTCHours())+":"+a(this.getUTCMinutes())+":"+a(this.getUTCSeconds())+"Z":null},Boolean.prototype.toJSON=function(){return this.valueOf()},Number.prototype.toJSON=function(){return this.valueOf()}, String.prototype.toJSON=function(){return this.valueOf()})}()),B=function(a){return function(b){"undefined"!==typeof html2canvas&&html2canvas(b,{onrendered:function(d){var e=document.createElement("canvas");e.width=d.width;e.height=d.height;var c=e.getContext("2d");c.rect(0,0,e.width,e.height);c.fillStyle="white";c.fill();c.drawImage(d,0,0);a.push("screenshot",{src:e.toDataURL(),size:{width:b.offsetWidth,height:b.offsetHeight}})}})}}(p),A=function(a,b){var d,e,c,k,f;c=function(a,b){function c(){b(a.val())} a.on("input",c);a.on("propertychange input",c);a.keyup(function(a){a=a.code||a.which;8!==a&&46!==a||c.apply(this)});return a};f=function(g,c,e){e=e||"li";var d=0;c=a(c).children(e);""!==g?b.each(c,function(){-1<this.innerHTML.toLowerCase().indexOf(g.toLowerCase())?(d++,a(this).show(0)):a(this).hide(0)}):c.show()};k=function(a,c){var d="";c=c||b.first(a);for(var f in a)d+="<span",f===c&&(d+=" class=active"),d+=' onclick=emu("'+f.replace('"','\\"')+'")>',d+=f.replace(/\_/g," ")+"</span>";e.innerHTML= d};d=function(b){e=a("<div/>").addClass("opts")[0];var f=a("<input/>");c(f,d.search);k(b);a("<div/>").addClass("top layer ctrl").append(a("<div/>").addClass("utils").append(f)).append(e).appendTo("#emu")};d.search=function(a){return f(a,e,"span")};d.build=k;return d}(h,q),w=function(a,b){var d,e,c,k,f;f=function(c){var d=c.width;c=c.height;var f=d/c,k=d,z=c,h=a("#emu .view")[0];c>h.offsetHeight-40&&(z=h.offsetHeight-40,k=f*z);k>h.offsetWidth-40&&(k=h.offsetWidth-40);f=Math.round(100*(k/d+1E-5))/100; a(e).css({height:c,width:d});b.scale(e,f);b.center(e)};c=function(){f({width:e.offsetHeight,height:e.offsetWidth})};k=function(){e.src=b.url["with"]({screenshot:""})};d=function(){var d=a("<button/>").text("rotate").click(c),f=a("<button/>").text("screenshot").click(k);e=a("<iframe/>",{src:b.url})[0];a("<div/>").addClass("top layer view").append(a("<div/>").addClass("utils").append(f).append(d)).append(e).appendTo("#emu")};d.resize=f;d.rotate=c;d.refresh=function(){e.src=b.url};d.rescale=function(){f({width:e.offsetWidth, height:e.offsetHeight})};return d}(h,q),t=function(a){var b,d,e,c,k,f,g=!1,h,m,l=0;m=function(){var a=new Date,b=a.getMonth()+1,c=a.getHours(),d=a.getMinutes(),e=a.getSeconds();10>c&&(c="0"+c);10>d&&(d="0"+d);10>e&&(e="0"+e);return c+":"+d+":"+e+" "+a.getDate()+"/"+b+"/"+(a.getFullYear()+"").slice(2)};h=function(){l=0;a(d).toggle();a(c).toggle();a(f).hide().text(0);a(e).hide().text(0);g=a(d).is(":visible")?!0:!1};b=function(){d=a("<div/>").addClass("logs").hide()[0];c=a("<div/>").addClass("images")[0]; k=a("<ul/>").appendTo(c);e=a("<span/>").hide()[0];f=a("<span/>").hide()[0];var b=a("<div/>").addClass("utils")[0];a("<span/>").text("Images").append(f).appendTo(b).click(function(){a(this).parent().children("span").removeClass("active");a(this).addClass("active");h()}).addClass("active");a("<span/>").text("Logs").append(e).appendTo(b).click(function(){a(this).parent().children("span").removeClass("active");a(this).addClass("active");h()});a("<div/>").addClass("btm layer").append(b).append(c).append(d).appendTo("#emu")}; b.log=function(b){a(d).prepend("<p>"+b+"<i>"+m()+"</i></p>");g||(l++,a(e).text(l).show())};b.image=function(b,d,e){var h=a("<li/>");a(c).show();a(k).prepend(h);h.css({width:h.height()/e.height*e.width,background:"url("+d+")","background-size":"cover"});h.html('<a target="_blank" href='+d+">"+b+"</a>");g&&(a(c).hide(),l++,a(f).text(l).show())};b.toggle=h;return b}(h);v=function(a){a=a.split("x");return{width:parseInt(a[0],10),height:parseInt(a[1],10)}};x=function(a){return 1200<=a.width?"large":922<= a.width?"medium":768<=a.width?"small":"extra small"};u=function(a){l=q.first(a);return function(a){l=a;w.resize(v(n[l]));A.build(n,l)}}(n);q.isTop?(r=function(a){a?(u(a),t.log("Simulating "+l.replace(/\_/g," ")+"... <u>"+x(v(n[l]))+"</u> device")):(h("<div/>",{id:"emu"}).appendTo("body"),A(n),w(),t(),h("<button/>").addClass("emu").text("emu").appendTo(document.body).click(function(){h("#emu").fadeToggle("fast",function(){})}),p.open("screenshot"),p.pull("screenshot",function(a){t.image(l.replace(/\_/g, " "),a.src,a.size)}),p.open("log"),p.pull("log",function(a){t.log(a)}))},h(document).ready(function(){u(l);h("#emu").hide()}),h(window).resize(function(){w.rescale()})):r=function(){q.url.param.has("screenshot")?h(window).load(function(){B(document.body)}):h(window).load(function(){p.push("log","Simulation done. Device is ready.")})};r.add=function(a,b){n[a]=b};return r}(jQuery);