-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhybrid-helper.ts
49 lines (38 loc) · 1.66 KB
/
hybrid-helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { downgradeComponent, downgradeInjectable } from '@angular/upgrade/static';
import { FactoryProvider } from '@angular/core';
export interface IComponentUpgradeOptions {
inputs?: string[],
outputs?: string[]
}
export interface IHybridHelper {
downgradeComponent(moduleName: string, componentSelector: string, componentClass: any, options?: IComponentUpgradeOptions): IHybridHelper,
downgradeProvider(moduleName: string, providerName: string, providerClass: any): IHybridHelper,
buildProviderForUpgrade(ng1Name: string, ng2Name?: string): FactoryProvider
}
export const HybridHelper: IHybridHelper {
downgradeComponent: (moduleName: string, componentName: string, componentClass: any, options?: IComponentUpgradeOptions): IHybridHelper => {
options = options || {};
const inputs = options.inputs || [];
const outputs = options.outputs || [];
const component = componentClass;
angular.module(moduleName).directive(componentName, downgradeComponent({
component, inputs, outputs
}) as angular.IDirectiveFactory);
return HybridHelper;
},
downgradeProvider: (moduleName: string, providerName: string, providerClass: any): IHybridHelper => {
angular.module(moduleName).factory(providerName, downgradeInjectable(providerClass));
return HybridHelper;
},
buildProviderForUpgrade: (ng1Name: string, ng2Name?: string): FactoryProvider => {
ng2Name = ng2Name || ng1Name;
return {
provide: ng2Name,
useFactory: buildFactoryForUpgradeProvider(ng1Name),
deps: ['$injector']
};
}
}
function buildFactoryForUpgradeProvider(ng1Name: string): Function {
return (injector: any) => injector.get(ng1Name);
}