Skip to content

Commit 33b6136

Browse files
committed
Processing possible absence of an element by selector
When creating list of steps, we take into account the possible absence of element by selector If .element is a selector for which there is no element at the configuration stage, then we replace .element specifying a getter/setter for it. In the getter we return the element via document.querySelector Since that moment we assume that .element should return HTMLElement by selector and if there is still not element - throwing Error. Thus to check raw field below we need use ._element
1 parent 55bac40 commit 33b6136

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/core/fetchIntroSteps.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,37 @@ export default function fetchIntroSteps(targetElm) {
2626
//use querySelector function only when developer used CSS selector
2727
if (typeof currentItem.element === "string") {
2828
//grab the element with given selector from the page
29-
currentItem.element = document.querySelector(currentItem.element);
30-
}
31-
32-
//intro without element
33-
if (
29+
const el = document.querySelector(currentItem.element);
30+
if (el !== null) {
31+
currentItem.element = el;
32+
} else {
33+
// If element is not exists yet, we'll get it on step
34+
const elSelector = currentItem.element;
35+
Object.defineProperty(currentItem, "element", {
36+
get() {
37+
if (typeof this._element === "string") {
38+
const result = document.querySelector(this._element);
39+
if (result === null)
40+
throw new Error(
41+
"There is no element with given selector: " + this._element
42+
);
43+
return result;
44+
} else {
45+
return this._element;
46+
}
47+
},
48+
set(value) {
49+
this._element = value;
50+
},
51+
enumerable: true,
52+
});
53+
currentItem.element = elSelector;
54+
}
55+
} else if (
3456
typeof currentItem.element === "undefined" ||
3557
currentItem.element === null
3658
) {
59+
//intro without element
3760
let floatingElementQuery = document.querySelector(
3861
".introjsFloatingElement"
3962
);
@@ -58,7 +81,7 @@ export default function fetchIntroSteps(targetElm) {
5881
currentItem.disableInteraction = this._options.disableInteraction;
5982
}
6083

61-
if (currentItem.element !== null) {
84+
if (currentItem._element !== null || currentItem.element !== null) {
6285
introItems.push(currentItem);
6386
}
6487
});

0 commit comments

Comments
 (0)