-
-
Notifications
You must be signed in to change notification settings - Fork 958
/
Copy pathroutehelper.js
106 lines (93 loc) · 3.44 KB
/
routehelper.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
(function() {
'use strict';
angular
.module('blocks.router')
.provider('routehelperConfig', routehelperConfig)
.factory('routehelper', routehelper);
routehelper.$inject = ['$location', '$rootScope', '$route', 'logger', 'routehelperConfig'];
// Must configure via the routehelperConfigProvider
function routehelperConfig() {
/* jshint validthis:true */
this.config = {
// These are the properties we need to set
// $routeProvider: undefined
// docTitle: ''
// resolveAlways: {ready: function(){ } }
};
this.$get = function() {
return {
config: this.config
};
};
}
function routehelper($location, $rootScope, $route, logger, routehelperConfig) {
var handlingRouteChangeError = false;
var routeCounts = {
errors: 0,
changes: 0
};
var routes = [];
var $routeProvider = routehelperConfig.config.$routeProvider;
var service = {
configureRoutes: configureRoutes,
getRoutes: getRoutes,
routeCounts: routeCounts
};
init();
return service;
///////////////
function configureRoutes(routes) {
routes.forEach(function(route) {
route.config.resolve =
angular.extend(route.config.resolve || {}, routehelperConfig.config.resolveAlways);
$routeProvider.when(route.url, route.config);
});
$routeProvider.otherwise({redirectTo: '/'});
}
function handleRoutingErrors() {
// Route cancellation:
// On routing error, go to the dashboard.
// Provide an exit clause if it tries to do it twice.
$rootScope.$on('$routeChangeError',
function(event, current, previous, rejection) {
if (handlingRouteChangeError) {
return;
}
routeCounts.errors++;
handlingRouteChangeError = true;
var destination = (current && (current.title || current.name || current.loadedTemplateUrl)) ||
'unknown target';
var msg = 'Error routing to ' + destination + '. ' + (rejection.msg || '');
logger.warning(msg, [current]);
$location.path('/');
}
);
}
function init() {
handleRoutingErrors();
updateDocTitle();
}
function getRoutes() {
for (var prop in $route.routes) {
if ($route.routes.hasOwnProperty(prop)) {
var route = $route.routes[prop];
var isRoute = !!route.title;
if (isRoute) {
routes.push(route);
}
}
}
return routes;
}
function updateDocTitle() {
$rootScope.$on('$routeChangeSuccess',
function(event, current, previous) {
routeCounts.changes++;
handlingRouteChangeError = false;
var title = routehelperConfig.config.docTitle + ' ' + (current.title || '');
$rootScope.title = title; // data bind to <title>
}
);
}
}
})();