diff --git a/django_better_admin_arrayfield/forms/widgets.py b/django_better_admin_arrayfield/forms/widgets.py index f0a57ae..c76b03b 100644 --- a/django_better_admin_arrayfield/forms/widgets.py +++ b/django_better_admin_arrayfield/forms/widgets.py @@ -1,4 +1,5 @@ from django import forms +from django.contrib.admin.widgets import AdminSplitDateTime class DynamicArrayWidget(forms.TextInput): @@ -46,3 +47,23 @@ class DynamicArrayTextareaWidget(DynamicArrayWidget): def __init__(self, *args, **kwargs): kwargs.setdefault("subwidget_form", forms.Textarea) super().__init__(*args, **kwargs) + +class DatetimeWidget(DynamicArrayWidget): + """Datetime widget for array datetime lists""" + + def __init__(self, *args, **kwargs): + kwargs['subwidget_form'] = AdminSplitDateTime + super().__init__(*args, **kwargs) + + def value_from_datadict(self, data, files, name): + try: + getter = data.getlist + dates = getter(name + '_0') + times = getter(name + '_1') + return_data = [] + for i in range(len(dates)): + if dates[i] and times[i]: + return_data.append(' '.join([dates[i], times[i]])) + return return_data + except AttributeError: + return data.get(name) 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..4759516 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 @@ -33,9 +33,29 @@ window.addEventListener('load', function () { addRemoveEventListener(newElement); parentElement.appendChild(newElement); - }); - } + for (const inp of document.getElementsByTagName('input')) { + if ( + inp.type === 'text' && + inp.classList.contains('vTimeField') && + !inp.nextSibling?.classList.contains('datetimeshortcuts') + ) { + DateTimeShortcuts.addClock(inp); + DateTimeShortcuts.addTimezoneWarning(inp); + inp.value = ''; + } else if ( + inp.type === 'text' && + inp.classList.contains('vDateField') && + !inp.nextSibling?.classList.contains('datetimeshortcuts') + ) { + DateTimeShortcuts.addCalendar(inp); + DateTimeShortcuts.addTimezoneWarning(inp); + inp.value = ''; + } + } + }); + } + django.jQuery(".dynamic-array-widget").not(".empty-form .dynamic-array-widget").each( (index, widgetElement) => initializeWidget(widgetElement) ); @@ -45,4 +65,4 @@ window.addEventListener('load', function () { widgetElement => initializeWidget(widgetElement) ); }); - }); + }); \ No newline at end of file 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..4cb1bb4 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(){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);for(const a of document.getElementsByTagName("input"))"text"===a.type&&a.classList.contains("vTimeField")&&!a.nextSibling?.classList.contains("datetimeshortcuts")?(DateTimeShortcuts.addClock(a),DateTimeShortcuts.addTimezoneWarning(a),a.value=""):"text"===a.type&&a.classList.contains("vDateField")&&!a.nextSibling?.classList.contains("datetimeshortcuts")&&(DateTimeShortcuts.addCalendar(a),DateTimeShortcuts.addTimezoneWarning(a),a.value="")})}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))})}); \ No newline at end of file