2015-08-20 16:19:34 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A module for adding new a routing system Angular 1.
|
|
|
|
*/
|
|
|
|
angular.module('ngComponentRouter', [])
|
|
|
|
.directive('ngOutlet', ngOutletDirective)
|
|
|
|
.directive('ngOutlet', ngOutletFillContentDirective)
|
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 18:53:50 -04:00
|
|
|
.directive('ngLink', ngLinkDirective);
|
2015-08-20 16:19:34 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A module for inspecting controller constructors
|
|
|
|
*/
|
|
|
|
angular.module('ng')
|
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 18:53:50 -04:00
|
|
|
.provider('$$directiveIntrospector', $$directiveIntrospectorProvider)
|
|
|
|
.config(compilerProviderDecorator);
|
2015-08-20 16:19:34 -04: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 18:53:50 -04:00
|
|
|
* decorates $compileProvider so that we have access to routing metadata
|
2015-08-20 16:19:34 -04: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 18:53:50 -04:00
|
|
|
function compilerProviderDecorator($compileProvider, $$directiveIntrospectorProvider) {
|
|
|
|
var directive = $compileProvider.directive;
|
|
|
|
$compileProvider.directive = function (name, factory) {
|
|
|
|
$$directiveIntrospectorProvider.register(name, factory);
|
|
|
|
return directive.apply(this, arguments);
|
2015-08-20 16:19:34 -04: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 18:53:50 -04:00
|
|
|
|
2015-08-20 16:19:34 -04:00
|
|
|
/*
|
|
|
|
* private service that holds route mappings for each controller
|
|
|
|
*/
|
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 18:53:50 -04:00
|
|
|
function $$directiveIntrospectorProvider() {
|
|
|
|
var directiveBuffer = [];
|
|
|
|
var directiveFactoriesByName = {};
|
|
|
|
var onDirectiveRegistered = null;
|
2015-08-20 16:19:34 -04:00
|
|
|
return {
|
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 18:53:50 -04:00
|
|
|
register: function (name, factory) {
|
|
|
|
if (angular.isArray(factory)) {
|
|
|
|
factory = factory[factory.length - 1];
|
2015-08-20 16:19:34 -04: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 18:53:50 -04:00
|
|
|
directiveFactoriesByName[name] = factory;
|
|
|
|
if (onDirectiveRegistered) {
|
|
|
|
onDirectiveRegistered(name, factory);
|
2015-08-20 16:19:34 -04:00
|
|
|
} else {
|
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 18:53:50 -04:00
|
|
|
directiveBuffer.push({name: name, factory: factory});
|
2015-08-20 16:19:34 -04: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 18:53:50 -04:00
|
|
|
$get: function () {
|
2015-08-20 16:19:34 -04:00
|
|
|
var fn = function (newOnControllerRegistered) {
|
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 18:53:50 -04:00
|
|
|
onDirectiveRegistered = newOnControllerRegistered;
|
|
|
|
while (directiveBuffer.length > 0) {
|
|
|
|
var directive = directiveBuffer.pop();
|
|
|
|
onDirectiveRegistered(directive.name, directive.factory);
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn.getTypeByName = function (name) {
|
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 18:53:50 -04:00
|
|
|
return directiveFactoriesByName[name];
|
2015-08-20 16:19:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return fn;
|
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 18:53:50 -04:00
|
|
|
}
|
2015-08-20 16:19:34 -04: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.
|
|
|
|
*/
|
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 18:53:50 -04:00
|
|
|
function ngOutletDirective($animate, $q, $router) {
|
2015-08-20 16:19:34 -04:00
|
|
|
var rootRouter = $router;
|
|
|
|
|
|
|
|
return {
|
|
|
|
restrict: 'AE',
|
|
|
|
transclude: 'element',
|
|
|
|
terminal: true,
|
|
|
|
priority: 400,
|
|
|
|
require: ['?^^ngOutlet', 'ngOutlet'],
|
|
|
|
link: outletLink,
|
|
|
|
controller: function () {},
|
|
|
|
controllerAs: '$$ngOutlet'
|
|
|
|
};
|
|
|
|
|
|
|
|
function outletLink(scope, $element, attrs, ctrls, $transclude) {
|
|
|
|
var outletName = attrs.ngOutlet || 'default',
|
|
|
|
parentCtrl = ctrls[0],
|
|
|
|
myCtrl = ctrls[1],
|
|
|
|
router = (parentCtrl && parentCtrl.$$router) || rootRouter;
|
|
|
|
|
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 18:53:50 -04:00
|
|
|
myCtrl.$$currentComponent = null;
|
|
|
|
|
2015-08-20 16:19:34 -04:00
|
|
|
var childRouter,
|
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 18:53:50 -04:00
|
|
|
currentController,
|
2015-08-20 16:19:34 -04:00
|
|
|
currentInstruction,
|
|
|
|
currentScope,
|
|
|
|
currentElement,
|
|
|
|
previousLeaveAnimation;
|
|
|
|
|
|
|
|
function cleanupLastView() {
|
|
|
|
if (previousLeaveAnimation) {
|
|
|
|
$animate.cancel(previousLeaveAnimation);
|
|
|
|
previousLeaveAnimation = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentScope) {
|
|
|
|
currentScope.$destroy();
|
|
|
|
currentScope = null;
|
|
|
|
}
|
|
|
|
if (currentElement) {
|
|
|
|
previousLeaveAnimation = $animate.leave(currentElement);
|
|
|
|
previousLeaveAnimation.then(function () {
|
|
|
|
previousLeaveAnimation = null;
|
|
|
|
});
|
|
|
|
currentElement = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 00:25:46 -04:00
|
|
|
router.registerPrimaryOutlet({
|
|
|
|
reuse: function (instruction) {
|
2015-08-20 16:19:58 -04:00
|
|
|
var next = $q.when(true);
|
2015-08-31 00:25:46 -04:00
|
|
|
var previousInstruction = currentInstruction;
|
|
|
|
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 18:53:50 -04:00
|
|
|
if (currentController && currentController.$onReuse) {
|
|
|
|
next = $q.when(currentController.$onReuse(currentInstruction, previousInstruction));
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
2015-08-31 00:25:46 -04:00
|
|
|
|
|
|
|
return next;
|
2015-08-20 16:19:34 -04:00
|
|
|
},
|
|
|
|
canReuse: function (nextInstruction) {
|
|
|
|
var result;
|
|
|
|
if (!currentInstruction ||
|
2015-08-31 00:25:46 -04:00
|
|
|
currentInstruction.componentType !== nextInstruction.componentType) {
|
2015-08-20 16:19:34 -04:00
|
|
|
result = false;
|
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 18:53:50 -04:00
|
|
|
} else if (currentController && currentController.$canReuse) {
|
|
|
|
result = currentController.$canReuse(nextInstruction, currentInstruction);
|
2015-08-20 16:19:34 -04:00
|
|
|
} else {
|
2015-08-31 00:25:46 -04:00
|
|
|
result = nextInstruction === currentInstruction ||
|
|
|
|
angular.equals(nextInstruction.params, currentInstruction.params);
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
2015-08-31 00:25:46 -04:00
|
|
|
return $q.when(result);
|
2015-08-20 16:19:34 -04:00
|
|
|
},
|
|
|
|
canDeactivate: function (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 18:53:50 -04:00
|
|
|
if (currentController && currentController.$canDeactivate) {
|
|
|
|
return $q.when(currentController.$canDeactivate(instruction, currentInstruction));
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
|
|
|
return $q.when(true);
|
|
|
|
},
|
|
|
|
deactivate: function (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 18:53:50 -04:00
|
|
|
if (currentController && currentController.$onDeactivate) {
|
|
|
|
return $q.when(currentController.$onDeactivate(instruction, currentInstruction));
|
2015-08-31 00:25:46 -04:00
|
|
|
}
|
|
|
|
return $q.when();
|
2015-08-20 16:19:34 -04:00
|
|
|
},
|
|
|
|
activate: function (instruction) {
|
|
|
|
var previousInstruction = currentInstruction;
|
|
|
|
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 18:53:50 -04:00
|
|
|
|
|
|
|
var componentName = myCtrl.$$componentName = instruction.componentType;
|
|
|
|
|
|
|
|
if (typeof componentName != 'string') {
|
|
|
|
throw new Error('Component is not a string for ' + instruction.urlPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
myCtrl.$$routeParams = instruction.params;
|
|
|
|
|
|
|
|
myCtrl.$$template = '<div ' + dashCase(componentName) + '></div>';
|
|
|
|
|
|
|
|
myCtrl.$$router = router.childRouter(instruction.componentType);
|
|
|
|
|
|
|
|
var newScope = scope.$new();
|
|
|
|
|
|
|
|
var clone = $transclude(newScope, function (clone) {
|
|
|
|
$animate.enter(clone, null, currentElement || $element);
|
|
|
|
cleanupLastView();
|
2015-08-20 16:19:34 -04: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 18:53:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
currentElement = clone;
|
|
|
|
currentScope = newScope;
|
|
|
|
|
|
|
|
// TODO: prefer the other directive retrieving the controller
|
|
|
|
// by debug mode
|
|
|
|
currentController = currentElement.children().eq(0).controller(componentName);
|
|
|
|
|
|
|
|
if (currentController && currentController.$onActivate) {
|
|
|
|
return currentController.$onActivate(instruction, previousInstruction);
|
|
|
|
}
|
|
|
|
return $q.when();
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
2015-08-31 00:25:46 -04:00
|
|
|
});
|
2015-08-20 16:19:34 -04: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 18:53:50 -04:00
|
|
|
/**
|
|
|
|
* This directive is responsible for compiling the contents of ng-outlet
|
|
|
|
*/
|
2015-08-20 16:19:34 -04:00
|
|
|
function ngOutletFillContentDirective($compile) {
|
|
|
|
return {
|
|
|
|
restrict: 'EA',
|
|
|
|
priority: -400,
|
|
|
|
require: 'ngOutlet',
|
|
|
|
link: function (scope, $element, attrs, ctrl) {
|
|
|
|
var template = ctrl.$$template;
|
|
|
|
$element.html(template);
|
|
|
|
var link = $compile($element.contents());
|
|
|
|
link(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 18:53:50 -04:00
|
|
|
|
|
|
|
// TODO: move to primary directive
|
|
|
|
var componentInstance = scope[ctrl.$$componentName];
|
|
|
|
if (componentInstance) {
|
|
|
|
ctrl.$$currentComponent = componentInstance;
|
|
|
|
|
|
|
|
componentInstance.$router = ctrl.$$router;
|
|
|
|
componentInstance.$routeParams = ctrl.$$routeParams;
|
|
|
|
}
|
2015-08-20 16:19:34 -04: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 })"`
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* angular.module('myApp', ['ngFuturisticRouter'])
|
|
|
|
* .controller('AppController', ['$router', function($router) {
|
|
|
|
* $router.config({ path: '/user/:id' component: 'user' });
|
|
|
|
* 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>
|
|
|
|
* ```
|
|
|
|
*/
|
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 18:53:50 -04:00
|
|
|
function ngLinkDirective($router, $parse) {
|
2015-08-20 16:19:34 -04:00
|
|
|
var rootRouter = $router;
|
|
|
|
|
|
|
|
return {
|
|
|
|
require: '?^^ngOutlet',
|
|
|
|
restrict: 'A',
|
|
|
|
link: ngLinkDirectiveLinkFn
|
|
|
|
};
|
|
|
|
|
|
|
|
function ngLinkDirectiveLinkFn(scope, elt, attrs, ctrl) {
|
|
|
|
var router = (ctrl && ctrl.$$router) || rootRouter;
|
|
|
|
if (!router) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 18:53:50 -04:00
|
|
|
var instruction = null;
|
2015-08-20 16:19:34 -04:00
|
|
|
var link = attrs.ngLink || '';
|
|
|
|
|
|
|
|
function getLink(params) {
|
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 18:53:50 -04:00
|
|
|
instruction = router.generate(params);
|
|
|
|
return './' + angular.stringifyInstruction(instruction);
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var routeParamsGetter = $parse(link);
|
|
|
|
// we can avoid adding a watcher if it's a literal
|
|
|
|
if (routeParamsGetter.constant) {
|
|
|
|
var params = routeParamsGetter();
|
|
|
|
elt.attr('href', getLink(params));
|
|
|
|
} else {
|
|
|
|
scope.$watch(function () {
|
|
|
|
return routeParamsGetter(scope);
|
|
|
|
}, function (params) {
|
|
|
|
elt.attr('href', getLink(params));
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
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 18:53:50 -04:00
|
|
|
elt.on('click', function (event) {
|
|
|
|
if (event.which !== 1 || !instruction) {
|
2015-08-20 16:19:34 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 18:53:50 -04:00
|
|
|
$router.navigateByInstruction(instruction);
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
}
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function dashCase(str) {
|
|
|
|
return str.replace(/([A-Z])/g, function ($1) {
|
|
|
|
return '-' + $1.toLowerCase();
|
|
|
|
});
|
|
|
|
}
|