forked from aurora-is-near/rainbow-bridge-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomHelpers.js
133 lines (113 loc) Β· 4.05 KB
/
domHelpers.js
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
import render from './render'
import * as urlParams from './urlParams'
const isObject = x =>
Object.prototype.toString.call(x) === '[object Object]'
const fillCache = {}
// Update DOM elements that have a "data-behavior" attribute
// Given `<span data-behavior="thing"></span>`
// You can `fill('thing').with('whatever')` to set the innerHTML
// You can pass an array, and it will be joined with empty string
// You can `fill('thing').with({ title: 'whatever' }) to set any other attribute
//
// The values filled are cached, so attempts to fill the same content multiple
// times will not cause unnecessary DOM manipulations
export const fill = selector => ({
with: (contentOrAttrObj) => {
const attrs = isObject(contentOrAttrObj)
? contentOrAttrObj
: { innerHTML: contentOrAttrObj }
Object.entries(attrs).forEach(([attr, content]) => {
const contentString = Array.isArray(content)
? content.join('')
: content
if (fillCache[`${selector}:${attr}`] !== contentString) {
fillCache[`${selector}:${attr}`] = contentString
findAll(selector).forEach(n => {
n[attr] = contentString
})
}
})
}
})
// A better String coercion than `String()`
export function toString (whatever) {
if (!whatever) return ''
return String(whatever)
}
// Attach a click event handler to elements with the specified
// "data-behavior" attribute
export function onClick (behavior, fn) {
document.querySelector('body').addEventListener('click', event => {
// did they click the thing?
const thing = event.target.closest(`[data-behavior=${behavior}]`)
if (thing) fn(event)
})
}
// Find element with the given "data-behavior" attribute
export const find = selector =>
document.querySelector(`[data-behavior="${selector}"]`)
// Find all elements with the given "data-behavior" attribute
// returns as array
export const findAll = selector =>
Array.from(document.querySelectorAll(`[data-behavior="${selector}"]`))
// Hide DOM elements that have the given "data-behavior" attribute
export const hide = selector =>
findAll(selector).forEach(n => { n.style.display = 'none' })
// Show DOM elements that have the given "data-behavior" attribute
export const show = (selector, display) =>
findAll(selector)
.forEach(n => {
if (display) {
n.style.display = display
} else {
n.style.removeProperty('display')
}
})
// call this once, after page load
export function init () {
onClick('goHome', function goHome () {
urlParams.clear('erc20n', 'erc20', 'new')
render()
})
onClick('closeModal', function closeModal (e) {
e.target.closest('.modal').style.display = 'none'
})
onClick('disconnectEthereum', async function disconnectEthereum (e) {
await window.web3Modal.clearCachedProvider()
localStorage.removeItem('walletconnect')
setTimeout(() => window.location.reload())
})
onClick('disconnectNear', function disconnectNear () {
window.nearConnection.signOut()
setTimeout(() => window.location.reload())
})
onClick('newRecovery', function startTransferRecovery () {
if (!(window.ethInitialized && window.nearInitialized)) return
window.dom.show('locateTransfer')
window.dom.hide('searchingTransfer')
window.dom.hide('transferFound')
window.dom.hide('noTransferFound')
window.dom.find('txHashInput').value = ''
window.urlParams.setPush({ new: 'restore' })
render()
})
onClick('newTransfer', function startTransfer () {
// Add url param with history.pushState to enable goBack.
urlParams.setPush({ new: 'transfer' })
window.render()
})
onClick('goBack', function goBack () {
window.history.back()
})
// avoid page refreshes when submitting "get" forms
document.querySelectorAll('form[method="get"]').forEach(form => {
form.onsubmit = e => {
e.preventDefault()
// Replace url params with history.pushState to enable goBack.
Array.from(e.target.elements).forEach(el => {
if (el.name) urlParams.setPush({ [el.name]: el.value })
})
render()
}
})
}