-
Notifications
You must be signed in to change notification settings - Fork 42
/
angular-breadcrumbs.js
51 lines (44 loc) · 1.81 KB
/
angular-breadcrumbs.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
/**
* angular-breadcrumb.js - A better AngularJS service to help with breadcrumb-style navigation between views.
* Based on breadcrumb.js (https://github.com/angular-app/angular-app/blob/master/client/src/common/services/breadcrumbs.js)
*
* @author Ian Kennington Walter (http://www.iankwalter.com)
*/
angular.module('services.breadcrumbs', []);
angular.module('services.breadcrumbs').factory('breadcrumbs', ['$rootScope', '$location', '$route', function($rootScope, $location, $route) {
var breadcrumbs = [],
breadcrumbsService = {},
routes = $route.routes;
var generateBreadcrumbs = function() {
breadcrumbs = [];
var pathElements = $location.path().split('/'),
path = '';
var getRoute = function(route) {
angular.forEach($route.current.params, function(value, key) {
var re = new RegExp(value);
route = route.replace(re, ':' + key);
});
return route;
};
if (pathElements[1] == '') delete pathElements[1];
angular.forEach(pathElements, function(el) {
path += path === '/' ? el : '/' + el;
var route = getRoute(path);
if (routes[route] && routes[route].label) {
breadcrumbs.push({ label: routes[route].label, path: path });
}
});
};
// We want to update breadcrumbs only when a route is actually changed
// as $location.path() will get updated immediately (even if route change fails!)
$rootScope.$on('$routeChangeSuccess', function(event, current) {
generateBreadcrumbs();
});
breadcrumbsService.getAll = function() {
return breadcrumbs;
};
breadcrumbsService.getFirst = function() {
return breadcrumbs[0] || {};
};
return breadcrumbsService;
}]);