1
1
import {
2
- ComponentFactoryResolver ,
3
2
ComponentRef ,
4
3
Directive ,
5
4
ElementRef ,
@@ -95,7 +94,6 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
95
94
@Inject ( DOCUMENT ) private document : any ,
96
95
private renderer : Renderer2 ,
97
96
private hostElement : ElementRef ,
98
- private componentFactoryResolver : ComponentFactoryResolver ,
99
97
private viewContainerRef : ViewContainerRef ,
100
98
private listenersService : ListenersService
101
99
) { }
@@ -150,9 +148,8 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
150
148
151
149
private createPopoverElement ( ) : void {
152
150
if ( ! this . popoverRef ) {
153
- const popoverComponent =
154
- this . componentFactoryResolver . resolveComponentFactory ( PopoverComponent ) ;
155
- this . popoverRef = popoverComponent . create ( this . viewContainerRef . injector ) ;
151
+ this . popoverRef = this . viewContainerRef . createComponent < PopoverComponent > ( PopoverComponent ) ;
152
+ // this.viewContainerRef.detach();
156
153
}
157
154
}
158
155
@@ -162,8 +159,8 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
162
159
// @ts -ignore
163
160
this . popoverRef = undefined ;
164
161
this . popperInstance ?. destroy ( ) ;
165
- this . viewContainerRef . detach ( ) ;
166
- this . viewContainerRef . clear ( ) ;
162
+ this . viewContainerRef ? .detach ( ) ;
163
+ this . viewContainerRef ? .clear ( ) ;
167
164
}
168
165
169
166
private addPopoverElement ( ) : void {
@@ -172,36 +169,46 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
172
169
}
173
170
this . popoverRef . instance . content = this . content ;
174
171
this . popover = this . popoverRef . location . nativeElement ;
172
+ this . renderer . addClass ( this . popover , 'd-none' ) ;
175
173
this . renderer . addClass ( this . popover , 'fade' ) ;
176
174
175
+ this . popperInstance ?. destroy ( ) ;
176
+
177
177
setTimeout ( ( ) => {
178
178
this . popperInstance = createPopper (
179
179
this . hostElement . nativeElement ,
180
180
this . popover ,
181
181
{ ...this . popperOptions }
182
182
) ;
183
183
this . viewContainerRef . insert ( this . popoverRef . hostView ) ;
184
+ this . renderer . appendChild ( this . document . body , this . popover ) ;
185
+ if ( ! this . visible ) {
186
+ this . removePopoverElement ( ) ;
187
+ return ;
188
+ }
184
189
setTimeout ( ( ) => {
185
190
this . popoverId = this . getUID ( 'popover' ) ;
186
191
this . popoverRef . instance . id = this . popoverId ;
192
+ if ( ! this . visible ) {
193
+ this . removePopoverElement ( ) ;
194
+ return ;
195
+ }
196
+ this . renderer . removeClass ( this . popover , 'd-none' ) ;
187
197
this . popoverRef . instance . visible = this . visible ;
188
- this . renderer . appendChild ( this . document . body , this . popover ) ;
189
198
this . popperInstance . forceUpdate ( ) ;
190
- // this.popoverRef.changeDetectorRef.markForCheck();
191
199
} , 100 ) ;
192
200
} ) ;
193
201
}
194
202
195
203
private removePopoverElement ( ) : void {
204
+ this . popoverId = '' ;
196
205
if ( ! this . popoverRef ) {
197
206
return ;
198
207
}
199
208
this . popoverRef . instance . visible = this . visible ;
200
209
this . popoverRef . instance . id = undefined ;
201
210
setTimeout ( ( ) => {
202
211
this . viewContainerRef . detach ( ) ;
203
- this . popperInstance ?. destroy ( ) ;
204
- this . popoverId = '' ;
205
212
} , 300 ) ;
206
213
}
207
214
}
0 commit comments