Skip to content
This repository was archived by the owner on Feb 17, 2021. It is now read-only.

Commit 30efeb6

Browse files
author
Gordon Koo
committed
Fix showPrevButton bug, respect step delay for changeStep
1 parent 5815e7c commit 30efeb6

File tree

3 files changed

+66
-50
lines changed

3 files changed

+66
-50
lines changed

js/exampletour.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ var tour = {
1919
orientation: 'left',
2020
width: 320,
2121
height: 480,
22-
showSkip: true,
22+
//showSkip: true,
2323
fixedElement: true,
2424
//xOffset: 20,
2525
zindex: 15,
26-
showPrevButton: true,
27-
showNextButton: true,
28-
delay: 1000,
26+
showPrevButton: false,
27+
//delay: 1000,
2928
onPrev: function() {
3029
document.getElementById('pageTitle').style.color = '#000';
3130
}
@@ -61,15 +60,15 @@ var tour = {
6160
content: 'We made it!! Polar bears are very interesting creatures.',
6261
target: 'polarbears',
6362
orientation: 'right',
64-
showPrevButton: false
63+
//showPrevButton: false
6564
},
6665
{
6766
title: 'Returning to the first page',
6867
content: 'Time to go back home... Please click this link to return to the first page.',
6968
target: 'firstpagelink',
7069
orientation: 'bottom',
7170
showNextButton: false,
72-
showPrevButton: false,
71+
//showPrevButton: false,
7372
multipage: true // this indicates that next step will be on a different page
7473
},
7574
{
@@ -83,6 +82,7 @@ var tour = {
8382
//animate: true,
8483
//smoothScroll: false,
8584
//showCloseButton: false,
85+
showPrevButton: true,
8686
scrollTopMargin: 50,
8787
arrowWidth: 20,
8888
//scrollDuration: 2000,

js/hopscotch-0.0.2.js

+59-43
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@
886886
},
887887
*/
888888

889+
// Used for nextOnTargetClick
889890
targetClickNextFn = function() {
890891
self.nextStep(false);
891892
},
@@ -926,7 +927,6 @@
926927
// This is our final target scroll value.
927928
scrollToVal = targetTop - opt.scrollTopMargin,
928929

929-
self = this,
930930
scrollEl,
931931
yuiAnim,
932932
yuiEase,
@@ -1020,27 +1020,38 @@
10201020
* @param {Number} direction Either 1 for incrementing or -1 for decrementing
10211021
* @returns {Number} step number we landed on
10221022
*/
1023-
goToStepWithTarget = function(direction) {
1024-
var foundTarget = false,
1023+
goToStepWithTarget = function(direction, cb) {
1024+
var target,
10251025
step;
10261026

1027-
while (!foundTarget &&
1028-
currStepNum + direction >= 0 &&
1029-
currStepNum + direction < currTour.steps.length) {
1027+
if (currStepNum + direction >= 0 &&
1028+
currStepNum + direction < currTour.steps.length) {
1029+
10301030
currStepNum += direction;
10311031
step = getCurrStep();
1032-
foundTarget = utils.getStepTarget(step);
10331032

1034-
if (!foundTarget) {
1035-
utils.invokeCallbacks('error', [currTour.id, currStepNum]);
1036-
}
1037-
}
1033+
// This setTimeout is here because the next step may have a delay
1034+
// (e.g., if the state of the page changed that introduced the target
1035+
// for the next step, then we need to respect delay before calling
1036+
// utils.getStepTarget)
1037+
setTimeout(function() {
1038+
target = utils.getStepTarget(step);
10381039

1039-
if (!foundTarget) {
1040-
currStepNum = -1;
1040+
if (target) {
1041+
// We're done! Return the step number via the callback.
1042+
cb(currStepNum);
1043+
}
1044+
else {
1045+
// Haven't found a valid target yet. Recursively call
1046+
// goToStepWithTarget.
1047+
utils.invokeCallbacks('error', [currTour.id, currStepNum]);
1048+
goToStepWithTarget(direction, cb);
1049+
}
1050+
}, utils.valOrDefault(step.delay, 0));
1051+
}
1052+
else {
1053+
cb(-1); // signal that we didn't find any step with a valid target
10411054
}
1042-
1043-
return currStepNum;
10441055
},
10451056

10461057
/**
@@ -1054,22 +1065,44 @@
10541065
* @param {Number} direction Either 1 for "next" or -1 for "prev"
10551066
*/
10561067
changeStep = function(doCallbacks, direction) {
1057-
var bubble = getBubble();
1058-
1059-
doCallbacks = utils.valOrDefault(doCallbacks, true);
1068+
var bubble = getBubble(),
1069+
self = this,
1070+
step,
1071+
origStepNum,
1072+
changeStepCb;
10601073

10611074
bubble.hide();
10621075

1063-
var step = getCurrStep(),
1064-
origStepNum = currStepNum;
1065-
1066-
if (opt.skipIfNoElement) {
1067-
goToStepWithTarget(direction);
1076+
doCallbacks = utils.valOrDefault(doCallbacks, true);
1077+
step = getCurrStep();
1078+
origStepNum = currStepNum;
10681079

1069-
if (currStepNum === -1) {
1080+
// Callback for goToStepWithTarget
1081+
changeStepCb = function(stepNum) {
1082+
if (stepNum === -1) {
10701083
// Wasn't able to find a step with an existing element. End tour.
10711084
return this.endTour(true);
10721085
}
1086+
1087+
if (doCallbacks) {
1088+
// invoke callbacks
1089+
if (direction > 0 && step.onNext) {
1090+
step.onNext();
1091+
}
1092+
else if (direction < 0 && step.onPrev) {
1093+
step.onPrev();
1094+
}
1095+
utils.invokeCallbacks(direction > 0 ? 'next' : 'prev', [currTour.id, origStepNum]);
1096+
}
1097+
1098+
//this.showStep(currStepNum, currSubstepNum);
1099+
this.showStep(stepNum);
1100+
};
1101+
1102+
if (opt.skipIfNoElement) {
1103+
goToStepWithTarget(direction, function(stepNum) {
1104+
changeStepCb.call(self, stepNum);
1105+
});
10731106
}
10741107
else if (currStepNum + direction >= 0 && currStepNum + direction < currTour.steps.length) {
10751108
// only try incrementing once, and invoke error callback if no target is found
@@ -1079,22 +1112,9 @@
10791112
utils.invokeCallbacks('error', [currTour.id, currStepNum]);
10801113
return this.endTour(true, false);
10811114
}
1115+
this.changeStepCb(currStepNum);
10821116
}
10831117

1084-
if (doCallbacks) {
1085-
// invoke callbacks
1086-
if (direction > 0 && step.onNext) {
1087-
step.onNext();
1088-
}
1089-
else if (direction < 0 && step.onPrev) {
1090-
step.onPrev();
1091-
}
1092-
utils.invokeCallbacks(direction > 0 ? 'next' : 'prev', [currTour.id, origStepNum]);
1093-
}
1094-
1095-
//this.showStep(currStepNum, currSubstepNum);
1096-
this.showStep(currStepNum);
1097-
10981118
return this;
10991119
},
11001120

@@ -1108,7 +1128,6 @@
11081128
*/
11091129
loadTour = function(tour) {
11101130
var tmpOpt = {},
1111-
bubble,
11121131
prop,
11131132
stepPair,
11141133
tourState,
@@ -1290,8 +1309,7 @@
12901309
* @returns {Object} Hopscotch
12911310
*/
12921311
this.showStep = function(stepNum, substepNum) {
1293-
var self = this,
1294-
step = currTour.steps[stepNum];
1312+
var step = currTour.steps[stepNum];
12951313

12961314
setTimeout(function() {
12971315
var tourSteps = currTour.steps,
@@ -1633,8 +1651,6 @@
16331651
bubble.removeAnimate();
16341652
}
16351653

1636-
bubble.showPrevButton(options.showPrevButton, typeof options.showPrevButton !== undefinedStr);
1637-
bubble.showNextButton(options.showNextButton, typeof options.showNextButton !== undefinedStr);
16381654
bubble.showCloseButton(options.showCloseButton, typeof options.showCloseButton !== undefinedStr);
16391655

16401656
return this;

0 commit comments

Comments
 (0)