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/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 { diff --git a/src/pat/auto-submit/auto-submit.js b/src/pat/auto-submit/auto-submit.js index 1df3d5886..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 @@ -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); }, }); 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();