-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
179 lines (159 loc) · 4.84 KB
/
index.js
File metadata and controls
179 lines (159 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
let atomicIframeEventListener
let atomicProduct = {
DEPOSIT: 'deposit',
VERIFY: 'verify',
IDENTIFY: 'identify',
WITHHOLD: 'withhold'
}
let atomicSDK = {
/**
* Launch the Transact experience
* @param options - Options for launching Transact
* @param options.config - Atomic config object to customize Transact - https://docs.atomicfi.com/reference/transact-sdk#sdk-parameters
* @param options.onInteraction - Callback for when a user interacts with Transact
* @param options.onDataRequest - Callback for when Transact requests additional data
* @param options.onFinish - Callback for when Transact is finished
* @param options.onClose - Callback for when Transact is closed
* @param options.onOpenUrl - Callback for when Transact opens an external URL
*/
transact: ({
config,
container = undefined,
environmentOverride = undefined,
onInteraction = undefined,
onDataRequest = undefined,
onFinish = undefined,
onClose = undefined,
onOpenUrl = undefined
} = {}) => {
config = config || {}
onInteraction = onInteraction || function () {}
onDataRequest = onDataRequest || function () {}
onFinish = onFinish || function () {}
onClose = onClose || function () {}
onOpenUrl = onOpenUrl || undefined
const origin = environmentOverride || 'https://transact.atomicfi.com'
if (!container) {
document.body.style.overflow = 'hidden'
}
let iframeElement = document.createElement('iframe')
iframeElement.setAttribute('allow', 'web-share; clipboard-write;')
const productType = config.operation || config.product || 'Atomic'
iframeElement.setAttribute('title', `${productType} Interface`)
iframeElement.setAttribute('aria-label', `${productType} Interface`)
iframeElement.src = `${origin}/initialize/${btoa(
JSON.stringify({
inSdk: true,
platform: {
name: 'browser',
sdkVersion: '__VERSION__',
systemVersion: `${navigator.platform}-${
navigator.vendor ?? 'unknown'
}`
},
...config
})
)}`
const baseStyles = [
{ width: '100%' },
{ height: '100%' },
{ 'z-index': '888888888' },
{ 'border-width': '0' },
{ 'overflow-x': 'hidden' },
{ 'overflow-y': 'auto' }
]
const modalStyles = [
{ position: 'fixed' },
{ top: '0' },
{ left: '0' },
{ right: '0' },
{ bottom: '0' }
]
const styles = [...baseStyles, ...(container ? [] : modalStyles)]
iframeElement.id = 'atomic-transact-iframe'
iframeElement.style.cssText = styles
.map((style) =>
Object.entries(style)
.map(([key, value]) => `${key}: ${value}; `)
.join('')
)
.join('')
.trim()
if (container) {
const containerElement = document.querySelector(container)
if (containerElement) {
containerElement.appendChild(iframeElement)
} else {
throw new Error(`No container found for "${container}"`)
}
} else {
document.body.appendChild(iframeElement)
}
atomicIframeEventListener = _handleIFrameEvent({
origin,
onInteraction,
onDataRequest,
onFinish,
onClose,
onOpenUrl
})
window.addEventListener('message', atomicIframeEventListener)
return {
close: () => _removeTransact()
}
}
}
function _handleIFrameEvent({
origin,
onInteraction,
onFinish,
onClose,
onDataRequest,
onOpenUrl
}) {
return (event) => {
if (origin !== event.origin) return
const { payload, event: eventName } = event.data
switch (eventName) {
case 'atomic-transact-close':
onClose(payload)
_removeTransact()
break
case 'atomic-transact-finish':
onFinish(payload)
_removeTransact()
break
case 'atomic-transact-interaction':
onInteraction(payload)
break
case 'atomic-transact-data-request':
onDataRequest(payload)
break
case 'atomic-transact-open-url':
if (onOpenUrl && typeof onOpenUrl === 'function') {
onOpenUrl({url: payload.url})
} else {
const url = new URL(payload.url)
if ('https:' === url.protocol)
window.open(payload.url, '_blank')
}
break
default:
console.log('Unhandled postMessage Event', eventName)
}
}
}
function _removeTransact() {
const iframeElement = document.querySelector('#atomic-transact-iframe')
window.removeEventListener('message', atomicIframeEventListener)
if (iframeElement?.parentNode !== document.body) return
document.body.style.removeProperty('overflow')
document.body.removeChild(iframeElement)
}
if (typeof exports !== 'undefined') {
exports.Product = atomicProduct
exports.Atomic = atomicSDK
} else {
window.Product = atomicProduct
window.Atomic = atomicSDK
}