qlfy-postmate 1.0.6 → 1.0.8
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 +110 -88
- package/lib/index.umd.js +1 -1
- package/lib/postMeta/ChildPostmate.d.ts +16 -0
- package/package.json +1 -1
package/lib/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
var u = Object.defineProperty;
|
|
2
|
-
var g = (
|
|
3
|
-
var
|
|
4
|
-
const
|
|
2
|
+
var g = (r, t, e) => t in r ? u(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
|
+
var h = (r, t, e) => g(r, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
const l = "application/x-postmate-v1+json";
|
|
5
5
|
let y = 0;
|
|
6
|
-
const
|
|
6
|
+
const P = {
|
|
7
7
|
handshake: 1,
|
|
8
8
|
"handshake-reply": 1,
|
|
9
9
|
call: 1,
|
|
@@ -11,28 +11,28 @@ const w = {
|
|
|
11
11
|
reply: 1,
|
|
12
12
|
request: 1
|
|
13
13
|
};
|
|
14
|
-
function f(
|
|
15
|
-
return (typeof t != "string" ||
|
|
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 === l && !!P[r.data.postmate];
|
|
16
16
|
}
|
|
17
17
|
class E {
|
|
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
20
|
if (!f(e, this.childOrigin)) return;
|
|
21
|
-
const { value:
|
|
22
|
-
e.data.postmate === "emit" &&
|
|
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
|
-
return new
|
|
27
|
-
const
|
|
28
|
-
a.data.uid ===
|
|
26
|
+
return new o.Promise((e) => {
|
|
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
|
-
type:
|
|
33
|
+
type: l,
|
|
34
34
|
property: t,
|
|
35
|
-
uid:
|
|
35
|
+
uid: s
|
|
36
36
|
},
|
|
37
37
|
this.childOrigin
|
|
38
38
|
);
|
|
@@ -42,7 +42,7 @@ class E {
|
|
|
42
42
|
this.child.postMessage(
|
|
43
43
|
{
|
|
44
44
|
postmate: "call",
|
|
45
|
-
type:
|
|
45
|
+
type: l,
|
|
46
46
|
property: t,
|
|
47
47
|
data: e
|
|
48
48
|
},
|
|
@@ -56,68 +56,68 @@ class E {
|
|
|
56
56
|
window.removeEventListener("message", this.listener, !1), this.frame.parentNode.removeChild(this.frame);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
class
|
|
59
|
+
class w {
|
|
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
62
|
if (!f(e, this.parentOrigin)) return;
|
|
63
|
-
const { postmate:
|
|
64
|
-
if (
|
|
65
|
-
const m = typeof
|
|
66
|
-
|
|
63
|
+
const { postmate: s, property: i, uid: a, data: d } = e.data, n = this.model[i];
|
|
64
|
+
if (s !== "call") {
|
|
65
|
+
const m = typeof n == "function" ? n() : n;
|
|
66
|
+
o.Promise.resolve(m).then((c) => {
|
|
67
67
|
e.source.postMessage(
|
|
68
68
|
{
|
|
69
|
-
property:
|
|
69
|
+
property: i,
|
|
70
70
|
postmate: "reply",
|
|
71
|
-
type:
|
|
71
|
+
type: l,
|
|
72
72
|
uid: a,
|
|
73
73
|
value: c
|
|
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) {
|
|
82
82
|
this.parent.postMessage(
|
|
83
83
|
{
|
|
84
84
|
postmate: "emit",
|
|
85
|
-
type:
|
|
85
|
+
type: l,
|
|
86
86
|
value: { name: t, data: e }
|
|
87
87
|
},
|
|
88
88
|
this.parentOrigin
|
|
89
89
|
);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
class
|
|
93
|
-
constructor({ container: t = document.body, model: e, url:
|
|
94
|
-
return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name =
|
|
92
|
+
class o {
|
|
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
98
|
const a = document.createElement("a");
|
|
99
99
|
a.href = t;
|
|
100
|
-
const d = a.protocol.length > 4 ? a.protocol : window.location.protocol,
|
|
101
|
-
return a.origin || `${d}//${
|
|
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
|
|
104
|
-
return new
|
|
105
|
-
const
|
|
103
|
+
let s = 0, i;
|
|
104
|
+
return new o.Promise((a, d) => {
|
|
105
|
+
const n = (p) => {
|
|
106
106
|
if (!f(p, e)) return !1;
|
|
107
|
-
p.data.postmate === "handshake-reply" ? (clearInterval(
|
|
107
|
+
p.data.postmate === "handshake-reply" ? (clearInterval(i), this.parent.removeEventListener("message", n, !1), this.childOrigin = p.origin, a(new E(this))) : d("Failed handshake");
|
|
108
108
|
};
|
|
109
|
-
this.parent.addEventListener("message",
|
|
109
|
+
this.parent.addEventListener("message", n, !1);
|
|
110
110
|
const m = () => {
|
|
111
|
-
|
|
111
|
+
s++, this.child.postMessage(
|
|
112
112
|
{
|
|
113
113
|
postmate: "handshake",
|
|
114
|
-
type:
|
|
114
|
+
type: l,
|
|
115
115
|
model: this.model
|
|
116
116
|
},
|
|
117
117
|
e
|
|
118
|
-
),
|
|
118
|
+
), s === 5 && clearInterval(i);
|
|
119
119
|
}, c = () => {
|
|
120
|
-
m(),
|
|
120
|
+
m(), i = setInterval(m, 500);
|
|
121
121
|
};
|
|
122
122
|
this.frame.onload = c, this.frame.attachEvent && this.frame.attachEvent("onload", c), this.frame.src = t;
|
|
123
123
|
});
|
|
@@ -128,83 +128,106 @@ class k {
|
|
|
128
128
|
return this.child = window, this.model = t, this.parent = window.parent, this.sendHandshakeReply();
|
|
129
129
|
}
|
|
130
130
|
sendHandshakeReply() {
|
|
131
|
-
return new
|
|
132
|
-
this.child.addEventListener("message", (
|
|
133
|
-
if (
|
|
134
|
-
if (
|
|
135
|
-
this.child.removeEventListener("message", this, !1),
|
|
131
|
+
return new o.Promise((t, e) => {
|
|
132
|
+
this.child.addEventListener("message", (s) => {
|
|
133
|
+
if (s.data.postmate) {
|
|
134
|
+
if (s.data.postmate !== "handshake") return e("Handshake Reply Failed");
|
|
135
|
+
this.child.removeEventListener("message", this, !1), s.source.postMessage(
|
|
136
136
|
{
|
|
137
137
|
postmate: "handshake-reply",
|
|
138
|
-
type:
|
|
138
|
+
type: l
|
|
139
139
|
},
|
|
140
|
-
|
|
141
|
-
), this.parentOrigin =
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
this.model[a] =
|
|
145
|
-
}), t(new
|
|
140
|
+
s.origin
|
|
141
|
+
), this.parentOrigin = s.origin;
|
|
142
|
+
const i = s.data.model;
|
|
143
|
+
i && Object.keys(i).forEach((a) => {
|
|
144
|
+
this.model[a] = i[a];
|
|
145
|
+
}), t(new w(this));
|
|
146
146
|
}
|
|
147
147
|
}, !1);
|
|
148
148
|
});
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
|
-
|
|
152
|
-
|
|
151
|
+
o.debug = !1;
|
|
152
|
+
o.Promise = (() => {
|
|
153
153
|
try {
|
|
154
154
|
return typeof window < "u" ? window.Promise : Promise;
|
|
155
155
|
} catch {
|
|
156
156
|
return Promise;
|
|
157
157
|
}
|
|
158
158
|
})();
|
|
159
|
-
|
|
159
|
+
o.Model = k;
|
|
160
160
|
class C {
|
|
161
|
-
constructor(t
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
init(t, e) {
|
|
169
|
-
const i = this;
|
|
170
|
-
this.childPostmate = new l.Model({
|
|
161
|
+
constructor(t) {
|
|
162
|
+
this.childPostmate = null, this.parentPostmate = null, this.isGetData = !1, this.getDataCllBack = null, window.frameElement ? t.error({ status: 500, data: null, message: "未在一体化平台中运行" }) : this.init(t);
|
|
163
|
+
}
|
|
164
|
+
init(t) {
|
|
165
|
+
const e = this;
|
|
166
|
+
this.childPostmate = new o.Model({
|
|
171
167
|
// 定义子页面可以暴露给父页面的方法
|
|
172
168
|
baseData(s) {
|
|
173
|
-
s = JSON.parse(s),
|
|
169
|
+
s = JSON.parse(s), e.isGetData ? e.getDataCllBack && e.getDataCllBack({
|
|
170
|
+
status: 200,
|
|
171
|
+
message: "success",
|
|
172
|
+
data: s
|
|
173
|
+
}) : t.success({
|
|
174
|
+
status: 200,
|
|
175
|
+
message: "success",
|
|
176
|
+
data: s
|
|
177
|
+
}), e.isGetData = !1;
|
|
174
178
|
}
|
|
175
179
|
}).then((s) => {
|
|
176
|
-
|
|
180
|
+
e.parentPostmate = s, e.getData(), t.success({
|
|
181
|
+
status: 200,
|
|
182
|
+
message: "success",
|
|
183
|
+
data: {
|
|
184
|
+
ChildPostmate: s
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}).catch((s) => {
|
|
188
|
+
t.error({ status: 500, data: null, message: s });
|
|
177
189
|
});
|
|
178
190
|
}
|
|
179
191
|
// 获取数据
|
|
180
|
-
getData(t =
|
|
181
|
-
if (!this.parentPostmate)
|
|
192
|
+
getData(t = null) {
|
|
193
|
+
if (!this.parentPostmate) {
|
|
194
|
+
t({
|
|
195
|
+
status: 500,
|
|
196
|
+
message: "父页面未连接",
|
|
197
|
+
data: null
|
|
198
|
+
});
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
182
201
|
t ? (this.isGetData = !0, this.getDataCllBack = t) : (this.isGetData = !1, this.getDataCllBack = null), this.parentPostmate.emit("requestBaseData");
|
|
183
202
|
}
|
|
184
203
|
}
|
|
185
|
-
class
|
|
186
|
-
constructor(t, e
|
|
204
|
+
class I {
|
|
205
|
+
constructor(t, e = "", s = "") {
|
|
187
206
|
/** 连接到的子postmate实例 */
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
207
|
+
h(this, "postmateParent", null);
|
|
208
|
+
/** 通讯数据 */
|
|
209
|
+
h(this, "dataInfo", {
|
|
210
|
+
loginInfo: null,
|
|
211
|
+
token: "",
|
|
212
|
+
isHideHeader: !0
|
|
213
|
+
});
|
|
195
214
|
/** iframe Dom */
|
|
196
|
-
|
|
215
|
+
h(this, "iframeEle", "");
|
|
197
216
|
/** iframe url */
|
|
198
|
-
|
|
199
|
-
|
|
217
|
+
h(this, "iframeUrl", "");
|
|
218
|
+
/** postmate 实例 */
|
|
219
|
+
h(this, "handshake", null);
|
|
220
|
+
this.dataInfo = t || this.dataInfo, this.iframeEle = e, this.iframeUrl = s, this.iframeEle && this.iframeUrl && this.init();
|
|
200
221
|
}
|
|
201
222
|
/** 初始化 */
|
|
202
223
|
async init(t = "") {
|
|
203
|
-
|
|
204
|
-
|
|
224
|
+
this.postmateParent && (this.postmateParent.destroy(), this.postmateParent = null);
|
|
225
|
+
const e = typeof this.iframeEle == "function" ? this.iframeEle() : this.iframeEle;
|
|
226
|
+
new o({
|
|
227
|
+
container: e,
|
|
205
228
|
url: this.iframeUrl
|
|
206
229
|
}).then((i) => {
|
|
207
|
-
this.
|
|
230
|
+
this.postmateParent = i, i.on("requestBaseData", (a) => {
|
|
208
231
|
this.sendData();
|
|
209
232
|
}), t && t();
|
|
210
233
|
});
|
|
@@ -219,14 +242,13 @@ class H {
|
|
|
219
242
|
}
|
|
220
243
|
/** 向子页面发送消息 */
|
|
221
244
|
sendData() {
|
|
222
|
-
this.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}));
|
|
245
|
+
this.postmateParent && this.postmateParent.call("baseData", JSON.stringify(this.dataInfo));
|
|
246
|
+
}
|
|
247
|
+
destroy() {
|
|
248
|
+
this.postmateParent && (this.postmateParent.destroy(), this.postmateParent = null);
|
|
227
249
|
}
|
|
228
250
|
}
|
|
229
251
|
export {
|
|
230
252
|
C as ChildPostmate,
|
|
231
|
-
|
|
253
|
+
I as ParentPostmate
|
|
232
254
|
};
|
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
|
|
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 M=(r,n,d)=>n in r?C(r,n,{enumerable:!0,configurable:!0,writable:!0,value:d}):r[n]=d;var m=(r,n,d)=>M(r,typeof n!="symbol"?n+"":n,d);const n="application/x-postmate-v1+json";let d=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 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(!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 l.Promise(e=>{const s=++d,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,h=this.model[i];if(s!=="call"){const p=typeof h=="function"?h():h;l.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 h=="function"&&h(c)})}emit(t,e){this.parent.postMessage({postmate:"emit",type:n,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 c=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||`${c}//${h}`})();let s=0,i;return new l.Promise((a,c)=>{const h=g=>{if(!u(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))):c("Failed handshake")};this.parent.addEventListener("message",h,!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 w{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: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)})}}l.debug=!1,l.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),l.Model=w;class k{constructor(t){this.childPostmate=null,this.parentPostmate=null,this.isGetData=!1,this.getDataCllBack=null,window.frameElement?t.error({status:500,data:null,message:"未在一体化平台中运行"}):this.init(t)}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.success({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=null){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="",s=""){m(this,"postmateParent",null);m(this,"dataInfo",{loginInfo:null,token:"",isHideHeader:!0});m(this,"iframeEle","");m(this,"iframeUrl","");m(this,"handshake",null);this.dataInfo=t||this.dataInfo,this.iframeEle=e,this.iframeUrl=s,this.iframeEle&&this.iframeUrl&&this.init()}async init(t=""){this.postmateParent&&(this.postmateParent.destroy(),this.postmateParent=null);const e=typeof this.iframeEle=="function"?this.iframeEle():this.iframeEle;new l({container:e,url:this.iframeUrl}).then(i=>{this.postmateParent=i,i.on("requestBaseData",a=>{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.postmateParent&&this.postmateParent.call("baseData",JSON.stringify(this.dataInfo))}destroy(){this.postmateParent&&(this.postmateParent.destroy(),this.postmateParent=null)}}r.ChildPostmate=k,r.ParentPostmate=D,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** 子类参数规范接口 */
|
|
2
|
+
interface EventObj {
|
|
3
|
+
error: (error: any) => void;
|
|
4
|
+
success: (parentPostmate: any) => void;
|
|
5
|
+
receive: (data: any) => void;
|
|
6
|
+
}
|
|
7
|
+
export default class ChildPostmate {
|
|
8
|
+
childPostmate: any;
|
|
9
|
+
parentPostmate: any;
|
|
10
|
+
isGetData: boolean;
|
|
11
|
+
getDataCllBack: Function | null;
|
|
12
|
+
constructor(eventObj: EventObj);
|
|
13
|
+
init(eventObj: EventObj): void;
|
|
14
|
+
getData(callback?: any): void;
|
|
15
|
+
}
|
|
16
|
+
export {};
|