qlfy-postmate 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.mjs CHANGED
@@ -2,8 +2,8 @@ var u = Object.defineProperty;
2
2
  var g = (n, t, e) => t in n ? u(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
3
  var r = (n, t, e) => g(n, typeof t != "symbol" ? t + "" : t, e);
4
4
  const h = "application/x-postmate-v1+json";
5
- let w = 0;
6
- const y = {
5
+ let y = 0;
6
+ const w = {
7
7
  handshake: 1,
8
8
  "handshake-reply": 1,
9
9
  call: 1,
@@ -12,7 +12,7 @@ const y = {
12
12
  request: 1
13
13
  };
14
14
  function f(n, t) {
15
- return (typeof t != "string" || n.origin === t) && !!n.data && (typeof n.data != "object" || "postmate" in n.data) && n.data.type === h && !!y[n.data.postmate];
15
+ return (typeof t != "string" || n.origin === t) && !!n.data && (typeof n.data != "object" || "postmate" in n.data) && n.data.type === h && !!w[n.data.postmate];
16
16
  }
17
17
  class E {
18
18
  constructor(t) {
@@ -24,7 +24,7 @@ class E {
24
24
  }
25
25
  get(t) {
26
26
  return new l.Promise((e) => {
27
- const s = ++w, i = (a) => {
27
+ const s = ++y, i = (a) => {
28
28
  a.data.uid === s && a.data.postmate === "reply" && (this.parent.removeEventListener("message", i, !1), e(a.data.value));
29
29
  };
30
30
  this.parent.addEventListener("message", i, !1), this.child.postMessage(
@@ -201,7 +201,7 @@ class H {
201
201
  /** 初始化 */
202
202
  async init(t = "") {
203
203
  new l({
204
- container: this.iframeEle,
204
+ container: typeof this.iframeEle == "function" ? this.iframeEle() : this.iframeEle,
205
205
  url: this.iframeUrl
206
206
  }).then((s) => {
207
207
  this.postmateChild = s, s.on("requestBaseData", (i) => {
package/lib/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r["qlfy-postmate"]={}))})(this,function(r){"use strict";var C=Object.defineProperty;var v=(r,n,m)=>n in r?C(r,n,{enumerable:!0,configurable:!0,writable:!0,value:m}):r[n]=m;var l=(r,n,m)=>v(r,typeof n!="symbol"?n+"":n,m);const n="application/x-postmate-v1+json";let m=0;const y={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1};function u(o,t){return(typeof t!="string"||o.origin===t)&&!!o.data&&(typeof o.data!="object"||"postmate"in o.data)&&o.data.type===n&&!!y[o.data.postmate]}class w{constructor(t){this.parent=t.parent,this.frame=t.frame,this.child=t.child,this.childOrigin=t.childOrigin,this.events={},this.listener=e=>{if(!u(e,this.childOrigin))return;const{value:s={}}=e.data,{name:i,data:a}=s;e.data.postmate==="emit"&&i in this.events&&this.events[i].call(this,a)},this.parent.addEventListener("message",this.listener,!1)}get(t){return new h.Promise(e=>{const s=++m,i=a=>{a.data.uid===s&&a.data.postmate==="reply"&&(this.parent.removeEventListener("message",i,!1),e(a.data.value))};this.parent.addEventListener("message",i,!1),this.child.postMessage({postmate:"request",type:n,property:t,uid:s},this.childOrigin)})}call(t,e){this.child.postMessage({postmate:"call",type:n,property:t,data:e},this.childOrigin)}on(t,e){this.events[t]=e}destroy(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)}}class E{constructor(t){this.model=t.model,this.parent=t.parent,this.parentOrigin=t.parentOrigin,this.child=t.child,this.child.addEventListener("message",e=>{if(!u(e,this.parentOrigin))return;const{postmate:s,property:i,uid:a,data:c}=e.data,d=this.model[i];if(s!=="call"){const p=typeof d=="function"?d():d;h.Promise.resolve(p).then(f=>{e.source.postMessage({property:i,postmate:"reply",type:n,uid:a,value:f},e.origin)})}else i in this.model&&typeof d=="function"&&d(c)})}emit(t,e){this.parent.postMessage({postmate:"emit",type:n,value:{name:t,data:e}},this.parentOrigin)}}class h{constructor({container:t=document.body,model:e,url:s,name:i,classListArray:a=[]}){return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=i||"",this.frame.classList.add(...a),t.appendChild(this.frame),this.child=this.frame.contentWindow,this.model=e||{},this.sendHandshake(s)}sendHandshake(t){const e=(()=>{const a=document.createElement("a");a.href=t;const c=a.protocol.length>4?a.protocol:window.location.protocol,d=a.host.length?a.port==="80"||a.port==="443"?a.hostname:a.host:window.location.host;return a.origin||`${c}//${d}`})();let s=0,i;return new h.Promise((a,c)=>{const d=g=>{if(!u(g,e))return!1;g.data.postmate==="handshake-reply"?(clearInterval(i),this.parent.removeEventListener("message",d,!1),this.childOrigin=g.origin,a(new w(this))):c("Failed handshake")};this.parent.addEventListener("message",d,!1);const p=()=>{s++,this.child.postMessage({postmate:"handshake",type:n,model:this.model},e),s===5&&clearInterval(i)},f=()=>{p(),i=setInterval(p,500)};this.frame.onload=f,this.frame.attachEvent&&this.frame.attachEvent("onload",f),this.frame.src=t})}}class P{constructor(t){return this.child=window,this.model=t,this.parent=window.parent,this.sendHandshakeReply()}sendHandshakeReply(){return new h.Promise((t,e)=>{this.child.addEventListener("message",s=>{if(s.data.postmate){if(s.data.postmate!=="handshake")return e("Handshake Reply Failed");this.child.removeEventListener("message",this,!1),s.source.postMessage({postmate:"handshake-reply",type:n},s.origin),this.parentOrigin=s.origin;const i=s.data.model;i&&Object.keys(i).forEach(a=>{this.model[a]=i[a]}),t(new E(this))}},!1)})}}h.debug=!1,h.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),h.Model=P;class k{constructor(t,e){l(this,"childPostmate",null);l(this,"parentPostmate",null);l(this,"isGetData",!1);l(this,"getDataCllBack",null);window.frameElement==null?this.init(t,e):console.warn("未嵌入主系统")}init(t,e){const s=this;this.childPostmate=new h.Model({baseData(i){s.isGetData?s.getDataCllBack(i):t(i),s.isGetData=!1}}).then(i=>{s.parentPostmate=i,s.getData(),e(i)})}getData(t=""){if(!this.parentPostmate)return console.warn("父页面未连接");t?(this.isGetData=!0,this.getDataCllBack=t):(this.isGetData=!1,this.getDataCllBack=null),this.parentPostmate.emit("requestBaseData")}}class D{constructor(t,e,s=!0,i="",a=""){l(this,"postmateChild",null);l(this,"loginInfo");l(this,"token");l(this,"isHideHeader",!0);l(this,"iframeEle","");l(this,"iframeUrl","");this.loginInfo=t,this.token=e,this.isHideHeader=s,this.iframeEle=i,this.iframeUrl=a,this.iframeEle&&this.iframeUrl&&this.init()}async init(t=""){new h({container:this.iframeEle,url:this.iframeUrl}).then(s=>{this.postmateChild=s,s.on("requestBaseData",i=>{this.sendData()}),t&&t()})}setIframeEle(t){this.iframeEle=t,this.iframeEle&&this.iframeUrl&&this.init()}setIframeUrl(t){this.iframeUrl=t,this.iframeEle&&this.iframeUrl&&this.init()}sendData(){this.postmateChild&&this.postmateChild.call("baseData",{loginInfo:this.loginInfo,token:this.token,isHideHeader:this.isHideHeader})}}r.ChildPostmate=k,r.ParentPostmate=D,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r["qlfy-postmate"]={}))})(this,function(r){"use strict";var C=Object.defineProperty;var v=(r,n,m)=>n in r?C(r,n,{enumerable:!0,configurable:!0,writable:!0,value:m}):r[n]=m;var l=(r,n,m)=>v(r,typeof n!="symbol"?n+"":n,m);const n="application/x-postmate-v1+json";let m=0;const y={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1};function u(o,t){return(typeof t!="string"||o.origin===t)&&!!o.data&&(typeof o.data!="object"||"postmate"in o.data)&&o.data.type===n&&!!y[o.data.postmate]}class w{constructor(t){this.parent=t.parent,this.frame=t.frame,this.child=t.child,this.childOrigin=t.childOrigin,this.events={},this.listener=e=>{if(!u(e,this.childOrigin))return;const{value:s={}}=e.data,{name:i,data:a}=s;e.data.postmate==="emit"&&i in this.events&&this.events[i].call(this,a)},this.parent.addEventListener("message",this.listener,!1)}get(t){return new h.Promise(e=>{const s=++m,i=a=>{a.data.uid===s&&a.data.postmate==="reply"&&(this.parent.removeEventListener("message",i,!1),e(a.data.value))};this.parent.addEventListener("message",i,!1),this.child.postMessage({postmate:"request",type:n,property:t,uid:s},this.childOrigin)})}call(t,e){this.child.postMessage({postmate:"call",type:n,property:t,data:e},this.childOrigin)}on(t,e){this.events[t]=e}destroy(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)}}class E{constructor(t){this.model=t.model,this.parent=t.parent,this.parentOrigin=t.parentOrigin,this.child=t.child,this.child.addEventListener("message",e=>{if(!u(e,this.parentOrigin))return;const{postmate:s,property:i,uid:a,data:c}=e.data,d=this.model[i];if(s!=="call"){const p=typeof d=="function"?d():d;h.Promise.resolve(p).then(f=>{e.source.postMessage({property:i,postmate:"reply",type:n,uid:a,value:f},e.origin)})}else i in this.model&&typeof d=="function"&&d(c)})}emit(t,e){this.parent.postMessage({postmate:"emit",type:n,value:{name:t,data:e}},this.parentOrigin)}}class h{constructor({container:t=document.body,model:e,url:s,name:i,classListArray:a=[]}){return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=i||"",this.frame.classList.add(...a),t.appendChild(this.frame),this.child=this.frame.contentWindow,this.model=e||{},this.sendHandshake(s)}sendHandshake(t){const e=(()=>{const a=document.createElement("a");a.href=t;const c=a.protocol.length>4?a.protocol:window.location.protocol,d=a.host.length?a.port==="80"||a.port==="443"?a.hostname:a.host:window.location.host;return a.origin||`${c}//${d}`})();let s=0,i;return new h.Promise((a,c)=>{const d=g=>{if(!u(g,e))return!1;g.data.postmate==="handshake-reply"?(clearInterval(i),this.parent.removeEventListener("message",d,!1),this.childOrigin=g.origin,a(new w(this))):c("Failed handshake")};this.parent.addEventListener("message",d,!1);const p=()=>{s++,this.child.postMessage({postmate:"handshake",type:n,model:this.model},e),s===5&&clearInterval(i)},f=()=>{p(),i=setInterval(p,500)};this.frame.onload=f,this.frame.attachEvent&&this.frame.attachEvent("onload",f),this.frame.src=t})}}class P{constructor(t){return this.child=window,this.model=t,this.parent=window.parent,this.sendHandshakeReply()}sendHandshakeReply(){return new h.Promise((t,e)=>{this.child.addEventListener("message",s=>{if(s.data.postmate){if(s.data.postmate!=="handshake")return e("Handshake Reply Failed");this.child.removeEventListener("message",this,!1),s.source.postMessage({postmate:"handshake-reply",type:n},s.origin),this.parentOrigin=s.origin;const i=s.data.model;i&&Object.keys(i).forEach(a=>{this.model[a]=i[a]}),t(new E(this))}},!1)})}}h.debug=!1,h.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),h.Model=P;class k{constructor(t,e){l(this,"childPostmate",null);l(this,"parentPostmate",null);l(this,"isGetData",!1);l(this,"getDataCllBack",null);window.frameElement==null?this.init(t,e):console.warn("未嵌入主系统")}init(t,e){const s=this;this.childPostmate=new h.Model({baseData(i){s.isGetData?s.getDataCllBack(i):t(i),s.isGetData=!1}}).then(i=>{s.parentPostmate=i,s.getData(),e(i)})}getData(t=""){if(!this.parentPostmate)return console.warn("父页面未连接");t?(this.isGetData=!0,this.getDataCllBack=t):(this.isGetData=!1,this.getDataCllBack=null),this.parentPostmate.emit("requestBaseData")}}class D{constructor(t,e,s=!0,i="",a=""){l(this,"postmateChild",null);l(this,"loginInfo");l(this,"token");l(this,"isHideHeader",!0);l(this,"iframeEle","");l(this,"iframeUrl","");this.loginInfo=t,this.token=e,this.isHideHeader=s,this.iframeEle=i,this.iframeUrl=a,this.iframeEle&&this.iframeUrl&&this.init()}async init(t=""){new h({container:typeof this.iframeEle=="function"?this.iframeEle():this.iframeEle,url:this.iframeUrl}).then(s=>{this.postmateChild=s,s.on("requestBaseData",i=>{this.sendData()}),t&&t()})}setIframeEle(t){this.iframeEle=t,this.iframeEle&&this.iframeUrl&&this.init()}setIframeUrl(t){this.iframeUrl=t,this.iframeEle&&this.iframeUrl&&this.init()}sendData(){this.postmateChild&&this.postmateChild.call("baseData",{loginInfo:this.loginInfo,token:this.token,isHideHeader:this.isHideHeader})}}r.ChildPostmate=k,r.ParentPostmate=D,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qlfy-postmate",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "types": "lib/index.d.ts",
6
6
  "module": "lib/index.umd.js",