Skip to content

Commit 6ad37df

Browse files
committed
Add onexit and oncompete callback
1 parent ae38d2a commit 6ad37df

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

intro.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
self._introItems = introItems;
5757

5858
//add overlay layer to the page
59-
if(_addOverlayLayer(targetElm)) {
59+
if(_addOverlayLayer.call(self, targetElm)) {
6060
//then, start the show
6161
_nextStep.call(self);
6262

@@ -66,7 +66,7 @@
6666
targetElm.onkeydown = function(e) {
6767
if(e.keyCode == 27) {
6868
//escape key pressed, exit the intro
69-
_exitIntro(targetElm);
69+
_exitIntro.call(self, targetElm);
7070
}
7171
if([37, 39].indexOf(e.keyCode) >= 0) {
7272
if(e.keyCode == 37) {
@@ -96,7 +96,11 @@
9696
}
9797
if((this._introItems.length) <= this._currentStep) {
9898
//end of the intro
99-
_exitIntro(this._targetElement);
99+
//check if any callback is defined
100+
if (this._introCompleteCallback != undefined){
101+
this._introCompleteCallback.call(this);
102+
}
103+
_exitIntro.call(this, this._targetElement);
100104
return;
101105
}
102106
_showElement.call(this, this._introItems[this._currentStep].element);
@@ -139,6 +143,10 @@
139143
showElement.className = showElement.className.replace(/introjs-showElement/,'').trim();
140144
//clean listeners
141145
targetElement.onkeydown = null;
146+
//check if any callback is defined
147+
if (this._introExitCallback != undefined){
148+
this._introExitCallback.call(this);
149+
}
142150
}
143151

144152
/**
@@ -218,7 +226,7 @@
218226
nextTooltipButton.innerHTML = "Next →";
219227

220228
skipTooltipButton.onclick = function() {
221-
_exitIntro(self._targetElement);
229+
_exitIntro.call(self, self._targetElement);
222230
};
223231

224232
var tooltipButtonsLayer = tooltipLayer.querySelector('.introjs-tooltipbuttons');
@@ -247,7 +255,9 @@
247255
*/
248256
function _addOverlayLayer(targetElm) {
249257
var overlayLayer = document.createElement("div"),
250-
styleText = "";
258+
styleText = "",
259+
self = this;
260+
251261
//set css class name
252262
overlayLayer.className = "introjs-overlay";
253263

@@ -261,9 +271,9 @@
261271
targetElm.appendChild(overlayLayer);
262272

263273
overlayLayer.onclick = function() {
264-
_exitIntro(targetElm);
274+
_exitIntro.call(self, targetElm);
265275
};
266-
276+
267277
setTimeout(function() {
268278
styleText += "opacity: .5;";
269279
overlayLayer.setAttribute("style", styleText);
@@ -338,9 +348,26 @@
338348
return IntroJs(this);
339349
},
340350
start: function () {
341-
return _introForElement.call(this, this._targetElement);
351+
_introForElement.call(this, this._targetElement);
352+
return this;
353+
},
354+
oncomplete: function(providedCallback) {
355+
if (typeof (providedCallback) === "function") {
356+
this._introCompleteCallback = providedCallback;
357+
} else {
358+
throw new Error("Provided callback for oncomplete was not a function.");
359+
}
360+
return this;
361+
},
362+
onexit: function(providedCallback) {
363+
if (typeof (providedCallback) === "function") {
364+
this._introExitCallback = providedCallback;
365+
} else {
366+
throw new Error("Provided callback for onexit was not a function.");
367+
}
368+
return this;
342369
}
343370
};
344371

345372
this['introJs'] = introJs;
346-
})();
373+
})();

0 commit comments

Comments
 (0)