qlfy-postmate 1.0.4 → 1.0.5
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 +37 -37
- package/lib/index.umd.js +1 -1
- package/package.json +1 -1
package/lib/index.mjs
CHANGED
|
@@ -18,21 +18,21 @@ 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: i = {} } = e.data, { name: s, data: a } = i;
|
|
22
|
+
e.data.postmate === "emit" && s in this.events && this.events[s].call(this, a);
|
|
23
23
|
}, this.parent.addEventListener("message", this.listener, !1);
|
|
24
24
|
}
|
|
25
25
|
get(t) {
|
|
26
26
|
return new l.Promise((e) => {
|
|
27
|
-
const
|
|
28
|
-
a.data.uid ===
|
|
27
|
+
const i = ++y, s = (a) => {
|
|
28
|
+
a.data.uid === i && a.data.postmate === "reply" && (this.parent.removeEventListener("message", s, !1), e(a.data.value));
|
|
29
29
|
};
|
|
30
|
-
this.parent.addEventListener("message",
|
|
30
|
+
this.parent.addEventListener("message", s, !1), this.child.postMessage(
|
|
31
31
|
{
|
|
32
32
|
postmate: "request",
|
|
33
33
|
type: h,
|
|
34
34
|
property: t,
|
|
35
|
-
uid:
|
|
35
|
+
uid: i
|
|
36
36
|
},
|
|
37
37
|
this.childOrigin
|
|
38
38
|
);
|
|
@@ -60,13 +60,13 @@ class P {
|
|
|
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 (
|
|
63
|
+
const { postmate: i, property: s, uid: a, data: d } = e.data, o = this.model[s];
|
|
64
|
+
if (i !== "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
|
-
property:
|
|
69
|
+
property: s,
|
|
70
70
|
postmate: "reply",
|
|
71
71
|
type: h,
|
|
72
72
|
uid: a,
|
|
@@ -75,7 +75,7 @@ class P {
|
|
|
75
75
|
e.origin
|
|
76
76
|
);
|
|
77
77
|
});
|
|
78
|
-
} else
|
|
78
|
+
} else s in this.model && typeof o == "function" && o(d);
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
emit(t, e) {
|
|
@@ -90,8 +90,8 @@ class P {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
class l {
|
|
93
|
-
constructor({ container: t = document.body, model: e, url:
|
|
94
|
-
return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name =
|
|
93
|
+
constructor({ container: t = document.body, model: e, url: i, name: s, classListArray: a = [] }) {
|
|
94
|
+
return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name = s || "", this.frame.classList.add(...a), t.appendChild(this.frame), this.child = this.frame.contentWindow, this.model = e || {}, this.sendHandshake(i);
|
|
95
95
|
}
|
|
96
96
|
sendHandshake(t) {
|
|
97
97
|
const e = (() => {
|
|
@@ -100,24 +100,24 @@ class l {
|
|
|
100
100
|
const d = a.protocol.length > 4 ? a.protocol : window.location.protocol, o = a.host.length ? a.port === "80" || a.port === "443" ? a.hostname : a.host : window.location.host;
|
|
101
101
|
return a.origin || `${d}//${o}`;
|
|
102
102
|
})();
|
|
103
|
-
let
|
|
103
|
+
let i = 0, s;
|
|
104
104
|
return new l.Promise((a, d) => {
|
|
105
105
|
const o = (p) => {
|
|
106
106
|
if (!f(p, e)) return !1;
|
|
107
|
-
p.data.postmate === "handshake-reply" ? (clearInterval(
|
|
107
|
+
p.data.postmate === "handshake-reply" ? (clearInterval(s), this.parent.removeEventListener("message", o, !1), this.childOrigin = p.origin, a(new E(this))) : d("Failed handshake");
|
|
108
108
|
};
|
|
109
109
|
this.parent.addEventListener("message", o, !1);
|
|
110
110
|
const m = () => {
|
|
111
|
-
|
|
111
|
+
i++, this.child.postMessage(
|
|
112
112
|
{
|
|
113
113
|
postmate: "handshake",
|
|
114
114
|
type: h,
|
|
115
115
|
model: this.model
|
|
116
116
|
},
|
|
117
117
|
e
|
|
118
|
-
),
|
|
118
|
+
), i === 5 && clearInterval(s);
|
|
119
119
|
}, c = () => {
|
|
120
|
-
m(),
|
|
120
|
+
m(), s = 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
|
});
|
|
@@ -129,19 +129,19 @@ class k {
|
|
|
129
129
|
}
|
|
130
130
|
sendHandshakeReply() {
|
|
131
131
|
return new l.Promise((t, e) => {
|
|
132
|
-
this.child.addEventListener("message", (
|
|
133
|
-
if (
|
|
134
|
-
if (
|
|
135
|
-
this.child.removeEventListener("message", this, !1),
|
|
132
|
+
this.child.addEventListener("message", (i) => {
|
|
133
|
+
if (i.data.postmate) {
|
|
134
|
+
if (i.data.postmate !== "handshake") return e("Handshake Reply Failed");
|
|
135
|
+
this.child.removeEventListener("message", this, !1), i.source.postMessage(
|
|
136
136
|
{
|
|
137
137
|
postmate: "handshake-reply",
|
|
138
138
|
type: h
|
|
139
139
|
},
|
|
140
|
-
|
|
141
|
-
), this.parentOrigin =
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
this.model[a] =
|
|
140
|
+
i.origin
|
|
141
|
+
), this.parentOrigin = i.origin;
|
|
142
|
+
const s = i.data.model;
|
|
143
|
+
s && Object.keys(s).forEach((a) => {
|
|
144
|
+
this.model[a] = s[a];
|
|
145
145
|
}), t(new P(this));
|
|
146
146
|
}
|
|
147
147
|
}, !1);
|
|
@@ -166,14 +166,14 @@ class C {
|
|
|
166
166
|
window.frameElement == null ? this.init(t, e) : console.warn("未嵌入主系统");
|
|
167
167
|
}
|
|
168
168
|
init(t, e) {
|
|
169
|
-
const
|
|
169
|
+
const i = this;
|
|
170
170
|
this.childPostmate = new l.Model({
|
|
171
171
|
// 定义子页面可以暴露给父页面的方法
|
|
172
|
-
baseData(
|
|
173
|
-
s.isGetData ?
|
|
172
|
+
baseData(s) {
|
|
173
|
+
s = JSON.parse(s), i.isGetData ? i.getDataCllBack(s) : t(s), i.isGetData = !1;
|
|
174
174
|
}
|
|
175
|
-
}).then((
|
|
176
|
-
|
|
175
|
+
}).then((s) => {
|
|
176
|
+
i.parentPostmate = s, i.getData(), e(s);
|
|
177
177
|
});
|
|
178
178
|
}
|
|
179
179
|
// 获取数据
|
|
@@ -183,7 +183,7 @@ class C {
|
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
class H {
|
|
186
|
-
constructor(t, e,
|
|
186
|
+
constructor(t, e, i = !0, s = "", a = "") {
|
|
187
187
|
/** 连接到的子postmate实例 */
|
|
188
188
|
r(this, "postmateChild", null);
|
|
189
189
|
/** 登陆信息 */
|
|
@@ -196,15 +196,15 @@ class H {
|
|
|
196
196
|
r(this, "iframeEle", "");
|
|
197
197
|
/** iframe url */
|
|
198
198
|
r(this, "iframeUrl", "");
|
|
199
|
-
this.loginInfo = t, this.token = e, this.isHideHeader =
|
|
199
|
+
this.loginInfo = t, this.token = e, this.isHideHeader = i, this.iframeEle = s, this.iframeUrl = a, this.iframeEle && this.iframeUrl && this.init();
|
|
200
200
|
}
|
|
201
201
|
/** 初始化 */
|
|
202
202
|
async init(t = "") {
|
|
203
203
|
new l({
|
|
204
204
|
container: typeof this.iframeEle == "function" ? this.iframeEle() : this.iframeEle,
|
|
205
205
|
url: this.iframeUrl
|
|
206
|
-
}).then((
|
|
207
|
-
this.postmateChild =
|
|
206
|
+
}).then((i) => {
|
|
207
|
+
this.postmateChild = i, i.on("requestBaseData", (s) => {
|
|
208
208
|
this.sendData();
|
|
209
209
|
}), t && t();
|
|
210
210
|
});
|
|
@@ -219,11 +219,11 @@ class H {
|
|
|
219
219
|
}
|
|
220
220
|
/** 向子页面发送消息 */
|
|
221
221
|
sendData() {
|
|
222
|
-
this.postmateChild && this.postmateChild.call("baseData", {
|
|
222
|
+
this.postmateChild && this.postmateChild.call("baseData", JSON.stringify({
|
|
223
223
|
loginInfo: this.loginInfo,
|
|
224
224
|
token: this.token,
|
|
225
225
|
isHideHeader: this.isHideHeader
|
|
226
|
-
});
|
|
226
|
+
}));
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
229
|
export {
|
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 O=(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)=>O(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:i={}}=e.data,{name:s,data:a}=i;e.data.postmate==="emit"&&s in this.events&&this.events[s].call(this,a)},this.parent.addEventListener("message",this.listener,!1)}get(t){return new h.Promise(e=>{const i=++m,s=a=>{a.data.uid===i&&a.data.postmate==="reply"&&(this.parent.removeEventListener("message",s,!1),e(a.data.value))};this.parent.addEventListener("message",s,!1),this.child.postMessage({postmate:"request",type:n,property:t,uid:i},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:i,property:s,uid:a,data:p}=e.data,d=this.model[s];if(i!=="call"){const c=typeof d=="function"?d():d;h.Promise.resolve(c).then(f=>{e.source.postMessage({property:s,postmate:"reply",type:n,uid:a,value:f},e.origin)})}else s in this.model&&typeof d=="function"&&d(p)})}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:i,name:s,classListArray:a=[]}){return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=s||"",this.frame.classList.add(...a),t.appendChild(this.frame),this.child=this.frame.contentWindow,this.model=e||{},this.sendHandshake(i)}sendHandshake(t){const e=(()=>{const a=document.createElement("a");a.href=t;const p=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||`${p}//${d}`})();let i=0,s;return new h.Promise((a,p)=>{const d=g=>{if(!u(g,e))return!1;g.data.postmate==="handshake-reply"?(clearInterval(s),this.parent.removeEventListener("message",d,!1),this.childOrigin=g.origin,a(new w(this))):p("Failed handshake")};this.parent.addEventListener("message",d,!1);const c=()=>{i++,this.child.postMessage({postmate:"handshake",type:n,model:this.model},e),i===5&&clearInterval(s)},f=()=>{c(),s=setInterval(c,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",i=>{if(i.data.postmate){if(i.data.postmate!=="handshake")return e("Handshake Reply Failed");this.child.removeEventListener("message",this,!1),i.source.postMessage({postmate:"handshake-reply",type:n},i.origin),this.parentOrigin=i.origin;const s=i.data.model;s&&Object.keys(s).forEach(a=>{this.model[a]=s[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 i=this;this.childPostmate=new h.Model({baseData(s){s=JSON.parse(s),i.isGetData?i.getDataCllBack(s):t(s),i.isGetData=!1}}).then(s=>{i.parentPostmate=s,i.getData(),e(s)})}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,i=!0,s="",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=i,this.iframeEle=s,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(i=>{this.postmateChild=i,i.on("requestBaseData",s=>{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",JSON.stringify({loginInfo:this.loginInfo,token:this.token,isHideHeader:this.isHideHeader}))}}r.ChildPostmate=k,r.ParentPostmate=D,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|