Skip to content

Commit 82443fa

Browse files
committed
added path functionality
1 parent ee329fa commit 82443fa

11 files changed

+1914
-1504
lines changed

app/tour_controller.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
(function angularBootstrapTour(app) {
44
'use strict';
55

6-
app.controller('TourController', ['$filter', function ($filter) {
6+
app.controller('TourController', ['$filter', '$timeout', function ($filter, $timeout) {
77

88
var self = this,
99
steps = [],
10-
tour;
10+
tour,
11+
newStepFound = angular.noop,
12+
dummyStep = {};
1113

1214
/**
1315
* Sorts steps based on "order" and set next and prev options appropriately
@@ -30,6 +32,20 @@
3032
* As steps are linked, add them to the tour options
3133
*/
3234
self.refreshTour = function () {
35+
//remove dummy steps that were previously added
36+
steps = steps.filter(function (step) {
37+
return step !== dummyStep;
38+
});
39+
40+
//if the first or last step redirects to another page, BT needs a step (dummyStep)
41+
if (steps[0] && steps[0].redirectPrev) {
42+
steps.unshift(dummyStep);
43+
}
44+
if (steps[steps.length-1] && steps[steps.length-1].redirectNext) {
45+
steps.push(dummyStep);
46+
}
47+
48+
//refresh
3349
if (tour) {
3450
tour._options.steps = [];
3551
tour.addSteps(orderSteps(steps));
@@ -48,6 +64,7 @@
4864

4965
steps.push(step);
5066
self.refreshTour();
67+
newStepFound(step);
5168
};
5269

5370
/**
@@ -73,6 +90,23 @@
7390
return steps;
7491
};
7592

93+
/**
94+
* Tells the tour to pause while ngView loads
95+
*
96+
* @param waitForStep
97+
*/
98+
self.waitFor = function (waitForStep) {
99+
tour.end();
100+
newStepFound = function (step) {
101+
if (step.stepId === waitForStep) {
102+
tour.setCurrentStep(steps.indexOf(step));
103+
$timeout(function () {
104+
tour.start(true);
105+
});
106+
}
107+
};
108+
};
109+
76110
/**
77111
* Initialize the tour
78112
*

app/tour_helpers.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
app.factory('TourHelpers', ['$templateCache', '$compile', 'TourConfig', function ($templateCache, $compile, TourConfig) {
77

8-
var helpers = {};
8+
var helpers = {},
9+
safeApply;
910

1011
/**
1112
* Helper function that calls scope.$apply if a digest is not currently in progress
@@ -14,7 +15,7 @@
1415
* @param {$rootScope.Scope} scope
1516
* @param {Function} fn
1617
*/
17-
function safeApply(scope, fn) {
18+
safeApply = helpers.safeApply = function(scope, fn) {
1819
var phase = scope.$$phase;
1920
if (phase === '$apply' || phase === '$digest') {
2021
if (fn && (typeof(fn) === 'function')) {
@@ -23,7 +24,7 @@
2324
} else {
2425
scope.$apply(fn);
2526
}
26-
}
27+
};
2728

2829
/**
2930
* Compiles and links a template to the provided scope

app/tour_step_directive.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'use strict';
55

66
function directive() {
7-
return ['TourHelpers', 'TourConfig', function (TourHelpers, TourConfig) {
7+
return ['TourHelpers', '$location', '$rootScope', function (TourHelpers, $location, $rootScope) {
88

99
return {
1010
restrict: 'EA',
@@ -14,14 +14,17 @@
1414

1515
//Assign required options
1616
var step = {
17-
element: element
17+
element: element,
18+
stepId: attrs.tourStep
1819
},
1920
events = 'onShow onShown onHide onHidden onNext onPrev onPause onResume'.split(' '),
20-
options = 'content title path animation container placement backdrop redirect orphan reflex'.split(' ');
21+
options = 'content title path animation container placement backdrop redirect orphan reflex path nextStep prevStep nextPath prevPath'.split(' '),
22+
orderWatch,
23+
skipWatch;
2124

2225
//Pass interpolated values through
2326
TourHelpers.attachInterpolatedValues(attrs, step, options);
24-
attrs.$observe(TourHelpers.getAttrName('order'), function (order) {
27+
orderWatch = attrs.$observe(TourHelpers.getAttrName('order'), function (order) {
2528
step.order = !isNaN(order*1) ? order*1 : 0;
2629
ctrl.refreshTour();
2730
});
@@ -43,25 +46,46 @@
4346
}
4447
return skipped;
4548
}
46-
scope.$watch(stepIsSkipped, function (skip) {
49+
skipWatch = scope.$watch(stepIsSkipped, function (skip) {
4750
if (skip) {
4851
ctrl.removeStep(step);
4952
} else {
5053
ctrl.addStep(step);
5154
}
5255
});
5356

57+
scope.$on('$destroy', function () {
58+
ctrl.removeStep(step);
59+
orderWatch();
60+
skipWatch();
61+
});
62+
5463
//If there is an options argument passed, just use that instead
5564
if (attrs[TourHelpers.getAttrName('options')]) {
5665
angular.extend(step, scope.$eval(attrs[TourHelpers.getAttrName('options')]));
5766
}
5867

5968
//set up redirects
60-
if (attrs[TourHelpers.getAttrName('path')]) {
61-
step.path = document.location.pathname + '#' + scope.$eval(TourHelpers.getAttrName('path'));
69+
function setRedirect(direction, path, targetName) {
70+
var oldHandler = step[direction];
71+
step[direction] = function (tour) {
72+
if (oldHandler) {
73+
oldHandler(tour);
74+
}
75+
ctrl.waitFor(targetName);
76+
77+
TourHelpers.safeApply(scope, function () {
78+
$location.path(path);
79+
});
80+
};
81+
}
82+
if (step.nextPath) {
83+
step.redirectNext = true;
84+
setRedirect('onNext', step.nextPath, step.nextStep);
6285
}
63-
if (attrs[TourHelpers.getAttrName('abspath')]) {
64-
step.path = scope.$eval(TourHelpers.getAttrName('abspath'));
86+
if (step.prevPath) {
87+
step.redirectPrev = true;
88+
setRedirect('onPrev', step.prevPath, step.prevStep);
6589
}
6690

6791
//Add step to tour

bower.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
"angular": "~1.2.23",
1919
"bootstrap-tour": "~0.9.3",
2020
"jquery": "1.11.1",
21-
"bootstrap": "3.0.3"
21+
"bootstrap": "3.0.3",
22+
"angular-route": "1.2.16"
2223
},
2324
"devDependencies": {
2425
"html5shiv": "~3.7.2",
2526
"respond": "~1.4.2"
27+
},
28+
"resolutions": {
29+
"angular": "1.2.16",
30+
"angular-route": "1.2.16"
2631
}
2732
}

0 commit comments

Comments
 (0)