ng-zorro-antd-core-services.mjs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. import * as i0 from '@angular/core';
  2. import { Injectable, inject } from '@angular/core';
  3. import { Subject } from 'rxjs';
  4. import { auditTime, finalize, map, filter, takeUntil, startWith, distinctUntilChanged } from 'rxjs/operators';
  5. import { environment } from 'ng-zorro-antd/core/environments';
  6. import { getEventPosition, isTouchEvent } from 'ng-zorro-antd/core/util';
  7. import { DOCUMENT } from '@angular/common';
  8. import { reqAnimFrame } from 'ng-zorro-antd/core/polyfill';
  9. import * as i2 from '@angular/cdk/layout';
  10. import * as i1 from '@angular/cdk/platform';
  11. /**
  12. * Use of this source code is governed by an MIT-style license that can be
  13. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  14. */
  15. const NOOP = () => { };
  16. class NzResizeService {
  17. ngZone;
  18. rendererFactory2;
  19. resizeSource$ = new Subject();
  20. listeners = 0;
  21. renderer;
  22. disposeHandle = NOOP;
  23. handler = () => {
  24. this.ngZone.run(() => {
  25. this.resizeSource$.next();
  26. });
  27. };
  28. constructor(ngZone, rendererFactory2) {
  29. this.ngZone = ngZone;
  30. this.rendererFactory2 = rendererFactory2;
  31. this.renderer = this.rendererFactory2.createRenderer(null, null);
  32. }
  33. ngOnDestroy() {
  34. // Caretaker note: the `handler` is an instance property (it's not defined on the class prototype).
  35. // The `handler` captures `this` and prevents the `NzResizeService` from being GC'd.
  36. this.handler = NOOP;
  37. }
  38. subscribe() {
  39. this.registerListener();
  40. return this.resizeSource$.pipe(auditTime(16), finalize(() => this.unregisterListener()));
  41. }
  42. unsubscribe() {
  43. this.unregisterListener();
  44. }
  45. registerListener() {
  46. if (this.listeners === 0) {
  47. this.ngZone.runOutsideAngular(() => {
  48. this.disposeHandle = this.renderer.listen('window', 'resize', this.handler);
  49. });
  50. }
  51. this.listeners += 1;
  52. }
  53. unregisterListener() {
  54. this.listeners -= 1;
  55. if (this.listeners === 0) {
  56. this.disposeHandle();
  57. this.disposeHandle = NOOP;
  58. }
  59. }
  60. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeService, deps: [{ token: i0.NgZone }, { token: i0.RendererFactory2 }], target: i0.ɵɵFactoryTarget.Injectable });
  61. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeService, providedIn: 'root' });
  62. }
  63. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzResizeService, decorators: [{
  64. type: Injectable,
  65. args: [{
  66. providedIn: 'root'
  67. }]
  68. }], ctorParameters: () => [{ type: i0.NgZone }, { type: i0.RendererFactory2 }] });
  69. /**
  70. * Use of this source code is governed by an MIT-style license that can be
  71. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  72. */
  73. /**
  74. * When running in test, singletons should not be destroyed. So we keep references of singletons
  75. * in this global variable.
  76. */
  77. const testSingleRegistry = new Map();
  78. /**
  79. * Some singletons should have life cycle that is same to Angular's. This service make sure that
  80. * those singletons get destroyed in HMR.
  81. */
  82. class NzSingletonService {
  83. get singletonRegistry() {
  84. return environment.isTestMode ? testSingleRegistry : this._singletonRegistry;
  85. }
  86. /**
  87. * This registry is used to register singleton in dev mode.
  88. * So that singletons get destroyed when hot module reload happens.
  89. *
  90. * This works in prod mode too but with no specific effect.
  91. */
  92. _singletonRegistry = new Map();
  93. registerSingletonWithKey(key, target) {
  94. const alreadyHave = this.singletonRegistry.has(key);
  95. const item = alreadyHave ? this.singletonRegistry.get(key) : this.withNewTarget(target);
  96. if (!alreadyHave) {
  97. this.singletonRegistry.set(key, item);
  98. }
  99. }
  100. unregisterSingletonWithKey(key) {
  101. if (this.singletonRegistry.has(key)) {
  102. this.singletonRegistry.delete(key);
  103. }
  104. }
  105. getSingletonWithKey(key) {
  106. return this.singletonRegistry.has(key) ? this.singletonRegistry.get(key).target : null;
  107. }
  108. withNewTarget(target) {
  109. return {
  110. target
  111. };
  112. }
  113. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzSingletonService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  114. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzSingletonService, providedIn: 'root' });
  115. }
  116. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzSingletonService, decorators: [{
  117. type: Injectable,
  118. args: [{
  119. providedIn: 'root'
  120. }]
  121. }] });
  122. /**
  123. * Use of this source code is governed by an MIT-style license that can be
  124. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  125. */
  126. function getPagePosition(event) {
  127. const e = getEventPosition(event);
  128. return {
  129. x: e.pageX,
  130. y: e.pageY
  131. };
  132. }
  133. /**
  134. * This module provide a global dragging service to other components.
  135. */
  136. class NzDragService {
  137. draggingThreshold = 5;
  138. currentDraggingSequence = null;
  139. currentStartingPoint = null;
  140. handleRegistry = new Set();
  141. renderer;
  142. constructor(rendererFactory2) {
  143. this.renderer = rendererFactory2.createRenderer(null, null);
  144. }
  145. requestDraggingSequence(event) {
  146. if (!this.handleRegistry.size) {
  147. this.registerDraggingHandler(isTouchEvent(event));
  148. }
  149. // Complete last dragging sequence if a new target is dragged.
  150. if (this.currentDraggingSequence) {
  151. this.currentDraggingSequence.complete();
  152. }
  153. this.currentStartingPoint = getPagePosition(event);
  154. this.currentDraggingSequence = new Subject();
  155. return this.currentDraggingSequence.pipe(map((e) => ({
  156. x: e.pageX - this.currentStartingPoint.x,
  157. y: e.pageY - this.currentStartingPoint.y
  158. })), filter((e) => Math.abs(e.x) > this.draggingThreshold || Math.abs(e.y) > this.draggingThreshold), finalize(() => this.teardownDraggingSequence()));
  159. }
  160. registerDraggingHandler(isTouch) {
  161. if (isTouch) {
  162. this.handleRegistry.add({
  163. teardown: this.renderer.listen('document', 'touchmove', (e) => {
  164. if (this.currentDraggingSequence) {
  165. this.currentDraggingSequence.next(e.touches[0] || e.changedTouches[0]);
  166. }
  167. })
  168. });
  169. this.handleRegistry.add({
  170. teardown: this.renderer.listen('document', 'touchend', () => {
  171. if (this.currentDraggingSequence) {
  172. this.currentDraggingSequence.complete();
  173. }
  174. })
  175. });
  176. }
  177. else {
  178. this.handleRegistry.add({
  179. teardown: this.renderer.listen('document', 'mousemove', e => {
  180. if (this.currentDraggingSequence) {
  181. this.currentDraggingSequence.next(e);
  182. }
  183. })
  184. });
  185. this.handleRegistry.add({
  186. teardown: this.renderer.listen('document', 'mouseup', () => {
  187. if (this.currentDraggingSequence) {
  188. this.currentDraggingSequence.complete();
  189. }
  190. })
  191. });
  192. }
  193. }
  194. teardownDraggingSequence() {
  195. this.currentDraggingSequence = null;
  196. }
  197. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzDragService, deps: [{ token: i0.RendererFactory2 }], target: i0.ɵɵFactoryTarget.Injectable });
  198. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzDragService, providedIn: 'root' });
  199. }
  200. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzDragService, decorators: [{
  201. type: Injectable,
  202. args: [{
  203. providedIn: 'root'
  204. }]
  205. }], ctorParameters: () => [{ type: i0.RendererFactory2 }] });
  206. /**
  207. * Use of this source code is governed by an MIT-style license that can be
  208. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  209. */
  210. function easeInOutCubic(t, b, c, d) {
  211. const cc = c - b;
  212. let tt = t / (d / 2);
  213. if (tt < 1) {
  214. return (cc / 2) * tt * tt * tt + b;
  215. }
  216. else {
  217. return (cc / 2) * ((tt -= 2) * tt * tt + 2) + b;
  218. }
  219. }
  220. class NzScrollService {
  221. ngZone;
  222. doc = inject(DOCUMENT);
  223. constructor(ngZone) {
  224. this.ngZone = ngZone;
  225. }
  226. /** Set the position of the scroll bar of `el`. */
  227. setScrollTop(el, topValue = 0) {
  228. if (el === window) {
  229. this.doc.body.scrollTop = topValue;
  230. this.doc.documentElement.scrollTop = topValue;
  231. }
  232. else {
  233. el.scrollTop = topValue;
  234. }
  235. }
  236. /** Get position of `el` against window. */
  237. getOffset(el) {
  238. const ret = {
  239. top: 0,
  240. left: 0
  241. };
  242. if (!el || !el.getClientRects().length) {
  243. return ret;
  244. }
  245. const rect = el.getBoundingClientRect();
  246. if (rect.width || rect.height) {
  247. const doc = el.ownerDocument.documentElement;
  248. ret.top = rect.top - doc.clientTop;
  249. ret.left = rect.left - doc.clientLeft;
  250. }
  251. else {
  252. ret.top = rect.top;
  253. ret.left = rect.left;
  254. }
  255. return ret;
  256. }
  257. /** Get the position of the scoll bar of `el`. */
  258. // TODO: remove '| Window' as the fallback already happens here
  259. getScroll(target, top = true) {
  260. if (typeof window === 'undefined') {
  261. return 0;
  262. }
  263. const method = top ? 'scrollTop' : 'scrollLeft';
  264. let result = 0;
  265. if (this.isWindow(target)) {
  266. result = target[top ? 'pageYOffset' : 'pageXOffset'];
  267. }
  268. else if (target instanceof Document) {
  269. result = target.documentElement[method];
  270. }
  271. else if (target) {
  272. result = target[method];
  273. }
  274. if (target && !this.isWindow(target) && typeof result !== 'number') {
  275. result = (target.ownerDocument || target).documentElement[method];
  276. }
  277. return result;
  278. }
  279. isWindow(obj) {
  280. return obj !== null && obj !== undefined && obj === obj.window;
  281. }
  282. /**
  283. * Scroll `el` to some position with animation.
  284. *
  285. * @param containerEl container, `window` by default
  286. * @param y Scroll to `top`, 0 by default
  287. */
  288. scrollTo(containerEl, y = 0, options = {}) {
  289. const target = containerEl ? containerEl : window;
  290. const scrollTop = this.getScroll(target);
  291. const startTime = Date.now();
  292. const { easing, callback, duration = 450 } = options;
  293. const frameFunc = () => {
  294. const timestamp = Date.now();
  295. const time = timestamp - startTime;
  296. const nextScrollTop = (easing || easeInOutCubic)(time > duration ? duration : time, scrollTop, y, duration);
  297. if (this.isWindow(target)) {
  298. target.scrollTo(window.pageXOffset, nextScrollTop);
  299. }
  300. else if (target instanceof HTMLDocument || target.constructor.name === 'HTMLDocument') {
  301. target.documentElement.scrollTop = nextScrollTop;
  302. }
  303. else {
  304. target.scrollTop = nextScrollTop;
  305. }
  306. if (time < duration) {
  307. reqAnimFrame(frameFunc);
  308. }
  309. else if (typeof callback === 'function') {
  310. // Caretaker note: the `frameFunc` is called within the `<root>` zone, but we have to re-enter
  311. // the Angular zone when calling custom callback to be backwards-compatible.
  312. this.ngZone.run(callback);
  313. }
  314. };
  315. // Caretaker note: the `requestAnimationFrame` triggers change detection, but updating a `scrollTop` property or
  316. // calling `window.scrollTo` doesn't require Angular to run `ApplicationRef.tick()`.
  317. this.ngZone.runOutsideAngular(() => reqAnimFrame(frameFunc));
  318. }
  319. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzScrollService, deps: [{ token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Injectable });
  320. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzScrollService, providedIn: 'root' });
  321. }
  322. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzScrollService, decorators: [{
  323. type: Injectable,
  324. args: [{
  325. providedIn: 'root'
  326. }]
  327. }], ctorParameters: () => [{ type: i0.NgZone }] });
  328. var NzBreakpointEnum;
  329. (function (NzBreakpointEnum) {
  330. NzBreakpointEnum["xxl"] = "xxl";
  331. NzBreakpointEnum["xl"] = "xl";
  332. NzBreakpointEnum["lg"] = "lg";
  333. NzBreakpointEnum["md"] = "md";
  334. NzBreakpointEnum["sm"] = "sm";
  335. NzBreakpointEnum["xs"] = "xs";
  336. })(NzBreakpointEnum || (NzBreakpointEnum = {}));
  337. const gridResponsiveMap = {
  338. xs: '(max-width: 575px)',
  339. sm: '(min-width: 576px)',
  340. md: '(min-width: 768px)',
  341. lg: '(min-width: 992px)',
  342. xl: '(min-width: 1200px)',
  343. xxl: '(min-width: 1600px)'
  344. };
  345. const siderResponsiveMap = {
  346. xs: '(max-width: 479.98px)',
  347. sm: '(max-width: 575.98px)',
  348. md: '(max-width: 767.98px)',
  349. lg: '(max-width: 991.98px)',
  350. xl: '(max-width: 1199.98px)',
  351. xxl: '(max-width: 1599.98px)'
  352. };
  353. class NzBreakpointService {
  354. resizeService;
  355. mediaMatcher;
  356. destroy$ = new Subject();
  357. constructor(resizeService, mediaMatcher) {
  358. this.resizeService = resizeService;
  359. this.mediaMatcher = mediaMatcher;
  360. this.resizeService
  361. .subscribe()
  362. .pipe(takeUntil(this.destroy$))
  363. .subscribe(() => { });
  364. }
  365. ngOnDestroy() {
  366. this.destroy$.next();
  367. }
  368. subscribe(breakpointMap, fullMap) {
  369. if (fullMap) {
  370. // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  371. const get = () => this.matchMedia(breakpointMap, true);
  372. return this.resizeService.subscribe().pipe(map(get), startWith(get()), distinctUntilChanged((x, y) => x[0] === y[0]), map(x => x[1]));
  373. }
  374. else {
  375. // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  376. const get = () => this.matchMedia(breakpointMap);
  377. return this.resizeService.subscribe().pipe(map(get), startWith(get()), distinctUntilChanged());
  378. }
  379. }
  380. matchMedia(breakpointMap, fullMap) {
  381. let bp = NzBreakpointEnum.md;
  382. const breakpointBooleanMap = {};
  383. Object.keys(breakpointMap).map(breakpoint => {
  384. const castBP = breakpoint;
  385. const matched = this.mediaMatcher.matchMedia(gridResponsiveMap[castBP]).matches;
  386. breakpointBooleanMap[breakpoint] = matched;
  387. if (matched) {
  388. bp = castBP;
  389. }
  390. });
  391. if (fullMap) {
  392. return [bp, breakpointBooleanMap];
  393. }
  394. else {
  395. return bp;
  396. }
  397. }
  398. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzBreakpointService, deps: [{ token: NzResizeService }, { token: i2.MediaMatcher }], target: i0.ɵɵFactoryTarget.Injectable });
  399. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzBreakpointService, providedIn: 'root' });
  400. }
  401. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzBreakpointService, decorators: [{
  402. type: Injectable,
  403. args: [{
  404. providedIn: 'root'
  405. }]
  406. }], ctorParameters: () => [{ type: NzResizeService }, { type: i2.MediaMatcher }] });
  407. /**
  408. * Use of this source code is governed by an MIT-style license that can be
  409. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  410. */
  411. class NzDestroyService extends Subject {
  412. ngOnDestroy() {
  413. this.next();
  414. this.complete();
  415. }
  416. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzDestroyService, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
  417. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzDestroyService });
  418. }
  419. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzDestroyService, decorators: [{
  420. type: Injectable
  421. }] });
  422. class ImagePreloadService {
  423. platform;
  424. counter = new Map();
  425. linkRefs = new Map();
  426. document = inject(DOCUMENT);
  427. constructor(platform) {
  428. this.platform = platform;
  429. }
  430. addPreload(option) {
  431. if (this.platform.isBrowser) {
  432. return () => void 0;
  433. }
  434. const uniqueKey = `${option.src}${option.srcset}`;
  435. let currentCount = this.counter.get(uniqueKey) || 0;
  436. currentCount++;
  437. this.counter.set(uniqueKey, currentCount);
  438. if (!this.linkRefs.has(uniqueKey)) {
  439. const linkNode = this.appendPreloadLink(option);
  440. this.linkRefs.set(uniqueKey, linkNode);
  441. }
  442. return () => {
  443. if (this.counter.has(uniqueKey)) {
  444. let count = this.counter.get(uniqueKey);
  445. count--;
  446. if (count === 0) {
  447. const linkNode = this.linkRefs.get(uniqueKey);
  448. this.removePreloadLink(linkNode);
  449. this.counter.delete(uniqueKey);
  450. this.linkRefs.delete(uniqueKey);
  451. }
  452. else {
  453. this.counter.set(uniqueKey, count);
  454. }
  455. }
  456. };
  457. }
  458. appendPreloadLink(option) {
  459. const linkNode = this.document.createElement('link');
  460. linkNode.setAttribute('rel', 'preload');
  461. linkNode.setAttribute('as', 'image');
  462. linkNode.setAttribute('href', option.src);
  463. if (option.srcset) {
  464. linkNode.setAttribute('imagesrcset', option.srcset);
  465. }
  466. this.document.head.appendChild(linkNode);
  467. return linkNode;
  468. }
  469. removePreloadLink(linkNode) {
  470. if (this.document.head.contains(linkNode)) {
  471. this.document.head.removeChild(linkNode);
  472. }
  473. }
  474. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ImagePreloadService, deps: [{ token: i1.Platform }], target: i0.ɵɵFactoryTarget.Injectable });
  475. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ImagePreloadService, providedIn: 'root' });
  476. }
  477. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ImagePreloadService, decorators: [{
  478. type: Injectable,
  479. args: [{
  480. providedIn: 'root'
  481. }]
  482. }], ctorParameters: () => [{ type: i1.Platform }] });
  483. /**
  484. * Use of this source code is governed by an MIT-style license that can be
  485. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  486. */
  487. /**
  488. * Generated bundle index. Do not edit.
  489. */
  490. export { ImagePreloadService, NzBreakpointEnum, NzBreakpointService, NzDestroyService, NzDragService, NzResizeService, NzScrollService, NzSingletonService, gridResponsiveMap, siderResponsiveMap };
  491. //# sourceMappingURL=ng-zorro-antd-core-services.mjs.map