Skip to content

Commit 4743f30

Browse files
committed
Add ability to define items without data-step
1 parent 59a78b3 commit 4743f30

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

intro.js

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,59 @@
8282
}
8383

8484
} else {
85-
//use steps from data-* annotations
86-
85+
//use steps from data-* annotations
8786
var allIntroSteps = targetElm.querySelectorAll('*[data-intro]');
8887
//if there's no element to intro
8988
if (allIntroSteps.length < 1) {
9089
return false;
9190
}
9291

92+
//first add intro items with data-step
9393
for (var i = 0, elmsLength = allIntroSteps.length; i < elmsLength; i++) {
9494
var currentElement = allIntroSteps[i];
9595
var step = parseInt(currentElement.getAttribute('data-step'), 10);
96-
introItems.push({
97-
element: currentElement,
98-
intro: currentElement.getAttribute('data-intro'),
99-
//if step==NaN set step to 100
100-
step: step ? step : 100,
101-
tooltipClass: currentElement.getAttribute('data-tooltipClass'),
102-
position: currentElement.getAttribute('data-position') || this._options.tooltipPosition
103-
});
96+
97+
if (step > 0) {
98+
introItems[step - 1] = {
99+
element: currentElement,
100+
intro: currentElement.getAttribute('data-intro'),
101+
step: parseInt(currentElement.getAttribute('data-step'), 10),
102+
tooltipClass: currentElement.getAttribute('data-tooltipClass'),
103+
position: currentElement.getAttribute('data-position') || this._options.tooltipPosition
104+
};
105+
}
106+
}
107+
108+
//next add intro items without data-step
109+
//todo: we need a cleanup here, two loops are redundant
110+
var nextStep = 0;
111+
for (var i = 0, elmsLength = allIntroSteps.length; i < elmsLength; i++) {
112+
var currentElement = allIntroSteps[i];
113+
114+
if (currentElement.getAttribute('data-step') == null) {
115+
116+
while(true) {
117+
if (typeof introItems[nextStep] == 'undefined') {
118+
break;
119+
} else {
120+
nextStep++;
121+
}
122+
}
123+
124+
introItems.push({
125+
element: currentElement,
126+
intro: currentElement.getAttribute('data-intro'),
127+
step: nextStep + 1,
128+
tooltipClass: currentElement.getAttribute('data-tooltipClass'),
129+
position: currentElement.getAttribute('data-position') || this._options.tooltipPosition
130+
});
131+
}
104132
}
105133
}
106134

135+
//removing undefined/null elements
136+
introItems = introItems.filter(function(n){ return n; });
137+
107138
//Ok, sort all items with given steps
108139
introItems.sort(function (a, b) {
109140
return a.step - b.step;

0 commit comments

Comments
 (0)