experimental: use dialog for adding category

This commit is contained in:
Mira Weller
2026-09-30 21:03:27 +02:00
parent d6091a0756
commit 1ea3aff7e6
2 changed files with 36 additions and 0 deletions
+4
View File
@@ -1142,6 +1142,10 @@ class ItemAddOnForm(I18nModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['addon_category'].queryset = self.event.categories.all()
self.fields['addon_category'].help_text = format_html('<a href="javascript:" data-django-dialog="{}?notify_parent=true">{}</a>', reverse('control:event.items.categories.add', kwargs={
'event': self.event.slug,
'organizer': self.event.organizer.slug,
}), _("Create new category"))
self.fields['addon_category'].widget = Select2(
attrs={
'data-model-select2': 'generic',
@@ -1122,6 +1122,38 @@ $(function () {
}).areYouSure({ message: gettext('You have unsaved changes!') })
})
function show_django_dialog(url, callback) {
function messageEvent(e) {
console.log('messageEvent', e.origin, e.source, e.data)
if (e.origin === location.origin && e.data.type === 'pretix:dialog-loaded') {
$dlg.find("iframe").attr("height", Math.min(window.innerHeight - 120, e.data.contentHeight|0)).css("visibility", "visible")
$dlg.find("center").remove()
}
if (e.origin === location.origin && e.data.type === 'pretix:notify-parent') {
$dlg[0].close()
if (!callback(e.data.data)) {
if (e.data.data.messages?.length) {
alert(e.data.data.messages.map(m => m.message).join('\n\n'))
}
}
}
}
var $dlg = $('<dialog class="modal-card no-padding no-scroll" closedby="any"><center><i class="fa fa-cog big-rotating-icon"></i></center><iframe height="400" width="100%"></iframe></div>')
.css('max-width', '60em')
$dlg.find("iframe").attr("src", url).css("visibility", "hidden").css("border", "0")
window.addEventListener('message', messageEvent)
$dlg.appendTo("body")
$dlg.on('close', function() {
window.removeEventListener('message', messageEvent)
$dlg.remove()
})
$dlg[0].showModal()
}
$(function() {
$("[data-django-dialog]").on("click", function(e) {
show_django_dialog(this.getAttribute("data-django-dialog"), function() {})
})
})
function notify_parent_frame() {
window.addEventListener('message', function(e) {
if (e.source === window) return