2014-02-05 01:50:13 +00:00
|
|
|
angular.module("angular-tour", [])
|
|
|
|
.provider('AngularTour', function() {
|
2014-11-24 21:07:38 +00:00
|
|
|
this.$get = ['$document', '$rootScope', '$compile', '$location', function($document, $rootScope, $compile, $location) {
|
2014-02-06 02:00:04 +00:00
|
|
|
$rootScope.angular_tour_current = null;
|
|
|
|
|
2014-11-24 21:07:38 +00:00
|
|
|
function _start(tour, opt_stepIndex, opt_existingScope) {
|
2014-02-06 02:00:04 +00:00
|
|
|
tour.initialStep = opt_stepIndex || tour.initialStep || 0;
|
|
|
|
tour.tourScope = opt_existingScope || null;
|
2014-02-05 01:50:13 +00:00
|
|
|
$rootScope.angular_tour_current = tour;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _stop() {
|
|
|
|
$rootScope.angular_tour_current = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
start: _start,
|
|
|
|
stop: _stop
|
|
|
|
};
|
|
|
|
}];
|
|
|
|
})
|
|
|
|
|
2014-02-06 02:00:04 +00:00
|
|
|
.directive('angularTourUi', function() {
|
2014-02-05 01:50:13 +00:00
|
|
|
var directiveDefinitionObject = {
|
|
|
|
priority: 0,
|
2014-02-06 02:00:04 +00:00
|
|
|
templateUrl: '/static/directives/angular-tour-ui.html',
|
2014-02-05 01:50:13 +00:00
|
|
|
replace: false,
|
|
|
|
transclude: false,
|
|
|
|
restrict: 'C',
|
|
|
|
scope: {
|
2014-02-06 02:00:04 +00:00
|
|
|
'tour': '=tour',
|
|
|
|
'inline': '=inline',
|
2014-02-05 01:50:13 +00:00
|
|
|
},
|
2014-02-06 02:00:04 +00:00
|
|
|
controller: function($rootScope, $scope, $element, $location, $interval, AngularTour) {
|
2014-06-16 18:05:27 +00:00
|
|
|
$scope.supported = !!window.EventSource;
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
var createNewScope = function(initialScope) {
|
|
|
|
var tourScope = jQuery.extend({}, initialScope || {});
|
|
|
|
tourScope['_replaceData'] = function(s) {
|
|
|
|
if (typeof s != 'string') {
|
|
|
|
return s;
|
|
|
|
}
|
2014-02-06 02:00:04 +00:00
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
for (var key in tourScope) {
|
|
|
|
if (key[0] == '_') { continue; }
|
|
|
|
if (tourScope.hasOwnProperty(key)) {
|
|
|
|
s = s.replace('{{' + key + '}}', tourScope[key]);
|
2014-02-06 02:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 01:58:26 +00:00
|
|
|
return s;
|
2014-02-06 02:00:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return tourScope;
|
|
|
|
};
|
|
|
|
|
2014-02-05 01:50:13 +00:00
|
|
|
$scope.stepIndex = 0;
|
|
|
|
$scope.step = null;
|
2014-02-06 02:00:04 +00:00
|
|
|
$scope.interval = null;
|
2014-02-07 01:58:26 +00:00
|
|
|
$scope.tourScope = null;
|
2014-02-06 02:00:04 +00:00
|
|
|
|
|
|
|
var getElement = function() {
|
|
|
|
if (typeof $scope.step['element'] == 'function') {
|
|
|
|
return $($scope.step['element'](tourScope));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $($scope.tourScope._replaceData($scope.step['element']));
|
|
|
|
};
|
2014-02-05 01:50:13 +00:00
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
var checkSignal = function() {
|
|
|
|
return $scope.step['signal'] && $scope.step['signal']($scope.tourScope);
|
|
|
|
};
|
|
|
|
|
|
|
|
var teardownSignal = function() {
|
|
|
|
if (!$scope.step) { return; }
|
|
|
|
|
|
|
|
var signal = $scope.step['signal'];
|
|
|
|
if (signal && signal.$teardown) {
|
|
|
|
signal.$teardown($scope.tourScope);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var setupSignal = function() {
|
|
|
|
if (!$scope.step) { return; }
|
|
|
|
|
|
|
|
var signal = $scope.step['signal'];
|
|
|
|
if (signal && signal.$setup) {
|
|
|
|
signal.$setup($scope.tourScope);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-05 01:50:13 +00:00
|
|
|
var checkSignalTimer = function() {
|
2014-02-07 01:58:26 +00:00
|
|
|
if (!$scope.step || !$scope.tourScope) {
|
2014-02-05 01:50:13 +00:00
|
|
|
stopSignalTimer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
if (checkSignal()) {
|
2014-02-05 01:50:13 +00:00
|
|
|
$scope.next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var stopSignalTimer = function() {
|
|
|
|
if (!$scope.interval) { return; }
|
|
|
|
|
|
|
|
$interval.cancel($scope.interval);
|
|
|
|
$scope.interval = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
var startSignalTimer = function() {
|
|
|
|
$scope.interval = $interval(checkSignalTimer, 500);
|
|
|
|
};
|
|
|
|
|
|
|
|
var closeDomHighlight = function() {
|
|
|
|
if (!$scope.step) { return; }
|
|
|
|
|
2014-02-06 02:00:04 +00:00
|
|
|
var element = getElement($scope.tourScope);
|
2014-02-05 01:50:13 +00:00
|
|
|
element.spotlight('close');
|
|
|
|
};
|
|
|
|
|
|
|
|
var updateDomHighlight = function() {
|
2014-02-06 02:00:04 +00:00
|
|
|
var element = getElement();
|
2014-02-05 01:50:13 +00:00
|
|
|
if (!element.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
element.spotlight({
|
|
|
|
opacity: .5,
|
|
|
|
speed: 400,
|
|
|
|
color: '#333',
|
|
|
|
animate: true,
|
2014-11-24 21:07:38 +00:00
|
|
|
easing: 'linear',
|
2014-02-05 01:50:13 +00:00
|
|
|
exitEvent: 'mouseenter',
|
|
|
|
exitEventAppliesToElement: true,
|
|
|
|
paddingX: 1,
|
|
|
|
paddingY: 1
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-13 22:27:50 +00:00
|
|
|
var fireMixpanelEvent = function() {
|
2014-04-09 01:10:33 +00:00
|
|
|
if (!$scope.step || !window['mixpanel']) { return; }
|
2014-02-13 22:27:50 +00:00
|
|
|
|
|
|
|
var eventName = $scope.step['mixpanelEvent'];
|
|
|
|
if (eventName) {
|
|
|
|
mixpanel.track(eventName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-05 01:50:13 +00:00
|
|
|
$scope.setStepIndex = function(stepIndex) {
|
|
|
|
// Close existing spotlight and signal timer.
|
2014-02-07 01:58:26 +00:00
|
|
|
teardownSignal();
|
2014-02-05 01:50:13 +00:00
|
|
|
closeDomHighlight();
|
|
|
|
stopSignalTimer();
|
|
|
|
|
|
|
|
// Check if there is a next step.
|
|
|
|
if (!$scope.tour || stepIndex >= $scope.tour.steps.length) {
|
|
|
|
$scope.step = null;
|
|
|
|
$scope.hasNextStep = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.step = $scope.tour.steps[stepIndex];
|
2015-06-04 18:15:47 +00:00
|
|
|
if ($scope.step.skip) {
|
|
|
|
$scope.setStepIndex(stepIndex + 1);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-06 02:00:04 +00:00
|
|
|
|
2014-02-13 22:27:50 +00:00
|
|
|
fireMixpanelEvent();
|
|
|
|
|
2014-02-06 02:00:04 +00:00
|
|
|
// If the signal is already true, then skip this step entirely.
|
2014-02-07 01:58:26 +00:00
|
|
|
setupSignal();
|
|
|
|
if (checkSignal()) {
|
2014-02-06 02:00:04 +00:00
|
|
|
$scope.setStepIndex(stepIndex + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:50:13 +00:00
|
|
|
$scope.stepIndex = stepIndex;
|
|
|
|
$scope.hasNextStep = stepIndex < $scope.tour.steps.length - 1;
|
|
|
|
|
|
|
|
// Need the timeout here to ensure the click event does not
|
2014-02-07 01:58:26 +00:00
|
|
|
// hide the spotlight, and it has to be longer than the hide
|
|
|
|
// spotlight animation timing.
|
2014-02-05 01:50:13 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
updateDomHighlight();
|
2014-02-07 01:58:26 +00:00
|
|
|
}, 500);
|
2014-02-05 01:50:13 +00:00
|
|
|
|
|
|
|
// Start listening for signals to move the tour forward.
|
|
|
|
if ($scope.step.signal) {
|
|
|
|
startSignalTimer();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.stop = function() {
|
2014-02-06 02:00:04 +00:00
|
|
|
closeDomHighlight();
|
2014-02-05 01:50:13 +00:00
|
|
|
$scope.tour = null;
|
2014-02-06 02:00:04 +00:00
|
|
|
AngularTour.stop();
|
2014-02-05 01:50:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.next = function() {
|
|
|
|
$scope.setStepIndex($scope.stepIndex + 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.$watch('tour', function(tour) {
|
|
|
|
stopSignalTimer();
|
2014-02-06 02:00:04 +00:00
|
|
|
if (tour) {
|
2014-02-07 01:58:26 +00:00
|
|
|
// Set the tour scope.
|
|
|
|
if (tour.tourScope) {
|
|
|
|
$scope.tourScope = tour.tourScope;
|
|
|
|
} else {
|
|
|
|
$scope.tourScope = $scope.tour.tourScope = createNewScope(tour.initialScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the initial step.
|
2014-02-06 02:00:04 +00:00
|
|
|
$scope.setStepIndex(tour.initialStep || 0);
|
|
|
|
}
|
2014-02-05 01:50:13 +00:00
|
|
|
});
|
2014-02-06 02:00:04 +00:00
|
|
|
|
|
|
|
// If this is an inline tour, then we need to monitor the page to determine when
|
|
|
|
// to transition it to an overlay tour.
|
|
|
|
if ($scope.inline) {
|
|
|
|
var counter = 0;
|
2014-11-24 21:07:38 +00:00
|
|
|
var unbind = $rootScope.$watch(function() {
|
|
|
|
return $location.path();
|
|
|
|
}, function(location) {
|
2014-02-06 02:00:04 +00:00
|
|
|
// Since this callback fires for the first page display, we only unbind it
|
|
|
|
// after the second call.
|
|
|
|
if (counter == 1) {
|
|
|
|
// Unbind the listener.
|
|
|
|
unbind();
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
// Teardown any existing signal listener.
|
|
|
|
teardownSignal();
|
|
|
|
|
2014-02-06 02:00:04 +00:00
|
|
|
// If there is an active tour, transition it over to the overlay.
|
|
|
|
if ($scope.tour && $scope.step && $scope.step['overlayable']) {
|
2014-02-13 22:39:01 +00:00
|
|
|
AngularTour.start($scope.tour, $scope.stepIndex + 1, $scope.tourScope);
|
2014-02-06 02:00:04 +00:00
|
|
|
$scope.tour = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
counter++;
|
|
|
|
});
|
|
|
|
}
|
2014-02-05 01:50:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return directiveDefinitionObject;
|
|
|
|
})
|
|
|
|
|
|
|
|
.factory('AngularTourSignals', ['$location', function($location) {
|
|
|
|
var signals = {};
|
|
|
|
|
|
|
|
// Signal: When the page location matches the given path.
|
|
|
|
signals.matchesLocation = function(locationPath) {
|
2014-02-06 02:00:04 +00:00
|
|
|
return function(tourScope) {
|
|
|
|
return $location.path() == tourScope._replaceData(locationPath);
|
2014-02-05 01:50:13 +00:00
|
|
|
};
|
|
|
|
};
|
2015-05-13 18:55:39 +00:00
|
|
|
|
|
|
|
// Signal: When an element is visible in the page's DOM.
|
|
|
|
signals.elementVisible = function(elementPath) {
|
|
|
|
return function(tourScope) {
|
|
|
|
return $(tourScope._replaceData(elementPath)).height() > 0;
|
|
|
|
};
|
|
|
|
};
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-05 01:50:13 +00:00
|
|
|
// Signal: When an element is found in the page's DOM.
|
|
|
|
signals.elementAvaliable = function(elementPath) {
|
2014-02-06 02:00:04 +00:00
|
|
|
return function(tourScope) {
|
|
|
|
return $(tourScope._replaceData(elementPath)).length > 0;
|
2014-02-05 01:50:13 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
// Signal: When a server-side event matches the predicate.
|
|
|
|
signals.serverEvent = function(url, matcher) {
|
|
|
|
var checker = function(tourScope) {
|
|
|
|
return checker.$message && matcher(checker.$message, tourScope);
|
|
|
|
};
|
|
|
|
|
|
|
|
checker.$message = null;
|
|
|
|
|
|
|
|
checker.$setup = function(tourScope) {
|
2014-06-16 18:05:27 +00:00
|
|
|
if (!window.EventSource) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
var fullUrl = tourScope._replaceData(url);
|
|
|
|
checker.$source = new EventSource(fullUrl);
|
|
|
|
checker.$source.onmessage = function(e) {
|
2016-07-27 20:41:26 +00:00
|
|
|
var parsed = JSON.parse(e.data);
|
|
|
|
if (!parsed['data']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
checker.$message = parsed;
|
2014-02-07 01:58:26 +00:00
|
|
|
};
|
2016-07-27 20:41:26 +00:00
|
|
|
|
|
|
|
checker.$source.onopen = function(e) {
|
|
|
|
checker.$hasError = false;
|
|
|
|
};
|
|
|
|
|
2016-06-28 19:35:21 +00:00
|
|
|
checker.$source.onerror = function(e) {
|
2016-07-27 20:41:26 +00:00
|
|
|
checker.$hasError = true;
|
2016-06-28 19:35:21 +00:00
|
|
|
};
|
2014-02-07 01:58:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
checker.$teardown = function() {
|
|
|
|
if (checker.$source) {
|
|
|
|
checker.$source.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return checker;
|
|
|
|
};
|
|
|
|
|
2014-02-05 01:50:13 +00:00
|
|
|
return signals;
|
|
|
|
}]);
|