UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

465 lines (464 loc) • 14.1 kB
import * as i0 from "@angular/core"; import { DOCUMENT, DestroyRef, Injectable, NgZone, RendererFactory2, inject } from "@angular/core"; import { Subject } from "rxjs"; import { auditTime, distinctUntilChanged, filter, finalize, map, startWith } from "rxjs/operators"; import { environment } from "ng-zorro-antd/core/environments"; import { getEventPosition, isTouchEvent } from "ng-zorro-antd/core/util"; import { requestAnimationFrame } from "ng-zorro-antd/core/polyfill"; import { MediaMatcher } from "@angular/cdk/layout"; import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import { Platform } from "@angular/cdk/platform"; const NOOP = () => {}; var NzResizeService = class NzResizeService { ngZone = inject(NgZone); destroyRef = inject(DestroyRef); resizeSource$ = new Subject(); listeners = 0; renderer = inject(RendererFactory2).createRenderer(null, null); disposeHandle = NOOP; handler = () => { this.ngZone.run(() => { this.resizeSource$.next(); }); }; constructor() { this.destroyRef.onDestroy(() => { this.handler = NOOP; }); } connect() { this.registerListener(); return this.resizeSource$.pipe(auditTime(16), finalize(() => this.unregisterListener())); } disconnet() { this.unregisterListener(); } registerListener() { if (this.listeners === 0) this.ngZone.runOutsideAngular(() => { this.disposeHandle = this.renderer.listen("window", "resize", this.handler); }); this.listeners += 1; } unregisterListener() { this.listeners -= 1; if (this.listeners === 0) { this.disposeHandle(); this.disposeHandle = NOOP; } } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzResizeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzResizeService, providedIn: "root" }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzResizeService, decorators: [{ type: Injectable, args: [{ providedIn: "root" }] }], ctorParameters: () => [] }); const testSingleRegistry = /* @__PURE__ */ new Map(); var NzSingletonService = class NzSingletonService { get singletonRegistry() { return environment.isTestMode ? testSingleRegistry : this._singletonRegistry; } _singletonRegistry = /* @__PURE__ */ new Map(); registerSingletonWithKey(key, target) { const alreadyHave = this.singletonRegistry.has(key); const item = alreadyHave ? this.singletonRegistry.get(key) : this.withNewTarget(target); if (!alreadyHave) this.singletonRegistry.set(key, item); } unregisterSingletonWithKey(key) { if (this.singletonRegistry.has(key)) this.singletonRegistry.delete(key); } getSingletonWithKey(key) { return this.singletonRegistry.has(key) ? this.singletonRegistry.get(key).target : null; } withNewTarget(target) { return { target }; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzSingletonService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzSingletonService, providedIn: "root" }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzSingletonService, decorators: [{ type: Injectable, args: [{ providedIn: "root" }] }] }); function getPagePosition(event) { const e = getEventPosition(event); return { x: e.pageX, y: e.pageY }; } var NzDragService = class NzDragService { draggingThreshold = 5; currentDraggingSequence = null; currentStartingPoint = null; handleRegistry = /* @__PURE__ */ new Set(); renderer = inject(RendererFactory2).createRenderer(null, null); requestDraggingSequence(event) { this.currentDraggingSequence?.complete(); this.teardownDraggingHandlers(); this.registerDraggingHandler(isTouchEvent(event)); this.currentStartingPoint = getPagePosition(event); this.currentDraggingSequence = new Subject(); return this.currentDraggingSequence.pipe(map((e) => ({ x: e.pageX - this.currentStartingPoint.x, y: e.pageY - this.currentStartingPoint.y })), filter((e) => Math.abs(e.x) > this.draggingThreshold || Math.abs(e.y) > this.draggingThreshold), finalize(() => this.teardownDraggingSequence())); } registerDraggingHandler(isTouch) { if (isTouch) { this.handleRegistry.add({ teardown: this.renderer.listen("document", "touchmove", (e) => { this.currentDraggingSequence?.next(e.touches[0] || e.changedTouches[0]); }) }); this.handleRegistry.add({ teardown: this.renderer.listen("document", "touchend", () => { this.currentDraggingSequence?.complete(); }) }); } else { this.handleRegistry.add({ teardown: this.renderer.listen("document", "mousemove", (e) => { this.currentDraggingSequence?.next(e); }) }); this.handleRegistry.add({ teardown: this.renderer.listen("document", "mouseup", () => { this.currentDraggingSequence?.complete(); }) }); } } teardownDraggingSequence() { this.currentDraggingSequence = null; this.teardownDraggingHandlers(); } teardownDraggingHandlers() { for (const { teardown } of this.handleRegistry) teardown(); this.handleRegistry.clear(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzDragService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzDragService, providedIn: "root" }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzDragService, decorators: [{ type: Injectable, args: [{ providedIn: "root" }] }] }); function easeInOutCubic(t, b, c, d) { const cc = c - b; let tt = t / (d / 2); if (tt < 1) return cc / 2 * tt * tt * tt + b; else return cc / 2 * ((tt -= 2) * tt * tt + 2) + b; } var NzScrollService = class NzScrollService { doc = inject(DOCUMENT); ngZone = inject(NgZone); setScrollTop(el, topValue = 0) { if (el === window) { this.doc.body.scrollTop = topValue; this.doc.documentElement.scrollTop = topValue; } else el.scrollTop = topValue; } getOffset(el) { const ret = { top: 0, left: 0 }; if (!el || !el.getClientRects().length) return ret; const rect = el.getBoundingClientRect(); if (rect.width || rect.height) { const doc = el.ownerDocument.documentElement; ret.top = rect.top - doc.clientTop; ret.left = rect.left - doc.clientLeft; } else { ret.top = rect.top; ret.left = rect.left; } return ret; } getScroll(target, top = true) { if (typeof window === "undefined") return 0; const method = top ? "scrollTop" : "scrollLeft"; let result = 0; if (this.isWindow(target)) result = target[top ? "pageYOffset" : "pageXOffset"]; else if (target instanceof Document) result = target.documentElement[method]; else if (target) result = target[method]; if (target && !this.isWindow(target) && typeof result !== "number") result = (target.ownerDocument || target).documentElement[method]; return result; } isWindow(obj) { return obj !== null && obj !== void 0 && obj === obj.window; } scrollTo(containerEl, y = 0, options = {}) { const target = containerEl ? containerEl : window; const scrollTop = this.getScroll(target); const startTime = Date.now(); const { easing, callback, duration = 450 } = options; const frameFunc = () => { const time = Date.now() - startTime; const nextScrollTop = (easing || easeInOutCubic)(time > duration ? duration : time, scrollTop, y, duration); if (this.isWindow(target)) target.scrollTo(window.pageXOffset, nextScrollTop); else if (target instanceof HTMLDocument || target.constructor.name === "HTMLDocument") target.documentElement.scrollTop = nextScrollTop; else target.scrollTop = nextScrollTop; if (time < duration) requestAnimationFrame(frameFunc); else if (typeof callback === "function") this.ngZone.run(callback); }; this.ngZone.runOutsideAngular(() => requestAnimationFrame(frameFunc)); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzScrollService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzScrollService, providedIn: "root" }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzScrollService, decorators: [{ type: Injectable, args: [{ providedIn: "root" }] }] }); var NzBreakpointEnum; (function(NzBreakpointEnum) { NzBreakpointEnum["xxxl"] = "xxxl"; NzBreakpointEnum["xxl"] = "xxl"; NzBreakpointEnum["xl"] = "xl"; NzBreakpointEnum["lg"] = "lg"; NzBreakpointEnum["md"] = "md"; NzBreakpointEnum["sm"] = "sm"; NzBreakpointEnum["xs"] = "xs"; })(NzBreakpointEnum || (NzBreakpointEnum = {})); const responsiveArray = [ "xxxl", "xxl", "xl", "lg", "md", "sm", "xs" ]; const gridResponsiveMap = { xs: "(max-width: 575px)", sm: "(min-width: 576px)", md: "(min-width: 768px)", lg: "(min-width: 992px)", xl: "(min-width: 1200px)", xxl: "(min-width: 1600px)", xxxl: "(min-width: 1920px)" }; const siderResponsiveMap = { xs: "(max-width: 479.98px)", sm: "(max-width: 575.98px)", md: "(max-width: 767.98px)", lg: "(max-width: 991.98px)", xl: "(max-width: 1199.98px)", xxl: "(max-width: 1599.98px)", xxxl: "(max-width: 1919.98px)" }; var NzBreakpointService = class NzBreakpointService { resizeService = inject(NzResizeService); mediaMatcher = inject(MediaMatcher); constructor() { this.resizeService.connect().pipe(takeUntilDestroyed()).subscribe(); } subscribe(breakpointMap, fullMap) { if (fullMap) return this.resizeService.connect().pipe(startWith(void 0), map(() => this.matchMedia(breakpointMap, true)), distinctUntilChanged((x, y) => x[0] === y[0]), map((x) => x[1])); else return this.resizeService.connect().pipe(startWith(void 0), map(() => this.matchMedia(breakpointMap)), distinctUntilChanged()); } matchMedia(breakpointMap, fullMap) { let bp = NzBreakpointEnum.md; const breakpointBooleanMap = {}; Object.keys(breakpointMap).map((breakpoint) => { const castBP = breakpoint; const matched = this.mediaMatcher.matchMedia(gridResponsiveMap[castBP]).matches; breakpointBooleanMap[castBP] = matched; if (matched) bp = castBP; }); if (fullMap) return [bp, breakpointBooleanMap]; else return bp; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzBreakpointService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzBreakpointService, providedIn: "root" }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzBreakpointService, decorators: [{ type: Injectable, args: [{ providedIn: "root" }] }], ctorParameters: () => [] }); var NzDestroyService = class NzDestroyService extends Subject { ngOnDestroy() { this.next(); this.complete(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzDestroyService, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzDestroyService }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzDestroyService, decorators: [{ type: Injectable }] }); var ImagePreloadService = class ImagePreloadService { counter = /* @__PURE__ */ new Map(); linkRefs = /* @__PURE__ */ new Map(); document = inject(DOCUMENT); platform = inject(Platform); addPreload(option) { if (this.platform.isBrowser) return () => void 0; const uniqueKey = `${option.src}${option.srcset}`; let currentCount = this.counter.get(uniqueKey) || 0; currentCount++; this.counter.set(uniqueKey, currentCount); if (!this.linkRefs.has(uniqueKey)) { const linkNode = this.appendPreloadLink(option); this.linkRefs.set(uniqueKey, linkNode); } return () => { if (this.counter.has(uniqueKey)) { let count = this.counter.get(uniqueKey); count--; if (count === 0) { const linkNode = this.linkRefs.get(uniqueKey); this.removePreloadLink(linkNode); this.counter.delete(uniqueKey); this.linkRefs.delete(uniqueKey); } else this.counter.set(uniqueKey, count); } }; } appendPreloadLink(option) { const linkNode = this.document.createElement("link"); linkNode.setAttribute("rel", "preload"); linkNode.setAttribute("as", "image"); linkNode.setAttribute("href", option.src); if (option.srcset) linkNode.setAttribute("imagesrcset", option.srcset); this.document.head.appendChild(linkNode); return linkNode; } removePreloadLink(linkNode) { if (this.document.head.contains(linkNode)) this.document.head.removeChild(linkNode); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: ImagePreloadService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: ImagePreloadService, providedIn: "root" }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: ImagePreloadService, decorators: [{ type: Injectable, args: [{ providedIn: "root" }] }] }); export { ImagePreloadService, NzBreakpointEnum, NzBreakpointService, NzDestroyService, NzDragService, NzResizeService, NzScrollService, NzSingletonService, gridResponsiveMap, responsiveArray, siderResponsiveMap }; //# sourceMappingURL=ng-zorro-antd-core-services.mjs.map