Skip to content

Commit 5170c7b

Browse files
committed
Code refactor + clean up
1 parent 78b7d3e commit 5170c7b

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

intro.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* MIT licensed
55
*
66
* Copyright (C) 2013 usabli.ca - A weekend project by Afshin Mehrabani (@afshinmeh)
7-
*/
7+
*/
88

99
(function () {
1010

@@ -35,15 +35,15 @@
3535

3636
//if there's no element to intro
3737
if(allIntroSteps.length < 1) {
38-
return;
38+
return false;
3939
}
4040

4141
for (var i = 0, elmsLength = allIntroSteps.length; i < elmsLength; i++) {
4242
var currentElement = allIntroSteps[i];
4343
introItems.push({
4444
element: currentElement,
4545
intro: currentElement.getAttribute("data-intro"),
46-
step: parseInt(currentElement.getAttribute("data-step")),
46+
step: parseInt(currentElement.getAttribute("data-step"), 10),
4747
position: currentElement.getAttribute("data-position") || 'bottom'
4848
});
4949
}
@@ -65,7 +65,7 @@
6565
nextStepButton = targetElm.querySelector(".introjs-nextbutton");
6666

6767
targetElm.onkeydown = function(e) {
68-
if(e.keyCode == 27) {
68+
if (e.keyCode == 27) {
6969
//escape key pressed, exit the intro
7070
_exitIntro.call(self, targetElm);
7171
} else if(e.keyCode == 37) {
@@ -87,11 +87,12 @@
8787
* @method _nextStep
8888
*/
8989
function _nextStep() {
90-
if(this._currentStep == undefined) {
90+
if (typeof(this._currentStep) === 'undefined') {
9191
this._currentStep = 0;
9292
} else {
9393
++this._currentStep;
9494
}
95+
9596
if((this._introItems.length) <= this._currentStep) {
9697
//end of the intro
9798
//check if any callback is defined
@@ -101,8 +102,8 @@
101102
_exitIntro.call(this, this._targetElement);
102103
return;
103104
}
104-
_showElement.call(this, this._introItems[this._currentStep].element);
105105

106+
_showElement.call(this, this._introItems[this._currentStep].element);
106107
}
107108

108109
/**
@@ -112,8 +113,8 @@
112113
* @method _nextStep
113114
*/
114115
function _previousStep() {
115-
if(this._currentStep == 0){
116-
return;
116+
if (this._currentStep == 0){
117+
return false;
117118
}
118119

119120
_showElement.call(this, this._introItems[--this._currentStep].element);
@@ -154,14 +155,23 @@
154155
}
155156
}
156157

158+
/**
159+
* Render tooltip box in the page
160+
*
161+
* @api private
162+
* @method _placeTooltip
163+
* @param {Object} targetElement
164+
* @param {Object} tooltipLayer
165+
* @param {Object} arrowLayer
166+
*/
157167
function _placeTooltip(targetElement, tooltipLayer, arrowLayer) {
158168
var tooltipLayerPosition = _getOffset(tooltipLayer);
159169
//reset the old style
160170
tooltipLayer.style.top = null;
161171
tooltipLayer.style.right = null;
162172
tooltipLayer.style.bottom = null;
163173
tooltipLayer.style.left = null;
164-
switch(targetElement.getAttribute('data-position')){
174+
switch (targetElement.getAttribute('data-position')) {
165175
case 'top':
166176
tooltipLayer.style.left = "15px";
167177
tooltipLayer.style.top = "-" + (tooltipLayerPosition.height + 10) + "px";
@@ -193,7 +203,7 @@
193203
* @param {Object} targetElement
194204
*/
195205
function _showElement(targetElement) {
196-
206+
197207
var self = this,
198208
oldHelperLayer = document.querySelector(".introjs-helperLayer"),
199209
elementPosition = _getOffset(targetElement);
@@ -293,7 +303,7 @@
293303

294304
//set css class name
295305
overlayLayer.className = "introjs-overlay";
296-
306+
297307
//set overlay layer position
298308
var elementPosition = _getOffset(targetElm);
299309
if(elementPosition) {
@@ -336,9 +346,9 @@
336346
var _x = 0;
337347
var _y = 0;
338348
while(element && !isNaN(element.offsetLeft) && !isNaN(element.offsetTop)) {
339-
_x += element.offsetLeft;
340-
_y += element.offsetTop;
341-
element = element.offsetParent;
349+
_x += element.offsetLeft;
350+
_y += element.offsetTop;
351+
element = element.offsetParent;
342352
}
343353
//set top
344354
elementPosition.top = _y;
@@ -357,7 +367,7 @@
357367
//select the target element with query selector
358368
var targetElement = document.querySelector(targetElm);
359369

360-
if(targetElement) {
370+
if (targetElement) {
361371
return new IntroJs(targetElement);
362372
} else {
363373
throw new Error("There's no element with given selector.");
@@ -378,7 +388,7 @@
378388
//Prototype
379389
introJs.fn = IntroJs.prototype = {
380390
clone: function () {
381-
return IntroJs(this);
391+
return new IntroJs(this);
382392
},
383393
start: function () {
384394
_introForElement.call(this, this._targetElement);
@@ -402,5 +412,5 @@
402412
}
403413
};
404414

405-
this['introJs'] = introJs;
415+
window['introJs'] = introJs;
406416
})();

0 commit comments

Comments
 (0)