Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/pat/inject/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,17 @@ const inject = {
} else if (
cfg.confirm === "form-data" &&
cfg.target &&
cfg.target !== "none"
cfg.target !== "none" &&
cfg.$target
) {
_confirm = this.elementIsDirty(cfg.$target);
} else if (cfg.confirm === "class" && cfg.target && cfg.target !== "none") {
_confirm = cfg.$target.hasClass("is-dirty");
} else if (
cfg.confirm === "class" &&
cfg.target &&
cfg.target !== "none" &&
cfg.$target
) {
_confirm = cfg.$target?.hasClass("is-dirty");
}
if (_confirm) {
should_confirm = true;
Expand Down Expand Up @@ -407,12 +413,12 @@ const inject = {
* Cancel button is pressed (this triggers reset event on the
* form) you would expect to populate with initial placeholder
*/
if (cfg.target === "none") {
if (cfg.target === "none" || !cfg.$target) {
// Special case, we don't want to display any return value.
return;
}
const $form = cfg.$target.parents("form");
if ($form.length !== 0 && cfg.$target.data("initial-value") === undefined) {
if ($form && $form.length !== 0 && cfg.$target.data("initial-value") === undefined) {
cfg.$target.data("initial-value", cfg.$target.html());
$form.on("reset", () => {
cfg.$target.html(cfg.$target.data("initial-value"));
Expand Down Expand Up @@ -455,7 +461,10 @@ const inject = {
* appended to the body.
*/
if (selector.slice(0, 1) !== "#") {
log.error("only id supported for non-existing target");
log.error(
"only id supported for non-existing target. selector: ",
selector
);
return null;
}
const $target = $("<div />").attr({ id: selector.slice(1) });
Expand Down Expand Up @@ -556,7 +565,7 @@ const inject = {
// 1) finding the scroll container
// 2) getting the element to scroll to (if not "top")
const scroll_target = ["top", "target"].includes(cfg.scroll)
? cfg.$target[0]
? cfg?.$target[0]
: dom.querySelectorAllAndMe($injected[0], cfg.scroll);

const scroll_container = dom.find_scroll_container(
Expand Down Expand Up @@ -606,7 +615,7 @@ const inject = {

for (const [idx1, cfg] of cfgs.entries()) {
const perform_inject = () => {
if (cfg.target !== "none") {
if (cfg.target !== "none" && cfg.$target) {
for (const target of cfg.$target) {
this._performInjection(
target,
Expand Down Expand Up @@ -688,7 +697,7 @@ const inject = {
if ("$created_target" in cfg) {
cfg.$created_target.remove();
}
cfg.$target.removeClass(cfg.loadingClass);
cfg.$target?.removeClass(cfg.loadingClass);
$el.removeClass(cfg.executingClass);
}
$el.off("pat-ajax-success.pat-inject");
Expand Down Expand Up @@ -736,8 +745,8 @@ const inject = {
}
// Add a loading class to the target.
// Can be used for loading-spinners.
if (cfg?.loadingClass && cfg?.target !== "none") {
cfg.$target.addClass(cfg.loadingClass);
if (cfg?.loadingClass && cfg?.target !== "none" && cfg.$target) {
cfg.$target?.addClass(cfg.loadingClass);
}
}

Expand Down Expand Up @@ -1141,7 +1150,7 @@ $(document).on("patterns-injected.inject", async (ev, cfg, trigger, injected) =>
return;
}
if (cfg) {
cfg.$target.removeClass(cfg.loadingClass);
cfg?.$target.removeClass(cfg.loadingClass);
// Remove the executing class, add the executed class to the element with pat.inject on it.
$(trigger).removeClass(cfg.executingClass).addClass(cfg.executedClass);
}
Expand Down