-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathscript.js
129 lines (105 loc) · 3.48 KB
/
script.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
// Upload file from browser
function upload() {
var ins = document.getElementById('inputfile').files.length;
var files = [];
for (var x = 0; x < ins; x++) {
files.push(document.getElementById('inputfile').files[x]);
}
upload_files( files );
return false;
}
// Upload file from files list array
file_id = 0;
function upload_files(files) {
let url = '/'
let formData = new FormData()
var uploaded = document.getElementById('uploaded');
formData.append('json', 'true');
for ( var i = 0; i < files.length; i++ )
{
var node = document.createElement('li');
node.innerText = 'Uploading...';
node.classList.add('uploading');
node.id = 'file_' + ++file_id;
uploaded.appendChild(node);
formData.append('file_' + file_id, files[i]);
}
fetch(url, {
method: 'POST',
body: formData
})
.then((r) => {
var dropper = document.getElementById('dropper');
var dropzone = document.getElementById('dropzone');
dropper.classList.remove('active');
dropzone.classList.remove('active');
return r.json();
})
.then((json) => {
var files = json;
for ( var file_key in files ) {
var file_progress = document.querySelector('#' + file_key);
file_progress.innerHTML = '<a target="_blank" href="' + files[file_key].url + '">' + files[file_key].url + '</a> ' + files[file_key].size + ' bytes';
file_progress.classList.remove('uploading');
}
var elements = document.getElementsByClassName('uploading');
for ( var i = 0; i < elements.length; i++ ) {
elements[i].innerText = 'Hmm';
}
})
.catch(() => {
var dropper = document.getElementById('dropper');
var dropzone = document.getElementById('dropzone');
dropper.classList.remove('active');
dropzone.classList.remove('active');
var elements = document.getElementsByClassName('uploading');
for ( var i = 0; i < elements.length; i++ ) {
elements[i].innerText = 'Failed :(';
}
});
}
// Dumb but simple upload progress animation
function uploading() {
var elements = document.getElementsByClassName('uploading');
for ( var i = 0; i < elements.length; i++ ) {
if ( elements[i].innerText == 'Uploading...' ) elements[i].innerText = 'Uploading';
else if ( elements[i].innerText == 'Uploading' ) elements[i].innerText = 'Uploading.';
else if ( elements[i].innerText == 'Uploading.' ) elements[i].innerText = 'Uploading..';
else elements[i].innerText = 'Uploading...';
}
setTimeout(uploading, 1000);
}
// Initialize uploader and drag and drop listeners
function init_uploads() {
var dropper = document.getElementById('dropper');
var dropzone = document.getElementById('dropzone');
dropzone.addEventListener('dragleave', function(e) {
dropper.classList.remove('active');
dropzone.classList.remove('active');
e.preventDefault();
e.stopPropagation();
}, false);
document.body.addEventListener('dragover', function(e) {
dropper.classList.add('active');
dropzone.classList.add('active');
e.preventDefault();
e.stopPropagation();
}, false);
dropzone.addEventListener('drop', function(e) {
upload_files(e.dataTransfer.files);
dropper.classList.remove('active');
dropzone.classList.remove('active');
e.preventDefault();
e.stopPropagation();
}, false);
uploading();
}
// Utilities
function copy_code_to_clipboard() {
var txt = document.getElementById("copy");
txt.value = document.querySelector('code').innerText;
txt.select();
txt.setSelectionRange(0, 99999);
document.execCommand("copy");
document.querySelector('#browser-header').innerText = 'Code copied to buffer successfully';
}