-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment.js
107 lines (97 loc) · 4.22 KB
/
assignment.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
var isAdvancedUpload = function() {
var div = document.createElement('div');
return (('draggable' in div) || ('ondragstart' in div && 'ondrop' in div)) && 'FormData' in window && 'FileReader' in window;
}();
let draggableFileArea = document.querySelector(".drag-file-area");
let browseFileText = document.querySelector(".browse-files");
let uploadIcon = document.querySelector(".upload-icon");
let dragDropText = document.querySelector(".dynamic-message");
let fileInput = document.querySelector(".default-file-input");
let cannotUploadMessage = document.querySelector(".cannot-upload-message");
let cancelAlertButton = document.querySelector(".cancel-alert-button");
let uploadedFile = document.querySelector(".file-block");
let fileName = document.querySelector(".file-name");
let fileSize = document.querySelector(".file-size");
let progressBar = document.querySelector(".progress-bar");
let removeFileButton = document.querySelector(".remove-file-icon");
let uploadButton = document.querySelector(".upload-button");
let fileFlag = 0;
fileInput.addEventListener("click", () => {
fileInput.value = '';
console.log(fileInput.value);
});
fileInput.addEventListener("change", e => {
console.log(" > " + fileInput.value)
uploadIcon.innerHTML = 'check_circle';
dragDropText.innerHTML = 'File Dropped Successfully!';
document.querySelector(".label").innerHTML = `drag & drop or <span class="browse-files"> <input type="file" class="default-file-input" style=""/> <span class="browse-files-text" style="top: 0;"> browse file</span></span>`;
uploadButton.innerHTML = `Upload`;
fileName.innerHTML = fileInput.files[0].name;
fileSize.innerHTML = (fileInput.files[0].size/1024).toFixed(1) + " KB";
uploadedFile.style.cssText = "display: flex;";
progressBar.style.width = 0;
fileFlag = 0;
});
uploadButton.addEventListener("click", () => {
let isFileUploaded = fileInput.value;
if(isFileUploaded != '') {
if (fileFlag == 0) {
fileFlag = 1;
var width = 0;
var id = setInterval(frame, 50);
function frame() {
if (width >= 390) {
clearInterval(id);
uploadButton.innerHTML = `<span class="material-icons-outlined upload-button-icon"> check_circle </span> Uploaded`;
} else {
width += 5;
progressBar.style.width = width + "px";
}
}
}
} else {
cannotUploadMessage.style.cssText = "display: flex; animation: fadeIn linear 1.5s;";
}
});
cancelAlertButton.addEventListener("click", () => {
cannotUploadMessage.style.cssText = "display: none;";
});
if(isAdvancedUpload) {
["drag", "dragstart", "dragend", "dragover", "dragenter", "dragleave", "drop"].forEach( evt =>
draggableFileArea.addEventListener(evt, e => {
e.preventDefault();
e.stopPropagation();
})
);
["dragover", "dragenter"].forEach( evt => {
draggableFileArea.addEventListener(evt, e => {
e.preventDefault();
e.stopPropagation();
uploadIcon.innerHTML = 'file_download';
dragDropText.innerHTML = 'Drop your file here!';
});
});
draggableFileArea.addEventListener("drop", e => {
uploadIcon.innerHTML = 'check_circle';
dragDropText.innerHTML = 'File Dropped Successfully!';
document.querySelector(".label").innerHTML = `drag & drop or <span class="browse-files"> <input type="file" class="default-file-input" style=""/> <span class="browse-files-text" style="top: -23px; left: -20px;"> browse file</span> </span>`;
uploadButton.innerHTML = `Upload`;
let files = e.dataTransfer.files;
fileInput.files = files;
console.log(files[0].name + " " + files[0].size);
console.log(document.querySelector(".default-file-input").value);
fileName.innerHTML = files[0].name;
fileSize.innerHTML = (files[0].size/1024).toFixed(1) + " KB";
uploadedFile.style.cssText = "display: flex;";
progressBar.style.width = 0;
fileFlag = 0;
});
}
removeFileButton.addEventListener("click", () => {
uploadedFile.style.cssText = "display: none;";
fileInput.value = '';
uploadIcon.innerHTML = 'file_upload';
dragDropText.innerHTML = 'Drag & drop any file here';
document.querySelector(".label").innerHTML = `or <span class="browse-files"> <input type="file" class="default-file-input"/> <span class="browse-files-text">browse file</span> <span>from device</span> </span>`;
uploadButton.innerHTML = `Upload`;
});