@cowprotocol/widget-lib 0.9.1 → 0.10.1
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/index.iife.js +1 -1
- package/index.js +1 -1
- package/index.mjs +51 -51
- package/package.json +1 -1
package/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var cowSwapWidget=function(o){"use strict";const T="cowSwapWidget";function f(e,n,t){const s={key:T,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function E(e,n,t){const i=s=>{!D(s.data)||s.data.key!==T||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function
|
|
1
|
+
var cowSwapWidget=function(o){"use strict";const T="cowSwapWidget";function f(e,n,t){const s={key:T,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function E(e,n,t){const i=s=>{!D(s.data)||s.data.key!==T||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function g(e,n,t){e.removeEventListener("message",t)}function w(e,n){e.removeEventListener("message",n)}var h=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(h||{}),p=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e))(p||{}),m=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(m||{});const L=["primary","background","paper","text","danger","warning","alert","info","success"],O=["connect","disconnect","close","chainChanged","accountsChanged"];class V{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)}}disconnect(){this.ethereumProvider=null,g(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():E(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),O.forEach(t=>{n.on(t,i=>this.onProviderEvent(t,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(a=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:a}})).catch(a=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:a}}))}onProviderEvent(n,t){f(this.iframeWidow,p.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){f(this.iframeWidow,p.PROVIDER_RPC_RESPONSE,n)}}function R(e){return!!(e&&typeof e=="object")}const _="_";function M(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=A(e);return n+"/#"+t+"?"+W(e)}function A(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=m.SWAP}=e,r=[(t==null?void 0:t.asset)||_,(i==null?void 0:i.asset)||_].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${r}`}function W(e){const n=new URLSearchParams;return y(U(n,e),e)}function U(e,n){const{sell:t,buy:i}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),i!=null&&i.amount&&e.append("buyAmount",i.amount),e}function y(e,n){const t=n.theme;return t?(R(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):(e.append("palette","null"),e.append("theme",t)),e):(e.append("palette","null"),e)}class b{constructor(){this.subscriptions={}}on(n){const{event:t,handler:i}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(i)}off(n){const{event:t,handler:i}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==i))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(t))}}class F{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new b,this.listeners=[],this.updateListeners(t),this.widgetListener=E(this.contentWindow,h.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){w(this.contentWindow,this.widgetListener)}updateListeners(n){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}class N{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{H(i.data)&&i.origin!==window.location.origin&&(k(i.data)?this.appWindow.parent.postMessage(i.data,"*"):G(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function H(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function k(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function G(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const v="640px",Q="450px",B=20;function $(e,n){const{params:t,provider:i,listeners:s}=n;let r=i,a=t;const c=q(t);e.innerHTML="",e.appendChild(c);const{contentWindow:u}=c;if(!u)throw console.error("Iframe does not contain a window",c),new Error("Iframe does not contain a window!");const l=[];l.push(K(u,t.appCode)),l.push(Y(c,t.height));const S=new F(window,s);let P=C(u,null,r);c.addEventListener("load",()=>I(u,a,r));const j=new N(window,u);return{updateParams:d=>{a=d,I(u,a,r)},updateListeners:d=>S.updateListeners(d),updateProvider:d=>{r=d,P=C(u,P,d)},destroy:()=>{P.disconnect(),S.stopListeningIframe(),l.forEach(d=>window.removeEventListener("message",d)),j.stopListening(),e.removeChild(c)}}}function C(e,n,t){n&&n.disconnect();const i=n||new V(e);return t&&i.onConnect(t),i}function q(e){const{width:n=Q,height:t=v}=e,i=document.createElement("iframe");return i.src=M(e),i.width=n,i.height=t,i.style.border="0",i}function I(e,n,t){const i=!!t,s=A(n),r=W(n).toString(),{theme:a,...c}=n;f(e,p.UPDATE_PARAMS,{urlParams:{pathname:s,search:r},appParams:c,hasProvider:i})}function K(e,n){const t=E(window,h.ACTIVATE,()=>{w(window,t),f(e,p.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function Y(e,n=v){return E(window,h.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+B}px`:n})}return o.TradeType=m,o.WIDGET_PALETTE_COLORS=L,o.WidgetMethodsEmit=h,o.WidgetMethodsListen=p,o.createCowSwapWidget=$,o.isCowSwapWidgetPalette=R,o.listenToMessageFromWindow=E,o.postMessageToWindow=f,o.stopListeningToMessageFromWindow=g,o.stopListeningWindowListener=w,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),o}({});
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _="cowSwapWidget";function u(e,n,t){const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _="cowSwapWidget";function u(e,n,t){const s={key:_,method:n,...typeof t=="object"?t:{}};e.postMessage(s,"*")}function f(e,n,t){const i=s=>{!D(s.data)||s.data.key!==_||s.data.method!==n||t(s.data)};return e.addEventListener("message",i),i}function D(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function A(e,n,t){e.removeEventListener("message",t)}function w(e,n){e.removeEventListener("message",n)}var h=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(h||{}),p=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e))(p||{}),l=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(l||{});const L=["primary","background","paper","text","danger","warning","alert","info","success"],O=["connect","disconnect","close","chainChanged","accountsChanged"];class V{constructor(n){this.iframeWidow=n,this.ethereumProvider=null,this.requestWaitingForConnection={},this.processRpcCallFromWindow=({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)}}disconnect(){this.ethereumProvider=null,A(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(n){this.ethereumProvider?this.disconnect():f(window,h.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=n,this.processPendingRequests(),O.forEach(t=>{n.on(t,i=>this.onProviderEvent(t,i))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(n=>{this.processRpcRequest(this.requestWaitingForConnection[n])}),this.requestWaitingForConnection={}}processRpcRequest(n){const{id:t,jsonrpc:i,method:s}=n;if(!this.ethereumProvider||!t)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...n,id:t})).then(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,result:r}})).catch(r=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:i,id:t,error:r}}))}onProviderEvent(n,t){u(this.iframeWidow,p.PROVIDER_ON_EVENT,{event:n,params:t})}forwardRpcResponseToIframe(n){u(this.iframeWidow,p.PROVIDER_RPC_RESPONSE,n)}}function W(e){return!!(e&&typeof e=="object")}const T="_";function M(e){const n=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",t=C(e);return n+"/#"+t+"?"+I(e)}function C(e){const{chainId:n=1,sell:t,buy:i,tradeType:s=l.SWAP}=e,o=[(t==null?void 0:t.asset)||T,(i==null?void 0:i.asset)||T].map(encodeURIComponent).join("/");return`/${n}/widget/${s}/${o}`}function I(e){const n=new URLSearchParams;return y(U(n,e),e)}function U(e,n){const{sell:t,buy:i}=n;return t!=null&&t.amount&&e.append("sellAmount",t.amount),i!=null&&i.amount&&e.append("buyAmount",i.amount),e}function y(e,n){const t=n.theme;return t?(W(t)?(e.append("palette",encodeURIComponent(JSON.stringify(t))),e.append("theme",t.baseTheme)):(e.append("palette","null"),e.append("theme",t)),e):(e.append("palette","null"),e)}class b{constructor(){this.subscriptions={}}on(n){const{event:t,handler:i}=n;this.subscriptions[t]||(this.subscriptions[t]=[]),this.subscriptions[t].push(i)}off(n){const{event:t,handler:i}=n;this.subscriptions[t]&&(this.subscriptions[t]=this.subscriptions[t].filter(s=>s!==i))}emit(n,t){this.subscriptions[n]&&this.subscriptions[n].forEach(i=>i(t))}}class F{constructor(n,t=[]){this.contentWindow=n,this.eventEmitter=new b,this.listeners=[],this.updateListeners(t),this.widgetListener=f(this.contentWindow,h.EMIT_COW_EVENT,i=>this.eventEmitter.emit(i.event,i.payload))}stopListeningIframe(){w(this.contentWindow,this.widgetListener)}updateListeners(n){for(const t of this.listeners)this.eventEmitter.off(t);this.listeners=n||[];for(const t of this.listeners)this.eventEmitter.on(t)}}class N{constructor(n,t){this.appWindow=n,this.iframeWidow=t,this.forwardSdkMessage=i=>{H(i.data)&&i.origin!==window.location.origin&&(k(i.data)?this.appWindow.parent.postMessage(i.data,"*"):G(i.data)&&this.iframeWidow.postMessage(i.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function H(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function k(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function G(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const v="640px",Q="450px",x=20;function B(e,n){const{params:t,provider:i,listeners:s}=n;let o=i,r=t;const a=$(t);e.innerHTML="",e.appendChild(a);const{contentWindow:d}=a;if(!d)throw console.error("Iframe does not contain a window",a),new Error("Iframe does not contain a window!");const E=[];E.push(q(d,t.appCode)),E.push(K(a,t.height));const P=new F(window,s);let m=g(d,null,o);a.addEventListener("load",()=>R(d,r,o));const S=new N(window,d);return{updateParams:c=>{r=c,R(d,r,o)},updateListeners:c=>P.updateListeners(c),updateProvider:c=>{o=c,m=g(d,m,c)},destroy:()=>{m.disconnect(),P.stopListeningIframe(),E.forEach(c=>window.removeEventListener("message",c)),S.stopListening(),e.removeChild(a)}}}function g(e,n,t){n&&n.disconnect();const i=n||new V(e);return t&&i.onConnect(t),i}function $(e){const{width:n=Q,height:t=v}=e,i=document.createElement("iframe");return i.src=M(e),i.width=n,i.height=t,i.style.border="0",i}function R(e,n,t){const i=!!t,s=C(n),o=I(n).toString(),{theme:r,...a}=n;u(e,p.UPDATE_PARAMS,{urlParams:{pathname:s,search:o},appParams:a,hasProvider:i})}function q(e,n){const t=f(window,h.ACTIVATE,()=>{w(window,t),u(e,p.UPDATE_APP_DATA,{metaData:n?{appCode:n}:void 0})});return t}function K(e,n=v){return f(window,h.UPDATE_HEIGHT,t=>{e.style.height=t.height?`${t.height+x}px`:n})}exports.TradeType=l;exports.WIDGET_PALETTE_COLORS=L;exports.WidgetMethodsEmit=h;exports.WidgetMethodsListen=p;exports.createCowSwapWidget=B;exports.isCowSwapWidgetPalette=W;exports.listenToMessageFromWindow=f;exports.postMessageToWindow=u;exports.stopListeningToMessageFromWindow=A;exports.stopListeningWindowListener=w;
|
package/index.mjs
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
const T = "cowSwapWidget";
|
|
2
2
|
function u(e, n, t) {
|
|
3
|
-
const
|
|
3
|
+
const s = {
|
|
4
4
|
key: T,
|
|
5
5
|
method: n,
|
|
6
6
|
...typeof t == "object" ? t : {}
|
|
7
7
|
};
|
|
8
8
|
e.postMessage(
|
|
9
|
-
|
|
9
|
+
s,
|
|
10
10
|
"*"
|
|
11
11
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
12
12
|
);
|
|
13
13
|
}
|
|
14
14
|
function f(e, n, t) {
|
|
15
|
-
const
|
|
16
|
-
!W(
|
|
15
|
+
const i = (s) => {
|
|
16
|
+
!W(s.data) || s.data.key !== T || s.data.method !== n || t(s.data);
|
|
17
17
|
};
|
|
18
|
-
return e.addEventListener("message",
|
|
18
|
+
return e.addEventListener("message", i), i;
|
|
19
19
|
}
|
|
20
20
|
function W(e) {
|
|
21
21
|
return typeof e == "object" && e !== null && "key" in e && "method" in e && typeof e.key == "string" && typeof e.method == "string";
|
|
@@ -27,7 +27,7 @@ function _(e, n) {
|
|
|
27
27
|
e.removeEventListener("message", n);
|
|
28
28
|
}
|
|
29
29
|
var h = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e))(h || {}), p = /* @__PURE__ */ ((e) => (e.UPDATE_PARAMS = "UPDATE_PARAMS", e.UPDATE_APP_DATA = "UPDATE_APP_DATA", e.PROVIDER_RPC_RESPONSE = "PROVIDER_RPC_RESPONSE", e.PROVIDER_ON_EVENT = "PROVIDER_ON_EVENT", e))(p || {}), g = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(g || {});
|
|
30
|
-
const
|
|
30
|
+
const q = [
|
|
31
31
|
"primary",
|
|
32
32
|
"background",
|
|
33
33
|
"paper",
|
|
@@ -65,7 +65,7 @@ class O {
|
|
|
65
65
|
*/
|
|
66
66
|
onConnect(n) {
|
|
67
67
|
this.ethereumProvider ? this.disconnect() : f(window, h.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = n, this.processPendingRequests(), D.forEach((t) => {
|
|
68
|
-
n.on(t, (
|
|
68
|
+
n.on(t, (i) => this.onProviderEvent(t, i));
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
71
|
processPendingRequests() {
|
|
@@ -78,16 +78,16 @@ class O {
|
|
|
78
78
|
* @param request - The JSON-RPC request to be processed.
|
|
79
79
|
*/
|
|
80
80
|
processRpcRequest(n) {
|
|
81
|
-
const { id: t, jsonrpc:
|
|
81
|
+
const { id: t, jsonrpc: i, method: s } = n;
|
|
82
82
|
if (!this.ethereumProvider || !t)
|
|
83
83
|
return;
|
|
84
|
-
(
|
|
84
|
+
(s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...n, id: t })).then(
|
|
85
85
|
(r) => this.forwardRpcResponseToIframe({
|
|
86
|
-
rpcResponse: { jsonrpc:
|
|
86
|
+
rpcResponse: { jsonrpc: i, id: t, result: r }
|
|
87
87
|
})
|
|
88
88
|
).catch(
|
|
89
89
|
(r) => this.forwardRpcResponseToIframe({
|
|
90
|
-
rpcResponse: { jsonrpc:
|
|
90
|
+
rpcResponse: { jsonrpc: i, id: t, error: r }
|
|
91
91
|
})
|
|
92
92
|
);
|
|
93
93
|
}
|
|
@@ -107,49 +107,49 @@ class O {
|
|
|
107
107
|
function L(e) {
|
|
108
108
|
return !!(e && typeof e == "object");
|
|
109
109
|
}
|
|
110
|
-
const
|
|
110
|
+
const w = "_";
|
|
111
111
|
function V(e) {
|
|
112
112
|
const n = typeof e.baseUrl == "string" ? e.baseUrl : "https://swap.cow.fi", t = A(e);
|
|
113
|
-
return n + "/#" + t + "?" +
|
|
113
|
+
return n + "/#" + t + "?" + I(e);
|
|
114
114
|
}
|
|
115
115
|
function A(e) {
|
|
116
|
-
const { chainId: n = 1, sell: t, buy:
|
|
117
|
-
return `/${n}/widget/${
|
|
116
|
+
const { chainId: n = 1, sell: t, buy: i, tradeType: s = g.SWAP } = e, o = [(t == null ? void 0 : t.asset) || w, (i == null ? void 0 : i.asset) || w].map(encodeURIComponent).join("/");
|
|
117
|
+
return `/${n}/widget/${s}/${o}`;
|
|
118
118
|
}
|
|
119
|
-
function
|
|
119
|
+
function I(e) {
|
|
120
120
|
const n = new URLSearchParams();
|
|
121
|
-
return
|
|
121
|
+
return b(U(n, e), e);
|
|
122
122
|
}
|
|
123
123
|
function U(e, n) {
|
|
124
|
-
const { sell: t, buy:
|
|
125
|
-
return t != null && t.amount && e.append("sellAmount", t.amount),
|
|
124
|
+
const { sell: t, buy: i } = n;
|
|
125
|
+
return t != null && t.amount && e.append("sellAmount", t.amount), i != null && i.amount && e.append("buyAmount", i.amount), e;
|
|
126
126
|
}
|
|
127
|
-
function
|
|
127
|
+
function b(e, n) {
|
|
128
128
|
const t = n.theme;
|
|
129
|
-
return t
|
|
129
|
+
return t ? (L(t) ? (e.append("palette", encodeURIComponent(JSON.stringify(t))), e.append("theme", t.baseTheme)) : (e.append("palette", "null"), e.append("theme", t)), e) : (e.append("palette", "null"), e);
|
|
130
130
|
}
|
|
131
|
-
class
|
|
131
|
+
class y {
|
|
132
132
|
constructor() {
|
|
133
133
|
this.subscriptions = {};
|
|
134
134
|
}
|
|
135
135
|
on(n) {
|
|
136
|
-
const { event: t, handler:
|
|
137
|
-
this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(
|
|
136
|
+
const { event: t, handler: i } = n;
|
|
137
|
+
this.subscriptions[t] || (this.subscriptions[t] = []), this.subscriptions[t].push(i);
|
|
138
138
|
}
|
|
139
139
|
off(n) {
|
|
140
|
-
const { event: t, handler:
|
|
141
|
-
this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((
|
|
140
|
+
const { event: t, handler: i } = n;
|
|
141
|
+
this.subscriptions[t] && (this.subscriptions[t] = this.subscriptions[t].filter((s) => s !== i));
|
|
142
142
|
}
|
|
143
143
|
emit(n, t) {
|
|
144
|
-
this.subscriptions[n] && this.subscriptions[n].forEach((
|
|
144
|
+
this.subscriptions[n] && this.subscriptions[n].forEach((i) => i(t));
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
class N {
|
|
148
148
|
constructor(n, t = []) {
|
|
149
|
-
this.contentWindow = n, this.eventEmitter = new
|
|
149
|
+
this.contentWindow = n, this.eventEmitter = new y(), this.listeners = [], this.updateListeners(t), this.widgetListener = f(
|
|
150
150
|
this.contentWindow,
|
|
151
151
|
h.EMIT_COW_EVENT,
|
|
152
|
-
(
|
|
152
|
+
(i) => this.eventEmitter.emit(i.event, i.payload)
|
|
153
153
|
);
|
|
154
154
|
}
|
|
155
155
|
stopListeningIframe() {
|
|
@@ -165,8 +165,8 @@ class N {
|
|
|
165
165
|
}
|
|
166
166
|
class M {
|
|
167
167
|
constructor(n, t) {
|
|
168
|
-
this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (
|
|
169
|
-
F(
|
|
168
|
+
this.appWindow = n, this.iframeWidow = t, this.forwardSdkMessage = (i) => {
|
|
169
|
+
F(i.data) && i.origin !== window.location.origin && (H(i.data) ? this.appWindow.parent.postMessage(i.data, "*") : k(i.data) && this.iframeWidow.postMessage(i.data, "*"));
|
|
170
170
|
}, this.startListening();
|
|
171
171
|
}
|
|
172
172
|
startListening() {
|
|
@@ -185,55 +185,55 @@ function H(e) {
|
|
|
185
185
|
function k(e) {
|
|
186
186
|
return "success" in e && typeof e.success == "boolean" && "version" in e && typeof e.version == "string";
|
|
187
187
|
}
|
|
188
|
-
const
|
|
188
|
+
const v = "640px", G = "450px", Q = 20;
|
|
189
189
|
function K(e, n) {
|
|
190
|
-
const { params: t, provider:
|
|
191
|
-
let o =
|
|
190
|
+
const { params: t, provider: i, listeners: s } = n;
|
|
191
|
+
let o = i, r = t;
|
|
192
192
|
const a = x(t);
|
|
193
193
|
e.innerHTML = "", e.appendChild(a);
|
|
194
194
|
const { contentWindow: d } = a;
|
|
195
195
|
if (!d)
|
|
196
196
|
throw console.error("Iframe does not contain a window", a), new Error("Iframe does not contain a window!");
|
|
197
197
|
const E = [];
|
|
198
|
-
E.push(
|
|
199
|
-
const
|
|
200
|
-
let m =
|
|
198
|
+
E.push(B(d, t.appCode)), E.push($(a, t.height));
|
|
199
|
+
const l = new N(window, s);
|
|
200
|
+
let m = P(d, null, o);
|
|
201
201
|
a.addEventListener("load", () => R(d, r, o));
|
|
202
202
|
const C = new M(window, d);
|
|
203
203
|
return {
|
|
204
204
|
updateParams: (c) => {
|
|
205
205
|
r = c, R(d, r, o);
|
|
206
206
|
},
|
|
207
|
-
updateListeners: (c) =>
|
|
207
|
+
updateListeners: (c) => l.updateListeners(c),
|
|
208
208
|
updateProvider: (c) => {
|
|
209
|
-
o = c, m =
|
|
209
|
+
o = c, m = P(d, m, c);
|
|
210
210
|
},
|
|
211
211
|
destroy: () => {
|
|
212
|
-
m.disconnect(),
|
|
212
|
+
m.disconnect(), l.stopListeningIframe(), E.forEach((c) => window.removeEventListener("message", c)), C.stopListening(), e.removeChild(a);
|
|
213
213
|
}
|
|
214
214
|
};
|
|
215
215
|
}
|
|
216
|
-
function
|
|
216
|
+
function P(e, n, t) {
|
|
217
217
|
n && n.disconnect();
|
|
218
|
-
const
|
|
219
|
-
return t &&
|
|
218
|
+
const i = n || new O(e);
|
|
219
|
+
return t && i.onConnect(t), i;
|
|
220
220
|
}
|
|
221
221
|
function x(e) {
|
|
222
|
-
const { width: n = G, height: t =
|
|
223
|
-
return
|
|
222
|
+
const { width: n = G, height: t = v } = e, i = document.createElement("iframe");
|
|
223
|
+
return i.src = V(e), i.width = n, i.height = t, i.style.border = "0", i;
|
|
224
224
|
}
|
|
225
225
|
function R(e, n, t) {
|
|
226
|
-
const
|
|
226
|
+
const i = !!t, s = A(n), o = I(n).toString(), { theme: r, ...a } = n;
|
|
227
227
|
u(e, p.UPDATE_PARAMS, {
|
|
228
228
|
urlParams: {
|
|
229
|
-
pathname:
|
|
229
|
+
pathname: s,
|
|
230
230
|
search: o
|
|
231
231
|
},
|
|
232
232
|
appParams: a,
|
|
233
|
-
hasProvider:
|
|
233
|
+
hasProvider: i
|
|
234
234
|
});
|
|
235
235
|
}
|
|
236
|
-
function
|
|
236
|
+
function B(e, n) {
|
|
237
237
|
const t = f(window, h.ACTIVATE, () => {
|
|
238
238
|
_(window, t), u(e, p.UPDATE_APP_DATA, {
|
|
239
239
|
metaData: n ? { appCode: n } : void 0
|
|
@@ -241,14 +241,14 @@ function q(e, n) {
|
|
|
241
241
|
});
|
|
242
242
|
return t;
|
|
243
243
|
}
|
|
244
|
-
function
|
|
244
|
+
function $(e, n = v) {
|
|
245
245
|
return f(window, h.UPDATE_HEIGHT, (t) => {
|
|
246
246
|
e.style.height = t.height ? `${t.height + Q}px` : n;
|
|
247
247
|
});
|
|
248
248
|
}
|
|
249
249
|
export {
|
|
250
250
|
g as TradeType,
|
|
251
|
-
|
|
251
|
+
q as WIDGET_PALETTE_COLORS,
|
|
252
252
|
h as WidgetMethodsEmit,
|
|
253
253
|
p as WidgetMethodsListen,
|
|
254
254
|
K as createCowSwapWidget,
|