@@ -8,6 +8,7 @@ export class UploadComponent extends BaseComponent {
8
8
#currentFiles;
9
9
10
10
html ( ) {
11
+ // not using U.createField() because we need a different structure
11
12
const field = document . createElement ( "div" ) ;
12
13
field . classList . add ( "field" , "file" , "dropzone" ) ;
13
14
@@ -27,13 +28,13 @@ export class UploadComponent extends BaseComponent {
27
28
control . appendChild ( U . modalHint ( this . config ) ) ;
28
29
control . appendChild ( U . modal ( this . config ) ) ;
29
30
}
30
-
31
31
field . appendChild ( control ) ;
32
32
33
- const fileLabel = document . createElement ( 'label' ) ;
34
- fileLabel . classList . add ( 'file-label' ) ;
33
+ // input container must be a label to trigger upload
34
+ const fileInputContainer = document . createElement ( "label" ) ;
35
+ fileInputContainer . classList . add ( "buttons" , "file-label" ) ;
35
36
36
- control . appendChild ( fileLabel ) ;
37
+ control . appendChild ( fileInputContainer ) ;
37
38
38
39
// actual file input and Bulma call-to-action (cta) element
39
40
const input = document . createElement ( "input" ) ;
@@ -48,11 +49,11 @@ export class UploadComponent extends BaseComponent {
48
49
input . dataset [ 'max' ] = this . config . validation . filesize ;
49
50
}
50
51
51
- fileLabel . appendChild ( input ) ;
52
+ fileInputContainer . appendChild ( input ) ;
52
53
53
54
const cta = document . createElement ( "span" ) ;
54
55
cta . classList . add ( "file-cta" ) ;
55
- fileLabel . appendChild ( cta ) ;
56
+ fileInputContainer . appendChild ( cta ) ;
56
57
57
58
const labelSpan = document . createElement ( "span" ) ;
58
59
labelSpan . classList . add ( "file-label" ) ;
@@ -64,14 +65,39 @@ export class UploadComponent extends BaseComponent {
64
65
infoContainer . classList . add ( "notification" , "hidden" , "is-warning" ) ;
65
66
control . appendChild ( infoContainer ) ;
66
67
67
- // upload status from state
68
- this . #showStatus( infoContainer ) ;
69
-
70
68
const errorContainer = document . createElement ( "div" ) ;
71
69
errorContainer . classList . add ( "notification" , "hidden" , "is-danger" ) ;
72
70
control . appendChild ( errorContainer ) ;
73
71
74
- // drag & drop handling
72
+ // create upload status info from state
73
+ this . #showStatus( infoContainer ) ;
74
+
75
+ // button to delete existing uploads
76
+ const deleteButton = document . createElement ( "button" ) ;
77
+ deleteButton . type = "button" ;
78
+ deleteButton . classList . add ( "button" ) ;
79
+ deleteButton . innerText = U . getLang ( "button_upload_replace" ) ;
80
+
81
+ deleteButton . addEventListener ( "click" , e => {
82
+ this . myState . value = null ;
83
+ input . value = null ;
84
+ this . render ( ) ;
85
+ } ) ;
86
+
87
+ infoContainer . appendChild ( deleteButton ) ;
88
+
89
+ this . attachDragAndDropHandlers ( field , input ) ;
90
+
91
+ return field ;
92
+ }
93
+
94
+ /**
95
+ * Add drag & drop handlers
96
+ *
97
+ * @param {HTMLElement } field
98
+ * @param {HTMLElement } input
99
+ */
100
+ attachDragAndDropHandlers ( field , input ) {
75
101
field . addEventListener ( "drop" , e => {
76
102
e . preventDefault ( ) ;
77
103
e . stopPropagation ( ) ;
@@ -94,8 +120,6 @@ export class UploadComponent extends BaseComponent {
94
120
e . stopPropagation ( ) ;
95
121
e . target . classList . remove ( "highlight" ) ;
96
122
} ) ;
97
-
98
- return field ;
99
123
}
100
124
101
125
/** @override */
@@ -134,13 +158,21 @@ export class UploadComponent extends BaseComponent {
134
158
if ( ! this . myState . value ) {
135
159
return ;
136
160
}
137
- const uploads = document . createElement ( "ul" ) ;
138
- infoNotification . appendChild ( uploads ) ;
161
+
162
+ const oldUploads = infoNotification . querySelector ( "ul" ) ;
163
+ const newUploads = document . createElement ( "ul" ) ;
164
+
139
165
for ( const fileInfo of this . myState . value ) {
140
- uploads . insertAdjacentHTML (
166
+ newUploads . insertAdjacentHTML (
141
167
"beforeend" ,
142
168
`<li><a href="${ fileInfo . content } " download="${ fileInfo . file } ">${ fileInfo . file } </a></li>` )
143
169
}
170
+
171
+ if ( oldUploads !== null ) {
172
+ infoNotification . replaceChild ( newUploads , oldUploads ) ;
173
+ } else {
174
+ infoNotification . appendChild ( newUploads ) ;
175
+ }
144
176
infoNotification . classList . remove ( "hidden" ) ;
145
177
}
146
178
0 commit comments