From 58bf2fb5a242b018204777926cffbb5f16e4f30b Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Wed, 8 Nov 2023 13:46:40 +0100 Subject: [PATCH 1/4] input-change-events: Minor code optimization. --- src/lib/input-change-events.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib/input-change-events.js b/src/lib/input-change-events.js index 3048c4f18..1ef2a02af 100644 --- a/src/lib/input-change-events.js +++ b/src/lib/input-change-events.js @@ -1,17 +1,17 @@ // helper functions to make all input elements import $ from "jquery"; import logging from "../core/logging"; -var namespace = "input-change-events"; +const namespace = "input-change-events"; const log = logging.getLogger(namespace); -var _ = { +const _ = { setup: function ($el, pat) { if (!pat) { log.error("The name of the calling pattern has to be set."); return; } // list of patterns that installed input-change-event handlers - var patterns = $el.data(namespace) || []; + const patterns = $el.data(namespace) || []; log.debug("setup handlers for " + pat); if (!patterns.length) { @@ -42,9 +42,9 @@ var _ = { }, registerHandlersForElement: function () { - var $el = $(this), - isNumber = $el.is("input[type=number]"), - isText = $el.is("input:text, input[type=search], textarea"); + const $el = $(this); + const isNumber = $el.is("input[type=number]"); + const isText = $el.is("input:text, input[type=search], textarea"); if (isNumber) { // for we want to trigger the change @@ -74,7 +74,7 @@ var _ = { }, remove: function ($el, pat) { - var patterns = $el.data(namespace) || []; + let patterns = $el.data(namespace) || []; if (patterns.indexOf(pat) === -1) { log.warn("input-change-events were never installed for " + pat); } else { From 2e6b7e5949ce3ad23e4ffffa1070f5f598f70ba9 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Wed, 8 Nov 2023 13:43:30 +0100 Subject: [PATCH 2/4] feat(core events): Support the submitter property on submit_event events. --- src/core/events.js | 6 ++++-- src/core/events.test.js | 17 ++++++++++++++++- src/pat/inject/inject.test.js | 4 +++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/core/events.js b/src/core/events.js index 8106957fa..9919d8c67 100644 --- a/src/core/events.js +++ b/src/core/events.js @@ -237,11 +237,13 @@ const scroll_event = () => { }); }; -const submit_event = () => { - return new Event("submit", { +const submit_event = ({ submitter } = { submitter: undefined }) => { + const event = new Event("submit", { bubbles: true, cancelable: true, }); + event.submitter = submitter; // undefined or the submitting element + return event; }; const dragstart_event = () => { diff --git a/src/core/events.test.js b/src/core/events.test.js index f03e53f49..030fa94fc 100644 --- a/src/core/events.test.js +++ b/src/core/events.test.js @@ -546,12 +546,27 @@ describe("core.events tests", () => { }); it("submit event", async () => { - outer.addEventListener("submit", () => { + let submitter = "not defined"; + outer.addEventListener("submit", (e) => { catched = "outer"; + submitter = e.submitter; }); inner.dispatchEvent(events.submit_event()); await utils.timeout(1); expect(catched).toBe("outer"); + expect(submitter).toBe(undefined); + }); + + it("submit event with defined submitter", async () => { + let submitter = "not defined"; + outer.addEventListener("submit", (e) => { + catched = "outer"; + submitter = e.submitter; + }); + inner.dispatchEvent(events.submit_event({ submitter: inner })); + await utils.timeout(1); + expect(catched).toBe("outer"); + expect(submitter).toBe(inner); }); it("dragstart event", async () => { diff --git a/src/pat/inject/inject.test.js b/src/pat/inject/inject.test.js index eaa0b4820..d99c21247 100644 --- a/src/pat/inject/inject.test.js +++ b/src/pat/inject/inject.test.js @@ -1110,7 +1110,9 @@ describe("pat-inject", function () { // Work around jsDOM not submitting with image buttons. $submit[0].addEventListener("click", async () => { await utils.timeout(1); // wait a tick for click event reaching form before submitting. - $submit[0].form.dispatchEvent(events.submit_event()); + $submit[0].form.dispatchEvent( + events.submit_event({ submitter: $submit[0] }) + ); }); $submit[0].click(); From 36c0c36f1f3feed76510bb6d29762709dbc7e2b1 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Wed, 8 Nov 2023 14:02:20 +0100 Subject: [PATCH 3/4] fix(pat autosubmit): Trigger the submit event with the correct submitter property. This resp. the previous commit in event.js fixes a problem where the submitter was the added to the browser automatically and could lead to incorrect form submission data in combination with pat-ajax. E.g. we had a case where for a file upload the delete button's value was submitted, where it shouldn't have. --- src/pat/auto-submit/auto-submit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pat/auto-submit/auto-submit.js b/src/pat/auto-submit/auto-submit.js index 1df3d5886..1a95dac70 100644 --- a/src/pat/auto-submit/auto-submit.js +++ b/src/pat/auto-submit/auto-submit.js @@ -128,7 +128,7 @@ export default Base.extend({ onInputChange(e) { e.stopPropagation(); - this.el.dispatchEvent(events.submit_event()); + this.el.dispatchEvent(events.submit_event({ submitter: e.target })); log.debug("triggered by " + e.type); }, }); From 3be0170fb92a44d205b857f0636ba4ea9f1e1ea7 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Wed, 8 Nov 2023 14:05:18 +0100 Subject: [PATCH 4/4] fix(pat autosubmit): Fix a typo where pat-autosubmit wasn't working for subforms anymore. --- src/pat/auto-submit/auto-submit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pat/auto-submit/auto-submit.js b/src/pat/auto-submit/auto-submit.js index 1a95dac70..3a9c787de 100644 --- a/src/pat/auto-submit/auto-submit.js +++ b/src/pat/auto-submit/auto-submit.js @@ -66,7 +66,7 @@ export default Base.extend({ // get all subforms whice are not yet auto submit forms. const subforms = el.querySelectorAll( - ".pat-autosubmit:not(.pat-autosubmit):not(.pat-auto-submit)" + ".pat-subform:not(.pat-autosubmit):not(.pat-auto-submit)" ); for (const subform of subforms) { // register autosubmit on subform