-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiframeResizer.min.js
executable file
·9 lines (8 loc) · 10.1 KB
/
iframeResizer.min.js
1
2
3
4
5
6
7
8
9
/*! iFrame Resizer (iframeSizer.min.js ) - v3.1.1 - 2015-08-30
* Desc: Force cross domain iframes to size to content.
* Requires: iframeResizer.contentWindow.min.js to be loaded into the target frame.
* Copyright: (c) 2015 David J. Bradshaw - [email protected]
* License: MIT
*/
window.__testHooks__=window.__testHooks__||{},window.__testHooks__.parent={},function(a){"use strict";function b(b,c,d){"addEventListener"in a?b.addEventListener(c,d,!1):"attachEvent"in a&&b.attachEvent("on"+c,d)}function c(){var b,c=["moz","webkit","o","ms"];for(b=0;b<c.length&&!F;b+=1)F=a[c[b]+"RequestAnimationFrame"];F||f("setup","RequestAnimationFrame not supported")}function d(b){var c="Host page: "+b;return a.top!==a.self&&(c=a.parentIFrame?a.parentIFrame.getId()+": "+b:"Nested host page: "+b),c}function e(a){return C+"["+d(a)+"]"}function f(b,c){(H[b]?H[b].log:y)&&"object"==typeof a.console&&console.log(e(b),c)}function g(b,c){"object"==typeof a.console&&console.warn(e(b),c)}function h(b){function c(){function a(){m(L),"resetPage"===L.type&&k(M)}e("Height"),e("Width"),n(a,L,"resetPage")}function d(){var a=K.substr(D).split(":");return{iframe:H[a[0]].iframe,id:a[0],height:a[1],width:a[2],type:a[3]}}function e(a){var b=Number(H[M]["max"+a]),c=Number(H[M]["min"+a]),d=a.toLowerCase(),e=Number(L[d]);if(c>b)throw new Error("Value for min"+a+" can not be greater than max"+a);f(M,"Checking "+d+" is in range "+c+"-"+b),c>e&&(e=c,f(M,"Set "+d+" to min value")),e>b&&(e=b,f(M,"Set "+d+" to max value")),L[d]=""+e}function h(){function a(){function a(){f(M,"Checking connection is from allowed list of origins: "+d);var a;for(a=0;a<d.length;a++)if(d[a]===c)return!0;return!1}function b(){var a=H[M].remoteHost;return f(M,"Checking connection is from: "+a),c===a}return d.constructor===Array?a():b()}var c=b.origin,d=H[M].checkOrigin;if(d&&""+c!="null"&&!a())throw new Error("Unexpected message received from: "+c+" for "+L.iframe.id+". Message was: "+b.data+". This error can be disabled by setting the checkOrigin: false option or by providing of array of trusted domains.");return!0}function q(){return C===(""+K).substr(0,D)&&K.substr(D).split(":")[0]in H}function r(){var a=L.type in{"true":1,"false":1,undefined:1};return a&&f(M,"Ignoring init message from meta parent page"),a}function s(a){return K.substr(K.indexOf(":")+B+a)}function t(a){f(M,"MessageCallback passed: {iframe: "+L.iframe.id+", message: "+a+"}"),H[M].messageCallback({iframe:L.iframe,message:JSON.parse(a)}),f(M,"--")}function u(){return null===L.iframe?(g(M,"IFrame ("+L.id+") not found"),!1):!0}function v(a){var b=a.getBoundingClientRect();return j(M),{x:parseInt(b.left,10)+parseInt(E.x,10),y:parseInt(b.top,10)+parseInt(E.y,10)}}function w(b){function c(){E=h,x(),f(M,"--")}function d(){return{x:Number(L.width)+e.x,y:Number(L.height)+e.y}}var e=b?v(L.iframe):{x:0,y:0},h=d();f(M,"Reposition requested from iFrame (offset x:"+e.x+" y:"+e.y+")"),a.top!==a.self?a.parentIFrame?a.parentIFrame["scrollTo"+(b?"Offset":"")](h.x,h.y):g(M,"Unable to scroll to requested position, window.parentIFrame not found"):c()}function x(){!1!==H[M].scrollCallback(E)&&k(M)}function y(b){function c(a){var b=v(a);f(M,"Moving to in page link (#"+d+") at x: "+b.x+" y: "+b.y),E={x:b.x,y:b.y},x(),f(M,"--")}var d=b.split("#")[1]||"",e=decodeURIComponent(d),g=document.getElementById(e)||document.getElementsByName(e)[0];a.top!==a.self?a.parentIFrame?a.parentIFrame.moveToAnchor(d):f(M,"In page link #"+d+" not found and window.parentIFrame not found"):g?c(g):f(M,"In page link #"+d+" not found")}function z(a,b){"function"==typeof a&&a(b)}function A(){switch(H[M].firstRun&&I(),L.type){case"close":i(L.iframe);break;case"message":t(s(6));break;case"scrollTo":w(!1);break;case"scrollToOffset":w(!0);break;case"inPageLink":y(s(9));break;case"reset":l(L);break;case"init":c(),z(H[M].initCallback,L.iframe),z(H[M].resizedCallback,L);break;default:c(),z(H[M].resizedCallback,L)}}function F(a){var b=!0;return H[a]||(b=!1,g(L.type+" No settings for "+a+". Message was: "+K)),b}function G(){for(var a in H)o("iFrame requested init",p(a),document.getElementById(a),a)}function I(){H[M].firstRun=!1}var K=b.data,L={},M=null;"[iFrameResizerChild]Ready"===K?G():q()&&(L=d(),M=J=L.id,!r()&&F(M)&&(f(M,"Received: "+K),u()&&h()&&A()))}function i(a){var b=a.id;f(b,"Removing iFrame: "+b),a.parentNode.removeChild(a),H[b]&&H[b].closedCallback(b),f(b,"--"),delete H[b]}function j(b){null===E&&(E={x:void 0!==a.pageXOffset?a.pageXOffset:document.documentElement.scrollLeft,y:void 0!==a.pageYOffset?a.pageYOffset:document.documentElement.scrollTop},f(b,"Get page position: "+E.x+","+E.y))}function k(b){null!==E&&(a.scrollTo(E.x,E.y),f(b,"Set page position: "+E.x+","+E.y),E=null)}function l(a){function b(){m(a),o("reset","reset",a.iframe,a.id)}f(a.id,"Size reset requested by "+("init"===a.type?"host page":"iFrame")),j(a.id),n(b,a,"reset")}function m(a){function b(b){a.iframe.style[b]=a[b]+"px",f(a.id,"IFrame ("+e+") "+b+" set to "+a[b]+"px")}function c(b){z||"0"!==a[b]||(z=!0,f(e,"Hidden iFrame detected, creating visibility listener"),t())}function d(a){b(a),c(a)}var e=a.iframe.id;H[e]&&(H[e].sizeHeight&&d("height"),H[e].sizeWidth&&d("width"))}function n(a,b,c){c!==b.type&&F?(f(b.id,"Requesting animation frame"),F(a)):a()}function o(a,b,c,d){function e(){f(d,"["+a+"] Sending msg to iframe["+d+"] ("+b+")"),c.contentWindow.postMessage(C+b,i)}function h(){g(d,"["+a+"] IFrame("+d+") not found"),H[d]&&delete H[d]}d=d||c.id;var i=H[d].targetOrigin;c&&"contentWindow"in c?e():h()}function p(a){return a+":"+H[a].bodyMarginV1+":"+H[a].sizeWidth+":"+H[a].log+":"+H[a].interval+":"+H[a].enablePublicMethods+":"+H[a].autoResize+":"+H[a].bodyMargin+":"+H[a].heightCalculationMethod+":"+H[a].bodyBackground+":"+H[a].bodyPadding+":"+H[a].tolerance+":"+H[a].inPageLinks+":"+H[a].resizeFrom+":"+H[a].widthCalculationMethod}function q(a,c){function d(){function b(b){1/0!==H[v][b]&&0!==H[v][b]&&(a.style[b]=H[v][b]+"px",f(v,"Set "+b+" = "+H[v][b]+"px"))}b("maxHeight"),b("minHeight"),b("maxWidth"),b("minWidth")}function e(b){return J=b,""===b&&(a.id=b=(c.id||K.id)+x++,y=(c||{}).log,J=b,f(b,"Added missing iframe ID: "+b+" ("+a.src+")")),b}function h(){f(v,"IFrame scrolling "+(H[v].scrolling?"enabled":"disabled")+" for "+v),a.style.overflow=!1===H[v].scrolling?"hidden":"auto",a.scrolling=!1===H[v].scrolling?"no":"yes"}function j(){("number"==typeof H[v].bodyMargin||"0"===H[v].bodyMargin)&&(H[v].bodyMarginV1=H[v].bodyMargin,H[v].bodyMargin=""+H[v].bodyMargin+"px")}function k(){var b=H[v].firstRun,c=H[v].heightCalculationMethod in G;!b&&c&&l({iframe:a,height:0,width:0,type:"init"})}function m(){Function.prototype.bind&&(H[v].iframe.iFrameResizer={close:i.bind(null,H[v].iframe),resize:o.bind(null,"Window resize","resize",H[v].iframe),moveToAnchor:function(a){o("Move to anchor","inPageLink:"+a,H[v].iframe,v)},sendMessage:function(a){a=JSON.stringify(a),o("Send Message","message:"+a,H[v].iframe,v)}})}function n(c){function d(){o("iFrame.onload",c,a),k()}b(a,"load",d),o("init",c,a)}function q(a){if("object"!=typeof a)throw new TypeError("Options is not an object.")}function r(a){for(var b in K)K.hasOwnProperty(b)&&(H[v][b]=a.hasOwnProperty(b)?a[b]:K[b])}function s(a){return""===a||"file://"===a?"*":a}function t(b){b=b||{},H[v]={firstRun:!0,iframe:a,remoteHost:a.src.split("/").slice(0,3).join("/")},q(b),r(b),H[v].targetOrigin=!0===H[v].checkOrigin?s(H[v].remoteHost):"*"}function u(){return v in H&&"iFrameResizer"in a}var v=e(a.id);u()?g(v,"Ignored iFrame, already setup."):(t(c),h(),d(),j(),n(p(v)),m())}function r(a,b){null===I&&(I=setTimeout(function(){I=null,a()},b))}function s(a){return null!==a.offsetParent}function t(){function b(){function a(a){function b(b){return"0px"===H[a].iframe.style[b]}s(H[a].iframe)&&(b("height")||b("width"))&&o("Visibility change","resize",H[a].iframe,a)}for(var b in H)a(b)}function c(a){f("window","Mutation observed: "+a[0].target+" "+a[0].type),r(b,16)}function d(){var a=document.querySelector("body"),b={attributes:!0,attributeOldValue:!1,characterData:!0,characterDataOldValue:!1,childList:!0,subtree:!0},d=new e(c);d.observe(a,b)}var e=a.MutationObserver||a.WebKitMutationObserver;e&&d()}function u(){function c(a){function b(){e("Window "+a,"resize")}f("window","Trigger event: "+a),r(b,16)}function d(){function a(){e("Tab Visable","resize")}"hidden"!==document.visibilityState&&(f("document","Trigger event: Visiblity change"),r(a,16))}function e(a,b){function c(a){return"parent"===H[a].resizeFrom&&H[a].autoResize&&!H[a].firstRun}for(var d in H)c(d)&&o(a,b,document.getElementById(d),d)}b(a,"message",h),b(a,"resize",function(){c("resize")}),b(document,"visibilitychange",d),b(document,"-webkit-visibilitychange",d),b(a,"focusin",function(){c("focus")}),b(a,"focus",function(){c("focus")})}function v(){function a(a,c){if(!c.tagName)throw new TypeError("Object is not a valid DOM element");if("IFRAME"!==c.tagName.toUpperCase())throw new TypeError("Expected <IFRAME> tag, found <"+c.tagName+">.");q(c,a),b.push(c)}var b;return c(),u(),function(c,d){switch(b=[],typeof d){case"undefined":case"string":Array.prototype.forEach.call(document.querySelectorAll(d||"iframe"),a.bind(void 0,c));break;case"object":a(c,d);break;default:throw new TypeError("Unexpected data type ("+typeof d+").")}return b}}function w(a){a.fn.iFrameResize=function(a){return this.filter("iframe").each(function(b,c){q(c,a)}).end()}}var x=0,y=!1,z=!1,A="message",B=A.length,C="[iFrameSizer]",D=C.length,E=null,F=a.requestAnimationFrame,G={max:1,scroll:1,bodyScroll:1,documentElementScroll:1},H={},I=null,J="Host Page",K={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(){g("MessageCallback function not defined")},resizedCallback:function(){},scrollCallback:function(){return!0}};a.jQuery&&w(jQuery),"function"==typeof define&&define.amd?define([],v):"object"==typeof module&&"object"==typeof module.exports?module.exports=v():a.iFrameResize=a.iFrameResize||v()}(window||{});
//# sourceMappingURL=iframeResizer.map