qlfy-postmate 1.0.0 → 1.0.2
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.d.ts +2 -3
- package/lib/index.mjs +53 -46
- package/lib/index.umd.js +1 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export { ChildPostmate, ParentPostmate };
|
|
1
|
+
export declare const ChildPostmate: any;
|
|
2
|
+
export declare const ParentPostmate: any;
|
package/lib/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var u = Object.defineProperty;
|
|
2
|
-
var g = (
|
|
3
|
-
var
|
|
4
|
-
const
|
|
2
|
+
var g = (n, t, e) => t in n ? u(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var r = (n, t, e) => g(n, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
const h = "application/x-postmate-v1+json";
|
|
5
5
|
let w = 0;
|
|
6
6
|
const y = {
|
|
7
7
|
handshake: 1,
|
|
@@ -11,10 +11,10 @@ const y = {
|
|
|
11
11
|
reply: 1,
|
|
12
12
|
request: 1
|
|
13
13
|
};
|
|
14
|
-
function f(
|
|
15
|
-
return (typeof t != "string" ||
|
|
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];
|
|
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
20
|
if (!f(e, this.childOrigin)) return;
|
|
@@ -23,14 +23,14 @@ class E {
|
|
|
23
23
|
}, this.parent.addEventListener("message", this.listener, !1);
|
|
24
24
|
}
|
|
25
25
|
get(t) {
|
|
26
|
-
return new
|
|
26
|
+
return new l.Promise((e) => {
|
|
27
27
|
const s = ++w, a = (i) => {
|
|
28
28
|
i.data.uid === s && i.data.postmate === "reply" && (this.parent.removeEventListener("message", a, !1), e(i.data.value));
|
|
29
29
|
};
|
|
30
30
|
this.parent.addEventListener("message", a, !1), this.child.postMessage(
|
|
31
31
|
{
|
|
32
32
|
postmate: "request",
|
|
33
|
-
type:
|
|
33
|
+
type: h,
|
|
34
34
|
property: t,
|
|
35
35
|
uid: s
|
|
36
36
|
},
|
|
@@ -42,7 +42,7 @@ class E {
|
|
|
42
42
|
this.child.postMessage(
|
|
43
43
|
{
|
|
44
44
|
postmate: "call",
|
|
45
|
-
type:
|
|
45
|
+
type: h,
|
|
46
46
|
property: t,
|
|
47
47
|
data: e
|
|
48
48
|
},
|
|
@@ -56,19 +56,19 @@ class E {
|
|
|
56
56
|
window.removeEventListener("message", this.listener, !1), this.frame.parentNode.removeChild(this.frame);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
class
|
|
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
62
|
if (!f(e, this.parentOrigin)) return;
|
|
63
63
|
const { postmate: s, property: a, uid: i, data: d } = e.data, o = this.model[a];
|
|
64
64
|
if (s !== "call") {
|
|
65
65
|
const m = typeof o == "function" ? o() : o;
|
|
66
|
-
|
|
66
|
+
l.Promise.resolve(m).then((c) => {
|
|
67
67
|
e.source.postMessage(
|
|
68
68
|
{
|
|
69
69
|
property: a,
|
|
70
70
|
postmate: "reply",
|
|
71
|
-
type:
|
|
71
|
+
type: h,
|
|
72
72
|
uid: i,
|
|
73
73
|
value: c
|
|
74
74
|
},
|
|
@@ -82,14 +82,14 @@ class P {
|
|
|
82
82
|
this.parent.postMessage(
|
|
83
83
|
{
|
|
84
84
|
postmate: "emit",
|
|
85
|
-
type:
|
|
85
|
+
type: h,
|
|
86
86
|
value: { name: t, data: e }
|
|
87
87
|
},
|
|
88
88
|
this.parentOrigin
|
|
89
89
|
);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
class
|
|
92
|
+
class l {
|
|
93
93
|
constructor({ container: t = document.body, model: e, url: s, name: a, classListArray: i = [] }) {
|
|
94
94
|
return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name = a || "", this.frame.classList.add(...i), t.appendChild(this.frame), this.child = this.frame.contentWindow, this.model = e || {}, this.sendHandshake(s);
|
|
95
95
|
}
|
|
@@ -101,17 +101,17 @@ class h {
|
|
|
101
101
|
return i.origin || `${d}//${o}`;
|
|
102
102
|
})();
|
|
103
103
|
let s = 0, a;
|
|
104
|
-
return new
|
|
104
|
+
return new l.Promise((i, d) => {
|
|
105
105
|
const o = (p) => {
|
|
106
106
|
if (!f(p, e)) return !1;
|
|
107
|
-
p.data.postmate === "handshake-reply" ? (clearInterval(a), this.parent.removeEventListener("message", o, !1), this.childOrigin = p.origin, i(new
|
|
107
|
+
p.data.postmate === "handshake-reply" ? (clearInterval(a), this.parent.removeEventListener("message", o, !1), this.childOrigin = p.origin, i(new P(this))) : d("Failed handshake");
|
|
108
108
|
};
|
|
109
109
|
this.parent.addEventListener("message", o, !1);
|
|
110
110
|
const m = () => {
|
|
111
111
|
s++, this.child.postMessage(
|
|
112
112
|
{
|
|
113
113
|
postmate: "handshake",
|
|
114
|
-
type:
|
|
114
|
+
type: h,
|
|
115
115
|
model: this.model
|
|
116
116
|
},
|
|
117
117
|
e
|
|
@@ -128,46 +128,46 @@ 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
|
|
131
|
+
return new l.Promise((t, e) => {
|
|
132
132
|
this.child.addEventListener("message", (s) => {
|
|
133
133
|
if (s.data.postmate) {
|
|
134
134
|
if (s.data.postmate !== "handshake") return e("Handshake Reply Failed");
|
|
135
135
|
this.child.removeEventListener("message", this, !1), s.source.postMessage(
|
|
136
136
|
{
|
|
137
137
|
postmate: "handshake-reply",
|
|
138
|
-
type:
|
|
138
|
+
type: h
|
|
139
139
|
},
|
|
140
140
|
s.origin
|
|
141
141
|
), this.parentOrigin = s.origin;
|
|
142
142
|
const a = s.data.model;
|
|
143
143
|
a && Object.keys(a).forEach((i) => {
|
|
144
144
|
this.model[i] = a[i];
|
|
145
|
-
}), t(new
|
|
145
|
+
}), t(new E(this));
|
|
146
146
|
}
|
|
147
147
|
}, !1);
|
|
148
148
|
});
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
|
-
|
|
152
|
-
|
|
151
|
+
l.debug = !1;
|
|
152
|
+
l.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
|
-
|
|
160
|
-
class
|
|
159
|
+
l.Model = k;
|
|
160
|
+
let D = class {
|
|
161
161
|
constructor(t, e) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
window.frameElement == null ? this.init(t, e) : console.warn("
|
|
162
|
+
r(this, "childPostmate", null);
|
|
163
|
+
r(this, "parentPostmate", null);
|
|
164
|
+
r(this, "isGetData", !1);
|
|
165
|
+
r(this, "getDataCllBack", null);
|
|
166
|
+
window.frameElement == null ? this.init(t, e) : console.warn("未嵌入主系统");
|
|
167
167
|
}
|
|
168
168
|
init(t, e) {
|
|
169
169
|
const s = this;
|
|
170
|
-
this.childPostmate = new
|
|
170
|
+
this.childPostmate = new l.Model({
|
|
171
171
|
// 定义子页面可以暴露给父页面的方法
|
|
172
172
|
baseData(a) {
|
|
173
173
|
s.isGetData ? s.getDataCllBack(a) : t(a), s.isGetData = !1;
|
|
@@ -181,24 +181,30 @@ class C {
|
|
|
181
181
|
if (!this.parentPostmate) return console.warn("父页面未连接");
|
|
182
182
|
t ? (this.isGetData = !0, this.getDataCllBack = t) : (this.isGetData = !1, this.getDataCllBack = null), this.parentPostmate.emit("requestBaseData");
|
|
183
183
|
}
|
|
184
|
-
}
|
|
185
|
-
class H {
|
|
184
|
+
}, C = class {
|
|
186
185
|
constructor(t, e, s = !0, a = "", i = "") {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
186
|
+
/** 连接到的子postmate实例 */
|
|
187
|
+
r(this, "postmateChild", null);
|
|
188
|
+
/** 登陆信息 */
|
|
189
|
+
r(this, "loginInfo");
|
|
190
|
+
/** token */
|
|
191
|
+
r(this, "token");
|
|
192
|
+
/** 是否隐藏头部 */
|
|
193
|
+
r(this, "isHideHeader", !0);
|
|
194
|
+
/** iframe Dom */
|
|
195
|
+
r(this, "iframeEle", "");
|
|
196
|
+
/** iframe url */
|
|
197
|
+
r(this, "iframeUrl", "");
|
|
198
|
+
this.loginInfo = t, this.token = e, this.isHideHeader = s, this.iframeEle = a, this.iframeUrl = i, this.iframeEle && this.iframeUrl && this.init();
|
|
199
|
+
}
|
|
200
|
+
/** 初始化 */
|
|
195
201
|
async init(t = "") {
|
|
196
|
-
new
|
|
202
|
+
new l({
|
|
197
203
|
container: this.iframeEle,
|
|
198
204
|
url: this.iframeUrl
|
|
199
205
|
}).then((s) => {
|
|
200
206
|
this.postmateChild = s, s.on("requestBaseData", (a) => {
|
|
201
|
-
|
|
207
|
+
this.sendData();
|
|
202
208
|
}), t && t();
|
|
203
209
|
});
|
|
204
210
|
}
|
|
@@ -213,13 +219,14 @@ class H {
|
|
|
213
219
|
/** 向子页面发送消息 */
|
|
214
220
|
sendData() {
|
|
215
221
|
this.postmateChild && this.postmateChild.call("baseData", {
|
|
216
|
-
|
|
222
|
+
loginInfo: this.loginInfo,
|
|
217
223
|
token: this.token,
|
|
218
224
|
isHideHeader: this.isHideHeader
|
|
219
225
|
});
|
|
220
226
|
}
|
|
221
|
-
}
|
|
227
|
+
};
|
|
228
|
+
const I = D, O = C;
|
|
222
229
|
export {
|
|
223
|
-
|
|
224
|
-
|
|
230
|
+
I as ChildPostmate,
|
|
231
|
+
O as ParentPostmate
|
|
225
232
|
};
|
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
|
|
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 H=Object.defineProperty;var M=(r,n,m)=>n in r?H(r,n,{enumerable:!0,configurable:!0,writable:!0,value:m}):r[n]=m;var l=(r,n,m)=>M(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 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 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 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(!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 P(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 E{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 w(this))}},!1)})}}h.debug=!1,h.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),h.Model=E;let k=class{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")}},D=class{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})}};const C=k,v=D;r.ChildPostmate=C,r.ParentPostmate=v,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|