Skip to content

Commit e22a2dd

Browse files
authored
Merge pull request #2034 from Ryczko/fetch-intro-steps-refactor
FetchIntroSteps refactor
2 parents 3cab791 + eb1bacb commit e22a2dd

File tree

1 file changed

+33
-62
lines changed

1 file changed

+33
-62
lines changed

src/core/fetchIntroSteps.ts

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export default function fetchIntroSteps(
7878
return [];
7979
}
8080

81+
const itemsWithoutStep: IntroStep[] = [];
82+
8183
for (const currentElement of allIntroSteps) {
8284
// start intro for groups of elements
8385
if (
@@ -92,81 +94,50 @@ export default function fetchIntroSteps(
9294
continue;
9395
}
9496

95-
const step = parseInt(currentElement.getAttribute("data-step") || "", 10);
97+
// get the step for the current element or set as 0 if is not present
98+
const step = parseInt(
99+
currentElement.getAttribute("data-step") || "0",
100+
10
101+
);
96102

97103
disableInteraction = intro._options.disableInteraction;
98104
if (currentElement.hasAttribute("data-disable-interaction")) {
99105
disableInteraction = !!currentElement.getAttribute(
100106
"data-disable-interaction"
101107
);
102108
}
109+
const newIntroStep: IntroStep = {
110+
step: step,
111+
element: currentElement,
112+
title: currentElement.getAttribute("data-title") || "",
113+
intro: currentElement.getAttribute("data-intro") || "",
114+
tooltipClass:
115+
currentElement.getAttribute("data-tooltip-class") || undefined,
116+
highlightClass:
117+
currentElement.getAttribute("data-highlight-class") || undefined,
118+
position: (currentElement.getAttribute("data-position") ||
119+
intro._options.tooltipPosition) as TooltipPosition,
120+
scrollTo:
121+
(currentElement.getAttribute("data-scroll-to") as ScrollTo) ||
122+
intro._options.scrollTo,
123+
disableInteraction,
124+
};
103125

104126
if (step > 0) {
105-
introItems[step - 1] = {
106-
step: step,
107-
element: currentElement,
108-
title: currentElement.getAttribute("data-title") || "",
109-
intro: currentElement.getAttribute("data-intro") || "",
110-
tooltipClass:
111-
currentElement.getAttribute("data-tooltip-class") || undefined,
112-
highlightClass:
113-
currentElement.getAttribute("data-highlight-class") || undefined,
114-
position: (currentElement.getAttribute("data-position") ||
115-
intro._options.tooltipPosition) as TooltipPosition,
116-
scrollTo:
117-
(currentElement.getAttribute("data-scroll-to") as ScrollTo) ||
118-
intro._options.scrollTo,
119-
disableInteraction,
120-
};
127+
introItems[step - 1] = newIntroStep;
128+
} else {
129+
itemsWithoutStep.push(newIntroStep);
121130
}
122131
}
123132

124-
//next add intro items without data-step
125-
//todo: we need a cleanup here, two loops are redundant
126-
let nextStep = 0;
127-
128-
for (const currentElement of allIntroSteps) {
129-
// start intro for groups of elements
130-
if (
131-
intro._options.group &&
132-
currentElement.getAttribute("data-intro-group") !== intro._options.group
133-
) {
134-
continue;
135-
}
136-
137-
if (currentElement.getAttribute("data-step") === null) {
138-
while (true) {
139-
if (typeof introItems[nextStep] === "undefined") {
140-
break;
141-
} else {
142-
nextStep++;
143-
}
144-
}
145-
146-
if (currentElement.hasAttribute("data-disable-interaction")) {
147-
disableInteraction = !!currentElement.getAttribute(
148-
"data-disable-interaction"
149-
);
150-
} else {
151-
disableInteraction = intro._options.disableInteraction;
152-
}
133+
//fill items without step in blanks and update their step
134+
for (let i = 0; itemsWithoutStep.length > 0; i++) {
135+
if (typeof introItems[i] === "undefined") {
136+
const newStep = itemsWithoutStep.shift();
137+
if (!newStep) break;
153138

154-
introItems[nextStep] = {
155-
element: currentElement,
156-
title: currentElement.getAttribute("data-title") || "",
157-
intro: currentElement.getAttribute("data-intro") || "",
158-
step: nextStep + 1,
159-
tooltipClass:
160-
currentElement.getAttribute("data-tooltip-class") || undefined,
161-
highlightClass:
162-
currentElement.getAttribute("data-highlight-class") || undefined,
163-
position: (currentElement.getAttribute("data-position") ||
164-
intro._options.tooltipPosition) as TooltipPosition,
165-
scrollTo:
166-
(currentElement.getAttribute("data-scroll-to") as ScrollTo) ||
167-
intro._options.scrollTo,
168-
disableInteraction,
169-
};
139+
newStep.step = i + 1;
140+
introItems[i] = newStep;
170141
}
171142
}
172143
}

0 commit comments

Comments
 (0)