/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

  1. $RouteProvider.$inject = ['$stateProvider', '$urlRouterProvider'];
  2. function $RouteProvider( $stateProvider, $urlRouterProvider) {
  3. var routes = [];
  4. onEnterRoute.$inject = ['$$state'];
  5. function onEnterRoute( $$state) {
  6. /*jshint validthis: true */
  7. this.locals = $$state.locals.globals;
  8. this.params = this.locals.$stateParams;
  9. }
  10. function onExitRoute() {
  11. /*jshint validthis: true */
  12. this.locals = null;
  13. this.params = null;
  14. }
  15. this.when = when;
  16. function when(url, route) {
  17. /*jshint validthis: true */
  18. if (route.redirectTo != null) {
  19. // Redirect, configure directly on $urlRouterProvider
  20. var redirect = route.redirectTo, handler;
  21. if (isString(redirect)) {
  22. handler = redirect; // leave $urlRouterProvider to handle
  23. } else if (isFunction(redirect)) {
  24. // Adapt to $urlRouterProvider API
  25. handler = function (params, $location) {
  26. return redirect(params, $location.path(), $location.search());
  27. };
  28. } else {
  29. throw new Error("Invalid 'redirectTo' in when()");
  30. }
  31. $urlRouterProvider.when(url, handler);
  32. } else {
  33. // Regular route, configure as state
  34. $stateProvider.state(inherit(route, {
  35. parent: null,
  36. name: 'route:' + encodeURIComponent(url),
  37. url: url,
  38. onEnter: onEnterRoute,
  39. onExit: onExitRoute
  40. }));
  41. }
  42. routes.push(route);
  43. return this;
  44. }
  45. this.$get = $get;
  46. $get.$inject = ['$state', '$rootScope', '$routeParams'];
  47. function $get( $state, $rootScope, $routeParams) {
  48. var $route = {
  49. routes: routes,
  50. params: $routeParams,
  51. current: undefined
  52. };
  53. function stateAsRoute(state) {
  54. return (state.name !== '') ? state : undefined;
  55. }
  56. $rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
  57. $rootScope.$broadcast('$routeChangeStart', stateAsRoute(to), stateAsRoute(from));
  58. });
  59. $rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
  60. $route.current = stateAsRoute(to);
  61. $rootScope.$broadcast('$routeChangeSuccess', stateAsRoute(to), stateAsRoute(from));
  62. copy(toParams, $route.params);
  63. });
  64. $rootScope.$on('$stateChangeError', function (ev, to, toParams, from, fromParams, error) {
  65. $rootScope.$broadcast('$routeChangeError', stateAsRoute(to), stateAsRoute(from), error);
  66. });
  67. return $route;
  68. }
  69. }
  70. angular.module('ui.router.compat')
  71. .provider('$route', $RouteProvider)
  72. .directive('ngView', $ViewDirective);