@@ -333,20 +333,19 @@ async function onEditContent(event) {
333
333
let dz ;
334
334
const $dropzone = $editContentZone . find ( '.dropzone' ) ;
335
335
if ( $dropzone . length === 1 ) {
336
- $dropzone . data ( 'saved' , false ) ;
337
-
338
- const fileUuidDict = { } ;
339
- dz = await createDropzone ( $dropzone [ 0 ] , {
340
- url : $dropzone . data ( 'upload-url' ) ,
336
+ let disableRemovedfileEvent = false ; // when resetting the dropzone (removeAllFiles), disable the "removedfile" event
337
+ let fileUuidDict = { } ; // to record: if a comment has been saved, then the uploaded files won't be deleted from server when clicking the Remove in the dropzone
338
+ const dz = await createDropzone ( $dropzone [ 0 ] , {
339
+ url : $dropzone . attr ( 'data-upload-url' ) ,
341
340
headers : { 'X-Csrf-Token' : csrfToken } ,
342
- maxFiles : $dropzone . data ( ' max-file') ,
343
- maxFilesize : $dropzone . data ( ' max-size') ,
344
- acceptedFiles : ( [ '*/*' , '' ] . includes ( $dropzone . data ( ' accepts') ) ) ? null : $dropzone . data ( ' accepts') ,
341
+ maxFiles : $dropzone . attr ( 'data- max-file') ,
342
+ maxFilesize : $dropzone . attr ( 'data- max-size') ,
343
+ acceptedFiles : ( [ '*/*' , '' ] . includes ( $dropzone . attr ( 'data- accepts') ) ) ? null : $dropzone . attr ( 'data- accepts') ,
345
344
addRemoveLinks : true ,
346
- dictDefaultMessage : $dropzone . data ( ' default-message') ,
347
- dictInvalidFileType : $dropzone . data ( ' invalid-input-type') ,
348
- dictFileTooBig : $dropzone . data ( ' file-too-big') ,
349
- dictRemoveFile : $dropzone . data ( ' remove-file') ,
345
+ dictDefaultMessage : $dropzone . attr ( 'data- default-message') ,
346
+ dictInvalidFileType : $dropzone . attr ( 'data- invalid-input-type') ,
347
+ dictFileTooBig : $dropzone . attr ( 'data- file-too-big') ,
348
+ dictRemoveFile : $dropzone . attr ( 'data- remove-file') ,
350
349
timeout : 0 ,
351
350
thumbnailMethod : 'contain' ,
352
351
thumbnailWidth : 480 ,
@@ -359,9 +358,10 @@ async function onEditContent(event) {
359
358
$dropzone . find ( '.files' ) . append ( input ) ;
360
359
} ) ;
361
360
this . on ( 'removedfile' , ( file ) => {
361
+ if ( disableRemovedfileEvent ) return ;
362
362
$ ( `#${ file . uuid } ` ) . remove ( ) ;
363
- if ( $dropzone . data ( ' remove-url') && ! fileUuidDict [ file . uuid ] . submitted ) {
364
- $ . post ( $dropzone . data ( ' remove-url') , {
363
+ if ( $dropzone . attr ( 'data- remove-url') && ! fileUuidDict [ file . uuid ] . submitted ) {
364
+ $ . post ( $dropzone . attr ( 'data- remove-url') , {
365
365
file : file . uuid ,
366
366
_csrf : csrfToken ,
367
367
} ) ;
@@ -373,20 +373,25 @@ async function onEditContent(event) {
373
373
} ) ;
374
374
} ) ;
375
375
this . on ( 'reload' , ( ) => {
376
- $ . getJSON ( $editContentZone . data ( 'attachment-url' ) , ( data ) => {
376
+ $ . getJSON ( $editContentZone . attr ( 'data-attachment-url' ) , ( data ) => {
377
+ // do not trigger the "removedfile" event, otherwise the attachments would be deleted from server
378
+ disableRemovedfileEvent = true ;
377
379
dz . removeAllFiles ( true ) ;
378
380
$dropzone . find ( '.files' ) . empty ( ) ;
379
- $ . each ( data , function ( ) {
380
- const imgSrc = `${ $dropzone . data ( 'link-url' ) } /${ this . uuid } ` ;
381
- dz . emit ( 'addedfile' , this ) ;
382
- dz . emit ( 'thumbnail' , this , imgSrc ) ;
383
- dz . emit ( 'complete' , this ) ;
384
- dz . files . push ( this ) ;
385
- fileUuidDict [ this . uuid ] = { submitted : true } ;
381
+ fileUuidDict = { } ;
382
+ disableRemovedfileEvent = false ;
383
+
384
+ for ( const attachment of data ) {
385
+ const imgSrc = `${ $dropzone . attr ( 'data-link-url' ) } /${ attachment . uuid } ` ;
386
+ dz . emit ( 'addedfile' , attachment ) ;
387
+ dz . emit ( 'thumbnail' , attachment , imgSrc ) ;
388
+ dz . emit ( 'complete' , attachment ) ;
389
+ dz . files . push ( attachment ) ;
390
+ fileUuidDict [ attachment . uuid ] = { submitted : true } ;
386
391
$dropzone . find ( `img[src='${ imgSrc } ']` ) . css ( 'max-width' , '100%' ) ;
387
- const input = $ ( `<input id="${ this . uuid } " name="files" type="hidden">` ) . val ( this . uuid ) ;
392
+ const input = $ ( `<input id="${ attachment . uuid } " name="files" type="hidden">` ) . val ( attachment . uuid ) ;
388
393
$dropzone . find ( '.files' ) . append ( input ) ;
389
- } ) ;
394
+ }
390
395
} ) ;
391
396
} ) ;
392
397
} ,
0 commit comments