1
- /* eslint-disable deprecation/deprecation */
2
1
import { DOCUMENT } from '@angular/common' ;
3
2
import {
4
3
ApplicationRef ,
5
- ComponentFactoryResolver ,
6
4
ComponentRef ,
5
+ createComponent ,
7
6
EventEmitter ,
8
7
Inject ,
9
8
Injectable ,
10
9
Injector ,
11
10
NgZone ,
12
- TemplateRef
11
+ TemplateRef ,
12
+ Type
13
13
} from '@angular/core' ;
14
14
import { finalize } from 'rxjs/operators' ;
15
15
import { Subject } from 'rxjs' ;
@@ -58,8 +58,7 @@ export class NgbOffcanvasStack {
58
58
}
59
59
}
60
60
61
- open ( moduleCFR : ComponentFactoryResolver , contentInjector : Injector , content : any , options : NgbOffcanvasOptions ) :
62
- NgbOffcanvasRef {
61
+ open ( contentInjector : Injector , content : any , options : NgbOffcanvasOptions ) : NgbOffcanvasRef {
63
62
const containerEl = options . container instanceof HTMLElement ? options . container : isDefined ( options . container ) ?
64
63
this . _document . querySelector ( options . container ) :
65
64
this . _document . body ;
@@ -72,11 +71,11 @@ export class NgbOffcanvasStack {
72
71
}
73
72
74
73
const activeOffcanvas = new NgbActiveOffcanvas ( ) ;
75
- const contentRef = this . _getContentRef ( moduleCFR , options . injector || contentInjector , content , activeOffcanvas ) ;
74
+ const contentRef = this . _getContentRef ( options . injector || contentInjector , content , activeOffcanvas ) ;
76
75
77
76
let backdropCmptRef : ComponentRef < NgbOffcanvasBackdrop > | undefined =
78
- options . backdrop !== false ? this . _attachBackdrop ( moduleCFR , containerEl ) : undefined ;
79
- let panelCmptRef : ComponentRef < NgbOffcanvasPanel > = this . _attachWindowComponent ( moduleCFR , containerEl , contentRef ) ;
77
+ options . backdrop !== false ? this . _attachBackdrop ( containerEl ) : undefined ;
78
+ let panelCmptRef : ComponentRef < NgbOffcanvasPanel > = this . _attachWindowComponent ( containerEl , contentRef . nodes ) ;
80
79
let ngbOffcanvasRef : NgbOffcanvasRef =
81
80
new NgbOffcanvasRef ( panelCmptRef , contentRef , backdropCmptRef , options . beforeDismiss ) ;
82
81
@@ -102,18 +101,18 @@ export class NgbOffcanvasStack {
102
101
103
102
hasOpenOffcanvas ( ) : boolean { return ! ! this . _offcanvasRef ; }
104
103
105
- private _attachBackdrop ( moduleCFR : ComponentFactoryResolver , containerEl : any ) : ComponentRef < NgbOffcanvasBackdrop > {
106
- let backdropFactory = moduleCFR . resolveComponentFactory ( NgbOffcanvasBackdrop ) ;
107
- let backdropCmptRef = backdropFactory . create ( this . _injector ) ;
104
+ private _attachBackdrop ( containerEl : Element ) : ComponentRef < NgbOffcanvasBackdrop > {
105
+ let backdropCmptRef = createComponent (
106
+ NgbOffcanvasBackdrop , { environmentInjector : this . _applicationRef . injector , elementInjector : this . _injector } ) ;
108
107
this . _applicationRef . attachView ( backdropCmptRef . hostView ) ;
109
108
containerEl . appendChild ( backdropCmptRef . location . nativeElement ) ;
110
109
return backdropCmptRef ;
111
110
}
112
111
113
- private _attachWindowComponent ( moduleCFR : ComponentFactoryResolver , containerEl : any , contentRef : any ) :
114
- ComponentRef < NgbOffcanvasPanel > {
115
- let panelFactory = moduleCFR . resolveComponentFactory ( NgbOffcanvasPanel ) ;
116
- let panelCmptRef = panelFactory . create ( this . _injector , contentRef . nodes ) ;
112
+ private _attachWindowComponent ( containerEl : Element , projectableNodes : Node [ ] [ ] ) : ComponentRef < NgbOffcanvasPanel > {
113
+ let panelCmptRef = createComponent (
114
+ NgbOffcanvasPanel ,
115
+ { environmentInjector : this . _applicationRef . injector , elementInjector : this . _injector , projectableNodes } ) ;
117
116
this . _applicationRef . attachView ( panelCmptRef . hostView ) ;
118
117
containerEl . appendChild ( panelCmptRef . location . nativeElement ) ;
119
118
return panelCmptRef ;
@@ -136,7 +135,7 @@ export class NgbOffcanvasStack {
136
135
}
137
136
138
137
private _getContentRef (
139
- moduleCFR : ComponentFactoryResolver , contentInjector : Injector , content : any ,
138
+ contentInjector : Injector , content : Type < any > | TemplateRef < any > | string ,
140
139
activeOffcanvas : NgbActiveOffcanvas ) : ContentRef {
141
140
if ( ! content ) {
142
141
return new ContentRef ( [ ] ) ;
@@ -145,17 +144,17 @@ export class NgbOffcanvasStack {
145
144
} else if ( isString ( content ) ) {
146
145
return this . _createFromString ( content ) ;
147
146
} else {
148
- return this . _createFromComponent ( moduleCFR , contentInjector , content , activeOffcanvas ) ;
147
+ return this . _createFromComponent ( contentInjector , content , activeOffcanvas ) ;
149
148
}
150
149
}
151
150
152
- private _createFromTemplateRef ( content : TemplateRef < any > , activeOffcanvas : NgbActiveOffcanvas ) : ContentRef {
151
+ private _createFromTemplateRef ( templateRef : TemplateRef < any > , activeOffcanvas : NgbActiveOffcanvas ) : ContentRef {
153
152
const context = {
154
153
$implicit : activeOffcanvas ,
155
154
close ( result ) { activeOffcanvas . close ( result ) ; } ,
156
155
dismiss ( reason ) { activeOffcanvas . dismiss ( reason ) ; }
157
156
} ;
158
- const viewRef = content . createEmbeddedView ( context ) ;
157
+ const viewRef = templateRef . createEmbeddedView ( context ) ;
159
158
this . _applicationRef . attachView ( viewRef ) ;
160
159
return new ContentRef ( [ viewRef . rootNodes ] , viewRef ) ;
161
160
}
@@ -165,13 +164,12 @@ export class NgbOffcanvasStack {
165
164
return new ContentRef ( [ [ component ] ] ) ;
166
165
}
167
166
168
- private _createFromComponent (
169
- moduleCFR : ComponentFactoryResolver , contentInjector : Injector , content : any ,
170
- context : NgbActiveOffcanvas ) : ContentRef {
171
- const contentCmptFactory = moduleCFR . resolveComponentFactory ( content ) ;
172
- const offcanvasContentInjector =
167
+ private _createFromComponent ( contentInjector : Injector , componentType : Type < any > , context : NgbActiveOffcanvas ) :
168
+ ContentRef {
169
+ const elementInjector =
173
170
Injector . create ( { providers : [ { provide : NgbActiveOffcanvas , useValue : context } ] , parent : contentInjector } ) ;
174
- const componentRef = contentCmptFactory . create ( offcanvasContentInjector ) ;
171
+ const componentRef =
172
+ createComponent ( componentType , { environmentInjector : this . _applicationRef . injector , elementInjector} ) ;
175
173
const componentNativeEl = componentRef . location . nativeElement ;
176
174
this . _applicationRef . attachView ( componentRef . hostView ) ;
177
175
return new ContentRef ( [ [ componentNativeEl ] ] , componentRef . hostView , componentRef ) ;
0 commit comments