/
tour_step_directive.js
106 lines (89 loc) · 4.17 KB
/
tour_step_directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* global angular: false */
(function (app) {
'use strict';
function directive() {
return ['TourHelpers', '$location', function (TourHelpers, $location) {
return {
restrict: 'EA',
scope: true,
require: '^tour',
link: function (scope, element, attrs, ctrl) {
//Assign required options
var step = {
element: element,
stepId: attrs.tourStep
},
events = 'onShow onShown onHide onHidden onNext onPrev onPause onResume'.split(' '),
options = 'content title path animation container placement backdrop redirect orphan reflex duration nextStep prevStep nextPath prevPath'.split(' '),
orderWatch,
skipWatch,
templateReady;
//Pass interpolated values through
TourHelpers.attachInterpolatedValues(attrs, step, options);
orderWatch = attrs.$observe(TourHelpers.getAttrName('order'), function (order) {
step.order = !isNaN(order*1) ? order*1 : 0;
ctrl.refreshTour();
});
//Attach event handlers
TourHelpers.attachEventHandlers(scope, attrs, step, events);
//Compile templates
templateReady = TourHelpers.attachTemplate(scope, attrs, step);
//Check whether or not the step should be skipped
function stepIsSkipped() {
var skipped;
if (attrs[TourHelpers.getAttrName('skip')]) {
skipped = scope.$eval(attrs[TourHelpers.getAttrName('skip')]);
}
if (!skipped) {
skipped = !!step.path || (element.is(':hidden') && !attrs.availableWhenHidden);
}
return skipped;
}
skipWatch = scope.$watch(stepIsSkipped, function (skip) {
if (skip) {
ctrl.removeStep(step);
} else {
ctrl.addStep(step);
}
});
scope.$on('$destroy', function () {
ctrl.removeStep(step);
orderWatch();
skipWatch();
});
//If there is an options argument passed, just use that instead
if (attrs[TourHelpers.getAttrName('options')]) {
angular.extend(step, scope.$eval(attrs[TourHelpers.getAttrName('options')]));
}
//set up redirects
function setRedirect(direction, path, targetName) {
var oldHandler = step[direction];
step[direction] = function (tour) {
if (oldHandler) {
oldHandler(tour);
}
ctrl.waitFor(targetName);
TourHelpers.safeApply(scope, function () {
$location.path(path);
});
};
}
if (step.nextPath) {
step.redirectNext = true;
setRedirect('onNext', step.nextPath, step.nextStep);
}
if (step.prevPath) {
step.redirectPrev = true;
setRedirect('onPrev', step.prevPath, step.prevStep);
}
//Add step to tour
templateReady.then(function () {
ctrl.addStep(step);
});
}
};
}];
}
app.directive('tourStep', directive());
app.directive('bsTourStep', directive());
}(angular.module('bm.bsTour')));