Skip to content

Commit 0f01c63

Browse files
MayaKirovaMKirovakdinevdamyanpetev
authored
refactor(grids): Remove ComponentFactory and ComponentFactoryResolver (#12860)
* Remove ComponentFactory and ComponentFactoryResolver. * Replace Component Factory with reflectComponentType. --------- Co-authored-by: MKirova <MKirova@DEV-MKIROVA> Co-authored-by: Konstantin Dinev <[email protected]> Co-authored-by: Damyan Petev <[email protected]>
1 parent b468f96 commit 0f01c63

File tree

10 files changed

+54
-92
lines changed

10 files changed

+54
-92
lines changed

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
AfterViewInit,
44
ChangeDetectorRef,
55
Component,
6-
ComponentFactoryResolver,
76
Directive,
87
Injectable,
98
IterableDiffers,
@@ -1336,7 +1335,6 @@ export class TestIgxForOfDirective<T> extends IgxForOfDirective<T> {
13361335
public viewContainer: ViewContainerRef,
13371336
public template: TemplateRef<NgForOfContext<T>>,
13381337
public differs: IterableDiffers,
1339-
public fResolver: ComponentFactoryResolver,
13401338
public changeDet: ChangeDetectorRef,
13411339
public zone: NgZone,
13421340
protected syncService: IgxForOfScrollSyncService,

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
AfterViewInit,
55
ApplicationRef,
66
ChangeDetectorRef,
7-
ComponentFactory,
8-
ComponentFactoryResolver,
97
ComponentRef,
108
ContentChild,
119
ContentChildren,
10+
createComponent,
1211
Directive,
1312
DoCheck,
1413
ElementRef,
14+
EnvironmentInjector,
1515
EventEmitter,
1616
HostBinding,
1717
HostListener,
@@ -21,7 +21,6 @@ import {
2121
IterableChangeRecord,
2222
IterableDiffers,
2323
LOCALE_ID,
24-
NgModuleRef,
2524
NgZone,
2625
OnDestroy,
2726
OnInit,
@@ -3351,12 +3350,11 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
33513350
protected zone: NgZone,
33523351
@Inject(DOCUMENT) public document: any,
33533352
public cdr: ChangeDetectorRef,
3354-
protected resolver: ComponentFactoryResolver,
33553353
protected differs: IterableDiffers,
33563354
protected viewRef: ViewContainerRef,
33573355
private appRef: ApplicationRef,
3358-
private moduleRef: NgModuleRef<any>,
3359-
private injector: Injector,
3356+
protected injector: Injector,
3357+
protected envInjector: EnvironmentInjector,
33603358
public navigation: IgxGridNavigationService,
33613359
public filteringService: IgxFilteringService,
33623360
@Inject(IgxOverlayService) protected overlayService: IgxOverlayService,
@@ -3835,26 +3833,8 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
38353833
}
38363834

38373835
private createComponentInstance(component: any) {
3838-
// TODO: create component instance view viewContainerRef.createComponent(Component, Settings) overload
3839-
let dynamicFactory: ComponentFactory<any>;
3840-
const factoryResolver = this.moduleRef
3841-
? this.moduleRef.componentFactoryResolver
3842-
: this.resolver;
3843-
try {
3844-
dynamicFactory = factoryResolver.resolveComponentFactory(component);
3845-
} catch (error) {
3846-
console.error(error);
3847-
return null;
3848-
}
3849-
3850-
const injector = this.moduleRef
3851-
? this.moduleRef.injector
3852-
: this.injector;
3853-
const dynamicComponent: ComponentRef<any> = dynamicFactory.create(
3854-
injector
3855-
);
3836+
const dynamicComponent: ComponentRef<any> = createComponent(component, { environmentInjector: this.envInjector, elementInjector: this.injector } );
38563837
this.appRef.attachView(dynamicComponent.hostView);
3857-
38583838
return dynamicComponent;
38593839
}
38603840

@@ -7156,12 +7136,11 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
71567136
*/
71577137
protected autogenerateColumns() {
71587138
const data = this.gridAPI.get_data();
7159-
const factory = this.resolver.resolveComponentFactory(IgxColumnComponent);
71607139
const fields = this.generateDataFields(data);
71617140
const columns = [];
71627141

71637142
fields.forEach((field) => {
7164-
const ref = factory.create(this.viewRef.injector);
7143+
const ref = createComponent(IgxColumnComponent, { environmentInjector: this.envInjector, elementInjector: this.injector});
71657144
ref.instance.field = field;
71667145
ref.instance.dataType = this.resolveDataTypes(data[0][field]);
71677146
ref.changeDetectorRef.detectChanges();

projects/igniteui-angular/src/lib/grids/hierarchical-grid/hierarchical-grid-base.directive.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import {
22
ApplicationRef,
33
ChangeDetectorRef,
4-
ComponentFactoryResolver,
4+
createComponent,
55
Directive,
66
ElementRef,
7+
EnvironmentInjector,
78
EventEmitter,
89
Inject,
910
Injector,
1011
Input,
1112
IterableDiffers,
1213
LOCALE_ID,
13-
NgModuleRef,
1414
NgZone,
1515
Optional,
1616
Output,
17+
reflectComponentType,
1718
TemplateRef,
1819
ViewChild,
1920
ViewContainerRef
@@ -158,12 +159,11 @@ export abstract class IgxHierarchicalGridBaseDirective extends IgxGridBaseDirect
158159
zone: NgZone,
159160
@Inject(DOCUMENT) public document,
160161
cdr: ChangeDetectorRef,
161-
resolver: ComponentFactoryResolver,
162162
differs: IterableDiffers,
163163
viewRef: ViewContainerRef,
164164
appRef: ApplicationRef,
165-
moduleRef: NgModuleRef<any>,
166165
injector: Injector,
166+
envInjector: EnvironmentInjector,
167167
navigation: IgxHierarchicalGridNavigationService,
168168
filteringService: IgxFilteringService,
169169
@Inject(IgxOverlayService) protected overlayService: IgxOverlayService,
@@ -182,12 +182,11 @@ export abstract class IgxHierarchicalGridBaseDirective extends IgxGridBaseDirect
182182
zone,
183183
document,
184184
cdr,
185-
resolver,
186185
differs,
187186
viewRef,
188187
appRef,
189-
moduleRef,
190188
injector,
189+
envInjector,
191190
navigation,
192191
filteringService,
193192
overlayService,
@@ -212,8 +211,8 @@ export abstract class IgxHierarchicalGridBaseDirective extends IgxGridBaseDirect
212211
this.updateColumns(result);
213212
this.initPinning();
214213

215-
const factoryColumn = this.resolver.resolveComponentFactory(IgxColumnComponent);
216-
const outputs = factoryColumn.outputs.filter(o => o.propName !== 'columnChange');
214+
const mirror = reflectComponentType(IgxColumnComponent);
215+
const outputs = mirror.outputs.filter(o => o.propName !== 'columnChange');
217216
outputs.forEach(output => {
218217
this.columns.forEach(column => {
219218
if (column[output.propName]) {
@@ -237,10 +236,10 @@ export abstract class IgxHierarchicalGridBaseDirective extends IgxGridBaseDirect
237236
}
238237

239238
protected _createColGroupComponent(col: IgxColumnGroupComponent) {
240-
const factoryGroup = this.resolver.resolveComponentFactory(IgxColumnGroupComponent);
241-
const ref = this.viewRef.createComponent(IgxColumnGroupComponent, { injector: this.viewRef.injector });
239+
const ref = createComponent(IgxColumnGroupComponent, { environmentInjector: this.envInjector, elementInjector: this.injector });
242240
ref.changeDetectorRef.detectChanges();
243-
factoryGroup.inputs.forEach((input) => {
241+
const mirror = reflectComponentType(IgxColumnGroupComponent);
242+
mirror.inputs.forEach((input) => {
244243
const propName = input.propName;
245244
ref.instance[propName] = col[propName];
246245
});
@@ -258,9 +257,9 @@ export abstract class IgxHierarchicalGridBaseDirective extends IgxGridBaseDirect
258257
}
259258

260259
protected _createColComponent(col) {
261-
const factoryColumn = this.resolver.resolveComponentFactory(IgxColumnComponent);
262-
const ref = this.viewRef.createComponent(IgxColumnComponent, { injector: this.viewRef.injector });
263-
factoryColumn.inputs.forEach((input) => {
260+
const ref = createComponent(IgxColumnComponent, { environmentInjector: this.envInjector, elementInjector: this.injector });
261+
const mirror = reflectComponentType(IgxColumnComponent);
262+
mirror.inputs.forEach((input) => {
264263
const propName = input.propName;
265264
if (!(col[propName] instanceof IgxSummaryOperand)) {
266265
ref.instance[propName] = col[propName];

projects/igniteui-angular/src/lib/grids/hierarchical-grid/hierarchical-grid.component.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
ChangeDetectionStrategy,
55
ChangeDetectorRef,
66
Component,
7-
ComponentFactoryResolver,
87
ContentChild,
98
ContentChildren,
109
DoCheck,
@@ -15,6 +14,7 @@ import {
1514
OnDestroy,
1615
OnInit,
1716
QueryList,
17+
reflectComponentType,
1818
SimpleChanges,
1919
TemplateRef,
2020
ViewChild,
@@ -153,7 +153,6 @@ export class IgxChildGridRowComponent implements AfterViewInit, OnInit {
153153
constructor(
154154
@Inject(IGX_GRID_SERVICE_BASE) public gridAPI: IgxHierarchicalGridAPIService,
155155
public element: ElementRef<HTMLElement>,
156-
private resolver: ComponentFactoryResolver,
157156
public cdr: ChangeDetectorRef) { }
158157

159158
/**
@@ -204,10 +203,10 @@ export class IgxChildGridRowComponent implements AfterViewInit, OnInit {
204203
private setupEventEmitters() {
205204
const destructor = takeUntil(this.hGrid.destroy$);
206205

207-
const factory = this.resolver.resolveComponentFactory(IgxGridComponent);
206+
const mirror = reflectComponentType(IgxGridComponent);
208207
// exclude outputs related to two-way binding functionality
209-
const inputNames = factory.inputs.map(input => input.propName);
210-
const outputs = factory.outputs.filter(o => {
208+
const inputNames = mirror.inputs.map(input => input.propName);
209+
const outputs = mirror.outputs.filter(o => {
211210
const matchingInputPropName = o.propName.slice(0, o.propName.indexOf('Change'));
212211
return inputNames.indexOf(matchingInputPropName) === -1;
213212
});

projects/igniteui-angular/src/lib/grids/hierarchical-grid/row-island.component.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
ChangeDetectionStrategy,
66
ChangeDetectorRef,
77
Component,
8-
ComponentFactoryResolver,
98
ContentChild,
109
ContentChildren,
1110
ElementRef,
11+
EnvironmentInjector,
1212
EventEmitter,
1313
forwardRef,
1414
Inject,
@@ -17,7 +17,6 @@ import {
1717
IterableChangeRecord,
1818
IterableDiffers,
1919
LOCALE_ID,
20-
NgModuleRef,
2120
NgZone,
2221
OnChanges,
2322
OnDestroy,
@@ -228,11 +227,10 @@ export class IgxRowIslandComponent extends IgxHierarchicalGridBaseDirective
228227
zone: NgZone,
229228
@Inject(DOCUMENT) public document,
230229
cdr: ChangeDetectorRef,
231-
resolver: ComponentFactoryResolver,
232230
differs: IterableDiffers,
233231
viewRef: ViewContainerRef,
234-
moduleRef: NgModuleRef<any>,
235232
injector: Injector,
233+
envInjector: EnvironmentInjector,
236234
appRef: ApplicationRef,
237235
navigation: IgxHierarchicalGridNavigationService,
238236
filteringService: IgxFilteringService,
@@ -252,12 +250,11 @@ export class IgxRowIslandComponent extends IgxHierarchicalGridBaseDirective
252250
zone,
253251
document,
254252
cdr,
255-
resolver,
256253
differs,
257254
viewRef,
258255
appRef,
259-
moduleRef,
260256
injector,
257+
envInjector,
261258
navigation,
262259
filteringService,
263260
overlayService,

0 commit comments

Comments
 (0)