-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpostmate.min.js
More file actions
8 lines (8 loc) · 5.27 KB
/
postmate.min.js
File metadata and controls
8 lines (8 loc) · 5.27 KB
1
2
3
4
5
6
7
8
/**
postmatex - Fork of original: A powerful, simple, promise-based postMessage library
@version v1.7.5
@link https://github.com/johndcarmichael/postmatex
@author Jacob Kelley <jakie8@gmail.com>
@license MIT
**/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Postmate=t()}(this,function(){"use strict";function h(){var e;return o.debug?(e=console).log.apply(e,arguments):null}function l(e,t){return("string"!=typeof t||e.origin===t)&&(!!e.data&&(("object"!=typeof e.data||"postmate"in e.data)&&(e.data.type===c&&!!n[e.data.postmate])))}var c="application/x-postmate-v1+json",r=0,n={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},p=function(){function e(e){var i=this;this.parent=e.parent,this.frame=e.frame,this.child=e.child,this.childOrigin=e.childOrigin,this.events={},h("Parent: Registering API"),h("Parent: Awaiting messages..."),this.listener=function(e){if(!l(e,i.childOrigin))return!1;var t=((e||{}).data||{}).value||{},n=t.data,t=t.name;"emit"===e.data.postmate&&(h("Parent: Received event emission: "+t),t in i.events&&i.events[t].forEach(function(e){e.call(i,n)}))},this.parent.addEventListener("message",this.listener,!1),h("Parent: Awaiting event emissions from Child")}var t=e.prototype;return t.get=function(e){var a=this;return new o.Promise(function(t){function n(e){e.data.uid===i&&"reply"===e.data.postmate&&(a.parent.removeEventListener("message",n,!1),t(e.data.value))}var i=++r;a.parent.addEventListener("message",n,!1),a.child.postMessage({postmate:"request",type:c,property:e,uid:i},a.childOrigin)})},t.call=function(e,t){this.child.postMessage({postmate:"call",type:c,property:e,data:t},this.childOrigin)},t.on=function(e,t){this.events[e]||(this.events[e]=[]),this.events[e].push(t)},t.destroy=function(){h("Parent: Destroying Postmate instance"),window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)},e}(),s=function(){function e(e){var a=this;this.model=e.model,this.parent=e.parent,this.parentOrigin=e.parentOrigin,this.child=e.child,h("Child: Registering API"),h("Child: Awaiting messages..."),this.child.addEventListener("message",function(t){var n,i,e;l(t,a.parentOrigin)&&(h("Child: Received request",t.data),e=t.data,n=e.property,i=e.uid,e=e.data,"call"!==t.data.postmate?function(e,t){t="function"==typeof e[t]?e[t]():e[t];return o.Promise.resolve(t)}(a.model,n).then(function(e){return t.source.postMessage({property:n,postmate:"reply",type:c,uid:i,value:e},t.origin)}):n in a.model&&"function"==typeof a.model[n]&&a.model[n](e))})}return e.prototype.emit=function(e,t){h('Child: Emitting Event "'+e+'"',t),this.parent.postMessage({postmate:"emit",type:c,value:{name:e,data:t}},this.parentOrigin)},e}(),o=function(){function e(e){var t=this,n=e.container,i=void 0===n?void 0!==i?i:document.body:n,a=e.model,r=e.url,s=e.name,n=e.classListArray,n=void 0===n?[]:n,e=e.attributes,e=void 0===e?[]:e;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=s||"",e.forEach(function(e){t.frame.setAttribute(e.name,e.value)}),0<n.length&&this.frame.classList.add.apply(this.frame.classList,n),i.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=a||{},this.sendHandshake(r)}return e.prototype.sendHandshake=function(a){var r,s=this,o=function(e){var t=document.createElement("a");t.href=e;var n=(4<t.protocol.length?t:window.location).protocol,e=t.host.length?"80"===t.port||"443"===t.port?t.hostname:t.host:window.location.host;return t.origin||n+"//"+e}(a),d=0;return new e.Promise(function(n,i){var e=function e(t){return!!l(t,o)&&("handshake-reply"===t.data.postmate?(clearInterval(r),h("Parent: Received handshake reply from Child"),s.parent.removeEventListener("message",e,!1),s.childOrigin=t.origin,h("Parent: Saving Child origin",s.childOrigin),n(new p(s))):(h("Parent: Invalid handshake reply"),i("Failed handshake")))};s.parent.addEventListener("message",e,!1);function t(){if(5<++d)return clearInterval(r),i("Handshake Timeout Reached");h("Parent: Sending handshake attempt "+d,{childOrigin:o}),s.child.postMessage({postmate:"handshake",type:c,model:s.model},o)}e=function(){t(),r=setInterval(t,500)};s.frame.attachEvent?s.frame.attachEvent("onload",e):s.frame.addEventListener("load",e),h("Parent: Loading frame",{url:a}),s.frame.src=a})},e}();return o.debug=!1,o.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),o.Model=function(){function e(e){return this.child=window,this.model=e,this.parent=this.child.parent,this.sendHandshakeReply()}return e.prototype.sendHandshakeReply=function(){var r=this;return new o.Promise(function(n,i){function a(e){if(e.data.postmate){if("handshake"!==e.data.postmate)return i("Handshake Reply Failed");h("Child: Received handshake from Parent"),r.child.removeEventListener("message",a,!1),h("Child: Sending handshake reply to Parent"),e.source.postMessage({postmate:"handshake-reply",type:c},e.origin),r.parentOrigin=e.origin;var t=e.data.model;return t&&(Object.keys(t).forEach(function(e){r.model[e]=t[e]}),h("Child: Inherited and extended model from Parent")),h("Child: Saving Parent origin",r.parentOrigin),n(new s(r))}}r.child.addEventListener("message",a,!1)})},e}(),o});