1
1
import {
2
+ AfterViewInit ,
2
3
ChangeDetectorRef ,
3
4
ComponentRef ,
4
5
Directive ,
@@ -15,19 +16,22 @@ import {
15
16
ViewContainerRef
16
17
} from '@angular/core' ;
17
18
import { DOCUMENT } from '@angular/common' ;
19
+ import { Subscription } from 'rxjs' ;
20
+ import { debounceTime } from 'rxjs/operators' ;
18
21
import { createPopper , Instance , Options } from '@popperjs/core' ;
19
22
20
23
import { Triggers } from '../coreui.types' ;
21
24
import { PopoverComponent } from './popover/popover.component' ;
22
25
import { IListenersConfig , ListenersService } from '../services/listeners.service' ;
26
+ import { IntersectionService } from '../services' ;
23
27
24
28
@Directive ( {
25
29
selector : '[cPopover]' ,
26
30
exportAs : 'cPopover' ,
27
- providers : [ ListenersService ] ,
31
+ providers : [ ListenersService , IntersectionService ] ,
28
32
standalone : true
29
33
} )
30
- export class PopoverDirective implements OnChanges , OnDestroy , OnInit {
34
+ export class PopoverDirective implements OnChanges , OnDestroy , OnInit , AfterViewInit {
31
35
32
36
/**
33
37
* Content of popover
@@ -92,15 +96,23 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
92
96
]
93
97
} ;
94
98
99
+ private intersectingSubscription ?: Subscription ;
100
+
95
101
constructor (
96
102
@Inject ( DOCUMENT ) private document : Document ,
97
103
private renderer : Renderer2 ,
98
104
private hostElement : ElementRef ,
99
105
private viewContainerRef : ViewContainerRef ,
100
106
private listenersService : ListenersService ,
101
- private changeDetectorRef : ChangeDetectorRef
107
+ private changeDetectorRef : ChangeDetectorRef ,
108
+ private intersectionService : IntersectionService
102
109
) { }
103
110
111
+ ngAfterViewInit ( ) : void {
112
+ this . intersectionService . createIntersectionObserver ( this . hostElement ) ;
113
+ this . intersectionServiceSubscribe ( ) ;
114
+ }
115
+
104
116
ngOnChanges ( changes : SimpleChanges ) : void {
105
117
if ( changes [ 'visible' ] ) {
106
118
changes [ 'visible' ] . currentValue ? this . addPopoverElement ( ) : this . removePopoverElement ( ) ;
@@ -110,6 +122,7 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
110
122
ngOnDestroy ( ) : void {
111
123
this . clearListeners ( ) ;
112
124
this . destroyPopoverElement ( ) ;
125
+ this . intersectionServiceSubscribe ( false ) ;
113
126
}
114
127
115
128
ngOnInit ( ) : void {
@@ -140,6 +153,21 @@ export class PopoverDirective implements OnChanges, OnDestroy, OnInit {
140
153
this . listenersService . clearListeners ( ) ;
141
154
}
142
155
156
+ private intersectionServiceSubscribe ( subscribe : boolean = true ) : void {
157
+ if ( subscribe ) {
158
+ this . intersectingSubscription = this . intersectionService . intersecting$
159
+ . pipe (
160
+ debounceTime ( 100 )
161
+ )
162
+ . subscribe ( isIntersecting => {
163
+ this . visible = isIntersecting ? this . visible : false ;
164
+ ! this . visible && this . removePopoverElement ( ) ;
165
+ } ) ;
166
+ } else {
167
+ this . intersectingSubscription ?. unsubscribe ( ) ;
168
+ }
169
+ }
170
+
143
171
private getUID ( prefix : string ) : string {
144
172
let uid = prefix ?? 'random-id' ;
145
173
do {
0 commit comments