UNPKG

@ng-doc/ui-kit

Version:

<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/ng-doc/ng-doc"> <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px"> </a>

53 lines (49 loc) 2.43 kB
import * as i0 from '@angular/core'; import { inject, ElementRef, NgZone, DestroyRef, Input, Directive } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { asArray } from '@ng-doc/core/helpers/as-array'; import { toElement } from '@ng-doc/ui-kit/helpers'; import { ngDocZoneDetach } from '@ng-doc/ui-kit/observables'; import { merge, fromEvent } from 'rxjs'; class NgDocEventSwitcherDirective { constructor() { this.elementRef = inject(ElementRef); this.ngZone = inject(NgZone); this.switchTo = null; this.events = []; this.destroyRef = inject(DestroyRef); } ngOnInit() { merge(...asArray(this.events).map((eventName) => fromEvent(this.elementRef.nativeElement, eventName))) .pipe(ngDocZoneDetach(this.ngZone), takeUntilDestroyed(this.destroyRef)) .subscribe((event) => { if (this.switchTo && !event.defaultPrevented && event.bubbles) { event.stopPropagation(); this.makeEvent(event, toElement(this.switchTo)); } }); } makeEvent(from, target) { const eventConstructor = from.constructor; target.dispatchEvent(new eventConstructor(from.type, from)); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: NgDocEventSwitcherDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.0.6", type: NgDocEventSwitcherDirective, isStandalone: true, selector: "[ngDocEventSwitcher]", inputs: { switchTo: ["ngDocEventSwitcher", "switchTo"], events: "events" }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.6", ngImport: i0, type: NgDocEventSwitcherDirective, decorators: [{ type: Directive, args: [{ selector: '[ngDocEventSwitcher]', standalone: true, }] }], ctorParameters: () => [], propDecorators: { switchTo: [{ type: Input, args: ['ngDocEventSwitcher'] }], events: [{ type: Input }] } }); /** * Generated bundle index. Do not edit. */ export { NgDocEventSwitcherDirective }; //# sourceMappingURL=ng-doc-ui-kit-directives-event-switcher.mjs.map