-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
243 lines (204 loc) · 8.17 KB
/
functions.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
function createStoredFilesTable(){
//$('#storedItemsTableWrapper').updatePolyfill();
var jsonObj;
$.getJSON('/storedFilesList.json').done(function( obj ){ // loading external JSON file with info about stored files
$('#storedItemsTableWrapper').empty();
jsonObj = obj;
storedItemsContent='<table class="table table-striped">\
<tr><th class="textLeft">File Name</th><th>Size (Bytes)</th><th colspan=\"5\"></th></tr>';
$.each( obj, function(key, value){ // output: filename [object]
storedItemsContent += '<tr><td class=\"textLeft\">' + key + '</td>';
$.each( value, function( attr, val ){ // parsing '[object]'
switch(attr){
case 'path':
storedItemsContent += '<td><a href=\"/uploadedFiles/' + key + '\">Download</a></td>';
break;
case 'size':
storedItemsContent += '<td>' + val + '</td>';
break;
case 'expDate':
storedItemsContent += '<td class=\"hidden\"><input type=\"date\"value=\"' + val + '\"></input></td>';
break;
case 'password':
storedItemsContent += '<td class=\"hidden\"><input type=\"password\" value=\"' + val + '\"></input></td>';
break;
case 'showInSearch':
storedItemsContent += '<td class=\"hidden\"><input type=\"checkbox\" value=\"' + val + '\"></input></td>';
break;
}
});
storedItemsContent += '<td><a class=\"deleteLink\">Delete</a></td><td><a class=\"details\">Details</a></td></tr>';
});
storedItemsContent += '</table>';
$('#storedItemsTableWrapper').append(storedItemsContent);
}).done(function(){
$('.deleteLink').click(function(event){
var filename = event.target.parentNode.parentNode.children[0].innerHTML;
delete jsonObj[filename];
//-------------------------------------------------//
//--- REQUEST WITH NAME OF A FILE TO BE DELETED ---//
//-------------------------------------------------//
var url = '/delete';
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
console.log('Delete OK.');
createStoredFilesTable();
} else {
console.log('ERROR: ' + xhr.responseText + ' : ' + xhr.status);
}
}
};
xhr.open('POST', url, true);
xhr.send(filename);
})
$('.details').click(function(event){
$('.modal').modal('show');
$('#modalPasswordSuccess').hide();
$('#modalPasswordWarning').hide();
$('#modalDateInput').val(event.target.parentNode.parentNode.children[2].children[0].value);
$('#modalPasswordInput').val(event.target.parentNode.parentNode.children[4].children[0].value);
$('#modalShowCheckbox').prop('checked', (event.target.parentNode.parentNode.children[5].children[0].value == 'true'));
console.log('checkbox: ' + event.target.parentNode.parentNode.children[5].children[0].value);
console.log('checkbox: ' + typeof(event.target.parentNode.parentNode.children[5].children[0].value));
console.log((event.target.parentNode.parentNode.children[5].children[0].value == 'true'));
$('#modalButton').click(function(){
if( $('#modalPasswordInput')[0].checkValidity() ) {
$('#modalPasswordWarning').hide();
var filename = event.target.parentNode.parentNode.children[0].innerHTML;
var password = $('#modalPasswordInput').val();
var expDate = $('#modalDateInput').val();
var showInSearch = $('#modalShowCheckbox').prop('checked');
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
console.log('Adding details OK.');
createStoredFilesTable();
$('#modalPasswordSuccess').show();
} else {
console.log('ERROR: ' + xhr.responseText + ' : ' + xhr.status);
}
}
};
xhr.open('POST', '/addDetails', true);
xhr.send(filename + ';' + password + ';' + expDate + ';' + showInSearch);
} else {
$('#modalPasswordWarning').show();
$('#modalPasswordSuccess').hide();
}
});
});
});
}
function updateProgressBar( event, bar, backBar) {
if ( event.lengthComputable ) {
var completed = (event.loaded / event.total) * 100;
backBar.style.display = 'block';
bar.style.width = completed + "%";
bar.innerHTML = parseInt(completed) + "%";
} else {
alert('Progress bar error!');
}
}
/**
* 1 interface 1 - drag&drop, 2 - upload by button
*
* Data below are all included in one parameter 'data'
* 2 queue Queue for uploaded data - this macro uses synchronous upload
* 3 formData Uploaded data - 'filepath', file, filename
* 4 url Where to upload files
* 5 $btn Upload button - only used by interface 2!
* 6 uploadSuccess Div with success notice
* 7 warning Div with warning notice
* 8 progressBarWrapperArray divs containing progressBarArray and progressTitleArray - only used by interface1!
* 9 progressBarArray Array containing progress bars
* 10 progressTitleArray Array containing titles above progress bars - only used by interface 1!
* 11 bar Colourful part of progress bar
* 12 backBar Background part of progress bar
* 13 progressTitle Title containing filename staying above progress bar
*/
function requestHandle( interface, data ) {
//------------------------------------------//
//--- GETTING DATA FROM 'data' PARAMETER ---//
//------------------------------------------//
var queue = data[0];
var formData = data[1];
var url = data[2];
var $btn = data[3];
var uploadSuccess = data[4];
var warning = data[5];
var progressBarWrapperArray = data[6];
var progressBarArray = data[7];
var progressTitleArray = data[8];
var bar = data[9];
var backBar = data[10];
var progressTitle = data[11];
if (interface != 1 && interface != 2) {
alert('DEBUG: Unknown interface code!');
}
//alert(url);
var req = new XMLHttpRequest();
//------------------------//
//--- WARNING MESSAGES ---//
//------------------------//
var noFileSelected = " You haven't selected any files to upload! ";
var resourceNotFound = "Resource couldn\'t be found!";
var internalServerError = "Internal server error!";
//---------------------//
// --- BIND LOADBAR ---//
//---------------------//
req.upload.onprogress = function(event) {
updateProgressBar(event, bar, backBar)
}
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) { // SUCCESS
if (queue.length == 0) {
if (interface == 1) { // drag drop interface
removeWrapperArray( progressBarWrapperArray );
document.getElementById("DragDropField").innerHTML = "Drag your attachments here";
} else {
$btn.button("reset");
}
hideProgressBarArray( progressBarArray );
uploadSuccess.style.display = 'block';
}
} else if (req.status == 404) {
warning.innerHTML = resourceNotFound;
warning.style.display = 'block';
} else if (req.status == 500) {
warning.innerHTML = internalServerError;
warning.style.display = 'block';
} else {
warning.innerHTML = req.status + ' : ' + req.statusText;
warning.style.display = 'block';
}
var newData = queue.shift();
if( newData ) {
requestHandle( interface, newData );
} else {
activeUploading = false;
}
}
}
req.open('POST', url, true);
req.send(formData);
}
function removeWrapperArray( wrapperArray ) {
var len = wrapperArray.length;
for( var i = 0; i < len ; i++ ) {
wrapperArray[0].remove();
}
}
function switchInterface() {
document.getElementById("DragDrop").style.display = 'none';
document.getElementById("UploadButton").style.display = 'block';
}
function hideProgressBarArray( progressBarArray ) {
var len = progressBarArray .length;
for( var i = 0; i < len ; i++ ) {
progressBarArray [i].style.display = 'none';
}
}