ng-zorro-antd-cdk-resize-observer.mjs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import * as i0 from '@angular/core';
  2. import { Injectable, EventEmitter, booleanAttribute, Input, Output, Directive, NgModule } from '@angular/core';
  3. import { coerceElement } from '@angular/cdk/coercion';
  4. import { Observable, Subject } from 'rxjs';
  5. /**
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  8. */
  9. /**
  10. * Factory that creates a new ResizeObserver and allows us to stub it out in unit tests.
  11. */
  12. class NzResizeObserverFactory {
  13. create(callback) {
  14. return typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(callback);
  15. }
  16. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  17. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverFactory, providedIn: 'root' });
  18. }
  19. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverFactory, decorators: [{
  20. type: Injectable,
  21. args: [{ providedIn: 'root' }]
  22. }] });
  23. /** An injectable service that allows watching elements for changes to their content. */
  24. class NzResizeObserver {
  25. nzResizeObserverFactory;
  26. /** Keeps track of the existing ResizeObservers so they can be reused. */
  27. observedElements = new Map();
  28. constructor(nzResizeObserverFactory) {
  29. this.nzResizeObserverFactory = nzResizeObserverFactory;
  30. }
  31. ngOnDestroy() {
  32. this.observedElements.forEach((_, element) => this.cleanupObserver(element));
  33. }
  34. observe(elementOrRef) {
  35. const element = coerceElement(elementOrRef);
  36. return new Observable((observer) => {
  37. const stream = this.observeElement(element);
  38. const subscription = stream.subscribe(observer);
  39. return () => {
  40. subscription.unsubscribe();
  41. this.unobserveElement(element);
  42. };
  43. });
  44. }
  45. /**
  46. * Observes the given element by using the existing ResizeObserver if available, or creating a
  47. * new one if not.
  48. */
  49. observeElement(element) {
  50. if (!this.observedElements.has(element)) {
  51. const stream = new Subject();
  52. const observer = this.nzResizeObserverFactory.create((mutations) => stream.next(mutations));
  53. if (observer) {
  54. observer.observe(element);
  55. }
  56. this.observedElements.set(element, { observer, stream, count: 1 });
  57. }
  58. else {
  59. this.observedElements.get(element).count++;
  60. }
  61. return this.observedElements.get(element).stream;
  62. }
  63. /**
  64. * Un-observes the given element and cleans up the underlying ResizeObserver if nobody else is
  65. * observing this element.
  66. */
  67. unobserveElement(element) {
  68. if (this.observedElements.has(element)) {
  69. this.observedElements.get(element).count--;
  70. if (!this.observedElements.get(element).count) {
  71. this.cleanupObserver(element);
  72. }
  73. }
  74. }
  75. /** Clean up the underlying ResizeObserver for the specified element. */
  76. cleanupObserver(element) {
  77. if (this.observedElements.has(element)) {
  78. const { observer, stream } = this.observedElements.get(element);
  79. if (observer) {
  80. observer.disconnect();
  81. }
  82. stream.complete();
  83. this.observedElements.delete(element);
  84. }
  85. }
  86. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserver, deps: [{ token: NzResizeObserverFactory }], target: i0.ɵɵFactoryTarget.Injectable });
  87. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserver, providedIn: 'root' });
  88. }
  89. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserver, decorators: [{
  90. type: Injectable,
  91. args: [{ providedIn: 'root' }]
  92. }], ctorParameters: () => [{ type: NzResizeObserverFactory }] });
  93. /**
  94. * Use of this source code is governed by an MIT-style license that can be
  95. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  96. */
  97. class NzResizeObserverDirective {
  98. nzResizeObserver;
  99. elementRef;
  100. nzResizeObserve = new EventEmitter();
  101. nzResizeObserverDisabled = false;
  102. currentSubscription = null;
  103. subscribe() {
  104. this.unsubscribe();
  105. this.currentSubscription = this.nzResizeObserver.observe(this.elementRef).subscribe(this.nzResizeObserve);
  106. }
  107. unsubscribe() {
  108. this.currentSubscription?.unsubscribe();
  109. }
  110. constructor(nzResizeObserver, elementRef) {
  111. this.nzResizeObserver = nzResizeObserver;
  112. this.elementRef = elementRef;
  113. }
  114. ngAfterContentInit() {
  115. if (!this.currentSubscription && !this.nzResizeObserverDisabled) {
  116. this.subscribe();
  117. }
  118. }
  119. ngOnDestroy() {
  120. this.unsubscribe();
  121. }
  122. ngOnChanges(changes) {
  123. const { nzResizeObserve } = changes;
  124. if (nzResizeObserve) {
  125. if (this.nzResizeObserverDisabled) {
  126. this.unsubscribe();
  127. }
  128. else {
  129. this.subscribe();
  130. }
  131. }
  132. }
  133. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverDirective, deps: [{ token: NzResizeObserver }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
  134. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "16.1.0", version: "19.2.2", type: NzResizeObserverDirective, isStandalone: true, selector: "[nzResizeObserver]", inputs: { nzResizeObserverDisabled: ["nzResizeObserverDisabled", "nzResizeObserverDisabled", booleanAttribute] }, outputs: { nzResizeObserve: "nzResizeObserve" }, providers: [NzResizeObserverFactory], usesOnChanges: true, ngImport: i0 });
  135. }
  136. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverDirective, decorators: [{
  137. type: Directive,
  138. args: [{
  139. selector: '[nzResizeObserver]',
  140. providers: [NzResizeObserverFactory]
  141. }]
  142. }], ctorParameters: () => [{ type: NzResizeObserver }, { type: i0.ElementRef }], propDecorators: { nzResizeObserve: [{
  143. type: Output
  144. }], nzResizeObserverDisabled: [{
  145. type: Input,
  146. args: [{ transform: booleanAttribute }]
  147. }] } });
  148. /**
  149. * Use of this source code is governed by an MIT-style license that can be
  150. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  151. */
  152. class NzResizeObserverModule {
  153. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  154. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverModule, imports: [NzResizeObserverDirective], exports: [NzResizeObserverDirective] });
  155. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverModule });
  156. }
  157. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeObserverModule, decorators: [{
  158. type: NgModule,
  159. args: [{
  160. imports: [NzResizeObserverDirective],
  161. exports: [NzResizeObserverDirective]
  162. }]
  163. }] });
  164. /**
  165. * Use of this source code is governed by an MIT-style license that can be
  166. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  167. */
  168. /**
  169. * Generated bundle index. Do not edit.
  170. */
  171. export { NzResizeObserver, NzResizeObserverDirective, NzResizeObserverFactory, NzResizeObserverModule };
  172. //# sourceMappingURL=ng-zorro-antd-cdk-resize-observer.mjs.map