From 5d9ca31e8403f61bb3bd20df1c993f838291c6e3 Mon Sep 17 00:00:00 2001 From: Marco Bonetti Date: Mon, 2 Oct 2023 10:09:03 +0200 Subject: [PATCH 1/3] fix: initialize dropzoneSelector inside inline formsets in a Django 4.1 compatible way --- filer/static/filer/js/addons/dropzone.init.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/filer/static/filer/js/addons/dropzone.init.js b/filer/static/filer/js/addons/dropzone.init.js index f9bfdac55..6ab2ae159 100644 --- a/filer/static/filer/js/addons/dropzone.init.js +++ b/filer/static/filer/js/addons/dropzone.init.js @@ -164,7 +164,27 @@ djQuery(function ($) { // There is no way to feature detect the new behavior implemented in Django 1.9 if (!window.__admin_utc_offset__) { $(document).on('formset:added', function (ev, row) { - var dropzones = $(row).find(dropzoneSelector); + if(ev.detail.formsetName) { + /* + Django 4.1 changed the event type being fired when adding + a new formset from a jQuery to a vanilla JavaScript event. + https://docs.djangoproject.com/en/4.1/ref/contrib/admin/javascript/ + + In this case we find the newly added row and initialize the + dropzone on any dropzoneSelector on that row. + */ + let rowIdx = parseInt( + document.getElementById( + 'id_' + event.detail.formsetName + '-TOTAL_FORMS' + ).value, 10 + ) - 1; + let row_ = document.getElementById( event.detail.formsetName + '-' + rowIdx); + var dropzones = $(row_).find(dropzoneSelector) + + } else { + var dropzones = $(row).find(dropzoneSelector); + } + dropzones.each(createDropzone); }); } else { From 797fd2157f719c7ab93da92bc0f8a712bfe111aa Mon Sep 17 00:00:00 2001 From: Marco Bonetti Date: Mon, 2 Oct 2023 10:29:31 +0200 Subject: [PATCH 2/3] fix: double-check that ev.detail exists --- filer/static/filer/js/addons/dropzone.init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filer/static/filer/js/addons/dropzone.init.js b/filer/static/filer/js/addons/dropzone.init.js index 6ab2ae159..26b99a123 100644 --- a/filer/static/filer/js/addons/dropzone.init.js +++ b/filer/static/filer/js/addons/dropzone.init.js @@ -164,7 +164,7 @@ djQuery(function ($) { // There is no way to feature detect the new behavior implemented in Django 1.9 if (!window.__admin_utc_offset__) { $(document).on('formset:added', function (ev, row) { - if(ev.detail.formsetName) { + if(ev.detail && ev.detail.formsetName) { /* Django 4.1 changed the event type being fired when adding a new formset from a jQuery to a vanilla JavaScript event. From bfa7045793b72b1ec4b355c0bddb82f19447478b Mon Sep 17 00:00:00 2001 From: Marco Bonetti Date: Mon, 2 Oct 2023 10:54:08 +0200 Subject: [PATCH 3/3] chore: drop support for Django < 1.9 in the dropzone init handler --- filer/static/filer/js/addons/dropzone.init.js | 53 ++++++++----------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/filer/static/filer/js/addons/dropzone.init.js b/filer/static/filer/js/addons/dropzone.init.js index 26b99a123..d3c7ff67c 100644 --- a/filer/static/filer/js/addons/dropzone.init.js +++ b/filer/static/filer/js/addons/dropzone.init.js @@ -160,38 +160,31 @@ djQuery(function ($) { Dropzone.autoDiscover = false; } dropzones.each(createDropzone); - // window.__admin_utc_offset__ is used as canary to detect Django 1.8 - // There is no way to feature detect the new behavior implemented in Django 1.9 - if (!window.__admin_utc_offset__) { - $(document).on('formset:added', function (ev, row) { - if(ev.detail && ev.detail.formsetName) { - /* - Django 4.1 changed the event type being fired when adding - a new formset from a jQuery to a vanilla JavaScript event. - https://docs.djangoproject.com/en/4.1/ref/contrib/admin/javascript/ - In this case we find the newly added row and initialize the - dropzone on any dropzoneSelector on that row. - */ - let rowIdx = parseInt( - document.getElementById( - 'id_' + event.detail.formsetName + '-TOTAL_FORMS' - ).value, 10 - ) - 1; - let row_ = document.getElementById( event.detail.formsetName + '-' + rowIdx); - var dropzones = $(row_).find(dropzoneSelector) + // Handle initialization of the dropzone on dynamic formsets (i.e. Django admin inlines) + $(document).on('formset:added', function (ev, row) { + if(ev.detail && ev.detail.formsetName) { + /* + Django 4.1 changed the event type being fired when adding + a new formset from a jQuery to a vanilla JavaScript event. + https://docs.djangoproject.com/en/4.1/ref/contrib/admin/javascript/ - } else { - var dropzones = $(row).find(dropzoneSelector); - } + In this case we find the newly added row and initialize the + dropzone on any dropzoneSelector on that row. + */ + let rowIdx = parseInt( + document.getElementById( + 'id_' + event.detail.formsetName + '-TOTAL_FORMS' + ).value, 10 + ) - 1; + let row_ = document.getElementById(event.detail.formsetName + '-' + rowIdx); + var dropzones = $(row_).find(dropzoneSelector) - dropzones.each(createDropzone); - }); - } else { - $('.add-row a').on('click', function () { - var dropzones = $(dropzoneSelector); - dropzones.each(createDropzone); - }); - } + } else { + var dropzones = $(row).find(dropzoneSelector); + } + + dropzones.each(createDropzone); + }); } });