-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
85 lines (71 loc) · 2.21 KB
/
common.js
File metadata and controls
85 lines (71 loc) · 2.21 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
// common JS utils shared among demo apps
l=console.log
var fallback = setTimeout(()=>{
//main.innerHTML="Couldn't connect to local node at "+fair_origin+". <a href='https://fairlayer.com/#install'>Please install Fairlayer first</a>"
}, 3000)
commy = (b, dot = true) => {
let prefix = b < 0 ? '-' : ''
b = Math.abs(b).toString()
if (dot) {
if (b.length == 1) {
b = '0.0' + b
} else if (b.length == 2) {
b = '0.' + b
} else {
var insert_dot_at = b.length - 2
b = b.slice(0, insert_dot_at) + '.' + b.slice(insert_dot_at)
}
}
return prefix + b.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
escapeHTML = (str)=>{
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
}
return str.replace(/[&<>]/g, function(tag) {
return tagsToReplace[tag] || tag;
});
}
window.onload = function(){
// invoice can also be included as #hash to the address
deposit.innerHTML = our_address + '#' + invoice
yourid.innerHTML = invoice
var opts = '<option disabled>Select Fair asset to operate with</option>'
for (var a of assets) {
opts += `<option value="${a.id}">${escapeHTML(a.name)} (${escapeHTML(a.ticker)}): ${user[a.id] ? commy(user[a.id]) : '0.00'}</option>`
}
picker.innerHTML = opts
picker.onchange = (e)=>localStorage.last_asset=picker.value
picker.value = localStorage.last_asset ? localStorage.last_asset : '1'
withdraw.onclick = function(){
axios.post('/init', {
address: address.value,
amount: amount.value,
asset: parseInt(picker.value)
}).then((r)=>{
if (r.data.status == 'paid') {
alert("Sent!")
location.reload()
} else {
alert(r.data.error)
}
})
}
deposit.onclick = function(){
fair_w = window.open(fair_origin+'#wallet?address='+our_address+'&invoice='+invoice+'&asset='+picker.value+'&editable=amount')
window.addEventListener('message', function(e){
if(e.origin != fair_origin) return
// the wallet claims the payment has gone through
if (e.data.status == 'paid') {
fair_w.close()
setTimeout(()=>{
location.reload()
}, 1300)
} else if (e.data.status == 'login') {
// login token shared
}
})
}
}