Skip to content

Commit

Permalink
Merge pull request #1189 from Patternslib/fix-pat-inject
Browse files Browse the repository at this point in the history
pat inject fixes
  • Loading branch information
thet authored Nov 8, 2023
2 parents e71c270 + 3be0170 commit aeb653d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/core/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
17 changes: 16 additions & 1 deletion src/core/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
14 changes: 7 additions & 7 deletions src/lib/input-change-events.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 <input type="number" /> we want to trigger the change
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/pat/auto-submit/auto-submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
},
});
4 changes: 3 additions & 1 deletion src/pat/inject/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit aeb653d

Please sign in to comment.