qlfy-postmate 1.1.6 → 1.1.7
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/README.md +1 -1
- package/lib/index.mjs +37 -37
- package/lib/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
package/lib/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
var
|
|
2
|
-
var g = (r, t, e) => t in r ?
|
|
1
|
+
var p = Object.defineProperty;
|
|
2
|
+
var g = (r, t, e) => t in r ? p(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
3
|
var l = (r, t, e) => g(r, typeof t != "symbol" ? t + "" : t, e);
|
|
4
4
|
const h = "application/x-postmate-v1+json";
|
|
5
5
|
let y = 0;
|
|
6
|
-
const
|
|
6
|
+
const w = {
|
|
7
7
|
handshake: 1,
|
|
8
8
|
"handshake-reply": 1,
|
|
9
9
|
call: 1,
|
|
@@ -11,23 +11,23 @@ const P = {
|
|
|
11
11
|
reply: 1,
|
|
12
12
|
request: 1
|
|
13
13
|
};
|
|
14
|
-
function
|
|
15
|
-
return (typeof t != "string" || r.origin === t) && !!r.data && (typeof r.data != "object" || "postmate" in r.data) && r.data.type === h && !!
|
|
14
|
+
function f(r, t) {
|
|
15
|
+
return (typeof t != "string" || r.origin === t) && !!r.data && (typeof r.data != "object" || "postmate" in r.data) && r.data.type === h && !!w[r.data.postmate];
|
|
16
16
|
}
|
|
17
|
-
class
|
|
17
|
+
class P {
|
|
18
18
|
constructor(t) {
|
|
19
19
|
this.parent = t.parent, this.frame = t.frame, this.child = t.child, this.childOrigin = t.childOrigin, this.events = {}, this.listener = (e) => {
|
|
20
|
-
if (!
|
|
21
|
-
const { value: s = {} } = e.data, { name:
|
|
22
|
-
e.data.postmate === "emit" &&
|
|
20
|
+
if (!f(e, this.childOrigin)) return;
|
|
21
|
+
const { value: s = {} } = e.data, { name: i, data: a } = s;
|
|
22
|
+
e.data.postmate === "emit" && i in this.events && this.events[i].call(this, a);
|
|
23
23
|
}, this.parent.addEventListener("message", this.listener, !1);
|
|
24
24
|
}
|
|
25
25
|
get(t) {
|
|
26
26
|
return new o.Promise((e) => {
|
|
27
|
-
const s = ++y,
|
|
28
|
-
|
|
27
|
+
const s = ++y, i = (a) => {
|
|
28
|
+
a.data.uid === s && a.data.postmate === "reply" && (this.parent.removeEventListener("message", i, !1), e(a.data.value));
|
|
29
29
|
};
|
|
30
|
-
this.parent.addEventListener("message",
|
|
30
|
+
this.parent.addEventListener("message", i, !1), this.child.postMessage(
|
|
31
31
|
{
|
|
32
32
|
postmate: "request",
|
|
33
33
|
type: h,
|
|
@@ -59,23 +59,23 @@ class w {
|
|
|
59
59
|
class E {
|
|
60
60
|
constructor(t) {
|
|
61
61
|
this.model = t.model, this.parent = t.parent, this.parentOrigin = t.parentOrigin, this.child = t.child, this.child.addEventListener("message", (e) => {
|
|
62
|
-
if (!
|
|
63
|
-
const { postmate: s, property:
|
|
62
|
+
if (!f(e, this.parentOrigin)) return;
|
|
63
|
+
const { postmate: s, property: i, uid: a, data: d } = e.data, n = this.model[i];
|
|
64
64
|
if (s !== "call") {
|
|
65
65
|
const c = typeof n == "function" ? n() : n;
|
|
66
66
|
o.Promise.resolve(c).then((m) => {
|
|
67
67
|
e.source.postMessage(
|
|
68
68
|
{
|
|
69
|
-
property:
|
|
69
|
+
property: i,
|
|
70
70
|
postmate: "reply",
|
|
71
71
|
type: h,
|
|
72
|
-
uid:
|
|
72
|
+
uid: a,
|
|
73
73
|
value: m
|
|
74
74
|
},
|
|
75
75
|
e.origin
|
|
76
76
|
);
|
|
77
77
|
});
|
|
78
|
-
} else
|
|
78
|
+
} else i in this.model && typeof n == "function" && n(d);
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
emit(t, e) {
|
|
@@ -90,21 +90,21 @@ class E {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
class o {
|
|
93
|
-
constructor({ container: t = document.body, model: e, url: s, name:
|
|
94
|
-
return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name =
|
|
93
|
+
constructor({ container: t = document.body, model: e, url: s, name: i, classListArray: a = [] }) {
|
|
94
|
+
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);
|
|
95
95
|
}
|
|
96
96
|
sendHandshake(t) {
|
|
97
97
|
const e = (() => {
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
const d =
|
|
101
|
-
return
|
|
98
|
+
const a = document.createElement("a");
|
|
99
|
+
a.href = t;
|
|
100
|
+
const d = a.protocol.length > 4 ? a.protocol : window.location.protocol, n = a.host.length ? a.port === "80" || a.port === "443" ? a.hostname : a.host : window.location.host;
|
|
101
|
+
return a.origin || `${d}//${n}`;
|
|
102
102
|
})();
|
|
103
|
-
let s = 0,
|
|
104
|
-
return new o.Promise((
|
|
103
|
+
let s = 0, i;
|
|
104
|
+
return new o.Promise((a, d) => {
|
|
105
105
|
const n = (u) => {
|
|
106
|
-
if (!
|
|
107
|
-
u.data.postmate === "handshake-reply" ? (clearInterval(
|
|
106
|
+
if (!f(u, e)) return !1;
|
|
107
|
+
u.data.postmate === "handshake-reply" ? (clearInterval(i), this.parent.removeEventListener("message", n, !1), this.childOrigin = u.origin, a(new P(this))) : d("Failed handshake");
|
|
108
108
|
};
|
|
109
109
|
this.parent.addEventListener("message", n, !1);
|
|
110
110
|
const c = () => {
|
|
@@ -115,9 +115,9 @@ class o {
|
|
|
115
115
|
model: this.model
|
|
116
116
|
},
|
|
117
117
|
e
|
|
118
|
-
), s === 5 && clearInterval(
|
|
118
|
+
), s === 5 && clearInterval(i);
|
|
119
119
|
}, m = () => {
|
|
120
|
-
c(),
|
|
120
|
+
c(), i = setInterval(c, 500);
|
|
121
121
|
};
|
|
122
122
|
this.frame.onload = m, this.frame.attachEvent && this.frame.attachEvent("onload", m), this.frame.src = t;
|
|
123
123
|
});
|
|
@@ -139,9 +139,9 @@ class I {
|
|
|
139
139
|
},
|
|
140
140
|
s.origin
|
|
141
141
|
), this.parentOrigin = s.origin;
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
this.model[
|
|
142
|
+
const i = s.data.model;
|
|
143
|
+
i && Object.keys(i).forEach((a) => {
|
|
144
|
+
this.model[a] = i[a];
|
|
145
145
|
}), t(new E(this));
|
|
146
146
|
}
|
|
147
147
|
}, !1);
|
|
@@ -239,14 +239,14 @@ class v {
|
|
|
239
239
|
container: e,
|
|
240
240
|
url: this.currentUrl
|
|
241
241
|
});
|
|
242
|
-
this.currentIframe = e.querySelector("iframe"), this.currentIframe.addEventListener("load", () => {
|
|
242
|
+
this.currentIframe = e.querySelector("iframe"), this.currentIframe.setAttribute("frameborder", "0"), this.currentIframe.setAttribute("allow", "fullscreen"), this.currentIframe.addEventListener("load", () => {
|
|
243
243
|
this.loadFunction();
|
|
244
|
-
}), s.then((
|
|
245
|
-
|
|
244
|
+
}), s.then((i) => {
|
|
245
|
+
this.postmateParent = i, i.on("requestBaseData", (a) => {
|
|
246
246
|
this.sendData();
|
|
247
247
|
}), typeof t == "function" && t();
|
|
248
|
-
}).catch((
|
|
249
|
-
console.error("Postmate 连接失败:",
|
|
248
|
+
}).catch((i) => {
|
|
249
|
+
console.error("Postmate 连接失败:", i);
|
|
250
250
|
});
|
|
251
251
|
}
|
|
252
252
|
/** 设置load执行事件 */
|
package/lib/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n["qlfy-postmate"]={}))})(this,function(n){"use strict";var k=Object.defineProperty;var v=(n,r,c)=>r in n?k(n,r,{enumerable:!0,configurable:!0,writable:!0,value:c}):n[r]=c;var d=(n,r,c)=>v(n,typeof r!="symbol"?r+"":r,c);const r="application/x-postmate-v1+json";let c=0;const y={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1};function p(o,t){return(typeof t!="string"||o.origin===t)&&!!o.data&&(typeof o.data!="object"||"postmate"in o.data)&&o.data.type===r&&!!y[o.data.postmate]}class P{constructor(t){this.parent=t.parent,this.frame=t.frame,this.child=t.child,this.childOrigin=t.childOrigin,this.events={},this.listener=e=>{if(!p(e,this.childOrigin))return;const{value:s={}}=e.data,{name:
|
|
1
|
+
(function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n["qlfy-postmate"]={}))})(this,function(n){"use strict";var k=Object.defineProperty;var v=(n,r,c)=>r in n?k(n,r,{enumerable:!0,configurable:!0,writable:!0,value:c}):n[r]=c;var d=(n,r,c)=>v(n,typeof r!="symbol"?r+"":r,c);const r="application/x-postmate-v1+json";let c=0;const y={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1};function p(o,t){return(typeof t!="string"||o.origin===t)&&!!o.data&&(typeof o.data!="object"||"postmate"in o.data)&&o.data.type===r&&!!y[o.data.postmate]}class P{constructor(t){this.parent=t.parent,this.frame=t.frame,this.child=t.child,this.childOrigin=t.childOrigin,this.events={},this.listener=e=>{if(!p(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 l.Promise(e=>{const s=++c,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:r,property:t,uid:s},this.childOrigin)})}call(t,e){this.child.postMessage({postmate:"call",type:r,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 w{constructor(t){this.model=t.model,this.parent=t.parent,this.parentOrigin=t.parentOrigin,this.child=t.child,this.child.addEventListener("message",e=>{if(!p(e,this.parentOrigin))return;const{postmate:s,property:i,uid:a,data:m}=e.data,h=this.model[i];if(s!=="call"){const u=typeof h=="function"?h():h;l.Promise.resolve(u).then(f=>{e.source.postMessage({property:i,postmate:"reply",type:r,uid:a,value:f},e.origin)})}else i in this.model&&typeof h=="function"&&h(m)})}emit(t,e){this.parent.postMessage({postmate:"emit",type:r,value:{name:t,data:e}},this.parentOrigin)}}class l{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 m=a.protocol.length>4?a.protocol:window.location.protocol,h=a.host.length?a.port==="80"||a.port==="443"?a.hostname:a.host:window.location.host;return a.origin||`${m}//${h}`})();let s=0,i;return new l.Promise((a,m)=>{const h=g=>{if(!p(g,e))return!1;g.data.postmate==="handshake-reply"?(clearInterval(i),this.parent.removeEventListener("message",h,!1),this.childOrigin=g.origin,a(new P(this))):m("Failed handshake")};this.parent.addEventListener("message",h,!1);const u=()=>{s++,this.child.postMessage({postmate:"handshake",type:r,model:this.model},e),s===5&&clearInterval(i)},f=()=>{u(),i=setInterval(u,500)};this.frame.onload=f,this.frame.attachEvent&&this.frame.attachEvent("onload",f),this.frame.src=t})}}class E{constructor(t){return this.child=window,this.model=t,this.parent=window.parent,this.sendHandshakeReply()}sendHandshakeReply(){return new l.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:r},s.origin),this.parentOrigin=s.origin;const i=s.data.model;i&&Object.keys(i).forEach(a=>{this.model[a]=i[a]}),t(new w(this))}},!1)})}}l.debug=!1,l.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),l.Model=E;class I{constructor(t){this.childPostmate=null,this.parentPostmate=null,this.isGetData=!1,this.getDataCllBack=null,window.top!==window.self?this.init(t):t.error({status:500,data:null,message:"未在一体化平台中运行"})}init(t){const e=this;this.childPostmate=new l.Model({baseData(s){s=JSON.parse(s),e.isGetData?e.getDataCllBack&&e.getDataCllBack({status:200,message:"success",data:s}):t.receive({status:200,message:"success",data:s}),e.isGetData=!1}}).then(s=>{e.parentPostmate=s,e.getData(),t.success({status:200,message:"success",data:{ChildPostmate:s}})}).catch(s=>{t.error({status:500,data:null,message:s})})}getData(t){if(!this.parentPostmate){t({status:500,message:"父页面未连接",data:null});return}t?(this.isGetData=!0,this.getDataCllBack=t):(this.isGetData=!1,this.getDataCllBack=null),this.parentPostmate.emit("requestBaseData")}}class D{constructor(t,e=null,s=""){d(this,"postmateParent",null);d(this,"dataInfo",{loginInfo:null,token:"",isHideHeader:!0});d(this,"iframeEle",null);d(this,"currentUrl","");d(this,"currentIframe",null);d(this,"loadFunction",null);this.dataInfo=t||this.dataInfo,this.setIframeEle(e),this.setIframeUrl(s)}clear(){this.postmateParent&&(this.postmateParent.destroy(),this.postmateParent=null),this.currentIframe&&this.currentIframe.parentNode&&this.currentIframe.parentNode.removeChild(this.currentIframe),this.currentIframe=null}async init(t=null){if(this.clear(),!this.iframeEle||!this.currentUrl){console.warn("iframeEle 和 currentUrl 是必需的");return}const e=typeof this.iframeEle=="function"?this.iframeEle():this.iframeEle;if(!e){console.warn("无法获取 iframe 容器");return}const s=new l({container:e,url:this.currentUrl});this.currentIframe=e.querySelector("iframe"),this.currentIframe.setAttribute("frameborder","0"),this.currentIframe.setAttribute("allow","fullscreen"),this.currentIframe.addEventListener("load",()=>{this.loadFunction()}),s.then(i=>{this.postmateParent=i,i.on("requestBaseData",a=>{this.sendData()}),typeof t=="function"&&t()}).catch(i=>{console.error("Postmate 连接失败:",i)})}setLoadFunction(t){this.loadFunction=t}setIframeEle(t){this.iframeEle!==t&&(this.iframeEle=t,this.iframeEle&&this.currentUrl&&this.init())}setIframeUrl(t){this.currentUrl!==t&&(this.currentUrl=t,this.iframeEle&&this.currentUrl&&this.init())}sendData(){this.postmateParent&&this.postmateParent.call("baseData",JSON.stringify(this.dataInfo))}destroy(){this.clear()}}n.ChildPostmate=I,n.ParentPostmate=D,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
|