forked from 4Science/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in task/dspace-cris-2023_02_x/DSC-1874 (pull request DSpace#2113)
[DSC-1874] Improve related-items popover open/close behaviour Approved-by: Stefano Maffei
- Loading branch information
Showing
3 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/app/shared/metadata-link-view/sticky-popover.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { | ||
ElementRef, | ||
Directive, | ||
Input, | ||
TemplateRef, | ||
Renderer2, | ||
OnInit, | ||
OnDestroy, | ||
Injector, | ||
ViewContainerRef, | ||
NgZone, | ||
ChangeDetectorRef, | ||
ApplicationRef, Inject | ||
} from '@angular/core'; | ||
import {NgbPopover, NgbPopoverConfig} from '@ng-bootstrap/ng-bootstrap'; | ||
import {DOCUMENT} from '@angular/common'; | ||
import {NavigationStart, Router} from '@angular/router'; | ||
import {Subscription} from 'rxjs'; | ||
|
||
/** | ||
* Directive to create a sticky popover using NgbPopover. | ||
* The popover remains open when the mouse is over its content and closes when the mouse leaves. | ||
*/ | ||
@Directive({ | ||
selector: '[dsStickyPopover]' | ||
}) | ||
export class StickyPopoverDirective extends NgbPopover implements OnInit, OnDestroy { | ||
/** Template for the sticky popover content */ | ||
@Input() dsStickyPopover: TemplateRef<any>; | ||
|
||
/** Subscriptions to manage router events */ | ||
subs: Subscription[] = []; | ||
|
||
/** Flag to determine if the popover can be closed */ | ||
private canClosePopover: boolean; | ||
|
||
/** Reference to the element the directive is applied to */ | ||
private readonly _elRef; | ||
|
||
/** Renderer to listen to and manipulate DOM elements */ | ||
private readonly _render; | ||
|
||
constructor( | ||
_elementRef: ElementRef<HTMLElement>, | ||
_renderer: Renderer2, injector: Injector, | ||
viewContainerRef: ViewContainerRef, | ||
config: NgbPopoverConfig, | ||
_ngZone: NgZone, | ||
@Inject(DOCUMENT) _document: Document, | ||
_changeDetector: ChangeDetectorRef, | ||
applicationRef: ApplicationRef, | ||
private router: Router | ||
) { | ||
super(_elementRef, _renderer, injector, viewContainerRef, config, _ngZone, document, _changeDetector, applicationRef); | ||
this._elRef = _elementRef; | ||
this._render = _renderer; | ||
this.triggers = 'manual'; | ||
this.container = 'body'; | ||
} | ||
|
||
/** | ||
* Sets up event listeners for mouse enter, mouse leave, and click events. | ||
*/ | ||
ngOnInit(): void { | ||
super.ngOnInit(); | ||
this.ngbPopover = this.dsStickyPopover; | ||
|
||
this._render.listen(this._elRef.nativeElement, 'mouseenter', () => { | ||
this.canClosePopover = true; | ||
this.open(); | ||
}); | ||
|
||
this._render.listen(this._elRef.nativeElement, 'mouseleave', () => { | ||
setTimeout(() => { | ||
if (this.canClosePopover) { | ||
this.close(); | ||
} | ||
}, 100); | ||
}); | ||
|
||
this._render.listen(this._elRef.nativeElement, 'click', () => { | ||
this.close(); | ||
}); | ||
|
||
this.subs.push( | ||
this.router.events.subscribe((event) => { | ||
if (event instanceof NavigationStart) { | ||
this.close(); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
/** | ||
* Opens the popover and sets up event listeners for mouse over and mouse out events on the popover. | ||
*/ | ||
open() { | ||
super.open(); | ||
const popover = window.document.querySelector('.popover'); | ||
this._render.listen(popover, 'mouseover', () => { | ||
this.canClosePopover = false; | ||
}); | ||
|
||
this._render.listen(popover, 'mouseout', () => { | ||
this.canClosePopover = true; | ||
setTimeout(() => { | ||
if (this.canClosePopover) { | ||
this.close(); | ||
} | ||
}, 0); | ||
}); | ||
} | ||
|
||
/** | ||
* Unsubscribes from all subscriptions when the directive is destroyed. | ||
*/ | ||
ngOnDestroy() { | ||
super.ngOnDestroy(); | ||
this.subs.forEach((sub) => sub.unsubscribe()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters