Skip to content

Commit d5f69c2

Browse files
committed
pat-inject: Minor code optimization: Rework most forEach to for loops.
1 parent fa07469 commit d5f69c2

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/pat/inject/inject.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ const inject = {
102102
} else {
103103
switch (cfgs[0].trigger) {
104104
case "default":
105-
cfgs.forEach((cfg) => {
105+
for (const cfg of cfgs) {
106106
if (cfg.delay) {
107107
cfg.processDelay = cfg.delay;
108108
}
109-
});
109+
}
110110
// setup event handlers
111111
if (el?.nodeName === "FORM") {
112112
log.debug("Initializing form with injection on", el);
@@ -218,6 +218,7 @@ const inject = {
218218
cfgs = this.extractConfig($(cfg_node), opts);
219219
}
220220

221+
// store the params of the form in the config, to be used by history
221222
for (const cfg of cfgs) {
222223
cfg.params = $.param($el.serializeArray());
223224
}
@@ -230,13 +231,13 @@ const inject = {
230231
submitSubform($sub) {
231232
/* This method is called from pat-subform
232233
*/
233-
const $el = $sub.parents("form");
234+
const $el = $($sub[0].closest("form"));
234235
const cfgs = $sub.data("pat-inject");
235236

236237
// store the params of the subform in the config, to be used by history
237-
$(cfgs).each((i, v) => {
238-
v.params = $.param($sub.serializeArray());
239-
});
238+
for (const cfg of cfgs) {
239+
cfg.params = $.param($sub.serializeArray());
240+
}
240241

241242
try {
242243
$el.trigger("patterns-inject-triggered");
@@ -251,7 +252,7 @@ const inject = {
251252
options = Object.assign({}, options); // copy
252253

253254
const cfgs = parser.parse($el, options, true);
254-
cfgs.forEach((cfg) => {
255+
for (const cfg of cfgs) {
255256
cfg.$context = $el;
256257
// options and cfg have priority, fall back to href/action
257258
cfg.url =
@@ -283,7 +284,7 @@ const inject = {
283284
}
284285
}
285286
cfg.processDelay = 0;
286-
});
287+
}
287288
return cfgs;
288289
},
289290

@@ -577,7 +578,7 @@ const inject = {
577578
cfgs.forEach((cfg, idx1) => {
578579
const perform_inject = () => {
579580
if (cfg.target !== "none") {
580-
cfg.$target.each((idx2, target) => {
581+
for (const target of cfg.$target) {
581582
this._performInjection(
582583
target,
583584
$el,
@@ -586,7 +587,7 @@ const inject = {
586587
ev.target,
587588
title
588589
);
589-
});
590+
}
590591
}
591592
};
592593
if (cfg.processDelay) {
@@ -654,13 +655,13 @@ const inject = {
654655
}
655656

656657
// clean up
657-
cfgs.forEach((cfg) => {
658+
for (const cfg of cfgs) {
658659
if ("$injected" in cfg) {
659660
cfg.$injected.remove();
660661
}
661662
cfg.$target.removeClass(cfg.loadingClass);
662663
$el.removeClass(cfg.executingClass);
663-
});
664+
}
664665
$el.off("pat-ajax-success.pat-inject");
665666
$el.off("pat-ajax-error.pat-inject");
666667

@@ -1039,25 +1040,36 @@ const inject = {
10391040
}, timeout);
10401041

10411042
const unsub = () => {
1042-
["scroll", "resize"].forEach((e) =>
1043-
window.removeEventListener(e, onInteraction)
1044-
);
1045-
[
1043+
for (const event of ["scroll", "resize"]) {
1044+
window.removeEventListener(event, onInteraction);
1045+
}
1046+
for (const event of [
10461047
"click",
10471048
"keypress",
10481049
"keyup",
10491050
"mousemove",
10501051
"touchstart",
10511052
"touchend",
1052-
].forEach((e) => document.removeEventListener(e, onInteraction));
1053+
]) {
1054+
document.removeEventListener(event, onInteraction);
1055+
}
10531056
};
10541057

10551058
onInteraction();
10561059

1057-
["scroll", "resize"].forEach((e) => window.addEventListener(e, onInteraction));
1058-
["click", "keypress", "keyup", "mousemove", "touchstart", "touchend"].forEach(
1059-
(e) => document.addEventListener(e, onInteraction)
1060-
);
1060+
for (const event of ["scroll", "resize"]) {
1061+
window.addEventListener(event, onInteraction);
1062+
}
1063+
for (const event of [
1064+
"click",
1065+
"keypress",
1066+
"keyup",
1067+
"mousemove",
1068+
"touchstart",
1069+
"touchend",
1070+
]) {
1071+
document.addEventListener(event, onInteraction);
1072+
}
10611073
},
10621074

10631075
registerTypeHandler(type, handler) {

0 commit comments

Comments
 (0)