/src/78 - Testing With Protractor Page Objects/app/vendor/angular-ui-router/src/compat.js
https://gitlab.com/bruce.ng/angularjs-lesson · JavaScript · 84 lines · 66 code · 12 blank · 6 comment · 7 complexity · 12be8b98fa058a5f6efd1ffd27171e99 MD5 · raw file
- $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.router.compat')
- .provider('$route', $RouteProvider)
- .directive('ngView', $ViewDirective);