Skip to content

Commit 7f5242d

Browse files
committed
pat-inject: Minor code optimization: Reword most forEach to for loops.
1 parent 236bde0 commit 7f5242d

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

src/pat/inject/inject.js

Lines changed: 34 additions & 22 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);
@@ -216,6 +216,7 @@ const inject = {
216216
cfgs = this.extractConfig($(cfg_node), opts);
217217
}
218218

219+
// store the params of the form in the config, to be used by history
219220
for (const cfg of cfgs) {
220221
cfg.params = $.param($el.serializeArray());
221222
}
@@ -228,13 +229,13 @@ const inject = {
228229
submitSubform($sub) {
229230
/* This method is called from pat-subform
230231
*/
231-
const $el = $sub.parents("form");
232+
const $el = $($sub[0].closest("form"));
232233
const cfgs = $sub.data("pat-inject");
233234

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

239240
try {
240241
$el.trigger("patterns-inject-triggered");
@@ -249,7 +250,7 @@ const inject = {
249250
options = Object.assign({}, options); // copy
250251

251252
const cfgs = parser.parse($el, options, true);
252-
cfgs.forEach((cfg) => {
253+
for (const cfg of cfgs) {
253254
cfg.$context = $el;
254255
// options and cfg have priority, fall back to href/action
255256
cfg.url =
@@ -281,7 +282,7 @@ const inject = {
281282
}
282283
}
283284
cfg.processDelay = 0;
284-
});
285+
}
285286
return cfgs;
286287
},
287288

@@ -575,7 +576,7 @@ const inject = {
575576
cfgs.forEach((cfg, idx1) => {
576577
const perform_inject = () => {
577578
if (cfg.target !== "none") {
578-
cfg.$target.each((idx2, target) => {
579+
for (const target of cfg.$target) {
579580
this._performInjection(
580581
target,
581582
$el,
@@ -584,7 +585,7 @@ const inject = {
584585
ev.target,
585586
title
586587
);
587-
});
588+
}
588589
}
589590
};
590591
if (cfg.processDelay) {
@@ -652,13 +653,13 @@ const inject = {
652653
}
653654

654655
// clean up
655-
cfgs.forEach((cfg) => {
656+
for (const cfg of cfgs) {
656657
if ("$injected" in cfg) {
657658
cfg.$injected.remove();
658659
}
659660
cfg.$target.removeClass(cfg.loadingClass);
660661
$el.removeClass(cfg.executingClass);
661-
});
662+
}
662663
$el.off("pat-ajax-success.pat-inject");
663664
$el.off("pat-ajax-error.pat-inject");
664665

@@ -1042,25 +1043,36 @@ const inject = {
10421043
}, timeout);
10431044

10441045
const unsub = () => {
1045-
["scroll", "resize"].forEach((e) =>
1046-
window.removeEventListener(e, onInteraction)
1047-
);
1048-
[
1046+
for (const event of ["scroll", "resize"]) {
1047+
window.removeEventListener(event, onInteraction);
1048+
}
1049+
for (const event of [
10491050
"click",
10501051
"keypress",
10511052
"keyup",
10521053
"mousemove",
10531054
"touchstart",
1054-
"touchend",
1055-
].forEach((e) => document.removeEventListener(e, onInteraction));
1055+
"touchend"
1056+
]) {
1057+
document.removeEventListener(event, onInteraction);
1058+
}
10561059
};
10571060

10581061
onInteraction();
10591062

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

10661078
registerTypeHandler(type, handler) {

0 commit comments

Comments
 (0)