From ee57aeec20075e4e8d13ff6b60974d22e3d97f11 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 4 Nov 2024 15:31:21 +0100 Subject: [PATCH] js: Use a submit event's `submitter` property to identify the submit button --- public/js/icinga/behavior/modal.js | 32 +++++++++++++++++++----------- public/js/icinga/events.js | 28 ++++++++++++++++---------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/public/js/icinga/behavior/modal.js b/public/js/icinga/behavior/modal.js index 4a13f313d4..4eb452758a 100644 --- a/public/js/icinga/behavior/modal.js +++ b/public/js/icinga/behavior/modal.js @@ -92,31 +92,39 @@ * @returns {boolean} */ Modal.prototype.onFormSubmit = function(event) { - var _this = event.data.self; - var $form = $(event.currentTarget).closest('form'); - var $modal = $form.closest('#modal'); - - var $button; - var $rememberedSubmittButton = $form.data('submitButton'); - if (typeof $rememberedSubmittButton != 'undefined') { - if ($form.has($rememberedSubmittButton)) { - $button = $rememberedSubmittButton; + const _this = event.data.self; + const $form = $(event.currentTarget).closest('form'); + const $modal = $form.closest('#modal'); + + let $button; + if (typeof event.originalEvent !== 'undefined' + && typeof event.originalEvent.submitter !== 'undefined' + && event.originalEvent.submitter !== null) { + $button = $(event.originalEvent.submitter); + } + + // Safari fallback only + const $rememberedSubmitButton = $form.data('submitButton'); + if (typeof $rememberedSubmitButton !== 'undefined') { + if (typeof $button === 'undefined' && $form.has($rememberedSubmitButton)) { + $button = $rememberedSubmitButton; } + $form.removeData('submitButton'); } let $autoSubmittedBy; - if (! $autoSubmittedBy && event.detail && event.detail.submittedBy) { + if (typeof event.detail !== 'undefined' && "submittedBy" in event.detail) { $autoSubmittedBy = $(event.detail.submittedBy); } // Prevent our other JS from running $modal[0].dataset.noIcingaAjax = ''; - var req = _this.icinga.loader.submitForm($form, $autoSubmittedBy, $button); + const req = _this.icinga.loader.submitForm($form, $autoSubmittedBy, $button); req.addToHistory = false; req.done(function (data, textStatus, req) { - var title = req.getResponseHeader('X-Icinga-Title'); + const title = req.getResponseHeader('X-Icinga-Title'); if (!! title) { _this.setTitle($modal, decodeURIComponent(title).replace(/\s::\s.*/, '')); } diff --git a/public/js/icinga/events.js b/public/js/icinga/events.js index 1878d8f604..8331b0e8da 100644 --- a/public/js/icinga/events.js +++ b/public/js/icinga/events.js @@ -200,26 +200,34 @@ * */ submitForm: function (event, $autoSubmittedBy) { - var _this = event.data.self; + const _this = event.data.self; // .closest is not required unless subelements to trigger this - var $form = $(event.currentTarget).closest('form'); + const $form = $(event.currentTarget).closest('form'); if ($form.closest('[data-no-icinga-ajax]').length > 0) { return true; } - - var $button; - var $rememberedSubmittButton = $form.data('submitButton'); - if (typeof $rememberedSubmittButton != 'undefined') { - if ($form.has($rememberedSubmittButton)) { - $button = $rememberedSubmittButton; + + let $button; + if (typeof event.originalEvent !== 'undefined' + && typeof event.originalEvent.submitter !== 'undefined' + && event.originalEvent.submitter !== null) { + $button = $(event.originalEvent.submitter); + } + + // Safari fallback only + const $rememberedSubmitButton = $form.data('submitButton'); + if (typeof $rememberedSubmitButton !== 'undefined') { + if (typeof $button === 'undefined' && $form.has($rememberedSubmitButton)) { + $button = $rememberedSubmitButton; } + $form.removeData('submitButton'); } if (typeof $button === 'undefined') { - var $el; + let $el; if (typeof event.originalEvent !== 'undefined' && typeof event.originalEvent.explicitOriginalTarget === 'object') { // Firefox @@ -239,7 +247,7 @@ } } - if (! $autoSubmittedBy && event.detail && event.detail.submittedBy) { + if (! $autoSubmittedBy && typeof event.detail !== 'undefined' && "submittedBy" in event.detail) { $autoSubmittedBy = $(event.detail.submittedBy); }