* Desc: Force cross domain iframes to size to content.
* Requires: iframeResizer.contentWindow.min.js to be loaded into the target frame.
* Copyright: (c) 2016 David J. Bradshaw - dave@bradshaw.net
* License: MIT
*/
!function(a){"use strict";functionb(b,c,d){"addEventListener"ina?b.addEventListener(c,d,!1):"attachEvent"ina&&b.attachEvent("on"+c,d)}functionc(b,c,d){"removeEventListener"ina?b.removeEventListener(c,d,!1):"detachEvent"ina&&b.detachEvent("on"+c,d)}functiond(){varb,c=["moz","webkit","o","ms"];for(b=0;b<c.length&&!N;b+=1)N=a[c[b]+"RequestAnimationFrame"];N||h("setup","RequestAnimationFrame not supported")}functione(b){varc="Host page: "+b;returna.top!==a.self&&(c=a.parentIFrame&&a.parentIFrame.getId?a.parentIFrame.getId()+": "+b:"Nested host page: "+b),c}functionf(a){returnK+"["+e(a)+"]"}functiong(a){returnP[a]?P[a].log:G}functionh(a,b){k("log",a,b,g(a))}functioni(a,b){k("info",a,b,g(a))}functionj(a,b){k("warn",a,b,!0)}functionk(b,c,d,e){!0===e&&"object"==typeofa.console&&console[b](f(c),d)}functionl(d){functione(){functiona(){s(V),p(W)}g("Height"),g("Width"),t(a,V,"init")}functionf(){vara=U.substr(L).split(":");return{iframe:P[a[0]].iframe,id:a[0],height:a[1],width:a[2],type:a[3]}}functiong(a){varb=Number(P[W]["max"+a]),c=Number(P[W]["min"+a]),d=a.toLowerCase(),e=Number(V[d]);h(W,"Checking "+d+" is in range "+c+"-"+b),c>e&&(e=c,h(W,"Set "+d+" to min value")),e>b&&(e=b,h(W,"Set "+d+" to max value")),V[d]=""+e}functionk(){functiona(){functiona(){vara=0,d=!1;for(h(W,"Checking connection is from allowed list of origins: "+c);a<c.length;a++)if(c[a]===b){d=!0;break}returnd}functiond(){vara=P[W].remoteHost;returnh(W,"Checking connection is from: "+a),b===a}returnc.constructor===Array?a():d()}varb=d.origin,c=P[W].checkOrigin;if(c&&""+b!="null"&&!a())thrownewError("Unexpected message received from: "+b+" for "+V.iframe.id+". Message was: "+d.data+". This error can be disabled by setting the checkOrigin: false option or by providing of array of trusted domains.");return!0}functionl(){returnK===(""+U).substr(0,L)&&U.substr(L).split(":")[0]inP}functionw(){vara=V.typein{"true":1,"false":1,undefined:1};returna&&h(W,"Ignoring init message from meta parent page"),a}functiony(a){returnU.substr(U.indexOf(":")+J+a)}functionz(a){h(W,"MessageCallback passed: {iframe: "+V.iframe.id+", message: "+a+"}"),N("messageCallback",{iframe:V.iframe,message:JSON.parse(a)}),h(W,"--")}functionA(){varb=document.body.getBoundingClientRect(),c=V.iframe.getBoundingClientRect();returnJSON.stringify({iframeHeight:c.height,iframeWidth:c.width,clientHeight:Math.max(document.documentElement.clientHeight,a.innerHeight||0),clientWidth:Math.max(document.documentElement.clientWidth,a.innerWidth||0),offsetTop:parseInt(c.top-b.top,10),offsetLeft:parseInt(c.left-b.left,10),scrollTop:a.pageYOffset,scrollLeft:a.pageXOffset})}functionB(a,b){functionc(){u("Send Page Info","pageInfo:"+A(),a,b)}x(c,32)}functionC(){functiond(b,c){functiond(){P[g]?B(P[g].iframe,g):e()}["scroll","resize"].forEach(function(e){h(g,b+e+" listener for sendPageInfo"),c(a,e,d)})}functione(){d("Remove ",c)}functionf(){d("Add ",b)}varg=W;f(),P[g].stopPageInfo=e}functionD(){P[W]&&P[W].stopPageInfo&&(P[W].stopPageInfo(),deleteP[W].stopPageInfo)}functionE(){vara=!0;returnnull===V.iframe&&(j(W,"IFrame ("+V.id+") not found"),a=!1),a}functionF(a){varb=a.getBoundingClientRect();returno(W),{x:Math.floor(Number(b.left)+Number(M.x)),y:Math.floor(Number(b.top)+Number(M.y))}}functionG(b){functionc(){M=g,H(),h(W,"--")}functiond(){return{x:Number(V.width)+f.x,y:Number(V.height)+f.y}}functione(){a.parentIFrame?a.parentIFrame["scrollTo"+(b?"Offset":"")](g.x,g.y):j(W,"Unable to scroll to requested position, window.parentIFrame not found")}varf=b?F(V.iframe):{x:0,y:0},g=d();h(W,"Reposition requested from iFrame (offset x:"+f.x+" y:"+f.y+")"),a.top!==a.self?e():c()}functionH(){!1!==N("scrollCallback",M)?p(W):q()}functionI(b){functionc(){vara=F(g);h(W,"Moving to in page link (#"+e+") at x: "+a.x+" y: "+a.y),M={x:a.x,y:a.y},H(),h(W,"--")}functiond(){a.parentIFrame?a.parentIFrame.moveToAnchor(e):h(W,"In page link #"+e+" not found and window.parentIFrame not found")}vare=b.split("#")[1]||"",f=decodeURIComponent(e),g=document.getElementById(f)||document.getElementsByName(f)[0];g?c():a.top!==a.self?d():h(W,"In page link #"+e+" not found")}functionN(a,b){returnm(W,a,b)}functionO(){switch(P[W].firstRun&&T(),V.type){case"close":n(V.iframe);break;case"message":z(y(6));break;case"scrollTo":G(!1);break;case"scrollToOffset":G(!0);break;case"pageInfo":B(P[W].iframe,W),C();break;case"pageInfoStop":D();break;case"inPageLink":I(y(9));break;case"reset":r(V);break;case"init":e(),N("initCallback",V.iframe),N("resizedCallback",V);break;default:e(),N("resizedCallback",V)}}functionQ(a){varb=!0;returnP[a]||(b=!1,j(V.type+" No settings for "+a+". Message was: "+U)),b}functionS(){for(varainP)u("iFrame requested init",v(a),document.getElementById(a),a)}functionT(){P[W].firstRun=!1}varU=d.data,V={},W=null;"[iFrameResizerChild]Ready"===U?S():l()?(V=f(),W=R=V.id,!w()&&Q(W)&&(h(W,"Received: "+U),E()&&k()&&O())):i(W,"Ignored: "+U)}functionm(a,b,c){vard=null,e=null;if(P[a]){if(d=P[a][b],"function"!=typeofd)thrownewTypeError(b+" on iFrame["+a+"] is not a function");e=d(c)}returne}functionn(a){varb=a.id;h(b,"Removing iFrame: "+b),a.parentNode.removeChild(a),m(b,"closedCallback",b),h(b,"--"),deleteP[b]}functiono(b){null===M&&(M={x:void0!==a.pageXOffset?a.pageXOffset:document.documentElement.scrollLeft,y:void0!==a.pageYOffset?a.pageYOffset:document.documentElement.scrollTop},h(b,"Get page position: "+M.x+","+M.y))}functionp(b){null!==M&&(a.scrollTo(M.x,M.y),h(b,"Set page position: "+M.x+","+M.y),q())}functionq(){M=null}functionr(a){functionb(){s(a),u("reset","reset",a.iframe,a.id)}h(a.id,"Size reset requested by "+("init"===a.type?"host page":"iFrame")),o(a.id),t(b,a,"reset")}functions(a){functionb(b){a.iframe.style[b]=a[b]+"px",h(a.id,"IFrame ("+e+") "+b+" set to "+a[b]+"px")}functionc(b){H||"0"!==a[b]||(H=!0,h(e,"Hidden iFrame detected, creating visibility listener"),y())}functiond(a){b(a),c(a)}vare=a.iframe.id;P[e]&&(P[e].sizeHeight&&d("height"),P[e].sizeWidth&&d("width"))}functiont(a,b,c){c!==b.type&&N?(h(b.id,"Requesting animation frame"),N(a)):a()}functionu(a,b,c,d){functione(){vare=P[d].targetOrigin;h(d,"["+a+"] Sending msg to iframe["+d+"] ("+b+") targetOrigin: "+e),c.contentWindow.postMessage(K+b,e)}functionf(){i(d,"["+a+"] IFrame("+d+") not found"),P[d]&&deleteP[d]}functiong(){c&&"contentWindow"inc&&null!==c.contentWindow?e():f()}d=d||c.id,P[d]&&g()}functionv(a){returna+":"+P[a].bodyMarginV1+":"+P[a].sizeWidth+":"+P[a].log+":"+P[a].interval+":"+P[a].enablePublicMethods+":"+P[a].autoResize+":"+P[a].bodyMargin+":"+P[a].heightCalculationMethod+":"+P[a].bodyBackground+":"+P[a].bodyPadding+":"+P[a].tolerance+":"+P[a].inPageLinks+":"+P[a].resizeFrom+":"+P[a].widthCalculationMethod}functionw(a,c){functiond(){functionb(b){1/0!==P[w][b]&&0!==P[w][b]&&(a.style[b]=P[w][b]+"px",h(w,"Set "+b+" = "+P[w][b]+"px"))}functionc(a){if(P[w]["min"+a]>P[w]["max"+a])thrownewError("Value for min"+a+" can not be greater than max"+a)}c("Height"),c("Width"),b("maxHeight"),b("minHeight"),b("maxWidth"),b("minWidth")}functione(){vara=c&&c.id||S.id+F++;returnnull!==document.getElementById(a)&&(a+=F++),a}functionf(b){returnR=b,""===b&&(a.id=b=e(),G=(c||{}).log,R=b,h(b,"Added missing iframe ID: "+b+" ("+a.src+")")),b}functiong(){h(w,"IFrame scrolling "+(P[w].scrolling?"enabled":"disabled")+" for "+w),a.style.overflow=!1===P[w].scrolling?"hidden":"auto",a.scrolling=!1===P[w].scrolling?"no":"yes"}functioni(){("number"==typeofP[w].bodyMargin||"0"===P[w].bodyMargin)&&(P[w].bodyMarginV1=P[w].bodyMargin,P[w].bodyMargin=""+P[w].bodyMargin+"px")}functionk(){varb=P[w].firstRun,c=P[w].heightCalculationMethodinO;!b&&c&&r({iframe:a,height:0,width:0,type:"init"})}functionl(){Function.prototype.bind&&(P[w].iframe.iFrameResizer={close:n.bind(null,P[w].iframe),resize:u.bind(null,"Window resize","resize",P[w].iframe),moveToAnchor:function(a){u("Move to anchor","moveToAnchor:"+a,P[w].iframe,w)},sendMessage:function(a){a=JSON.stringify(a),u("Send Message","message:"+a,P[w].iframe,w)}})}functionm(c){functiond(){u("iFrame.onload",c,a),k()}b(a,"load",d),u("init",c,a)}functiono(a){if("object"!=typeofa)thrownewTypeError("Options is not an object")}functionp(a){for(varbinS)S.hasOwnProperty(b)&&(P[w][b]=a.hasOwnProperty(b)?a[b]:S[b])}functionq(a){return""===a||"file://"===a?"*":a}functions(b){b=b||{},P[w]={firstRun:!0,iframe:a,remoteHost:a.src.split("/").slice(0,3).join("/")},o(b),p(b),P[w].targetOrigin=!0===P[w].checkOrigin?q(P[w].remoteHost):"*"}functiont(){returnwinP&&"iFrameResizer"ina}varw=f(a.id);t()?j(w,"Ignored iFrame, already setup."):(s(c),g(),d(),i(),m(v(w)),l())}functionx(a,b){null===Q&&(Q=setTimeout(function(){Q=null,a()},b))}functiony(){functionb(){functiona(a){functionb(b){return"0px"===P[a].iframe.style[b]}functionc(a){returnnull!==a.offsetParent}c(P[a].iframe)&&(b("height")||b("width"))&&u("Visibility change","resize",P[a].iframe,a)}for(varbinP)a(b)}functionc(a){h("window","Mutation observed: "+a[0].target+""+a[0].type),x(b,16)}functiond(){vara=document.querySelector("body"),b={attributes:!0,attributeOldValue:!1,characterData:!0,characterDataOldValue:!1,childList:!0,subtree:!0},d=newe(c);d.observe(a,b)}vare=a.MutationObserver||a.WebKitMutationObserver;e&&d()}functionz(a){functionb(){B("Window "+a,"resize")}h("window","Trigger event: "+a),x(b,16)}functionA(){functiona(){B("Tab Visable","resize")}"hidden"!==document.visibilityState&&(h("document","Trigger event: Visiblity change"),x(a,16))}functionB(a,b){functionc(a){return"parent"===P[a].resizeFrom&&P[a].autoResize&&!P[a].firstRun}for(vardinP)c(d)&&u(a,b,document.getElementById(d),d)}functionC(){b(a,"message",l),b(a,"resize",function(){z("resize")}),b(document,"visibilitychange",A),b(document,"-webkit-visibilitychange",A),b(a,"focusin",function(){z("focus")}),b(a,"focus",function(){z("focus")})}functionD(){functiona(a,c){functiond(){if(!c.tagName)thrownewTypeError("Object is not a valid DOM element");if("IFRAME"!==c.tagName.toUpperCase())thrownewTypeError("Expected <IFRAME> tag, found <"+c.tagName+">")}c&&(d(),w(c,a),b.push(c))}varb;returnd(),C(),function(c,d){switch(b=[],typeofd){case"undefined":case"string":Array.prototype.forEach.call(document.querySelectorAll(d||"iframe"),a.bind(void0,c));break;case"object":a(c,d);break;default:thrownewTypeError("Unexpected data type ("+typeofd+")")}returnb}}functionE(a){a.fn?a.fn.iFrameResize=function(a){functionb(b,c){w(c,a)}returnthis.filter("iframe").each(b).end()}:i("","Unable to bind to jQuery, it is not fully loaded.")}varF=0,G=!1,H=!1,I="message",J=I.length,K="[iFrameSizer]",L=K.length,M=null,N=a.requestAnimationFrame,O={max:1,scroll:1,bodyScroll:1,documentElementScroll:1},P={},Q=null,R="Host Page",S={autoResize:!0,bodyBackground:null,bodyMargin:null,bodyMarginV1:8,bodyPadding:null,checkOrigin:!0,inPageLinks:!1,enablePublicMethods:!0,heightCalculationMethod:"bodyOffset",id:"iFrameResizer",interval:32,log:!1,maxHeight:1/0,maxWidth:1/0,minHeight:0,minWidth:0,resizeFrom:"parent",scrolling:!1,sizeHeight:!0,sizeWidth:!1,tolerance:0,widthCalculationMethod:"scroll",closedCallback:function(){},initCallback:function(){},messageCallback:function(){j("MessageCallback function not defined")},resizedCallback:function(){},scrollCallback:function(){return!0}};a.jQuery&&E(jQuery),"function"==typeofdefine&&define.amd?define([],D):"object"==typeofmodule&&"object"==typeofmodule.exports?module.exports=D():a.iFrameResize=a.iFrameResize||D()}(window||{});