@ekolabs/iframily
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Iframily=t():e.Iframily=t()}(window,(function(){return function(e){var t={};function n(i){if(t[i])return t[i].exports;var r=t[i]={i:i,l:!1,exports:{}};return e[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,i){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(i,r,function(t){return e[t]}.bind(null,r));return i},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=2)}([function(e,t,n){"use strict";e.exports={PARENT:"parent",CHILD:"child",FRAMILY_ID_PREFIX:"__iframily__",FRAMILY_INIT:"iframily_init",FRAMILY_INIT_SUCCESSFUL:"iframily_init_successful",ATTEMPT_TO_CONNECT_INTERVAL:200}},function(e,t,n){"use strict";function i(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}var r=n(0),o=["sendMessage","dispose"];e.exports=function(){function e(t,n,i){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._id=t,this._onPairedHandler=i.onPairedHandler||function(){},this._onDisposedHandler=i.onDisposedHandler||function(){},this._targetOrigin=i.targetOrigin||"*",this._iframilyUid="".concat(r.FRAMILY_ID_PREFIX).concat(this._id),this._msgHandler=n||function(){},this._hasConnected=!1,this._disposed=!1,this._msgQueue=[],this._pendingCb={},this._cbUid=0,this._bindPublicMethods(),this._init()}var t,n,s;return t=e,(n=[{key:"_bindPublicMethods",value:function(){o.forEach(function(e){this[e]=this[e].bind(this)}.bind(this))}},{key:"_init",value:function(){throw new Error("To be overridden in extending classes.")}},{key:"_sendQueuedMessages",value:function(){this._msgQueue.forEach((function(e){e()})),this._msgQueue=[]}},{key:"_postMessage",value:function(e){try{this._targetWindow.postMessage(e,this._targetOrigin)}catch(e){console.error("[Iframily] - Error when posting message:",e)}}},{key:"_sendMessage",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i={_iframilyUid:this._iframilyUid,_fromType:this._iframilyType,msg:e};t&&(this._cbUid++,this._pendingCb[this._cbUid]=t,i._cbUid=this._cbUid),this._hasConnected||n.forceSend?this._postMessage(i):(console.warn("[Iframily] - No one connected yet, queuing message",e),this._msgQueue.push(this._postMessage.bind(this,i)))}},{key:"_sendResponse",value:function(e,t){var n={_iframilyUid:this._iframilyUid,_fromType:this._iframilyType,_isResponse:!0,_cbUid:e,_isResolved:t.isResolved,_isRejected:t.isRejected,_cbResolveValue:t.value,_cbRejectError:t.err};this._postMessage(n)}},{key:"_handleMessage",value:function(e){var t=this;e._isResponse?this._handleResponse(e):Promise.resolve().then((function(){return t._msgHandler(e.msg)})).then((function(n){t._sendResponse(e._cbUid,{isResolved:!0,value:n})})).catch((function(n){t._sendResponse(e._cbUid,{isRejected:!0,err:n})}))}},{key:"_handleResponse",value:function(e){if(e._cbUid){var t=this._pendingCb[e._cbUid];if(e._isResolved)t.resolve(e._cbResolveValue);else{if(!e._isRejected)throw new Error("Missing resolve/reject information on response: ".concat(e));t.reject(e._cbRejectError)}delete this._pendingCb[e._cbUid]}}},{key:"_displayDisposedError",value:function(){console.error("[Iframily] - Attempting to use a disposed instance")}},{key:"sendMessage",value:function(e){var t=this;return this._disposed?this._displayDisposedError():new Promise((function(n,i){t._sendMessage(e,{resolve:n,reject:i})}))}},{key:"dispose",value:function(){if(this._disposed)return this._displayDisposedError();this._hasConnected=!1,this._msgQueue=[],this._disposed=!0,this._onDisposedHandler()}},{key:"disposed",get:function(){return this._disposed}},{key:"id",get:function(){return this._id}}])&&i(t.prototype,n),s&&i(t,s),e}()},function(e,t,n){"use strict";function i(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}var r=n(3),o=n(4),s=n(0),a={},c={};e.exports=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e)}var t,n,u;return t=e,u=[{key:"initParent",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!a[e]||a[e].disposed)return a[e]=new o(e,t,n),a[e];console.error('[Iframily] - A parent iframily with id "'.concat(e,'" was already inited, please use another id or dispose the existing one first.'))}},{key:"initChild",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!c[e]||c[e].disposed)return c[e]=new r(e,t,n),c[e];console.error('[Iframily] - A child iframily with id "'.concat(e,'" was already inited, please use another id or dispose the existing one first.'))}},{key:"isIframilyMessage",value:function(e){var t=e&&e.data;return t&&t._iframilyUid&&t._iframilyUid.includes(s.FRAMILY_ID_PREFIX)}}],(n=null)&&i(t.prototype,n),u&&i(t,u),e}()},function(e,t,n){"use strict";function i(e){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function s(e,t,n){return(s="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var i=function(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=f(e)););return e}(e,t);if(i){var r=Object.getOwnPropertyDescriptor(i,t);return r.get?r.get.call(n):r.value}})(e,t,n||e)}function a(e,t){return(a=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,i=f(e);if(t){var r=f(this).constructor;n=Reflect.construct(i,arguments,r)}else n=i.apply(this,arguments);return u(this,n)}}function u(e,t){return!t||"object"!==i(t)&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}var l=n(1),d=n(0);e.exports=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&a(e,t)}(l,e);var t,n,i,u=c(l);function l(){return r(this,l),u.apply(this,arguments)}return t=l,(n=[{key:"_init",value:function(){var e=this;this._iframilyType=d.CHILD,this._targetWindow=window.parent,this._handleMessage=this._handleMessage.bind(this),window.addEventListener("message",this._handleMessage),this._attemptToConnectInterval=setInterval(function t(){return e._sendMessage(d.FRAMILY_INIT,null,{forceSend:!0}),t}(),d.ATTEMPT_TO_CONNECT_INTERVAL)}},{key:"_handleMessage",value:function(e){var t=e&&e.data;t&&t._iframilyUid===this._iframilyUid&&t._fromType===d.PARENT&&(this._hasConnected||t.msg!==d.FRAMILY_INIT_SUCCESSFUL?this._hasConnected&&s(f(l.prototype),"_handleMessage",this).call(this,t):(this._hasConnected=!0,clearInterval(this._attemptToConnectInterval),this._onPairedHandler(),this._sendQueuedMessages()))}},{key:"dispose",value:function(){s(f(l.prototype),"dispose",this).call(this),window.removeEventListener("message",this._handleMessage),clearInterval(this._attemptToConnectInterval)}}])&&o(t.prototype,n),i&&o(t,i),l}(l)},function(e,t,n){"use strict";function i(e){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function s(e,t,n){return(s="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var i=function(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=f(e)););return e}(e,t);if(i){var r=Object.getOwnPropertyDescriptor(i,t);return r.get?r.get.call(n):r.value}})(e,t,n||e)}function a(e,t){return(a=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,i=f(e);if(t){var r=f(this).constructor;n=Reflect.construct(i,arguments,r)}else n=i.apply(this,arguments);return u(this,n)}}function u(e,t){return!t||"object"!==i(t)&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}var l=n(1),d=n(0);e.exports=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&a(e,t)}(l,e);var t,n,i,u=c(l);function l(){return r(this,l),u.apply(this,arguments)}return t=l,(n=[{key:"_init",value:function(){this._iframilyType=d.PARENT,this._handleMessage=this._handleMessage.bind(this),window.addEventListener("message",this._handleMessage)}},{key:"_handleMessage",value:function(e){var t=e&&e.data;if(t&&t._iframilyUid===this._iframilyUid&&t._fromType===d.CHILD)if(t.msg===d.FRAMILY_INIT){if(!this._hasConnected)for(var n=document.getElementsByTagName("iframe"),i=0;i<n.length;i++)if(n[i].contentWindow===e.source){this._targetWindow=n[i].contentWindow,this._hasConnected=!0,this._onPairedHandler(),this._sendMessage(d.FRAMILY_INIT_SUCCESSFUL),this._sendQueuedMessages();break}}else this._hasConnected&&s(f(l.prototype),"_handleMessage",this).call(this,t)}},{key:"dispose",value:function(){s(f(l.prototype),"dispose",this).call(this),window.removeEventListener("message",this._handleMessage)}}])&&o(t.prototype,n),i&&o(t,i),l}(l)}])})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Iframily=t():e.Iframily=t()}(window,(function(){return function(e){var t={};function n(i){if(t[i])return t[i].exports;var r=t[i]={i:i,l:!1,exports:{}};return e[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,i){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(i,r,function(t){return e[t]}.bind(null,r));return i},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=2)}([function(e,t,n){"use strict";e.exports={PARENT:"parent",CHILD:"child",FRAMILY_ID_PREFIX:"__iframily__",FRAMILY_INIT:"iframily_init",FRAMILY_INIT_SUCCESSFUL:"iframily_init_successful",ATTEMPT_TO_CONNECT_INTERVAL:200}},function(e,t,n){"use strict";function i(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}var r=n(0),o=["sendMessage","dispose"];e.exports=function(){function e(t,n,i){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._id=t,this._onPairedHandler=i.onPairedHandler||function(){},this._onDisposedHandler=i.onDisposedHandler||function(){},this._targetOrigin=i.targetOrigin||"*",this._iframilyUid="".concat(r.FRAMILY_ID_PREFIX).concat(this._id),this._msgHandler=n||function(){},this._hasConnected=!1,this._disposed=!1,this._msgQueue=[],this._pendingCb={},this._cbUid=0,this._bindPublicMethods(),this._init()}var t,n,s;return t=e,(n=[{key:"_bindPublicMethods",value:function(){o.forEach(function(e){this[e]=this[e].bind(this)}.bind(this))}},{key:"_init",value:function(){throw new Error("To be overridden in extending classes.")}},{key:"_sendQueuedMessages",value:function(){this._msgQueue.forEach((function(e){e()})),this._msgQueue=[]}},{key:"_postMessage",value:function(e){try{this._targetWindow.postMessage(e,this._targetOrigin)}catch(e){console.error("[Iframily] - Error when posting message:",e)}}},{key:"_sendMessage",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i={_iframilyUid:this._iframilyUid,_fromType:this._iframilyType,msg:e};t&&(this._cbUid++,this._pendingCb[this._cbUid]=t,i._cbUid=this._cbUid),this._hasConnected||n.forceSend?this._postMessage(i):(console.warn("[Iframily] - No one connected yet, queuing message:",e),this._msgQueue.push(this._postMessage.bind(this,i)))}},{key:"_sendResponse",value:function(e,t){var n={_iframilyUid:this._iframilyUid,_fromType:this._iframilyType,_isResponse:!0,_cbUid:e,_isResolved:t.isResolved,_isRejected:t.isRejected,_cbResolveValue:t.value,_cbRejectError:t.err};this._postMessage(n)}},{key:"_handleMessage",value:function(e){var t=this;e._isResponse?this._handleResponse(e):Promise.resolve().then((function(){return t._msgHandler(e.msg)})).then((function(n){t._sendResponse(e._cbUid,{isResolved:!0,value:n})})).catch((function(n){t._sendResponse(e._cbUid,{isRejected:!0,err:n})}))}},{key:"_handleResponse",value:function(e){if(e._cbUid){var t=this._pendingCb[e._cbUid];if(e._isResolved)t.resolve(e._cbResolveValue);else{if(!e._isRejected)throw new Error("Missing resolve/reject information on response: ".concat(e));t.reject(e._cbRejectError)}delete this._pendingCb[e._cbUid]}}},{key:"_displayDisposedError",value:function(){console.error("[Iframily] - Attempting to use a disposed instance")}},{key:"sendMessage",value:function(e){var t=this;return this._disposed?this._displayDisposedError():new Promise((function(n,i){t._sendMessage(e,{resolve:n,reject:i})}))}},{key:"dispose",value:function(){if(this._disposed)return this._displayDisposedError();this._hasConnected=!1,this._msgQueue=[],this._disposed=!0,this._onDisposedHandler()}},{key:"disposed",get:function(){return this._disposed}},{key:"id",get:function(){return this._id}}])&&i(t.prototype,n),s&&i(t,s),e}()},function(e,t,n){"use strict";function i(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}var r=n(3),o=n(4),s=n(0),a={},c={};e.exports=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e)}var t,n,u;return t=e,u=[{key:"initParent",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!a[e]||a[e].disposed)return a[e]=new o(e,t,n),a[e];console.error('[Iframily] - A parent iframily with id "'.concat(e,'" was already inited, please use another id or dispose the existing one first.'))}},{key:"initChild",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!c[e]||c[e].disposed)return c[e]=new r(e,t,n),c[e];console.error('[Iframily] - A child iframily with id "'.concat(e,'" was already inited, please use another id or dispose the existing one first.'))}},{key:"isIframilyMessage",value:function(e){var t=e&&e.data;return t&&t._iframilyUid&&t._iframilyUid.includes(s.FRAMILY_ID_PREFIX)}}],(n=null)&&i(t.prototype,n),u&&i(t,u),e}()},function(e,t,n){"use strict";function i(e){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function s(e,t,n){return(s="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var i=function(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=f(e)););return e}(e,t);if(i){var r=Object.getOwnPropertyDescriptor(i,t);return r.get?r.get.call(n):r.value}})(e,t,n||e)}function a(e,t){return(a=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,i=f(e);if(t){var r=f(this).constructor;n=Reflect.construct(i,arguments,r)}else n=i.apply(this,arguments);return u(this,n)}}function u(e,t){return!t||"object"!==i(t)&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}var l=n(1),d=n(0);e.exports=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&a(e,t)}(l,e);var t,n,i,u=c(l);function l(){return r(this,l),u.apply(this,arguments)}return t=l,(n=[{key:"_init",value:function(){var e=this;this._iframilyType=d.CHILD,this._targetWindow=window.parent,this._handleMessage=this._handleMessage.bind(this),window.addEventListener("message",this._handleMessage),this._attemptToConnectInterval=setInterval(function t(){return e._sendMessage(d.FRAMILY_INIT,null,{forceSend:!0}),t}(),d.ATTEMPT_TO_CONNECT_INTERVAL)}},{key:"_handleMessage",value:function(e){var t=e&&e.data;t&&t._iframilyUid===this._iframilyUid&&t._fromType===d.PARENT&&(this._hasConnected||t.msg!==d.FRAMILY_INIT_SUCCESSFUL?this._hasConnected&&s(f(l.prototype),"_handleMessage",this).call(this,t):(this._hasConnected=!0,clearInterval(this._attemptToConnectInterval),this._onPairedHandler(),this._sendQueuedMessages()))}},{key:"dispose",value:function(){s(f(l.prototype),"dispose",this).call(this),window.removeEventListener("message",this._handleMessage),clearInterval(this._attemptToConnectInterval)}}])&&o(t.prototype,n),i&&o(t,i),l}(l)},function(e,t,n){"use strict";function i(e){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function s(e,t,n){return(s="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(e,t,n){var i=function(e,t){for(;!Object.prototype.hasOwnProperty.call(e,t)&&null!==(e=f(e)););return e}(e,t);if(i){var r=Object.getOwnPropertyDescriptor(i,t);return r.get?r.get.call(n):r.value}})(e,t,n||e)}function a(e,t){return(a=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,i=f(e);if(t){var r=f(this).constructor;n=Reflect.construct(i,arguments,r)}else n=i.apply(this,arguments);return u(this,n)}}function u(e,t){return!t||"object"!==i(t)&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}var l=n(1),d=n(0);e.exports=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&a(e,t)}(l,e);var t,n,i,u=c(l);function l(){return r(this,l),u.apply(this,arguments)}return t=l,(n=[{key:"_init",value:function(){this._iframilyType=d.PARENT,this._handleMessage=this._handleMessage.bind(this),window.addEventListener("message",this._handleMessage)}},{key:"_handleMessage",value:function(e){var t=e&&e.data;if(t&&t._iframilyUid===this._iframilyUid&&t._fromType===d.CHILD)if(t.msg===d.FRAMILY_INIT){if(!this._hasConnected)for(var n=document.getElementsByTagName("iframe"),i=0;i<n.length;i++)if(n[i].contentWindow===e.source){this._targetWindow=n[i].contentWindow,this._hasConnected=!0,this._onPairedHandler(),this._sendMessage(d.FRAMILY_INIT_SUCCESSFUL),this._sendQueuedMessages();break}}else this._hasConnected&&s(f(l.prototype),"_handleMessage",this).call(this,t)}},{key:"dispose",value:function(){s(f(l.prototype),"dispose",this).call(this),window.removeEventListener("message",this._handleMessage)}}])&&o(t.prototype,n),i&&o(t,i),l}(l)}])})); | ||
//# sourceMappingURL=iframily.min.js.map |
{ | ||
"name": "@ekolabs/iframily", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Modern iframes communication", | ||
@@ -5,0 +5,0 @@ "main": "dist/iframily.min.js", |
# Iframily | ||
Iframily is a tiny (zero dependency) library which modernizes IFrames communication. | ||
Iframily is a small javascript library that simplifies communication between frames. | ||
This is how it works: | ||
1. Create framilies in the parent frame. | ||
2. Create framilies in the child frame. | ||
1. Create iframilies in the parent frame. | ||
2. Create iframilies in the child frame. | ||
3. If a parent id and child id match, they will pair. | ||
4. Send messages between paired framilies. | ||
4. Send messages between paired iframilies. | ||
@@ -25,14 +25,6 @@ ## Features | ||
You can also add the library via script tag and use `window.Iframily`: | ||
You can also add the library via script tag and use `window.Iframily`, like so: | ||
```html | ||
<head> | ||
<script src="https://raw.githubusercontent.com/EkoLabs/iframily/main/dist/iframily.min.js"></script> | ||
</head> | ||
<body> | ||
<script> | ||
let Iframily = window.Iframily; | ||
</script> | ||
</body> | ||
<script src="https://unpkg.com/@ekolabs/iframily"></script> | ||
``` | ||
@@ -54,3 +46,3 @@ | ||
| :-------------: |:--------------:| :------------| | ||
| id | `string` | A unique id to identify this [iframily instance](#iframily-instance), this is used in order to match parent and child framilies. Will abort and log an error if the id already exists in the current frame. | | ||
| id | `string` | A unique id to identify this [iframily instance](#iframily-instance), this is used in order to match parent and child iframilies. Will abort and log an error if the id already exists in the current frame. | | ||
| msgHandler | `function` | Optional - A handler for incoming messages from the paired iframily in the parent/child frame. The `msgHandler` can return back a response value or a promise that will be resolved/rejected with a response value. | | ||
@@ -80,3 +72,3 @@ | options | `object` | Optional - Additional options, see possible options below: | | ||
### Iframily instance | ||
### iframily instance | ||
@@ -100,5 +92,5 @@ The iframily instance is the object returned when initing a new connecter using the `initParent()` or `initChild()` methods. | ||
> * You cannot reuse a disposed instance, you can however create a new instance with the same id. | ||
> * Any framilies paired with this instance will still keep sending messages to the disposed instance but they will be ignored. | ||
> * Any iframilies paired with this instance will still keep sending messages to the disposed instance but they will be ignored. | ||
This method is useful when you have a parent frame which recreates the same child frame and you want to use the same id for the framilies. | ||
This method is useful when you have a parent frame which recreates the same child frame and you want to use the same id for the iframilies. | ||
@@ -121,9 +113,9 @@ #### f.disposed -> `boolean` (read only) | ||
let msgHandler = function(msg) | ||
let msgHandler = function(msg) { | ||
console.log('parent got message:', msg); | ||
}; | ||
let parent = Iframily.initParent('myUniqueId', msgHandler); | ||
let iframilyParent = Iframily.initParent('myUniqueId', msgHandler); | ||
parent.sendMessage('do something') | ||
iframilyParent.sendMessage('do something') | ||
.then((response) => { | ||
@@ -133,3 +125,3 @@ console.log('parent got response:', response); | ||
parent.sendMessage('do something async') | ||
iframilyParent.sendMessage('do something async') | ||
.then((response) => { | ||
@@ -158,5 +150,5 @@ console.log('parent got async response:', response); | ||
let child = Iframily.initChild('myUniqueId', msgHandler); | ||
let iframilyChild = Iframily.initChild('myUniqueId', msgHandler); | ||
child.sendMessage({ text: 'fancy' }); | ||
iframilyChild.sendMessage({ text: 'fancy' }); | ||
``` | ||
@@ -168,3 +160,3 @@ | ||
* You can create multiple framilies in each frame but the unique ids cannot be used more than once per frame (unless the previous one has been disposed). | ||
* You can create multiple iframilies in each frame but the unique ids cannot be used more than once per frame (unless the previous one has been disposed). | ||
* Parent can connect to one child only and vice versa (due to previous note). | ||
@@ -176,3 +168,3 @@ * Designed for modern browsers (IE not supported). | ||
* `npm run dev` - builds unuglified bundle and watches for changes. | ||
* `npm run playground` - launch a simple page with a parent and child frame framilies to manually test out stuff. | ||
* `npm run playground` - launch a simple page with a parent and child frame iframilies to manually test out stuff. | ||
* Please make all push requests against the `develop` branch. | ||
@@ -179,0 +171,0 @@ * Please update/add tests coverage. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42948
178