UNPKG

2.96 kBJavaScriptView Raw
1import { Injectable, ElementRef, RendererFactory2, Inject, PLATFORM_ID, NgZone } from '@angular/core';
2import { isPlatformBrowser } from '@angular/common';
3import { positionElements } from './ng-positioning';
4import { fromEvent, merge, of, animationFrameScheduler, Subject } from 'rxjs';
5import * as i0 from "@angular/core";
6export class PositioningService {
7 constructor(ngZone, rendererFactory, platformId) {
8 this.update$$ = new Subject();
9 this.positionElements = new Map();
10 this.isDisabled = false;
11 if (isPlatformBrowser(platformId)) {
12 ngZone.runOutsideAngular(() => {
13 this.triggerEvent$ = merge(fromEvent(window, 'scroll', { passive: true }), fromEvent(window, 'resize', { passive: true }), of(0, animationFrameScheduler), this.update$$);
14 this.triggerEvent$.subscribe(() => {
15 if (this.isDisabled) {
16 return;
17 }
18 this.positionElements
19 // eslint-disable-next-line @typescript-eslint/no-explicit-any
20 .forEach((positionElement) => {
21 positionElements(_getHtmlElement(positionElement.target), _getHtmlElement(positionElement.element), positionElement.attachment, positionElement.appendToBody, this.options, rendererFactory.createRenderer(null, null));
22 });
23 });
24 });
25 }
26 }
27 position(options) {
28 this.addPositionElement(options);
29 }
30 get event$() {
31 return this.triggerEvent$;
32 }
33 disable() {
34 this.isDisabled = true;
35 }
36 enable() {
37 this.isDisabled = false;
38 }
39 addPositionElement(options) {
40 this.positionElements.set(_getHtmlElement(options.element), options);
41 }
42 calcPosition() {
43 this.update$$.next();
44 }
45 deletePositionElement(elRef) {
46 this.positionElements.delete(_getHtmlElement(elRef));
47 }
48 setOptions(options) {
49 this.options = options;
50 }
51}
52PositioningService.ɵprov = i0.ɵɵdefineInjectable({ factory: function PositioningService_Factory() { return new PositioningService(i0.ɵɵinject(i0.NgZone), i0.ɵɵinject(i0.RendererFactory2), i0.ɵɵinject(i0.PLATFORM_ID)); }, token: PositioningService, providedIn: "root" });
53PositioningService.decorators = [
54 { type: Injectable, args: [{ providedIn: 'root' },] }
55];
56PositioningService.ctorParameters = () => [
57 { type: NgZone },
58 { type: RendererFactory2 },
59 { type: Number, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] }
60];
61function _getHtmlElement(element) {
62 // it means that we got a selector
63 if (typeof element === 'string') {
64 return document.querySelector(element);
65 }
66 if (element instanceof ElementRef) {
67 return element.nativeElement;
68 }
69 return element !== null && element !== void 0 ? element : null;
70}
71//# sourceMappingURL=positioning.service.js.map
\No newline at end of file