-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
86 lines (76 loc) · 2.39 KB
/
main.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
function create_li(f) {
var li = document.createElement('li');
li.setAttribute('class', 'i-file entypo');
li.innerHTML = '<a href="' + f + '">' + f + '</a>';
return li;
}
function add_to_ul(f) {
var ul = document.querySelector('ul');
var li = create_li(f);
if (ul.children[0].innerText == '..') {
ul.insertBefore(li, ul.children[1]); // first is the back link if in sub-dir
} else {
ul.insertBefore(li, ul.children[0]);
}
}
function notify(note) {
var nojump = document.querySelector('#nojump');
nojump.innerHTML = note;
}
function post_file(file) {
var formdata = new FormData();
formdata.append("file", file, file.name);
var override = document.querySelector('#override_check');
if (override.checked) {
formdata.append('override', 'yes');
}
// request
var xhr = new XMLHttpRequest();
xhr.open("POST", '', true);
xhr.onreadystatechange = function () {
if (xhr.readyState != 4 || xhr.status != 200) {
notify('Error: ' + xhr.responseText);
} else {
notify('Saved to ' + xhr.responseText);
add_to_ul(xhr.responseText);
}
}
//var boundary = '-------------------' + Date.now().toString(16);
//xhr.setRequestHeader('Content-Type', 'multipart\/form-data; boundary=' + boundary);
xhr.send(formdata);
}
var submit = document.querySelector('label[type="submit"]');
submit.addEventListener('click', function(event) {
event.preventDefault();
var input = document.querySelector('input[type="file"]');
if (input.files.length < 1) {return;}
const file = input.files[0];
post_file(file);
})
// drag to upload
var target = document.querySelector('body');
var changed_color = '#dcf8c6'; //'#BEDDAA'
var original_color = target.getAttribute('background-color');
if (original_color == null) {
original_color = '';
}
target.addEventListener('drop', function(event) {
event.preventDefault();
if (event.type === 'drop') {
var file = event.dataTransfer.files[0];
post_file(file)
}
target.style.backgroundColor = original_color;
})
target.addEventListener('dragenter', function (event) {
event.preventDefault();
target.style.backgroundColor = changed_color;
})
target.addEventListener('dragover', function (event) {
event.preventDefault();
target.style.backgroundColor = changed_color;
})
target.addEventListener('dragleave', function (event) {
event.preventDefault();
target.style.backgroundColor = original_color;
})