From 1cfec45b44ef0a38cc237544eb6d2d9042db9a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Kami=C5=84ski?= <33148232+robert8888@users.noreply.github.com> Date: Tue, 30 Mar 2021 19:09:33 +0200 Subject: [PATCH 1/2] Update django_better_admin_arrayfield.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to documentation we can change 'subwidget_form' to any build in or custom widget.  This option doesn't work because line 29-32 in querySelector is hardcoded looking for input field, and don't have any protection in case when it doesn't find it, so script throws error when it's trying getAttribute('id) on undefined variable in case when we're using textarea.  I change this to finding tag of first element and then using it to next querySelector calls. (I test this with inputs and textareas ans selects and it works ok) Now it works with all kids of input (like it was) textarea and selects. --- .../js/django_better_admin_arrayfield.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.js b/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.js index 353004f..b3adc38 100644 --- a/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.js +++ b/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.js @@ -25,14 +25,20 @@ window.addEventListener('load', function () { widgetElement.querySelector('.add-array-item').addEventListener('click', () => { item_count++; - const newElement = elementTemplate.cloneNode(true); - const id_parts = newElement.querySelector('input').getAttribute('id').split('_'); + const newElementContainer = elementTemplate.cloneNode(true); + const newElementControl = newElementContainer.querySelectorAll('input, textarea, select')[0] + if(!newElementControl){ + console.warn('Django better admin array field - can\' find correct form element ' + + '- expect input or textarea as custom widget implementation') + return + } + const id_parts = newElementControl.getAttribute('id').split('_'); const id = id_parts.slice(0, -1).join('_') + '_' + String(item_count - 1); - newElement.querySelector('input').setAttribute('id', id); - newElement.querySelector('input').value = ''; + newElementControl.setAttribute('id', id); + newElementControl.value = ''; - addRemoveEventListener(newElement); - parentElement.appendChild(newElement); + addRemoveEventListener(newElementContainer); + parentElement.appendChild(newElementContainer); }); } From a04cf9b7c667c76793369acd1e5e0c9b561dd13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Kami=C5=84ski?= <33148232+robert8888@users.noreply.github.com> Date: Tue, 30 Mar 2021 20:51:52 +0200 Subject: [PATCH 2/2] minification minification --- .../static/js/django_better_admin_arrayfield.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.min.js b/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.min.js index 4c44f07..a61b2d6 100644 --- a/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.min.js +++ b/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.min.js @@ -1 +1 @@ -window.addEventListener("load",function(){function a(a){a.querySelectorAll(".remove").forEach(a=>{a.addEventListener("click",()=>{a.parentNode.remove()})})}function b(b){const d=b.querySelector(".array-item"),e=d.cloneNode(!0),f=d.parentElement;d.getAttribute("data-isNone")&&(d.remove(),e.removeAttribute("data-isNone"),e.removeAttribute("style")),a(b),b.querySelector(".add-array-item").addEventListener("click",()=>{c++;const b=e.cloneNode(!0),d=b.querySelector("input").getAttribute("id").split("_"),g=d.slice(0,-1).join("_")+"_"+(c-1+"");b.querySelector("input").setAttribute("id",g),b.querySelector("input").value="",a(b),f.appendChild(b)})}let c=1;django.jQuery(".dynamic-array-widget").not(".empty-form .dynamic-array-widget").each((a,c)=>b(c)),django.jQuery(document).on("formset:added",function(a,c){c[0].querySelectorAll(".dynamic-array-widget").forEach(a=>b(a))})}); +window.addEventListener("load",function(){let i=1;function d(e){e.querySelectorAll(".remove").forEach(e=>{e.addEventListener("click",()=>{e.parentNode.remove()})})}function n(e){const t=e.querySelector(".array-item"),a=t.cloneNode(!0),o=t.parentElement;t.getAttribute("data-isNone")&&(t.remove(),a.removeAttribute("data-isNone"),a.removeAttribute("style")),d(e),e.querySelector(".add-array-item").addEventListener("click",()=>{i++;const e=a.cloneNode(!0),t=e.querySelectorAll("input, textarea, select")[0];if(t){const n=t.getAttribute("id").split("_");var r=n.slice(0,-1).join("_")+"_"+String(i-1);t.setAttribute("id",r),t.value="",d(e),o.appendChild(e)}else console.warn("Django better admin array field - can' find correct form element - expect input or textarea as custom widget implementation")})}django.jQuery(".dynamic-array-widget").not(".empty-form .dynamic-array-widget").each((e,t)=>n(t)),django.jQuery(document).on("formset:added",function(e,t,r){t[0].querySelectorAll(".dynamic-array-widget").forEach(e=>n(e))})});