ng-zorro-antd-cdk-overflow.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import * as i0 from '@angular/core';
  2. import { inject, ElementRef, Directive, ContentChild, ContentChildren, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
  3. import { Subject, ReplaySubject, BehaviorSubject, combineLatest } from 'rxjs';
  4. import { map, distinctUntilChanged, startWith, tap, pairwise, withLatestFrom, switchMap, takeUntil, filter } from 'rxjs/operators';
  5. import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
  6. /**
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  9. */
  10. class NzOverflowItemDirective {
  11. cdr;
  12. nzResizeObserver = inject(NzResizeObserver);
  13. elementRef = inject(ElementRef);
  14. overflowStyle = undefined;
  15. itemWidth$ = this.nzResizeObserver.observe(this.elementRef.nativeElement).pipe(map(([item]) => item.target.offsetWidth), distinctUntilChanged(), startWith(undefined), tap(width => {
  16. this.itemWidth = width;
  17. }));
  18. itemWidth = undefined;
  19. constructor(cdr) {
  20. this.cdr = cdr;
  21. }
  22. setItemStyle(display, order) {
  23. const mergedHidden = !display;
  24. this.overflowStyle = {
  25. opacity: mergedHidden ? 0 : 1,
  26. height: mergedHidden ? 0 : undefined,
  27. overflowY: mergedHidden ? 'hidden' : undefined,
  28. order: order,
  29. pointerEvents: mergedHidden ? 'none' : undefined,
  30. position: mergedHidden ? 'absolute' : undefined
  31. };
  32. this.cdr.detectChanges();
  33. }
  34. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowItemDirective, deps: [{ token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
  35. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.2", type: NzOverflowItemDirective, isStandalone: true, selector: "[nzOverflowItem]", host: { properties: { "style": "overflowStyle" } }, ngImport: i0 });
  36. }
  37. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowItemDirective, decorators: [{
  38. type: Directive,
  39. args: [{
  40. selector: '[nzOverflowItem]',
  41. host: {
  42. '[style]': 'overflowStyle'
  43. }
  44. }]
  45. }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }] });
  46. /**
  47. * Use of this source code is governed by an MIT-style license that can be
  48. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  49. */
  50. class NzOverflowRestDirective {
  51. cdr;
  52. nzResizeObserver = inject(NzResizeObserver);
  53. elementRef = inject(ElementRef);
  54. restStyle = undefined;
  55. restWidth$ = this.nzResizeObserver.observe(this.elementRef.nativeElement).pipe(map(([item]) => item.target.offsetWidth), startWith(0), tap(width => (this.restWidth = width)));
  56. restWidth = 0;
  57. constructor(cdr) {
  58. this.cdr = cdr;
  59. }
  60. setRestStyle(display, order) {
  61. const mergedHidden = !display;
  62. this.restStyle = {
  63. opacity: mergedHidden ? 0 : 1,
  64. height: mergedHidden ? 0 : undefined,
  65. overflowY: mergedHidden ? 'hidden' : undefined,
  66. order: order,
  67. pointerEvents: mergedHidden ? 'none' : undefined,
  68. position: mergedHidden ? 'absolute' : undefined
  69. };
  70. this.cdr.detectChanges();
  71. }
  72. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowRestDirective, deps: [{ token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
  73. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.2", type: NzOverflowRestDirective, isStandalone: true, selector: "[nzOverflowRest]", host: { properties: { "style": "restStyle" } }, ngImport: i0 });
  74. }
  75. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowRestDirective, decorators: [{
  76. type: Directive,
  77. args: [{
  78. selector: '[nzOverflowRest]',
  79. host: {
  80. '[style]': 'restStyle'
  81. }
  82. }]
  83. }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }] });
  84. /**
  85. * Use of this source code is governed by an MIT-style license that can be
  86. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  87. */
  88. class NzOverflowSuffixDirective {
  89. cdr;
  90. nzResizeObserver = inject(NzResizeObserver);
  91. elementRef = inject(ElementRef);
  92. suffixStyle = {};
  93. suffixWidth$ = this.nzResizeObserver.observe(this.elementRef.nativeElement).pipe(map(([item]) => item.target.offsetWidth), tap(width => (this.suffixWidth = width)));
  94. suffixWidth = 0;
  95. constructor(cdr) {
  96. this.cdr = cdr;
  97. }
  98. setSuffixStyle(start, order) {
  99. if (start !== null) {
  100. this.suffixStyle = {
  101. position: 'absolute',
  102. left: `${start}px`,
  103. top: 0,
  104. opacity: 1,
  105. order: order
  106. };
  107. }
  108. else {
  109. this.suffixStyle = {
  110. opacity: 1,
  111. order: order
  112. };
  113. }
  114. this.cdr.detectChanges();
  115. }
  116. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowSuffixDirective, deps: [{ token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
  117. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.2", type: NzOverflowSuffixDirective, isStandalone: true, selector: "[nzOverflowSuffix]", host: { properties: { "style": "suffixStyle" } }, ngImport: i0 });
  118. }
  119. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowSuffixDirective, decorators: [{
  120. type: Directive,
  121. args: [{
  122. selector: '[nzOverflowSuffix]',
  123. host: {
  124. '[style]': 'suffixStyle'
  125. }
  126. }]
  127. }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }] });
  128. /**
  129. * Use of this source code is governed by an MIT-style license that can be
  130. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  131. */
  132. class NzOverflowContainerComponent {
  133. cdr;
  134. contentInit$ = new Subject();
  135. overflowItems = undefined;
  136. overflowSuffix = undefined;
  137. overflowRest = undefined;
  138. overflowItems$ = new ReplaySubject(1);
  139. destroy$ = new Subject();
  140. nzResizeObserver = inject(NzResizeObserver);
  141. elementRef = inject(ElementRef);
  142. containerWidth$ = this.nzResizeObserver
  143. .observe(this.elementRef.nativeElement)
  144. .pipe(map(([item]) => item.target.clientWidth || 0));
  145. restWidth$ = new BehaviorSubject(0);
  146. suffixWidth$ = new BehaviorSubject(0);
  147. suffixFixedStart$ = new BehaviorSubject(null);
  148. displayCount$ = new BehaviorSubject(Number.MAX_SAFE_INTEGER);
  149. restReady$ = new BehaviorSubject(false);
  150. maxRestWith$ = this.restWidth$.pipe(pairwise(), map(([prevRestWidth, restWidth]) => Math.max(prevRestWidth, restWidth)));
  151. omittedItems$ = combineLatest([this.overflowItems$, this.displayCount$]).pipe(withLatestFrom(this.contentInit$), map(([[overflowItems, displayCount]]) => overflowItems.toArray().slice(displayCount + 1)));
  152. displayRest$ = combineLatest([this.restReady$, this.omittedItems$]).pipe(map(([restReady, omittedItems]) => restReady && !!omittedItems.length));
  153. updateDisplayCount(count, notReady) {
  154. this.displayCount$.next(count);
  155. if (this.overflowItems && !notReady) {
  156. this.restReady$.next(count < this.overflowItems.length - 1);
  157. }
  158. }
  159. constructor(cdr) {
  160. this.cdr = cdr;
  161. }
  162. ngOnInit() {
  163. const overflowItemsWidth$ = this.overflowItems$.pipe(switchMap(items => combineLatest(items.map(item => item.itemWidth$))));
  164. this.overflowItems$.pipe(takeUntil(this.destroy$)).subscribe(overflowItems => {
  165. if (!overflowItems.length) {
  166. this.displayCount$.next(0);
  167. this.suffixFixedStart$.next(null);
  168. }
  169. });
  170. combineLatest([overflowItemsWidth$, this.containerWidth$, this.maxRestWith$, this.restWidth$, this.suffixWidth$])
  171. .pipe(filter(([, containerWidth, maxRestWith]) => !!(containerWidth && maxRestWith)), takeUntil(this.destroy$))
  172. .subscribe(([overflowItemsWidth, containerWidth, maxRestWith, restWidth, suffixWidth]) => {
  173. let totalWidth = suffixWidth;
  174. const len = overflowItemsWidth.length;
  175. const lastIndex = len - 1;
  176. for (let i = 0; i < len; i += 1) {
  177. const currentItemWidth = overflowItemsWidth[i];
  178. // Break since data not ready
  179. if (currentItemWidth === undefined) {
  180. this.updateDisplayCount(i - 1, true);
  181. break;
  182. }
  183. else {
  184. // Find best match
  185. totalWidth += currentItemWidth;
  186. if (
  187. // Only one means `totalWidth` is the final width
  188. (lastIndex === 0 && totalWidth <= containerWidth) ||
  189. // Last two width will be the final width
  190. (i === lastIndex - 1 &&
  191. overflowItemsWidth[lastIndex] !== undefined &&
  192. totalWidth + overflowItemsWidth[lastIndex] <= containerWidth)) {
  193. // Additional check if match the end
  194. this.updateDisplayCount(lastIndex);
  195. this.suffixFixedStart$.next(null);
  196. break;
  197. }
  198. else if (totalWidth + maxRestWith > containerWidth) {
  199. // Can not hold all the content to show rest
  200. this.updateDisplayCount(i - 1);
  201. this.suffixFixedStart$.next(totalWidth - currentItemWidth - suffixWidth + restWidth);
  202. break;
  203. }
  204. this.cdr.detectChanges();
  205. }
  206. }
  207. if (this.overflowSuffix &&
  208. overflowItemsWidth[0] !== undefined &&
  209. overflowItemsWidth[0] + suffixWidth > containerWidth) {
  210. this.suffixFixedStart$.next(null);
  211. }
  212. this.cdr.detectChanges();
  213. });
  214. combineLatest([this.suffixFixedStart$, this.displayCount$])
  215. .pipe(takeUntil(this.destroy$))
  216. .subscribe(([suffixFixedStart, displayCount]) => {
  217. this.overflowSuffix?.setSuffixStyle(suffixFixedStart, displayCount);
  218. });
  219. combineLatest([this.displayCount$, this.overflowItems$])
  220. .pipe(takeUntil(this.destroy$))
  221. .subscribe(([displayCount, overflowItems]) => overflowItems.forEach((item, index) => item.setItemStyle(index <= displayCount, index)));
  222. combineLatest([this.displayRest$, this.displayCount$])
  223. .pipe(takeUntil(this.destroy$))
  224. .subscribe(([displayRest, displayCount]) => {
  225. this.overflowRest?.setRestStyle(displayRest, displayRest ? displayCount : Number.MAX_SAFE_INTEGER);
  226. });
  227. }
  228. ngAfterContentInit() {
  229. this.overflowItems?.changes.pipe(startWith(this.overflowItems)).subscribe(this.overflowItems$);
  230. this.overflowSuffix?.suffixWidth$.pipe(takeUntil(this.destroy$)).subscribe(this.suffixWidth$);
  231. this.overflowRest?.restWidth$.pipe(takeUntil(this.destroy$)).subscribe(this.restWidth$);
  232. this.contentInit$.next();
  233. }
  234. ngOnDestroy() {
  235. this.destroy$.next();
  236. this.destroy$.complete();
  237. }
  238. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowContainerComponent, deps: [{ token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
  239. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.2.2", type: NzOverflowContainerComponent, isStandalone: true, selector: "nz-overflow-container", providers: [NzResizeObserver], queries: [{ propertyName: "overflowSuffix", first: true, predicate: NzOverflowSuffixDirective, descendants: true }, { propertyName: "overflowRest", first: true, predicate: NzOverflowRestDirective, descendants: true }, { propertyName: "overflowItems", predicate: NzOverflowItemDirective }], ngImport: i0, template: ` <ng-content></ng-content>
  240. <ng-content select="[appOverflowRest]"></ng-content>
  241. <ng-content select="[appOverflowSuffix]"></ng-content>`, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush });
  242. }
  243. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowContainerComponent, decorators: [{
  244. type: Component,
  245. args: [{
  246. selector: 'nz-overflow-container',
  247. template: ` <ng-content></ng-content>
  248. <ng-content select="[appOverflowRest]"></ng-content>
  249. <ng-content select="[appOverflowSuffix]"></ng-content>`,
  250. providers: [NzResizeObserver],
  251. changeDetection: ChangeDetectionStrategy.OnPush
  252. }]
  253. }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }], propDecorators: { overflowItems: [{
  254. type: ContentChildren,
  255. args: [NzOverflowItemDirective]
  256. }], overflowSuffix: [{
  257. type: ContentChild,
  258. args: [NzOverflowSuffixDirective]
  259. }], overflowRest: [{
  260. type: ContentChild,
  261. args: [NzOverflowRestDirective]
  262. }] } });
  263. /**
  264. * Use of this source code is governed by an MIT-style license that can be
  265. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  266. */
  267. class NzOverflowModule {
  268. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  269. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowModule, imports: [NzOverflowContainerComponent, NzOverflowItemDirective, NzOverflowRestDirective, NzOverflowSuffixDirective], exports: [NzOverflowContainerComponent, NzOverflowItemDirective, NzOverflowRestDirective, NzOverflowSuffixDirective] });
  270. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowModule });
  271. }
  272. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzOverflowModule, decorators: [{
  273. type: NgModule,
  274. args: [{
  275. imports: [NzOverflowContainerComponent, NzOverflowItemDirective, NzOverflowRestDirective, NzOverflowSuffixDirective],
  276. exports: [NzOverflowContainerComponent, NzOverflowItemDirective, NzOverflowRestDirective, NzOverflowSuffixDirective]
  277. }]
  278. }] });
  279. /**
  280. * Use of this source code is governed by an MIT-style license that can be
  281. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  282. */
  283. /**
  284. * Generated bundle index. Do not edit.
  285. */
  286. export { NzOverflowContainerComponent, NzOverflowItemDirective, NzOverflowModule, NzOverflowRestDirective, NzOverflowSuffixDirective };
  287. //# sourceMappingURL=ng-zorro-antd-cdk-overflow.mjs.map