1 | |||
2 | |||
3 | class DropFile |
||
4 | { |
||
5 | /** |
||
6 | * Constructor. |
||
7 | */ |
||
8 | constructor() |
||
9 | { |
||
10 | this.oDropFile = null; |
||
11 | this.oFileInput = null; |
||
12 | this.oFileSelect = null; |
||
13 | this.oSelectedFiles = null; |
||
14 | |||
15 | this.dataTransfer = new DataTransfer(); |
||
0 ignored issues
–
show
|
|||
16 | } |
||
17 | |||
18 | init(strName) |
||
0 ignored issues
–
show
|
|||
19 | { |
||
20 | this.oDropFile = document.querySelector('.dropfile'); |
||
21 | this.oFileInput = document.querySelector('[name="files[]"'); |
||
22 | this.oFileSelect = document.querySelector('.fileselect'); |
||
23 | this.oSelectedFiles = document.querySelector('.selectedfiles'); |
||
24 | |||
25 | ['drag', 'dragstart', 'dragend', 'dragover', 'dragenter', 'dragleave', 'drop'].forEach( event => this.oDropFile.addEventListener(event, function(e) { |
||
26 | e.preventDefault(); |
||
27 | e.stopPropagation(); |
||
28 | }), false ); |
||
29 | |||
30 | ['dragover', 'dragenter'].forEach( event => this.oDropFile.addEventListener(event, function(e) { |
||
0 ignored issues
–
show
|
|||
31 | g_oDropFile.oDropFile.classList.add('dragover'); |
||
0 ignored issues
–
show
|
|||
32 | }), false ); |
||
33 | |||
34 | ['dragleave', 'dragend', 'drop'].forEach( event => this.oDropFile.addEventListener(event, function(e) { |
||
0 ignored issues
–
show
|
|||
35 | g_oDropFile.oDropFile.classList.remove('dragover'); |
||
0 ignored issues
–
show
|
|||
36 | }), false ); |
||
37 | |||
38 | this.oDropFile.addEventListener('drop', function(e) { |
||
39 | for (var i = 0; i < e.dataTransfer.files.length; i++) { |
||
40 | // TODO: check, if allowed |
||
41 | g_oDropFile.dataTransfer.items.add(e.dataTransfer.files[i]); |
||
0 ignored issues
–
show
|
|||
42 | } |
||
43 | updateFileList(); |
||
44 | }, false ); |
||
45 | |||
46 | this.oFileInput.addEventListener('change', function(e) { |
||
0 ignored issues
–
show
|
|||
47 | for (var i = 0; i < g_oDropFile.oFileInput.files.length; i++) { |
||
0 ignored issues
–
show
|
|||
48 | // TODO: check, if allowed |
||
49 | g_oDropFile.dataTransfer.items.add(g_oDropFile.oFileInput.files[i]); |
||
50 | } |
||
51 | updateFileList(); |
||
52 | }, false ); |
||
53 | } |
||
54 | |||
55 | setSelectedFiles() |
||
56 | { |
||
57 | this.oFileInput.files = g_oDropFile.dataTransfer.files; |
||
0 ignored issues
–
show
|
|||
58 | |||
59 | return true; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | function updateFileList() |
||
64 | { |
||
65 | /* |
||
66 | const filesArray = Array.from(g_oDropFile.dataTransfer.files); |
||
67 | if (filesArray.length > 1) { |
||
68 | g_oDropFile.oSelectedFiles.innerHTML = '<p>Selected files:</p><ul><li>' + filesArray.map(f => f.name).join('</li><li>') + '</li></ul>'; |
||
69 | } else if (filesArray.length == 1) { |
||
70 | g_oDropFile.oSelectedFiles.innerHTML = `<p>Selected file: ${filesArray[0].name}</p>`; |
||
71 | } else { |
||
72 | g_oDropFile.oSelectedFiles.innerHTML = ''; |
||
73 | } |
||
74 | */ |
||
75 | if (g_oDropFile.dataTransfer.files.length === 0) { |
||
0 ignored issues
–
show
|
|||
76 | g_oDropFile.oSelectedFiles.innerHTML = ''; |
||
77 | return; |
||
78 | } |
||
79 | const aCSSClasses = { |
||
80 | 'image/jpg' : 'imgfile', |
||
81 | 'image/jpeg' : 'imgfile', |
||
82 | 'image/png' : 'imgfile', |
||
83 | 'image/gif' : 'imgfile', |
||
84 | 'image/bmp' : 'imgfile', |
||
85 | 'image/svg+xml' : 'imgfile', |
||
86 | 'application/pdf' : 'pdffile', |
||
87 | 'application/vnd.ms-excel' : 'xlsfile', |
||
88 | 'application/msexcel' : 'xlsfile', |
||
89 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' : 'xlsfile', |
||
90 | 'text/csv' : 'xlsfile', |
||
91 | 'application/vnd.ms-word' : 'docfile', |
||
92 | 'application/msword' : 'docfile', |
||
93 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' : 'docfile', |
||
94 | 'text/x-vcard' : 'vcard', |
||
95 | 'text/calendar' : 'ical', |
||
96 | 'application/zip' : 'archive', |
||
97 | 'application/x-zip-compressed' : 'archive', |
||
98 | 'application/vnd.rar' : 'archive', |
||
99 | 'application/x-rar-compressed' : 'archive', |
||
100 | 'application/x-tar' : 'archive', |
||
101 | 'application/x-tar-compressed' : 'archive', |
||
102 | 'application/x-7z-compressed' : 'archive', |
||
103 | 'audio/mpeg' : 'audiofile', |
||
104 | 'audio/mp3' : 'audiofile', |
||
105 | 'video/mpeg' : 'videofile', |
||
106 | 'video/mp4' : 'videofile', |
||
107 | 'video/x-msvideo' : 'videofile', |
||
108 | }; |
||
109 | var strSelectedFiles = ''; |
||
110 | for (var i = 0; i < g_oDropFile.dataTransfer.files.length; i++) { |
||
111 | var strCSSClass = 'file'; |
||
112 | if (aCSSClasses[g_oDropFile.dataTransfer.files[i].type]) { |
||
113 | strCSSClass += ' ' + aCSSClasses[g_oDropFile.dataTransfer.files[i].type]; |
||
114 | } |
||
115 | const EOL = "\n"; |
||
116 | const strName = g_oDropFile.dataTransfer.files[i].name; |
||
117 | const strOnClick = "deleteSelectedFile(" + i + ", '" + strName + "');"; |
||
118 | const strDeleteFromList = 'Eintrag aus der Liste entfernen'; |
||
119 | |||
120 | strSelectedFiles += '<div class="dropfileitem">' + EOL; |
||
121 | strSelectedFiles += ' <div class="dropedfile">' + EOL; |
||
122 | strSelectedFiles += ' <div class="dropedname">' + EOL; |
||
123 | strSelectedFiles += ' <span class="' + strCSSClass + '" title="' + strName + '">' + strName + '</span>' + EOL; |
||
124 | strSelectedFiles += ' </div>' + EOL; |
||
125 | strSelectedFiles += ' <div class="dropeddelete" onclick="' + strOnClick + '" title="' + strDeleteFromList + '"></div>' + EOL; |
||
126 | strSelectedFiles += ' </div>' + EOL; |
||
127 | strSelectedFiles += '</div>' + EOL; |
||
128 | } |
||
129 | g_oDropFile.oSelectedFiles.innerHTML = strSelectedFiles; |
||
130 | } |
||
131 | |||
132 | function deleteSelectedFile(iIndex, strFilename) |
||
133 | { |
||
134 | if (confirm('Soll die Datei [' + strFilename + '] aus der Liste entfernt werden?') == true) { |
||
0 ignored issues
–
show
Debugging Code
Best Practice
introduced
by
|
|||
135 | g_oDropFile.dataTransfer.items.remove(iIndex); |
||
0 ignored issues
–
show
|
|||
136 | updateFileList(); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | const g_oDropFile = new DropFile(); |
||
0 ignored issues
–
show
|
|||
141 | |||
142 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.