forked from angular-ui/ui-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompat.js
84 lines (72 loc) · 2.51 KB
/
compat.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
$RouteProvider.$inject = ['$stateProvider', '$urlRouterProvider'];
function $RouteProvider( $stateProvider, $urlRouterProvider) {
var routes = [];
onEnterRoute.$inject = ['$$state'];
function onEnterRoute( $$state) {
/*jshint validthis: true */
this.locals = $$state.locals.globals;
this.params = this.locals.$stateParams;
}
function onExitRoute() {
/*jshint validthis: true */
this.locals = null;
this.params = null;
}
this.when = when;
function when(url, route) {
/*jshint validthis: true */
if (route.redirectTo != null) {
// Redirect, configure directly on $urlRouterProvider
var redirect = route.redirectTo, handler;
if (isString(redirect)) {
handler = redirect; // leave $urlRouterProvider to handle
} else if (isFunction(redirect)) {
// Adapt to $urlRouterProvider API
handler = function (params, $location) {
return redirect(params, $location.path(), $location.search());
};
} else {
throw new Error("Invalid 'redirectTo' in when()");
}
$urlRouterProvider.when(url, handler);
} else {
// Regular route, configure as state
$stateProvider.state(inherit(route, {
parent: null,
name: 'route:' + encodeURIComponent(url),
url: url,
onEnter: onEnterRoute,
onExit: onExitRoute
}));
}
routes.push(route);
return this;
}
this.$get = $get;
$get.$inject = ['$state', '$rootScope', '$routeParams'];
function $get( $state, $rootScope, $routeParams) {
var $route = {
routes: routes,
params: $routeParams,
current: undefined
};
function stateAsRoute(state) {
return (state.name !== '') ? state : undefined;
}
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
$rootScope.$broadcast('$routeChangeStart', stateAsRoute(to), stateAsRoute(from));
});
$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
$route.current = stateAsRoute(to);
$rootScope.$broadcast('$routeChangeSuccess', stateAsRoute(to), stateAsRoute(from));
copy(toParams, $route.params);
});
$rootScope.$on('$stateChangeError', function (ev, to, toParams, from, fromParams, error) {
$rootScope.$broadcast('$routeChangeError', stateAsRoute(to), stateAsRoute(from), error);
});
return $route;
}
}
angular.module('ui.compat')
.provider('$route', $RouteProvider)
.directive('ngView', $ViewDirective);