2016-06-23 09:47:54 -07: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
|
|
|
|
|
*/
|
|
|
|
|
|
2017-01-19 13:04:24 +02:00
|
|
|
import {Compiler, CompilerOptions, Directive, Injector, NgModule, NgModuleRef, NgZone, Provider, Testability, Type} from '@angular/core';
|
2016-07-26 05:21:19 -07:00
|
|
|
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
import * as angular from '../common/angular1';
|
2017-01-19 13:04:24 +02:00
|
|
|
import {$$TESTABILITY, $COMPILE, $INJECTOR, $ROOT_SCOPE, COMPILER_KEY, INJECTOR_KEY, NG_ZONE_KEY} from '../common/constants';
|
|
|
|
|
import {downgradeComponent} from '../common/downgrade_component';
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
import {downgradeInjectable} from '../common/downgrade_injectable';
|
2017-01-19 13:04:24 +02:00
|
|
|
import {Deferred, controllerKey, onError} from '../common/util';
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
import {UpgradeNg1ComponentAdapterBuilder} from './upgrade_ng1_adapter';
|
2015-10-12 21:32:41 -07:00
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
let upgradeCount: number = 0;
|
2015-10-12 21:32:41 -07:00
|
|
|
|
|
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Use `UpgradeAdapter` to allow AngularJS and Angular to coexist in a single application.
|
2015-10-12 21:32:41 -07:00
|
|
|
*
|
|
|
|
|
* The `UpgradeAdapter` allows:
|
2017-01-26 22:30:42 -08:00
|
|
|
* 1. creation of Angular component from AngularJS component directive
|
2015-10-12 21:32:41 -07:00
|
|
|
* (See [UpgradeAdapter#upgradeNg1Component()])
|
2017-01-26 22:30:42 -08:00
|
|
|
* 2. creation of AngularJS directive from Angular component.
|
2015-10-12 21:32:41 -07:00
|
|
|
* (See [UpgradeAdapter#downgradeNg2Component()])
|
|
|
|
|
* 3. Bootstrapping of a hybrid Angular application which contains both of the frameworks
|
|
|
|
|
* coexisting in a single application.
|
|
|
|
|
*
|
|
|
|
|
* ## Mental Model
|
|
|
|
|
*
|
|
|
|
|
* When reasoning about how a hybrid application works it is useful to have a mental model which
|
|
|
|
|
* describes what is happening and explains what is happening at the lowest level.
|
|
|
|
|
*
|
|
|
|
|
* 1. There are two independent frameworks running in a single application, each framework treats
|
|
|
|
|
* the other as a black box.
|
|
|
|
|
* 2. Each DOM element on the page is owned exactly by one framework. Whichever framework
|
|
|
|
|
* instantiated the element is the owner. Each framework only updates/interacts with its own
|
|
|
|
|
* DOM elements and ignores others.
|
2017-01-26 22:30:42 -08:00
|
|
|
* 3. AngularJS directives always execute inside AngularJS framework codebase regardless of
|
2015-10-12 21:32:41 -07:00
|
|
|
* where they are instantiated.
|
2017-01-26 22:30:42 -08:00
|
|
|
* 4. Angular components always execute inside Angular framework codebase regardless of
|
2015-10-12 21:32:41 -07:00
|
|
|
* where they are instantiated.
|
2017-01-26 22:30:42 -08:00
|
|
|
* 5. An AngularJS component can be upgraded to an Angular component. This creates an
|
|
|
|
|
* Angular directive, which bootstraps the AngularJS component directive in that location.
|
|
|
|
|
* 6. An Angular component can be downgraded to an AngularJS component directive. This creates
|
|
|
|
|
* an AngularJS directive, which bootstraps the Angular component in that location.
|
2015-11-25 23:06:06 +01:00
|
|
|
* 7. Whenever an adapter component is instantiated the host element is owned by the framework
|
2015-10-12 21:32:41 -07:00
|
|
|
* doing the instantiation. The other framework then instantiates and owns the view for that
|
|
|
|
|
* component. This implies that component bindings will always follow the semantics of the
|
2017-01-26 22:30:42 -08:00
|
|
|
* instantiation framework. The syntax is always that of Angular syntax.
|
|
|
|
|
* 8. AngularJS is always bootstrapped first and owns the bottom most view.
|
|
|
|
|
* 9. The new application is running in Angular zone, and therefore it no longer needs calls to
|
2015-10-11 11:18:11 -07:00
|
|
|
* `$apply()`.
|
2015-10-12 21:32:41 -07:00
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-10-12 21:32:41 -07:00
|
|
|
*
|
|
|
|
|
* ```
|
2016-11-02 22:38:00 +00:00
|
|
|
* const adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module), myCompilerOptions);
|
|
|
|
|
* const module = angular.module('myExample', []);
|
2016-09-27 19:11:41 -04:00
|
|
|
* module.directive('ng2Comp', adapter.downgradeNg2Component(Ng2Component));
|
2015-10-12 21:32:41 -07:00
|
|
|
*
|
2016-08-05 13:32:04 -07:00
|
|
|
* module.directive('ng1Hello', function() {
|
2015-10-12 21:32:41 -07:00
|
|
|
* return {
|
2015-10-11 11:18:11 -07:00
|
|
|
* scope: { title: '=' },
|
2015-10-12 21:32:41 -07:00
|
|
|
* template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'
|
|
|
|
|
* };
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
2016-08-05 13:32:04 -07:00
|
|
|
* selector: 'ng2-comp',
|
2015-10-12 21:32:41 -07:00
|
|
|
* inputs: ['name'],
|
2016-08-05 13:32:04 -07:00
|
|
|
* template: 'ng2[<ng1-hello [title]="name">transclude</ng1-hello>](<ng-content></ng-content>)',
|
|
|
|
|
* directives:
|
2015-10-12 21:32:41 -07:00
|
|
|
* })
|
2016-08-05 13:32:04 -07:00
|
|
|
* class Ng2Component {
|
2015-10-12 21:32:41 -07:00
|
|
|
* }
|
|
|
|
|
*
|
2016-08-05 13:32:04 -07:00
|
|
|
* @NgModule({
|
|
|
|
|
* declarations: [Ng2Component, adapter.upgradeNg1Component('ng1Hello')],
|
|
|
|
|
* imports: [BrowserModule]
|
|
|
|
|
* })
|
|
|
|
|
* class MyNg2Module {}
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* document.body.innerHTML = '<ng2-comp name="World">project</ng2-comp>';
|
2015-10-12 21:32:41 -07:00
|
|
|
*
|
|
|
|
|
* adapter.bootstrap(document.body, ['myExample']).ready(function() {
|
|
|
|
|
* expect(document.body.textContent).toEqual(
|
|
|
|
|
* "ng2[ng1[Hello World!](transclude)](project)");
|
|
|
|
|
* });
|
2016-08-05 13:32:04 -07:00
|
|
|
*
|
2015-10-12 21:32:41 -07:00
|
|
|
* ```
|
2016-06-27 12:27:23 -07:00
|
|
|
*
|
2016-08-23 21:38:53 -07:00
|
|
|
* @stable
|
2015-10-12 21:32:41 -07:00
|
|
|
*/
|
|
|
|
|
export class UpgradeAdapter {
|
|
|
|
|
private idPrefix: string = `NG2_UPGRADE_${upgradeCount++}_`;
|
2017-01-19 13:04:24 +02:00
|
|
|
private downgradedComponents: Type<any>[] = [];
|
2016-08-05 13:32:04 -07:00
|
|
|
/**
|
|
|
|
|
* An internal map of ng1 components which need to up upgraded to ng2.
|
|
|
|
|
*
|
|
|
|
|
* We can't upgrade until injector is instantiated and we can retrieve the component metadata.
|
|
|
|
|
* For this reason we keep a list of components to upgrade until ng1 injector is bootstrapped.
|
|
|
|
|
*
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
private ng1ComponentsToBeUpgraded: {[name: string]: UpgradeNg1ComponentAdapterBuilder} = {};
|
2017-01-14 00:36:16 +02:00
|
|
|
private upgradedProviders: Provider[] = [];
|
2016-11-02 22:38:00 +00:00
|
|
|
private ngZone: NgZone;
|
|
|
|
|
private ng1Module: angular.IModule;
|
|
|
|
|
private moduleRef: NgModuleRef<any> = null;
|
|
|
|
|
private ng2BootstrapDeferred: Deferred<angular.IInjectorService>;
|
2015-10-12 21:32:41 -07:00
|
|
|
|
2016-10-11 18:48:08 -04:00
|
|
|
constructor(private ng2AppModule: Type<any>, private compilerOptions?: CompilerOptions) {
|
2016-08-19 13:51:45 -07:00
|
|
|
if (!ng2AppModule) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(
|
2017-01-26 22:30:42 -08:00
|
|
|
'UpgradeAdapter cannot be instantiated without an NgModule of the Angular app.');
|
2016-08-05 13:32:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-08-03 17:42:32 -07:00
|
|
|
|
2015-10-14 15:30:44 -07:00
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Allows Angular Component to be used from AngularJS.
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* Use `downgradeNg2Component` to create an AngularJS Directive Definition Factory from
|
|
|
|
|
* Angular Component. The adapter will bootstrap Angular component from within the
|
|
|
|
|
* AngularJS template.
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
|
|
|
|
* ## Mental Model
|
|
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* 1. The component is instantiated by being listed in AngularJS template. This means that the
|
|
|
|
|
* host element is controlled by AngularJS, but the component's view will be controlled by
|
|
|
|
|
* Angular.
|
|
|
|
|
* 2. Even thought the component is instantiated in AngularJS, it will be using Angular
|
|
|
|
|
* syntax. This has to be done, this way because we must follow Angular components do not
|
2015-10-14 15:30:44 -07:00
|
|
|
* declare how the attributes should be interpreted.
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
* 3. `ng-model` is controlled by AngularJS and communicates with the downgraded Angular component
|
|
|
|
|
* by way of the `ControlValueAccessor` interface from @angular/forms. Only components that
|
2017-01-23 14:23:45 -05:00
|
|
|
* implement this interface are eligible.
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
|
|
|
|
* ## Supported Features
|
|
|
|
|
*
|
|
|
|
|
* - Bindings:
|
|
|
|
|
* - Attribute: `<comp name="World">`
|
|
|
|
|
* - Interpolation: `<comp greeting="Hello {{name}}!">`
|
|
|
|
|
* - Expression: `<comp [name]="username">`
|
|
|
|
|
* - Event: `<comp (close)="doSomething()">`
|
2017-01-23 14:23:45 -05:00
|
|
|
* - ng-model: `<comp ng-model="name">`
|
2015-10-14 15:30:44 -07:00
|
|
|
* - Content projection: yes
|
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
|
|
|
|
* ```
|
2016-11-02 22:38:00 +00:00
|
|
|
* const adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module));
|
|
|
|
|
* const module = angular.module('myExample', []);
|
2015-10-14 15:30:44 -07:00
|
|
|
* module.directive('greet', adapter.downgradeNg2Component(Greeter));
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'greet',
|
|
|
|
|
* template: '{{salutation}} {{name}}! - <ng-content></ng-content>'
|
|
|
|
|
* })
|
|
|
|
|
* class Greeter {
|
|
|
|
|
* @Input() salutation: string;
|
|
|
|
|
* @Input() name: string;
|
|
|
|
|
* }
|
|
|
|
|
*
|
2016-08-05 13:32:04 -07:00
|
|
|
* @NgModule({
|
|
|
|
|
* declarations: [Greeter],
|
|
|
|
|
* imports: [BrowserModule]
|
|
|
|
|
* })
|
|
|
|
|
* class MyNg2Module {}
|
|
|
|
|
*
|
2015-10-14 15:30:44 -07:00
|
|
|
* document.body.innerHTML =
|
|
|
|
|
* 'ng1 template: <greet salutation="Hello" [name]="world">text</greet>';
|
|
|
|
|
*
|
|
|
|
|
* adapter.bootstrap(document.body, ['myExample']).ready(function() {
|
|
|
|
|
* expect(document.body.textContent).toEqual("ng1 template: Hello world! - text");
|
|
|
|
|
* });
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2017-01-19 13:04:24 +02:00
|
|
|
downgradeNg2Component(component: Type<any>): Function {
|
|
|
|
|
this.downgradedComponents.push(component);
|
|
|
|
|
|
2017-03-14 14:55:37 -07:00
|
|
|
return downgradeComponent({component});
|
2015-10-12 21:32:41 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 15:30:44 -07:00
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Allows AngularJS Component to be used from Angular.
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* Use `upgradeNg1Component` to create an Angular component from AngularJS Component
|
|
|
|
|
* directive. The adapter will bootstrap AngularJS component from within the Angular
|
2015-10-14 15:30:44 -07:00
|
|
|
* template.
|
|
|
|
|
*
|
|
|
|
|
* ## Mental Model
|
|
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* 1. The component is instantiated by being listed in Angular template. This means that the
|
|
|
|
|
* host element is controlled by Angular, but the component's view will be controlled by
|
|
|
|
|
* AngularJS.
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
|
|
|
|
* ## Supported Features
|
|
|
|
|
*
|
|
|
|
|
* - Bindings:
|
|
|
|
|
* - Attribute: `<comp name="World">`
|
|
|
|
|
* - Interpolation: `<comp greeting="Hello {{name}}!">`
|
|
|
|
|
* - Expression: `<comp [name]="username">`
|
|
|
|
|
* - Event: `<comp (close)="doSomething()">`
|
|
|
|
|
* - Transclusion: yes
|
|
|
|
|
* - Only some of the features of
|
|
|
|
|
* [Directive Definition Object](https://docs.angularjs.org/api/ng/service/$compile) are
|
|
|
|
|
* supported:
|
2017-01-26 22:30:42 -08:00
|
|
|
* - `compile`: not supported because the host element is owned by Angular, which does
|
2015-10-14 15:30:44 -07:00
|
|
|
* not allow modifying DOM structure during compilation.
|
|
|
|
|
* - `controller`: supported. (NOTE: injection of `$attrs` and `$transclude` is not supported.)
|
2016-12-22 13:36:24 -07:00
|
|
|
* - `controllerAs`: supported.
|
|
|
|
|
* - `bindToController`: supported.
|
|
|
|
|
* - `link`: supported. (NOTE: only pre-link function is supported.)
|
|
|
|
|
* - `name`: supported.
|
|
|
|
|
* - `priority`: ignored.
|
|
|
|
|
* - `replace`: not supported.
|
2015-10-14 15:30:44 -07:00
|
|
|
* - `require`: supported.
|
|
|
|
|
* - `restrict`: must be set to 'E'.
|
|
|
|
|
* - `scope`: supported.
|
|
|
|
|
* - `template`: supported.
|
|
|
|
|
* - `templateUrl`: supported.
|
|
|
|
|
* - `terminal`: ignored.
|
|
|
|
|
* - `transclude`: supported.
|
|
|
|
|
*
|
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
|
|
|
|
* ```
|
2016-11-02 22:38:00 +00:00
|
|
|
* const adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module));
|
|
|
|
|
* const module = angular.module('myExample', []);
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
|
|
|
|
* module.directive('greet', function() {
|
|
|
|
|
* return {
|
|
|
|
|
* scope: {salutation: '=', name: '=' },
|
|
|
|
|
* template: '{{salutation}} {{name}}! - <span ng-transclude></span>'
|
|
|
|
|
* };
|
|
|
|
|
* });
|
|
|
|
|
*
|
2016-09-27 19:11:41 -04:00
|
|
|
* module.directive('ng2', adapter.downgradeNg2Component(Ng2Component));
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'ng2',
|
|
|
|
|
* template: 'ng2 template: <greet salutation="Hello" [name]="world">text</greet>'
|
|
|
|
|
* })
|
2016-09-27 19:11:41 -04:00
|
|
|
* class Ng2Component {
|
2015-10-14 15:30:44 -07:00
|
|
|
* }
|
|
|
|
|
*
|
2016-08-05 13:32:04 -07:00
|
|
|
* @NgModule({
|
2016-09-27 19:11:41 -04:00
|
|
|
* declarations: [Ng2Component, adapter.upgradeNg1Component('greet')],
|
2016-08-05 13:32:04 -07:00
|
|
|
* imports: [BrowserModule]
|
|
|
|
|
* })
|
|
|
|
|
* class MyNg2Module {}
|
|
|
|
|
*
|
2015-10-14 15:30:44 -07:00
|
|
|
* document.body.innerHTML = '<ng2></ng2>';
|
|
|
|
|
*
|
|
|
|
|
* adapter.bootstrap(document.body, ['myExample']).ready(function() {
|
|
|
|
|
* expect(document.body.textContent).toEqual("ng2 template: Hello world! - text");
|
|
|
|
|
* });
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2016-08-10 18:21:28 -07:00
|
|
|
upgradeNg1Component(name: string): Type<any> {
|
2016-08-05 13:32:04 -07:00
|
|
|
if ((<any>this.ng1ComponentsToBeUpgraded).hasOwnProperty(name)) {
|
|
|
|
|
return this.ng1ComponentsToBeUpgraded[name].type;
|
2015-10-12 21:32:41 -07:00
|
|
|
} else {
|
2016-08-05 13:32:04 -07:00
|
|
|
return (this.ng1ComponentsToBeUpgraded[name] = new UpgradeNg1ComponentAdapterBuilder(name))
|
|
|
|
|
.type;
|
2015-10-12 21:32:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 22:25:13 +00:00
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Registers the adapter's AngularJS upgrade module for unit testing in AngularJS.
|
2016-11-03 22:25:13 +00:00
|
|
|
* Use this instead of `angular.mock.module()` to load the upgrade module into
|
2017-01-26 22:30:42 -08:00
|
|
|
* the AngularJS testing injector.
|
2016-11-03 22:25:13 +00:00
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
2016-12-08 07:29:04 +00:00
|
|
|
* const upgradeAdapter = new UpgradeAdapter(MyNg2Module);
|
2016-11-03 22:25:13 +00:00
|
|
|
*
|
|
|
|
|
* // configure the adapter with upgrade/downgrade components and services
|
|
|
|
|
* upgradeAdapter.downgradeNg2Component(MyComponent);
|
|
|
|
|
*
|
|
|
|
|
* let upgradeAdapterRef: UpgradeAdapterRef;
|
|
|
|
|
* let $compile, $rootScope;
|
|
|
|
|
*
|
|
|
|
|
* // We must register the adapter before any calls to `inject()`
|
|
|
|
|
* beforeEach(() => {
|
|
|
|
|
* upgradeAdapterRef = upgradeAdapter.registerForNg1Tests(['heroApp']);
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* beforeEach(inject((_$compile_, _$rootScope_) => {
|
|
|
|
|
* $compile = _$compile_;
|
|
|
|
|
* $rootScope = _$rootScope_;
|
|
|
|
|
* }));
|
|
|
|
|
*
|
|
|
|
|
* it("says hello", (done) => {
|
|
|
|
|
* upgradeAdapterRef.ready(() => {
|
|
|
|
|
* const element = $compile("<my-component></my-component>")($rootScope);
|
|
|
|
|
* $rootScope.$apply();
|
|
|
|
|
* expect(element.html()).toContain("Hello World");
|
|
|
|
|
* done();
|
|
|
|
|
* })
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* @param modules any AngularJS modules that the upgrade module should depend upon
|
2016-11-03 22:25:13 +00:00
|
|
|
* @returns an {@link UpgradeAdapterRef}, which lets you register a `ready()` callback to
|
2017-01-26 22:30:42 -08:00
|
|
|
* run assertions once the Angular components are ready to test through AngularJS.
|
2016-11-03 22:25:13 +00:00
|
|
|
*/
|
|
|
|
|
registerForNg1Tests(modules?: string[]): UpgradeAdapterRef {
|
|
|
|
|
const windowNgMock = (window as any)['angular'].mock;
|
|
|
|
|
if (!windowNgMock || !windowNgMock.module) {
|
|
|
|
|
throw new Error('Failed to find \'angular.mock.module\'.');
|
|
|
|
|
}
|
|
|
|
|
this.declareNg1Module(modules);
|
|
|
|
|
windowNgMock.module(this.ng1Module.name);
|
|
|
|
|
const upgrade = new UpgradeAdapterRef();
|
|
|
|
|
this.ng2BootstrapDeferred.promise.then(
|
2016-12-17 01:14:16 +02:00
|
|
|
(ng1Injector) => { (<any>upgrade)._bootstrapDone(this.moduleRef, ng1Injector); }, onError);
|
2016-11-03 22:25:13 +00:00
|
|
|
return upgrade;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 15:30:44 -07:00
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Bootstrap a hybrid AngularJS / Angular application.
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* This `bootstrap` method is a direct replacement (takes same arguments) for AngularJS
|
2015-10-14 15:30:44 -07:00
|
|
|
* [`bootstrap`](https://docs.angularjs.org/api/ng/function/angular.bootstrap) method. Unlike
|
2017-01-26 22:30:42 -08:00
|
|
|
* AngularJS, this bootstrap is asynchronous.
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-10-14 15:30:44 -07:00
|
|
|
*
|
|
|
|
|
* ```
|
2016-12-08 07:29:04 +00:00
|
|
|
* const adapter = new UpgradeAdapter(MyNg2Module);
|
2016-11-02 22:38:00 +00:00
|
|
|
* const module = angular.module('myExample', []);
|
2015-10-14 15:30:44 -07:00
|
|
|
* module.directive('ng2', adapter.downgradeNg2Component(Ng2));
|
|
|
|
|
*
|
|
|
|
|
* module.directive('ng1', function() {
|
|
|
|
|
* return {
|
|
|
|
|
* scope: { title: '=' },
|
|
|
|
|
* template: 'ng1[Hello {{title}}!](<span ng-transclude></span>)'
|
|
|
|
|
* };
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @Component({
|
|
|
|
|
* selector: 'ng2',
|
|
|
|
|
* inputs: ['name'],
|
2016-08-05 13:32:04 -07:00
|
|
|
* template: 'ng2[<ng1 [title]="name">transclude</ng1>](<ng-content></ng-content>)'
|
2015-10-14 15:30:44 -07:00
|
|
|
* })
|
|
|
|
|
* class Ng2 {
|
|
|
|
|
* }
|
|
|
|
|
*
|
2016-08-05 13:32:04 -07:00
|
|
|
* @NgModule({
|
|
|
|
|
* declarations: [Ng2, adapter.upgradeNg1Component('ng1')],
|
|
|
|
|
* imports: [BrowserModule]
|
|
|
|
|
* })
|
|
|
|
|
* class MyNg2Module {}
|
|
|
|
|
*
|
2015-10-14 15:30:44 -07:00
|
|
|
* document.body.innerHTML = '<ng2 name="World">project</ng2>';
|
|
|
|
|
*
|
|
|
|
|
* adapter.bootstrap(document.body, ['myExample']).ready(function() {
|
|
|
|
|
* expect(document.body.textContent).toEqual(
|
|
|
|
|
* "ng2[ng1[Hello World!](transclude)](project)");
|
|
|
|
|
* });
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2016-06-08 16:38:52 -07:00
|
|
|
bootstrap(element: Element, modules?: any[], config?: angular.IAngularBootstrapConfig):
|
|
|
|
|
UpgradeAdapterRef {
|
2016-11-02 22:38:00 +00:00
|
|
|
this.declareNg1Module(modules);
|
|
|
|
|
|
2016-11-12 14:08:58 +01:00
|
|
|
const upgrade = new UpgradeAdapterRef();
|
2016-11-02 22:38:00 +00:00
|
|
|
|
|
|
|
|
// Make sure resumeBootstrap() only exists if the current bootstrap is deferred
|
|
|
|
|
const windowAngular = (window as any /** TODO #???? */)['angular'];
|
|
|
|
|
windowAngular.resumeBootstrap = undefined;
|
|
|
|
|
|
|
|
|
|
this.ngZone.run(() => { angular.bootstrap(element, [this.ng1Module.name], config); });
|
|
|
|
|
const ng1BootstrapPromise = new Promise((resolve) => {
|
|
|
|
|
if (windowAngular.resumeBootstrap) {
|
|
|
|
|
const originalResumeBootstrap: () => void = windowAngular.resumeBootstrap;
|
|
|
|
|
windowAngular.resumeBootstrap = function() {
|
|
|
|
|
windowAngular.resumeBootstrap = originalResumeBootstrap;
|
|
|
|
|
windowAngular.resumeBootstrap.apply(this, arguments);
|
|
|
|
|
resolve();
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Promise.all([this.ng2BootstrapDeferred.promise, ng1BootstrapPromise]).then(([ng1Injector]) => {
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
angular.element(element).data(controllerKey(INJECTOR_KEY), this.moduleRef.injector);
|
2016-11-02 22:38:00 +00:00
|
|
|
this.moduleRef.injector.get(NgZone).run(
|
|
|
|
|
() => { (<any>upgrade)._bootstrapDone(this.moduleRef, ng1Injector); });
|
|
|
|
|
}, onError);
|
|
|
|
|
return upgrade;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Allows AngularJS service to be accessible from Angular.
|
2016-11-02 22:38:00 +00:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* class Login { ... }
|
|
|
|
|
* class Server { ... }
|
|
|
|
|
*
|
|
|
|
|
* @Injectable()
|
|
|
|
|
* class Example {
|
|
|
|
|
* constructor(@Inject('server') server, login: Login) {
|
|
|
|
|
* ...
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* const module = angular.module('myExample', []);
|
|
|
|
|
* module.service('server', Server);
|
|
|
|
|
* module.service('login', Login);
|
|
|
|
|
*
|
2016-12-08 07:29:04 +00:00
|
|
|
* const adapter = new UpgradeAdapter(MyNg2Module);
|
2016-11-02 22:38:00 +00:00
|
|
|
* adapter.upgradeNg1Provider('server');
|
|
|
|
|
* adapter.upgradeNg1Provider('login', {asToken: Login});
|
|
|
|
|
*
|
|
|
|
|
* adapter.bootstrap(document.body, ['myExample']).ready((ref) => {
|
|
|
|
|
* const example: Example = ref.ng2Injector.get(Example);
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2017-01-14 00:36:16 +02:00
|
|
|
upgradeNg1Provider(name: string, options?: {asToken: any}) {
|
2016-11-02 22:38:00 +00:00
|
|
|
const token = options && options.asToken || name;
|
2017-01-14 00:36:16 +02:00
|
|
|
this.upgradedProviders.push({
|
2016-11-02 22:38:00 +00:00
|
|
|
provide: token,
|
2017-01-14 00:36:16 +02:00
|
|
|
useFactory: ($injector: angular.IInjectorService) => $injector.get(name),
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
deps: [$INJECTOR]
|
2016-11-02 22:38:00 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Allows Angular service to be accessible from AngularJS.
|
2016-11-02 22:38:00 +00:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* class Example {
|
|
|
|
|
* }
|
|
|
|
|
*
|
2016-12-08 07:29:04 +00:00
|
|
|
* const adapter = new UpgradeAdapter(MyNg2Module);
|
2016-11-02 22:38:00 +00:00
|
|
|
*
|
|
|
|
|
* const module = angular.module('myExample', []);
|
|
|
|
|
* module.factory('example', adapter.downgradeNg2Provider(Example));
|
|
|
|
|
*
|
|
|
|
|
* adapter.bootstrap(document.body, ['myExample']).ready((ref) => {
|
|
|
|
|
* const example: Example = ref.ng1Injector.get('example');
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
downgradeNg2Provider(token: any): Function { return downgradeInjectable(token); }
|
2016-11-02 22:38:00 +00:00
|
|
|
|
|
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Declare the AngularJS upgrade module for this adapter without bootstrapping the whole
|
2016-11-02 22:38:00 +00:00
|
|
|
* hybrid application.
|
|
|
|
|
*
|
2016-11-03 22:25:13 +00:00
|
|
|
* This method is automatically called by `bootstrap()` and `registerForNg1Tests()`.
|
2016-11-02 22:38:00 +00:00
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* @param modules The AngularJS modules that this upgrade module should depend upon.
|
|
|
|
|
* @returns The AngularJS upgrade module that is declared by this method
|
2016-11-02 22:38:00 +00:00
|
|
|
*
|
|
|
|
|
* ### Example
|
|
|
|
|
*
|
|
|
|
|
* ```
|
2016-12-08 07:29:04 +00:00
|
|
|
* const upgradeAdapter = new UpgradeAdapter(MyNg2Module);
|
2016-11-02 22:38:00 +00:00
|
|
|
* upgradeAdapter.declareNg1Module(['heroApp']);
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
private declareNg1Module(modules: string[] = []): angular.IModule {
|
2016-11-12 14:08:58 +01:00
|
|
|
const delayApplyExps: Function[] = [];
|
|
|
|
|
let original$applyFn: Function;
|
|
|
|
|
let rootScopePrototype: any;
|
|
|
|
|
let rootScope: angular.IRootScopeService;
|
2016-11-02 22:38:00 +00:00
|
|
|
const upgradeAdapter = this;
|
|
|
|
|
const ng1Module = this.ng1Module = angular.module(this.idPrefix, modules);
|
|
|
|
|
const platformRef = platformBrowserDynamic();
|
|
|
|
|
|
|
|
|
|
this.ngZone = new NgZone({enableLongStackTrace: Zone.hasOwnProperty('longStackTraceZoneSpec')});
|
|
|
|
|
this.ng2BootstrapDeferred = new Deferred();
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
ng1Module.factory(INJECTOR_KEY, () => this.moduleRef.injector.get(Injector))
|
|
|
|
|
.constant(NG_ZONE_KEY, this.ngZone)
|
|
|
|
|
.factory(COMPILER_KEY, () => this.moduleRef.injector.get(Compiler))
|
2015-10-12 21:32:41 -07:00
|
|
|
.config([
|
2016-06-08 16:38:52 -07:00
|
|
|
'$provide', '$injector',
|
2016-11-02 20:33:55 +00:00
|
|
|
(provide: angular.IProvideService, ng1Injector: angular.IInjectorService) => {
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
provide.decorator($ROOT_SCOPE, [
|
2015-10-12 21:32:41 -07:00
|
|
|
'$delegate',
|
|
|
|
|
function(rootScopeDelegate: angular.IRootScopeService) {
|
2016-08-05 13:32:04 -07:00
|
|
|
// Capture the root apply so that we can delay first call to $apply until we
|
2017-01-26 22:30:42 -08:00
|
|
|
// bootstrap Angular and then we replay and restore the $apply.
|
2015-10-12 21:32:41 -07:00
|
|
|
rootScopePrototype = rootScopeDelegate.constructor.prototype;
|
|
|
|
|
if (rootScopePrototype.hasOwnProperty('$apply')) {
|
|
|
|
|
original$applyFn = rootScopePrototype.$apply;
|
2016-08-05 13:32:04 -07:00
|
|
|
rootScopePrototype.$apply = (exp: any) => delayApplyExps.push(exp);
|
2015-10-12 21:32:41 -07:00
|
|
|
} else {
|
2016-06-08 16:38:52 -07:00
|
|
|
throw new Error('Failed to find \'$apply\' on \'$rootScope\'!');
|
2015-10-12 21:32:41 -07:00
|
|
|
}
|
|
|
|
|
return rootScope = rootScopeDelegate;
|
|
|
|
|
}
|
|
|
|
|
]);
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
if (ng1Injector.has($$TESTABILITY)) {
|
|
|
|
|
provide.decorator($$TESTABILITY, [
|
2016-05-26 14:58:47 -04:00
|
|
|
'$delegate',
|
|
|
|
|
function(testabilityDelegate: angular.ITestabilityService) {
|
2016-11-12 14:08:58 +01:00
|
|
|
const originalWhenStable: Function = testabilityDelegate.whenStable;
|
2016-11-18 14:30:47 -08:00
|
|
|
// Cannot use arrow function below because we need the context
|
|
|
|
|
const newWhenStable = function(callback: Function) {
|
2016-08-05 13:32:04 -07:00
|
|
|
originalWhenStable.call(this, function() {
|
2016-11-02 22:38:00 +00:00
|
|
|
const ng2Testability: Testability =
|
|
|
|
|
upgradeAdapter.moduleRef.injector.get(Testability);
|
2016-05-26 14:58:47 -04:00
|
|
|
if (ng2Testability.isStable()) {
|
|
|
|
|
callback.apply(this, arguments);
|
|
|
|
|
} else {
|
2016-11-18 14:30:47 -08:00
|
|
|
ng2Testability.whenStable(newWhenStable.bind(this, callback));
|
2016-05-26 14:58:47 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-03-14 14:36:41 -07:00
|
|
|
|
2016-05-26 14:58:47 -04:00
|
|
|
testabilityDelegate.whenStable = newWhenStable;
|
|
|
|
|
return testabilityDelegate;
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
}
|
2015-10-12 21:32:41 -07:00
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
2016-11-03 14:48:11 +00:00
|
|
|
ng1Module.run([
|
|
|
|
|
'$injector', '$rootScope',
|
2016-11-02 22:38:00 +00:00
|
|
|
(ng1Injector: angular.IInjectorService, rootScope: angular.IRootScopeService) => {
|
|
|
|
|
UpgradeNg1ComponentAdapterBuilder.resolve(this.ng1ComponentsToBeUpgraded, ng1Injector)
|
2016-11-03 14:48:11 +00:00
|
|
|
.then(() => {
|
|
|
|
|
// At this point we have ng1 injector and we have lifted ng1 components into ng2, we
|
|
|
|
|
// now can bootstrap ng2.
|
|
|
|
|
const DynamicNgUpgradeModule =
|
|
|
|
|
NgModule({
|
|
|
|
|
providers: [
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
{provide: $INJECTOR, useFactory: () => ng1Injector},
|
|
|
|
|
{provide: $COMPILE, useFactory: () => ng1Injector.get($COMPILE)},
|
2017-01-14 00:36:16 +02:00
|
|
|
this.upgradedProviders
|
2016-11-03 14:48:11 +00:00
|
|
|
],
|
2017-01-19 13:04:24 +02:00
|
|
|
imports: [this.ng2AppModule],
|
|
|
|
|
entryComponents: this.downgradedComponents
|
2016-11-03 14:48:11 +00:00
|
|
|
}).Class({
|
|
|
|
|
constructor: function DynamicNgUpgradeModule() {},
|
|
|
|
|
ngDoBootstrap: function() {}
|
|
|
|
|
});
|
2016-11-02 22:38:00 +00:00
|
|
|
(platformRef as any)
|
2016-11-03 14:48:11 +00:00
|
|
|
._bootstrapModuleWithZone(
|
2017-01-19 13:04:24 +02:00
|
|
|
DynamicNgUpgradeModule, this.compilerOptions, this.ngZone)
|
2016-11-03 14:48:11 +00:00
|
|
|
.then((ref: NgModuleRef<any>) => {
|
2016-11-02 22:38:00 +00:00
|
|
|
this.moduleRef = ref;
|
|
|
|
|
this.ngZone.run(() => {
|
|
|
|
|
if (rootScopePrototype) {
|
|
|
|
|
rootScopePrototype.$apply = original$applyFn; // restore original $apply
|
|
|
|
|
while (delayApplyExps.length) {
|
|
|
|
|
rootScope.$apply(delayApplyExps.shift());
|
|
|
|
|
}
|
|
|
|
|
rootScopePrototype = null;
|
|
|
|
|
}
|
2016-11-03 14:48:11 +00:00
|
|
|
});
|
|
|
|
|
})
|
2017-01-13 20:19:57 +00:00
|
|
|
.then(() => this.ng2BootstrapDeferred.resolve(ng1Injector), onError)
|
|
|
|
|
.then(() => {
|
|
|
|
|
let subscription =
|
|
|
|
|
this.ngZone.onMicrotaskEmpty.subscribe({next: () => rootScope.$digest()});
|
|
|
|
|
rootScope.$on('$destroy', () => { subscription.unsubscribe(); });
|
|
|
|
|
});
|
2016-11-03 14:48:11 +00:00
|
|
|
})
|
2016-11-02 22:38:00 +00:00
|
|
|
.catch((e) => this.ng2BootstrapDeferred.reject(e));
|
|
|
|
|
}
|
|
|
|
|
]);
|
2016-03-14 14:36:41 -07:00
|
|
|
|
2016-11-02 22:38:00 +00:00
|
|
|
return ng1Module;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-14 14:36:41 -07:00
|
|
|
|
2016-11-02 22:38:00 +00:00
|
|
|
/**
|
|
|
|
|
* Synchronous promise-like object to wrap parent injectors,
|
2017-01-26 22:30:42 -08:00
|
|
|
* to preserve the synchronous nature of AngularJS's $compile.
|
2016-11-02 22:38:00 +00:00
|
|
|
*/
|
|
|
|
|
class ParentInjectorPromise {
|
|
|
|
|
private injector: Injector;
|
|
|
|
|
private callbacks: ((injector: Injector) => any)[] = [];
|
|
|
|
|
|
|
|
|
|
constructor(private element: angular.IAugmentedJQuery) {
|
|
|
|
|
// store the promise on the element
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
element.data(controllerKey(INJECTOR_KEY), this);
|
2015-10-12 21:32:41 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-02 22:38:00 +00:00
|
|
|
then(callback: (injector: Injector) => any) {
|
|
|
|
|
if (this.injector) {
|
|
|
|
|
callback(this.injector);
|
|
|
|
|
} else {
|
|
|
|
|
this.callbacks.push(callback);
|
|
|
|
|
}
|
2015-10-15 12:04:19 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-02 22:38:00 +00:00
|
|
|
resolve(injector: Injector) {
|
|
|
|
|
this.injector = injector;
|
|
|
|
|
|
|
|
|
|
// reset the element data to point to the real injector
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
this.element.data(controllerKey(INJECTOR_KEY), injector);
|
2016-11-02 22:38:00 +00:00
|
|
|
|
|
|
|
|
// clean out the element to prevent memory leaks
|
|
|
|
|
this.element = null;
|
|
|
|
|
|
|
|
|
|
// run all the queued callbacks
|
|
|
|
|
this.callbacks.forEach((callback) => callback(injector));
|
|
|
|
|
this.callbacks.length = 0;
|
2015-10-15 12:04:19 -07:00
|
|
|
}
|
2015-10-12 21:32:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-10-11 11:18:11 -07:00
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Use `UpgradeAdapterRef` to control a hybrid AngularJS / Angular application.
|
2016-06-27 12:27:23 -07:00
|
|
|
*
|
2016-08-23 21:38:53 -07:00
|
|
|
* @stable
|
2015-10-11 11:18:11 -07:00
|
|
|
*/
|
|
|
|
|
export class UpgradeAdapterRef {
|
|
|
|
|
/* @internal */
|
|
|
|
|
private _readyFn: (upgradeAdapterRef?: UpgradeAdapterRef) => void = null;
|
2015-10-12 21:32:41 -07:00
|
|
|
|
2015-10-15 12:04:19 -07:00
|
|
|
public ng1RootScope: angular.IRootScopeService = null;
|
2015-10-26 20:17:46 -07:00
|
|
|
public ng1Injector: angular.IInjectorService = null;
|
2016-08-02 02:32:27 -07:00
|
|
|
public ng2ModuleRef: NgModuleRef<any> = null;
|
2015-10-15 12:04:19 -07:00
|
|
|
public ng2Injector: Injector = null;
|
2015-10-11 11:18:11 -07:00
|
|
|
|
|
|
|
|
/* @internal */
|
2016-08-02 02:32:27 -07:00
|
|
|
private _bootstrapDone(ngModuleRef: NgModuleRef<any>, ng1Injector: angular.IInjectorService) {
|
|
|
|
|
this.ng2ModuleRef = ngModuleRef;
|
|
|
|
|
this.ng2Injector = ngModuleRef.injector;
|
2015-10-11 11:18:11 -07:00
|
|
|
this.ng1Injector = ng1Injector;
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
this.ng1RootScope = ng1Injector.get($ROOT_SCOPE);
|
2015-10-11 11:18:11 -07:00
|
|
|
this._readyFn && this._readyFn(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Register a callback function which is notified upon successful hybrid AngularJS / Angular
|
2015-10-11 11:18:11 -07:00
|
|
|
* application has been bootstrapped.
|
|
|
|
|
*
|
2017-01-26 22:30:42 -08:00
|
|
|
* The `ready` callback function is invoked inside the Angular zone, therefore it does not
|
2015-10-11 11:18:11 -07:00
|
|
|
* require a call to `$apply()`.
|
|
|
|
|
*/
|
|
|
|
|
public ready(fn: (upgradeAdapterRef?: UpgradeAdapterRef) => void) { this._readyFn = fn; }
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-26 22:30:42 -08:00
|
|
|
* Dispose of running hybrid AngularJS / Angular application.
|
2015-10-11 11:18:11 -07:00
|
|
|
*/
|
|
|
|
|
public dispose() {
|
feat(upgrade): return a function (instead of array) from `downgradeInjectable()` (#14037)
This makes it more consistent with the dynamic version of `upgrade` and makes it
possible to share code between the dynamic and static versions.
This commit also refactors the file layout, moving common and dynamic-specific
files to `common/` and `dynamic/` directories respectively and renaming `aot/`
to `static/`.
Some private keys, used as AngularJS DI tokens, have also been renamed, but this
should not affect apps, since these keys are undocumented and not supposed to
be used externally.
BREAKING CHANGE:
Previously, `upgrade/static/downgradeInjectable` returned an array of the form:
```js
['dep1', 'dep2', ..., function factory(dep1, dep2, ...) { ... }]
```
Now it returns a function with an `$inject` property:
```js
factory.$inject = ['dep1', 'dep2', ...];
function factory(dep1, dep2, ...) { ... }
```
It shouldn't affect the behavior of apps, since both forms are equally suitable
to be used for registering AngularJS injectable services, but it is possible
that type-checking might fail or that current code breaks if it relies on the
returned value being an array.
2017-01-13 16:20:28 +02:00
|
|
|
this.ng1Injector.get($ROOT_SCOPE).$destroy();
|
2016-08-02 02:32:27 -07:00
|
|
|
this.ng2ModuleRef.destroy();
|
2015-10-11 11:18:11 -07:00
|
|
|
}
|
2015-10-12 21:32:41 -07:00
|
|
|
}
|