Skip to content

Commit 68c3a5c

Browse files
authored
fix: initialize dropzoneSelector inside inline formsets in a Django 4.1 compatible way (#1431)
* fix: initialize dropzoneSelector inside inline formsets in a Django 4.1 compatible way * fix: double-check that ev.detail exists * chore: drop support for Django < 1.9 in the dropzone init handler
1 parent 421c86b commit 68c3a5c

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

filer/static/filer/js/addons/dropzone.init.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,31 @@ djQuery(function ($) {
160160
Dropzone.autoDiscover = false;
161161
}
162162
dropzones.each(createDropzone);
163-
// window.__admin_utc_offset__ is used as canary to detect Django 1.8
164-
// There is no way to feature detect the new behavior implemented in Django 1.9
165-
if (!window.__admin_utc_offset__) {
166-
$(document).on('formset:added', function (ev, row) {
163+
164+
// Handle initialization of the dropzone on dynamic formsets (i.e. Django admin inlines)
165+
$(document).on('formset:added', function (ev, row) {
166+
if(ev.detail && ev.detail.formsetName) {
167+
/*
168+
Django 4.1 changed the event type being fired when adding
169+
a new formset from a jQuery to a vanilla JavaScript event.
170+
https://docs.djangoproject.com/en/4.1/ref/contrib/admin/javascript/
171+
172+
In this case we find the newly added row and initialize the
173+
dropzone on any dropzoneSelector on that row.
174+
*/
175+
let rowIdx = parseInt(
176+
document.getElementById(
177+
'id_' + event.detail.formsetName + '-TOTAL_FORMS'
178+
).value, 10
179+
) - 1;
180+
let row_ = document.getElementById(event.detail.formsetName + '-' + rowIdx);
181+
var dropzones = $(row_).find(dropzoneSelector)
182+
183+
} else {
167184
var dropzones = $(row).find(dropzoneSelector);
168-
dropzones.each(createDropzone);
169-
});
170-
} else {
171-
$('.add-row a').on('click', function () {
172-
var dropzones = $(dropzoneSelector);
173-
dropzones.each(createDropzone);
174-
});
175-
}
185+
}
186+
187+
dropzones.each(createDropzone);
188+
});
176189
}
177190
});

0 commit comments

Comments
 (0)