2016-10-23 22:37:15 +02:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
///<reference path="../typings/angularjs/angular.d.ts"/>
|
2015-08-20 13:19:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name ngOutlet
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* An ngOutlet is where resolved content goes.
|
|
|
|
*
|
|
|
|
* ## Use
|
|
|
|
*
|
|
|
|
* ```html
|
|
|
|
* <div ng-outlet="name"></div>
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The value for the `ngOutlet` attribute is optional.
|
|
|
|
*/
|
2016-02-17 07:47:49 +00:00
|
|
|
function ngOutletDirective($animate, $q: ng.IQService, $rootRouter) {
|
2016-11-12 14:08:58 +01:00
|
|
|
const rootRouter = $rootRouter;
|
2015-08-20 13:19:34 -07:00
|
|
|
|
|
|
|
return {
|
|
|
|
restrict: 'AE',
|
|
|
|
transclude: 'element',
|
|
|
|
terminal: true,
|
|
|
|
priority: 400,
|
|
|
|
require: ['?^^ngOutlet', 'ngOutlet'],
|
|
|
|
link: outletLink,
|
2015-09-28 02:12:23 +03:00
|
|
|
controller: class {},
|
2015-08-20 13:19:34 -07:00
|
|
|
controllerAs: '$$ngOutlet'
|
|
|
|
};
|
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
function outletLink(scope, element, attrs, ctrls, $transclude) {
|
|
|
|
class Outlet {
|
|
|
|
constructor(private controller, private router) {}
|
2015-08-20 13:19:34 -07:00
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
private currentController;
|
|
|
|
private currentInstruction;
|
|
|
|
private currentScope;
|
|
|
|
private currentElement;
|
|
|
|
private previousLeaveAnimation;
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
private cleanupLastView() {
|
|
|
|
if (this.previousLeaveAnimation) {
|
|
|
|
$animate.cancel(this.previousLeaveAnimation);
|
|
|
|
this.previousLeaveAnimation = null;
|
|
|
|
}
|
2015-08-20 13:19:34 -07:00
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
if (this.currentScope) {
|
|
|
|
this.currentScope.$destroy();
|
|
|
|
this.currentScope = null;
|
|
|
|
}
|
|
|
|
if (this.currentElement) {
|
|
|
|
this.previousLeaveAnimation = $animate.leave(this.currentElement);
|
|
|
|
this.previousLeaveAnimation.then(() => this.previousLeaveAnimation = null);
|
|
|
|
this.currentElement = null;
|
|
|
|
}
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
reuse(instruction) {
|
|
|
|
let next = $q.when(true);
|
2016-11-12 14:08:58 +01:00
|
|
|
const previousInstruction = this.currentInstruction;
|
2015-09-28 02:12:23 +03:00
|
|
|
this.currentInstruction = instruction;
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
if (this.currentController && this.currentController.$routerOnReuse) {
|
2015-09-28 02:12:23 +03:00
|
|
|
next = $q.when(
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
this.currentController.$routerOnReuse(this.currentInstruction, previousInstruction));
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
2015-08-30 21:25:46 -07:00
|
|
|
|
|
|
|
return next;
|
2015-09-28 02:12:23 +03:00
|
|
|
}
|
|
|
|
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
routerCanReuse(nextInstruction) {
|
2015-09-28 02:12:23 +03:00
|
|
|
let result;
|
|
|
|
if (!this.currentInstruction ||
|
|
|
|
this.currentInstruction.componentType !== nextInstruction.componentType) {
|
2015-08-20 13:19:34 -07:00
|
|
|
result = false;
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
} else if (this.currentController && this.currentController.$routerCanReuse) {
|
|
|
|
result = this.currentController.$routerCanReuse(nextInstruction, this.currentInstruction);
|
2015-08-20 13:19:34 -07:00
|
|
|
} else {
|
2015-09-28 02:12:23 +03:00
|
|
|
result = nextInstruction === this.currentInstruction ||
|
2016-04-12 09:40:37 -07:00
|
|
|
angular.equals(nextInstruction.params, this.currentInstruction.params);
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
2015-08-30 21:25:46 -07:00
|
|
|
return $q.when(result);
|
2015-09-28 02:12:23 +03:00
|
|
|
}
|
|
|
|
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
routerCanDeactivate(instruction) {
|
|
|
|
if (this.currentController && this.currentController.$routerCanDeactivate) {
|
2015-09-28 02:12:23 +03:00
|
|
|
return $q.when(
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
this.currentController.$routerCanDeactivate(instruction, this.currentInstruction));
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
|
|
|
return $q.when(true);
|
2015-09-28 02:12:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
deactivate(instruction) {
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
if (this.currentController && this.currentController.$routerOnDeactivate) {
|
2015-09-28 02:12:23 +03:00
|
|
|
return $q.when(
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 17:04:36 -08:00
|
|
|
this.currentController.$routerOnDeactivate(instruction, this.currentInstruction));
|
2015-08-30 21:25:46 -07:00
|
|
|
}
|
|
|
|
return $q.when();
|
2015-09-28 02:12:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
activate(instruction) {
|
2016-02-10 16:59:26 -08:00
|
|
|
this.previousInstruction = this.currentInstruction;
|
2015-09-28 02:12:23 +03:00
|
|
|
this.currentInstruction = instruction;
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const componentName = this.controller.$$componentName = instruction.componentType;
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
if (typeof componentName !== 'string') {
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
throw new Error('Component is not a string for ' + instruction.urlPath);
|
|
|
|
}
|
|
|
|
|
2016-02-11 12:34:04 -08:00
|
|
|
this.controller.$$template = '<' + dashCase(componentName) + ' $router="::$$router"></' +
|
2016-04-12 09:40:37 -07:00
|
|
|
dashCase(componentName) + '>';
|
2015-09-28 02:12:23 +03:00
|
|
|
this.controller.$$router = this.router.childRouter(instruction.componentType);
|
2016-02-10 16:59:26 -08:00
|
|
|
this.controller.$$outlet = this;
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const newScope = scope.$new();
|
2016-02-07 20:40:00 +00:00
|
|
|
newScope.$$router = this.controller.$$router;
|
2016-02-10 16:59:26 -08:00
|
|
|
this.deferredActivation = $q.defer();
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const clone = $transclude(newScope, clone => {});
|
2016-03-26 10:35:06 +01:00
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const activateView = () => {
|
2015-09-28 02:12:23 +03:00
|
|
|
$animate.enter(clone, null, this.currentElement || element);
|
|
|
|
this.cleanupLastView();
|
2016-03-26 10:35:06 +01:00
|
|
|
this.currentElement = clone;
|
|
|
|
this.currentScope = newScope;
|
|
|
|
};
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
|
2016-03-26 10:35:06 +01:00
|
|
|
return this.deferredActivation.promise.then(activateView, activateView);
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
2015-09-28 02:12:23 +03:00
|
|
|
}
|
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const parentCtrl = ctrls[0], myCtrl = ctrls[1],
|
2015-09-28 02:12:23 +03:00
|
|
|
router = (parentCtrl && parentCtrl.$$router) || rootRouter;
|
|
|
|
|
|
|
|
myCtrl.$$currentComponent = null;
|
|
|
|
|
|
|
|
router.registerPrimaryOutlet(new Outlet(myCtrl, router));
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
|
|
|
}
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
/**
|
|
|
|
* This directive is responsible for compiling the contents of ng-outlet
|
|
|
|
*/
|
2015-08-20 13:19:34 -07:00
|
|
|
function ngOutletFillContentDirective($compile) {
|
|
|
|
return {
|
|
|
|
restrict: 'EA',
|
|
|
|
priority: -400,
|
|
|
|
require: 'ngOutlet',
|
2015-09-28 02:12:23 +03:00
|
|
|
link: (scope, element, attrs, ctrl) => {
|
2016-11-12 14:08:58 +01:00
|
|
|
const template = ctrl.$$template;
|
2015-09-28 02:12:23 +03:00
|
|
|
element.html(template);
|
2016-02-10 16:59:26 -08:00
|
|
|
$compile(element.contents())(scope);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
|
|
|
|
|
2016-02-10 16:59:26 -08:00
|
|
|
function routerTriggerDirective($q) {
|
|
|
|
return {
|
|
|
|
require: '^ngOutlet',
|
|
|
|
priority: -1000,
|
|
|
|
link: function(scope, element, attr, ngOutletCtrl) {
|
2016-11-12 14:08:58 +01:00
|
|
|
let promise = $q.when();
|
|
|
|
const outlet = ngOutletCtrl.$$outlet;
|
|
|
|
const currentComponent = outlet.currentController =
|
2016-02-10 16:59:26 -08:00
|
|
|
element.controller(ngOutletCtrl.$$componentName);
|
|
|
|
if (currentComponent.$routerOnActivate) {
|
2016-04-12 09:40:37 -07:00
|
|
|
promise = $q.when(currentComponent.$routerOnActivate(outlet.currentInstruction,
|
|
|
|
outlet.previousInstruction));
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
}
|
2016-02-10 16:59:26 -08:00
|
|
|
promise.then(outlet.deferredActivation.resolve, outlet.deferredActivation.reject);
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-10 16:59:26 -08:00
|
|
|
|
2015-08-20 13:19:34 -07:00
|
|
|
/**
|
|
|
|
* @name ngLink
|
|
|
|
* @description
|
|
|
|
* Lets you link to different parts of the app, and automatically generates hrefs.
|
|
|
|
*
|
|
|
|
* ## Use
|
|
|
|
* The directive uses a simple syntax: `ng-link="componentName({ param: paramValue })"`
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-08-20 13:19:34 -07:00
|
|
|
*
|
|
|
|
* ```js
|
2015-09-28 02:12:23 +03:00
|
|
|
* angular.module('myApp', ['ngComponentRouter'])
|
2016-02-17 07:47:49 +00:00
|
|
|
* .controller('AppController', ['$rootRouter', function($rootRouter) {
|
|
|
|
* $rootRouter.config({ path: '/user/:id', component: 'user' });
|
2015-08-20 13:19:34 -07:00
|
|
|
* this.user = { name: 'Brian', id: 123 };
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ```html
|
|
|
|
* <div ng-controller="AppController as app">
|
|
|
|
* <a ng-link="user({id: app.user.id})">{{app.user.name}}</a>
|
|
|
|
* </div>
|
|
|
|
* ```
|
|
|
|
*/
|
2016-02-17 07:47:49 +00:00
|
|
|
function ngLinkDirective($rootRouter, $parse) {
|
2015-09-28 02:12:23 +03:00
|
|
|
return {require: '?^^ngOutlet', restrict: 'A', link: ngLinkDirectiveLinkFn};
|
2015-08-20 13:19:34 -07:00
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
function ngLinkDirectiveLinkFn(scope, element, attrs, ctrl) {
|
2016-11-12 14:08:58 +01:00
|
|
|
const router = (ctrl && ctrl.$$router) || $rootRouter;
|
2015-08-20 13:19:34 -07:00
|
|
|
if (!router) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-04 09:14:27 +01:00
|
|
|
let navigationInstruction = null;
|
2016-11-12 14:08:58 +01:00
|
|
|
const link = attrs.ngLink || '';
|
2015-08-20 13:19:34 -07:00
|
|
|
|
|
|
|
function getLink(params) {
|
2016-05-20 18:48:03 +02:00
|
|
|
if (!params) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-04 09:14:27 +01:00
|
|
|
navigationInstruction = router.generate(params);
|
2016-02-03 21:50:51 -06:00
|
|
|
|
2016-04-12 09:40:37 -07:00
|
|
|
scope.$watch(function() { return router.isRouteActive(navigationInstruction); },
|
|
|
|
function(active) {
|
|
|
|
if (active) {
|
|
|
|
element.addClass('ng-link-active');
|
|
|
|
} else {
|
|
|
|
element.removeClass('ng-link-active');
|
|
|
|
}
|
|
|
|
});
|
2016-02-03 21:50:51 -06:00
|
|
|
|
2016-03-04 09:14:27 +01:00
|
|
|
const navigationHref = navigationInstruction.toLinkUrl();
|
|
|
|
return $rootRouter._location.prepareExternalUrl(navigationHref);
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const routeParamsGetter = $parse(link);
|
2015-08-20 13:19:34 -07:00
|
|
|
// we can avoid adding a watcher if it's a literal
|
|
|
|
if (routeParamsGetter.constant) {
|
2016-11-12 14:08:58 +01:00
|
|
|
const params = routeParamsGetter();
|
2015-09-28 02:12:23 +03:00
|
|
|
element.attr('href', getLink(params));
|
2015-08-20 13:19:34 -07:00
|
|
|
} else {
|
2016-04-12 09:40:37 -07:00
|
|
|
scope.$watch(() => routeParamsGetter(scope), params => element.attr('href', getLink(params)),
|
|
|
|
true);
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
element.on('click', event => {
|
2016-03-04 09:14:27 +01:00
|
|
|
if (event.which !== 1 || !navigationInstruction) {
|
2015-08-20 13:19:34 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-04 09:14:27 +01:00
|
|
|
$rootRouter.navigateByInstruction(navigationInstruction);
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 15:53:50 -07:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
}
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
|
|
|
|
2015-09-28 02:12:23 +03:00
|
|
|
function dashCase(str: string): string {
|
|
|
|
return str.replace(/[A-Z]/g, match => '-' + match.toLowerCase());
|
2015-08-20 13:19:34 -07:00
|
|
|
}
|
2015-09-28 02:12:23 +03:00
|
|
|
|
|
|
|
/*
|
2017-01-26 22:30:42 -08:00
|
|
|
* A module for adding new a routing system AngularJS.
|
2015-09-28 02:12:23 +03:00
|
|
|
*/
|
|
|
|
angular.module('ngComponentRouter', [])
|
2016-02-17 07:47:49 +00:00
|
|
|
.directive('ngOutlet', ['$animate', '$q', '$rootRouter', ngOutletDirective])
|
2015-11-04 05:48:06 -06:00
|
|
|
.directive('ngOutlet', ['$compile', ngOutletFillContentDirective])
|
2016-02-17 07:47:49 +00:00
|
|
|
.directive('ngLink', ['$rootRouter', '$parse', ngLinkDirective])
|
2016-02-22 14:01:02 +00:00
|
|
|
.directive('$router', ['$q', routerTriggerDirective]);
|