{"version":3,"file":"nw-style-guide-resize-observer.mjs","sources":["../../../projects/nw-style-guide/resize-observer/resize-observer.directive.ts","../../../projects/nw-style-guide/resize-observer/nw-style-guide-resize-observer.ts"],"sourcesContent":["import { Directive, Output, EventEmitter, ElementRef, OnInit, OnDestroy, Input, inject } from '@angular/core';\nimport { Subject } from 'rxjs';\nimport { skip, debounceTime, takeUntil, tap } from 'rxjs/operators';\n\n@Directive({\n    selector: '[nwResizeObserver]',\n    exportAs: 'nwResizeObserver'\n})\nexport class ResizeObserverDirective implements OnInit, OnDestroy {\n    /**\n     * Optional element to listen to the resize event for. If not\n     * provided defaults to the element the directive is attached to\n     */\n    @Input() element?: HTMLElement;\n    /**\n     * A pixel value specifying the minimum change in height or width that triggers the resize event\n     */\n    @Input() tolerance: number = 0;\n\n    @Output() nwResize: EventEmitter<null> = new EventEmitter();\n\n    private _observer: ResizeObserver;\n    private _nwResize: Subject<ResizeObserverEntry> = new Subject();\n    private _destroyed$: Subject<void> = new Subject();\n    private _currentElementRect: DOMRectReadOnly;\n\n    constructor() {\n        const _elRef = inject<ElementRef<HTMLElement>>(ElementRef);\n\n        this.element = this.element || _elRef.nativeElement;\n    }\n\n    ngOnInit() {\n        this.subscribeToResize();\n\n        if (ResizeObserver) {\n            this.createObserver();\n        }\n    }\n\n    subscribeToResize() {\n        /**\n         * Rather than call this.nwResize.emit() immediately after being notified\n         * of the resize event, we use this._nwResize as a proxy to filter events we\n         * don't want and to debounce as we don't want to emit hundreds of events\n         * if, for example, the width or height of the element is being animated\n         */\n        this._nwResize\n            .asObservable()\n            .pipe(\n                /**\n                 * Store the contentRect of the element so that we can compare changes\n                 */\n                tap(entry => (this._currentElementRect = entry.contentRect)),\n                /**\n                 * use skip(1) because an event is emitting immediately\n                 * after calling .observe on the observer\n                 */\n                skip(1),\n                debounceTime(50),\n                takeUntil(this._destroyed$)\n            )\n            .subscribe(res => this.nwResize.emit());\n    }\n\n    createObserver() {\n        this._observer = new ResizeObserver(([element]) => {\n            const widthChange = Math.abs(element.contentRect.width - (this._currentElementRect?.width ?? 0));\n            const heightChange = Math.abs(element.contentRect.height - (this._currentElementRect?.height ?? 0));\n\n            if (widthChange >= this.tolerance || heightChange >= this.tolerance) {\n                this._nwResize.next(element);\n            }\n        });\n        this._observer.observe(this.element);\n    }\n\n    ngOnDestroy() {\n        if (ResizeObserver && this._observer) {\n            this._observer.unobserve(this.element);\n        }\n        this._destroyed$.next();\n        this._destroyed$.complete();\n    }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;MAQa,uBAAuB,CAAA;AAkBhC,IAAA,WAAA,GAAA;AAZA;;AAEG;QACM,IAAA,CAAA,SAAS,GAAW,CAAC;AAEpB,QAAA,IAAA,CAAA,QAAQ,GAAuB,IAAI,YAAY,EAAE;AAGnD,QAAA,IAAA,CAAA,SAAS,GAAiC,IAAI,OAAO,EAAE;AACvD,QAAA,IAAA,CAAA,WAAW,GAAkB,IAAI,OAAO,EAAE;AAI9C,QAAA,MAAM,MAAM,GAAG,MAAM,CAA0B,UAAU,CAAC;QAE1D,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,MAAM,CAAC,aAAa;IACvD;IAEA,QAAQ,GAAA;QACJ,IAAI,CAAC,iBAAiB,EAAE;QAExB,IAAI,cAAc,EAAE;YAChB,IAAI,CAAC,cAAc,EAAE;QACzB;IACJ;IAEA,iBAAiB,GAAA;AACb;;;;;AAKG;AACH,QAAA,IAAI,CAAC;AACA,aAAA,YAAY;aACZ,IAAI;AACD;;AAEG;AACH,QAAA,GAAG,CAAC,KAAK,KAAK,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;AAC5D;;;AAGG;AACH,QAAA,IAAI,CAAC,CAAC,CAAC,EACP,YAAY,CAAC,EAAE,CAAC,EAChB,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC;AAE9B,aAAA,SAAS,CAAC,GAAG,IAAI,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