|
886 | 886 | },
|
887 | 887 | */
|
888 | 888 |
|
| 889 | + // Used for nextOnTargetClick |
889 | 890 | targetClickNextFn = function() {
|
890 | 891 | self.nextStep(false);
|
891 | 892 | },
|
|
926 | 927 | // This is our final target scroll value.
|
927 | 928 | scrollToVal = targetTop - opt.scrollTopMargin,
|
928 | 929 |
|
929 |
| - self = this, |
930 | 930 | scrollEl,
|
931 | 931 | yuiAnim,
|
932 | 932 | yuiEase,
|
|
1020 | 1020 | * @param {Number} direction Either 1 for incrementing or -1 for decrementing
|
1021 | 1021 | * @returns {Number} step number we landed on
|
1022 | 1022 | */
|
1023 |
| - goToStepWithTarget = function(direction) { |
1024 |
| - var foundTarget = false, |
| 1023 | + goToStepWithTarget = function(direction, cb) { |
| 1024 | + var target, |
1025 | 1025 | step;
|
1026 | 1026 |
|
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 | + |
1030 | 1030 | currStepNum += direction;
|
1031 | 1031 | step = getCurrStep();
|
1032 |
| - foundTarget = utils.getStepTarget(step); |
1033 | 1032 |
|
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); |
1038 | 1039 |
|
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 |
1041 | 1054 | }
|
1042 |
| - |
1043 |
| - return currStepNum; |
1044 | 1055 | },
|
1045 | 1056 |
|
1046 | 1057 | /**
|
|
1054 | 1065 | * @param {Number} direction Either 1 for "next" or -1 for "prev"
|
1055 | 1066 | */
|
1056 | 1067 | 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; |
1060 | 1073 |
|
1061 | 1074 | bubble.hide();
|
1062 | 1075 |
|
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; |
1068 | 1079 |
|
1069 |
| - if (currStepNum === -1) { |
| 1080 | + // Callback for goToStepWithTarget |
| 1081 | + changeStepCb = function(stepNum) { |
| 1082 | + if (stepNum === -1) { |
1070 | 1083 | // Wasn't able to find a step with an existing element. End tour.
|
1071 | 1084 | return this.endTour(true);
|
1072 | 1085 | }
|
| 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 | + }); |
1073 | 1106 | }
|
1074 | 1107 | else if (currStepNum + direction >= 0 && currStepNum + direction < currTour.steps.length) {
|
1075 | 1108 | // only try incrementing once, and invoke error callback if no target is found
|
|
1079 | 1112 | utils.invokeCallbacks('error', [currTour.id, currStepNum]);
|
1080 | 1113 | return this.endTour(true, false);
|
1081 | 1114 | }
|
| 1115 | + this.changeStepCb(currStepNum); |
1082 | 1116 | }
|
1083 | 1117 |
|
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 |
| - |
1098 | 1118 | return this;
|
1099 | 1119 | },
|
1100 | 1120 |
|
|
1108 | 1128 | */
|
1109 | 1129 | loadTour = function(tour) {
|
1110 | 1130 | var tmpOpt = {},
|
1111 |
| - bubble, |
1112 | 1131 | prop,
|
1113 | 1132 | stepPair,
|
1114 | 1133 | tourState,
|
|
1290 | 1309 | * @returns {Object} Hopscotch
|
1291 | 1310 | */
|
1292 | 1311 | this.showStep = function(stepNum, substepNum) {
|
1293 |
| - var self = this, |
1294 |
| - step = currTour.steps[stepNum]; |
| 1312 | + var step = currTour.steps[stepNum]; |
1295 | 1313 |
|
1296 | 1314 | setTimeout(function() {
|
1297 | 1315 | var tourSteps = currTour.steps,
|
|
1633 | 1651 | bubble.removeAnimate();
|
1634 | 1652 | }
|
1635 | 1653 |
|
1636 |
| - bubble.showPrevButton(options.showPrevButton, typeof options.showPrevButton !== undefinedStr); |
1637 |
| - bubble.showNextButton(options.showNextButton, typeof options.showNextButton !== undefinedStr); |
1638 | 1654 | bubble.showCloseButton(options.showCloseButton, typeof options.showCloseButton !== undefinedStr);
|
1639 | 1655 |
|
1640 | 1656 | return this;
|
|
0 commit comments