ng-zorro-antd-affix.mjs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. import { __esDecorate, __runInitializers } from 'tslib';
  2. import * as i5 from '@angular/cdk/bidi';
  3. import { BidiModule } from '@angular/cdk/bidi';
  4. import * as i3 from '@angular/cdk/platform';
  5. import { PlatformModule } from '@angular/cdk/platform';
  6. import { DOCUMENT } from '@angular/common';
  7. import * as i0 from '@angular/core';
  8. import { EventEmitter, inject, Output, Input, ViewChild, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
  9. import { Subscription, ReplaySubject, Subject, merge, fromEvent } from 'rxjs';
  10. import { takeUntil, map, throttleTime } from 'rxjs/operators';
  11. import * as i1 from 'ng-zorro-antd/core/config';
  12. import { WithConfig } from 'ng-zorro-antd/core/config';
  13. import { shallowEqual, getStyleAsText, numberAttributeWithZeroFallback } from 'ng-zorro-antd/core/util';
  14. import * as i2 from 'ng-zorro-antd/core/services';
  15. import * as i4 from 'ng-zorro-antd/cdk/resize-observer';
  16. /**
  17. * Use of this source code is governed by an MIT-style license that can be
  18. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  19. */
  20. var AffixRespondEvents;
  21. (function (AffixRespondEvents) {
  22. AffixRespondEvents["resize"] = "resize";
  23. AffixRespondEvents["scroll"] = "scroll";
  24. AffixRespondEvents["touchstart"] = "touchstart";
  25. AffixRespondEvents["touchmove"] = "touchmove";
  26. AffixRespondEvents["touchend"] = "touchend";
  27. AffixRespondEvents["pageshow"] = "pageshow";
  28. AffixRespondEvents["load"] = "LOAD";
  29. })(AffixRespondEvents || (AffixRespondEvents = {}));
  30. /**
  31. * Use of this source code is governed by an MIT-style license that can be
  32. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  33. */
  34. function isTargetWindow(target) {
  35. return typeof window !== 'undefined' && target === window;
  36. }
  37. function getTargetRect(target) {
  38. return !isTargetWindow(target)
  39. ? target.getBoundingClientRect()
  40. : {
  41. top: 0,
  42. left: 0,
  43. bottom: 0
  44. };
  45. }
  46. const NZ_CONFIG_MODULE_NAME = 'affix';
  47. const NZ_AFFIX_CLS_PREFIX = 'ant-affix';
  48. const NZ_AFFIX_DEFAULT_SCROLL_TIME = 20;
  49. let NzAffixComponent = (() => {
  50. let _nzOffsetTop_decorators;
  51. let _nzOffsetTop_initializers = [];
  52. let _nzOffsetTop_extraInitializers = [];
  53. let _nzOffsetBottom_decorators;
  54. let _nzOffsetBottom_initializers = [];
  55. let _nzOffsetBottom_extraInitializers = [];
  56. return class NzAffixComponent {
  57. static {
  58. const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
  59. _nzOffsetTop_decorators = [WithConfig()];
  60. _nzOffsetBottom_decorators = [WithConfig()];
  61. __esDecorate(null, null, _nzOffsetTop_decorators, { kind: "field", name: "nzOffsetTop", static: false, private: false, access: { has: obj => "nzOffsetTop" in obj, get: obj => obj.nzOffsetTop, set: (obj, value) => { obj.nzOffsetTop = value; } }, metadata: _metadata }, _nzOffsetTop_initializers, _nzOffsetTop_extraInitializers);
  62. __esDecorate(null, null, _nzOffsetBottom_decorators, { kind: "field", name: "nzOffsetBottom", static: false, private: false, access: { has: obj => "nzOffsetBottom" in obj, get: obj => obj.nzOffsetBottom, set: (obj, value) => { obj.nzOffsetBottom = value; } }, metadata: _metadata }, _nzOffsetBottom_initializers, _nzOffsetBottom_extraInitializers);
  63. if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
  64. }
  65. nzConfigService;
  66. scrollSrv;
  67. ngZone;
  68. platform;
  69. renderer;
  70. nzResizeObserver;
  71. cdr;
  72. directionality;
  73. _nzModuleName = NZ_CONFIG_MODULE_NAME;
  74. fixedEl;
  75. nzTarget;
  76. nzOffsetTop = __runInitializers(this, _nzOffsetTop_initializers, void 0);
  77. nzOffsetBottom = (__runInitializers(this, _nzOffsetTop_extraInitializers), __runInitializers(this, _nzOffsetBottom_initializers, void 0));
  78. nzChange = (__runInitializers(this, _nzOffsetBottom_extraInitializers), new EventEmitter());
  79. dir = 'ltr';
  80. placeholderNode;
  81. affixStyle;
  82. placeholderStyle;
  83. positionChangeSubscription = Subscription.EMPTY;
  84. offsetChanged$ = new ReplaySubject(1);
  85. destroy$ = new Subject();
  86. timeout;
  87. document = inject(DOCUMENT);
  88. get target() {
  89. const el = this.nzTarget;
  90. return (typeof el === 'string' ? this.document.querySelector(el) : el) || window;
  91. }
  92. constructor(el, nzConfigService, scrollSrv, ngZone, platform, renderer, nzResizeObserver, cdr, directionality) {
  93. this.nzConfigService = nzConfigService;
  94. this.scrollSrv = scrollSrv;
  95. this.ngZone = ngZone;
  96. this.platform = platform;
  97. this.renderer = renderer;
  98. this.nzResizeObserver = nzResizeObserver;
  99. this.cdr = cdr;
  100. this.directionality = directionality;
  101. // The wrapper would stay at the original position as a placeholder.
  102. this.placeholderNode = el.nativeElement;
  103. }
  104. ngOnInit() {
  105. this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
  106. this.dir = direction;
  107. this.registerListeners();
  108. this.updatePosition({});
  109. this.cdr.detectChanges();
  110. });
  111. this.dir = this.directionality.value;
  112. }
  113. ngOnChanges(changes) {
  114. const { nzOffsetBottom, nzOffsetTop, nzTarget } = changes;
  115. if (nzOffsetBottom || nzOffsetTop) {
  116. this.offsetChanged$.next();
  117. }
  118. if (nzTarget) {
  119. this.registerListeners();
  120. }
  121. }
  122. ngAfterViewInit() {
  123. this.registerListeners();
  124. }
  125. ngOnDestroy() {
  126. this.removeListeners();
  127. }
  128. registerListeners() {
  129. if (!this.platform.isBrowser) {
  130. return;
  131. }
  132. this.removeListeners();
  133. const el = this.target === window ? this.document.body : this.target;
  134. this.positionChangeSubscription = this.ngZone.runOutsideAngular(() => merge(...Object.keys(AffixRespondEvents).map(evName => fromEvent(this.target, evName)), this.offsetChanged$.pipe(map(() => ({}))), this.nzResizeObserver.observe(el))
  135. .pipe(throttleTime(NZ_AFFIX_DEFAULT_SCROLL_TIME, undefined, { trailing: true }), takeUntil(this.destroy$))
  136. .subscribe(e => this.updatePosition(e)));
  137. this.timeout = setTimeout(() => this.updatePosition({}));
  138. }
  139. removeListeners() {
  140. clearTimeout(this.timeout);
  141. this.positionChangeSubscription.unsubscribe();
  142. this.destroy$.next(true);
  143. this.destroy$.complete();
  144. }
  145. getOffset(element, target) {
  146. const elemRect = element.getBoundingClientRect();
  147. const targetRect = getTargetRect(target);
  148. const scrollTop = this.scrollSrv.getScroll(target, true);
  149. const scrollLeft = this.scrollSrv.getScroll(target, false);
  150. const docElem = this.document.body;
  151. const clientTop = docElem.clientTop || 0;
  152. const clientLeft = docElem.clientLeft || 0;
  153. return {
  154. top: elemRect.top - targetRect.top + scrollTop - clientTop,
  155. left: elemRect.left - targetRect.left + scrollLeft - clientLeft,
  156. width: elemRect.width,
  157. height: elemRect.height
  158. };
  159. }
  160. setAffixStyle(e, affixStyle) {
  161. const originalAffixStyle = this.affixStyle;
  162. const isWindow = this.target === window;
  163. if (e.type === 'scroll' && originalAffixStyle && affixStyle && isWindow) {
  164. return;
  165. }
  166. if (shallowEqual(originalAffixStyle, affixStyle)) {
  167. return;
  168. }
  169. const fixed = !!affixStyle;
  170. const wrapEl = this.fixedEl.nativeElement;
  171. this.renderer.setStyle(wrapEl, 'cssText', getStyleAsText(affixStyle));
  172. this.affixStyle = affixStyle;
  173. if (fixed) {
  174. wrapEl.classList.add(NZ_AFFIX_CLS_PREFIX);
  175. }
  176. else {
  177. wrapEl.classList.remove(NZ_AFFIX_CLS_PREFIX);
  178. }
  179. this.updateRtlClass();
  180. if ((affixStyle && !originalAffixStyle) || (!affixStyle && originalAffixStyle)) {
  181. this.nzChange.emit(fixed);
  182. }
  183. }
  184. setPlaceholderStyle(placeholderStyle) {
  185. const originalPlaceholderStyle = this.placeholderStyle;
  186. if (shallowEqual(placeholderStyle, originalPlaceholderStyle)) {
  187. return;
  188. }
  189. this.renderer.setStyle(this.placeholderNode, 'cssText', getStyleAsText(placeholderStyle));
  190. this.placeholderStyle = placeholderStyle;
  191. }
  192. syncPlaceholderStyle(e) {
  193. if (!this.affixStyle) {
  194. return;
  195. }
  196. this.renderer.setStyle(this.placeholderNode, 'cssText', '');
  197. this.placeholderStyle = undefined;
  198. const styleObj = {
  199. width: this.placeholderNode.offsetWidth,
  200. height: this.fixedEl.nativeElement.offsetHeight
  201. };
  202. this.setAffixStyle(e, {
  203. ...this.affixStyle,
  204. ...styleObj
  205. });
  206. this.setPlaceholderStyle(styleObj);
  207. }
  208. updatePosition(e) {
  209. if (!this.platform.isBrowser) {
  210. return;
  211. }
  212. const targetNode = this.target;
  213. let offsetTop = this.nzOffsetTop;
  214. const scrollTop = this.scrollSrv.getScroll(targetNode, true);
  215. const elemOffset = this.getOffset(this.placeholderNode, targetNode);
  216. const fixedNode = this.fixedEl.nativeElement;
  217. const elemSize = {
  218. width: fixedNode.offsetWidth,
  219. height: fixedNode.offsetHeight
  220. };
  221. const offsetMode = {
  222. top: false,
  223. bottom: false
  224. };
  225. // Default to `offsetTop=0`.
  226. if (typeof offsetTop !== 'number' && typeof this.nzOffsetBottom !== 'number') {
  227. offsetMode.top = true;
  228. offsetTop = 0;
  229. }
  230. else {
  231. offsetMode.top = typeof offsetTop === 'number';
  232. offsetMode.bottom = typeof this.nzOffsetBottom === 'number';
  233. }
  234. const targetRect = getTargetRect(targetNode);
  235. const targetInnerHeight = targetNode.innerHeight || targetNode.clientHeight;
  236. if (scrollTop >= elemOffset.top - offsetTop && offsetMode.top) {
  237. const width = elemOffset.width;
  238. const top = targetRect.top + offsetTop;
  239. this.setAffixStyle(e, {
  240. position: 'fixed',
  241. top,
  242. left: targetRect.left + elemOffset.left,
  243. width
  244. });
  245. this.setPlaceholderStyle({
  246. width,
  247. height: elemSize.height
  248. });
  249. }
  250. else if (scrollTop <= elemOffset.top + elemSize.height + this.nzOffsetBottom - targetInnerHeight &&
  251. offsetMode.bottom) {
  252. const targetBottomOffset = targetNode === window ? 0 : window.innerHeight - targetRect.bottom;
  253. const width = elemOffset.width;
  254. this.setAffixStyle(e, {
  255. position: 'fixed',
  256. bottom: targetBottomOffset + this.nzOffsetBottom,
  257. left: targetRect.left + elemOffset.left,
  258. width
  259. });
  260. this.setPlaceholderStyle({
  261. width,
  262. height: elemOffset.height
  263. });
  264. }
  265. else {
  266. if (e.type === AffixRespondEvents.resize &&
  267. this.affixStyle &&
  268. this.affixStyle.position === 'fixed' &&
  269. this.placeholderNode.offsetWidth) {
  270. this.setAffixStyle(e, {
  271. ...this.affixStyle,
  272. width: this.placeholderNode.offsetWidth
  273. });
  274. }
  275. else {
  276. this.setAffixStyle(e);
  277. }
  278. this.setPlaceholderStyle();
  279. }
  280. if (e.type === 'resize') {
  281. this.syncPlaceholderStyle(e);
  282. }
  283. }
  284. updateRtlClass() {
  285. const wrapEl = this.fixedEl.nativeElement;
  286. if (this.dir === 'rtl') {
  287. if (wrapEl.classList.contains(NZ_AFFIX_CLS_PREFIX)) {
  288. wrapEl.classList.add(`${NZ_AFFIX_CLS_PREFIX}-rtl`);
  289. }
  290. else {
  291. wrapEl.classList.remove(`${NZ_AFFIX_CLS_PREFIX}-rtl`);
  292. }
  293. }
  294. else {
  295. wrapEl.classList.remove(`${NZ_AFFIX_CLS_PREFIX}-rtl`);
  296. }
  297. }
  298. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAffixComponent, deps: [{ token: i0.ElementRef }, { token: i1.NzConfigService }, { token: i2.NzScrollService }, { token: i0.NgZone }, { token: i3.Platform }, { token: i0.Renderer2 }, { token: i4.NzResizeObserver }, { token: i0.ChangeDetectorRef }, { token: i5.Directionality }], target: i0.ɵɵFactoryTarget.Component });
  299. static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "19.2.2", type: NzAffixComponent, isStandalone: true, selector: "nz-affix", inputs: { nzTarget: "nzTarget", nzOffsetTop: ["nzOffsetTop", "nzOffsetTop", numberAttributeWithZeroFallback], nzOffsetBottom: ["nzOffsetBottom", "nzOffsetBottom", numberAttributeWithZeroFallback] }, outputs: { nzChange: "nzChange" }, viewQueries: [{ propertyName: "fixedEl", first: true, predicate: ["fixedEl"], descendants: true, static: true }], exportAs: ["nzAffix"], usesOnChanges: true, ngImport: i0, template: `
  300. <div #fixedEl>
  301. <ng-content></ng-content>
  302. </div>
  303. `, isInline: true, dependencies: [{ kind: "ngmodule", type: BidiModule }, { kind: "ngmodule", type: PlatformModule }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
  304. };
  305. })();
  306. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAffixComponent, decorators: [{
  307. type: Component,
  308. args: [{
  309. selector: 'nz-affix',
  310. exportAs: 'nzAffix',
  311. imports: [BidiModule, PlatformModule],
  312. template: `
  313. <div #fixedEl>
  314. <ng-content></ng-content>
  315. </div>
  316. `,
  317. changeDetection: ChangeDetectionStrategy.OnPush,
  318. encapsulation: ViewEncapsulation.None
  319. }]
  320. }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i1.NzConfigService }, { type: i2.NzScrollService }, { type: i0.NgZone }, { type: i3.Platform }, { type: i0.Renderer2 }, { type: i4.NzResizeObserver }, { type: i0.ChangeDetectorRef }, { type: i5.Directionality }], propDecorators: { fixedEl: [{
  321. type: ViewChild,
  322. args: ['fixedEl', { static: true }]
  323. }], nzTarget: [{
  324. type: Input
  325. }], nzOffsetTop: [{
  326. type: Input,
  327. args: [{ transform: numberAttributeWithZeroFallback }]
  328. }], nzOffsetBottom: [{
  329. type: Input,
  330. args: [{ transform: numberAttributeWithZeroFallback }]
  331. }], nzChange: [{
  332. type: Output
  333. }] } });
  334. /**
  335. * Use of this source code is governed by an MIT-style license that can be
  336. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  337. */
  338. class NzAffixModule {
  339. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAffixModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  340. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzAffixModule, imports: [NzAffixComponent], exports: [NzAffixComponent] });
  341. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAffixModule, imports: [NzAffixComponent] });
  342. }
  343. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzAffixModule, decorators: [{
  344. type: NgModule,
  345. args: [{
  346. exports: [NzAffixComponent],
  347. imports: [NzAffixComponent]
  348. }]
  349. }] });
  350. /**
  351. * Use of this source code is governed by an MIT-style license that can be
  352. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  353. */
  354. /**
  355. * Generated bundle index. Do not edit.
  356. */
  357. export { NzAffixComponent, NzAffixModule };
  358. //# sourceMappingURL=ng-zorro-antd-affix.mjs.map