-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·171 lines (144 loc) · 4.31 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>SNS: Super Nerd Space, InterPlanetary Digital Residence</title>
<style type="text/css">
.shadowbox {
width: 15em;
border: 1px solid #333;
box-shadow: 8px 8px 5px #555;
padding: 8px 12px;
background-image: linear-gradient(180deg, #fff, #ddd 40%, #ccc);
}
</style>
</head>
<body id='sns'>
<div class="shadowbox">
<p>
Welcome! :)
<ul>
<li>ibipfs.js <button id="switch" onclick="switchIBIPFS()" disabled>ON</button></li>
</ul>
</p>
<div id='dropbox'>
<hr>
=> adding or dropping new facilities
<ul id="facilities"></ul>
<br>
<br>
<br>
</div>
</div>
<script type="text/javascript">
const switchElem = document.querySelector('#switch')
const switchIBIPFS = () => {
if (!window.ibipfs) {
loadFile({ "scriptURL": "ibipfs.js"})
return
}
if ('ON' === switchElem.innerHTML) {
window.ibipfs
.start()
.then(() => {
switchElem.innerHTML = 'OFF'
})
.catch((err) => {
alert('Error@switchIBIPFS-ON: ' + err)
switchElem.disabled = true
})
} else if ('OFF' === switchElem.innerHTML) {
window.ibipfs
.stop()
.then(() => {
document.body.setAttribute('style', 'background-image: none;')
switchElem.innerHTML = 'ON'
})
.catch((err) => {
alert('Error@switchIBIPFS-OFF: ' + err)
switchElem.disabled = true
})
}
}
const loadFile = (file, onloadFunction) => {
const fileType = (file.scriptURL || file.type === 'text/javascript') ? 'script' : (file.type === 'text/css' ? 'style' : undefined)
if (!fileType) {
return
}
let newLoadedElem
switch (fileType) {
case 'script':
newLoadedElem = document.createElement('script');
newLoadedElem.type = 'text/javascript'
document.currentScript ?
document.currentScript.parentNode.insertBefore(newLoadedElem, document.currentScript) :
document.body.append(newLoadedElem)
newLoadedElem.src = file.scriptURL || URL.createObjectURL(file.slice())
break
case 'style':
newLoadedElem = document.createElement('style');
newLoadedElem.type = 'text/css'
new Response(file.slice())
.text()
.then((content) => {
newLoadedElem.innerHTML = content
})
document.head.append(newLoadedElem)
break
}
newLoadedElem.onerror = () => {
throw new URIError("The script " + oError.target.src + " didn't load correctly.")
}
if (onloadFunction) {
newLoadedElem.onload = onloadFunction
}
}
const init = () => {
window.SNS = {
"name": "Super Nerd Space",
"abstract": "InterPlanetary Digital Residence",
"facilities": {}
}
let dropboxElem = document.querySelector('#dropbox')
loadFile({ "scriptURL": "droppable.umd.js" }, () => {
window.SNS.facilities.droppable = new Droppable({
element: dropboxElem
})
window.SNS.facilities.droppable.onFilesDropped((files) => {
console.log('Stuff were dropped:', files);
files.forEach((item, index, array) => {
console.log('Processing ' + item.name + '[' + item.type + '] ...')
loadFile(item, () => {
let element = document.createElement('a')
let liElem = document.createElement('li')
element.setAttribute('href', '#')
element.innerHTML = item.name + '[' + item.type + ']'
liElem.append(element)
document.querySelector('#facilities').append(liElem)
})
})
})
window.addEventListener('ibipfs_cancel', () => {
switchElem.innerHTML = 'ON'
switchElem.disabled = false
})
window.addEventListener('ibipfs', () => {
window.ibipfs && window.ibipfs.on('start', () => {
switchElem.innerHTML = 'OFF'
switchElem.disabled = false
window.ibipfs
.cat('QmZCWqxc3fzvqVLhL64yweZhuWoLeqP5iUNtLqkb5rX7NR', (err, file) => {
if (err) {
console.log('Error: ' + err)
return
}
document.body.setAttribute('style', 'background-image: url(data:image/jpg;base64,' + file.toString('base64') + '); background-color: transparent;')
})
})
})
loadFile({ "scriptURL": "ibipfs.js"})
})
}
init()
</script>
</body>
</html>