ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
659 lines (658 loc) • 20.3 kB
JavaScript
import * as i0 from "@angular/core";
import { Component, DOCUMENT, DestroyRef, Directive, ElementRef, EventEmitter, Injectable, Input, NgModule, NgZone, Output, Renderer2, booleanAttribute, inject, numberAttribute } from "@angular/core";
import { Platform, normalizePassiveListenerOptions } from "@angular/cdk/platform";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { filter } from "rxjs/operators";
import { ensureInBounds, fromEventOutsideAngular, isTouchEvent } from "ng-zorro-antd/core/util";
import { Subject, merge } from "rxjs";
function getEventWithPoint(event) {
return isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] : event;
}
var NzResizableService = class NzResizableService {
document = inject(DOCUMENT);
ngZone = inject(NgZone);
listeners = /* @__PURE__ */ new Map();
handleMouseDownOutsideAngular$ = new Subject();
documentMouseUpOutsideAngular$ = new Subject();
documentMouseMoveOutsideAngular$ = new Subject();
mouseEnteredOutsideAngular$ = new Subject();
startResizing(event) {
const _isTouchEvent = isTouchEvent(event);
this.clearListeners();
const moveEvent = _isTouchEvent ? "touchmove" : "mousemove";
const upEvent = _isTouchEvent ? "touchend" : "mouseup";
const moveEventHandler = (e) => {
this.documentMouseMoveOutsideAngular$.next(e);
};
const upEventHandler = (e) => {
this.documentMouseUpOutsideAngular$.next(e);
this.clearListeners();
};
this.listeners.set(moveEvent, moveEventHandler);
this.listeners.set(upEvent, upEventHandler);
this.ngZone.runOutsideAngular(() => {
this.listeners.forEach((handler, name) => {
this.document.addEventListener(name, handler);
});
});
}
clearListeners() {
this.listeners.forEach((handler, name) => {
this.document.removeEventListener(name, handler);
});
this.listeners.clear();
}
ngOnDestroy() {
this.handleMouseDownOutsideAngular$.complete();
this.documentMouseUpOutsideAngular$.complete();
this.documentMouseMoveOutsideAngular$.complete();
this.mouseEnteredOutsideAngular$.complete();
this.clearListeners();
}
static ɵfac = i0.ɵɵngDeclareFactory({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableService,
deps: [],
target: i0.ɵɵFactoryTarget.Injectable
});
static ɵprov = i0.ɵɵngDeclareInjectable({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableService
});
};
i0.ɵɵngDeclareClassMetadata({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableService,
decorators: [{ type: Injectable }]
});
var NzResizableDirective = class NzResizableDirective {
elementRef = inject(ElementRef);
renderer = inject(Renderer2);
nzResizableService = inject(NzResizableService);
platform = inject(Platform);
ngZone = inject(NgZone);
destroyRef = inject(DestroyRef);
nzBounds = "parent";
nzMaxHeight;
nzMaxWidth;
nzMinHeight = 40;
nzMinWidth = 40;
nzGridColumnCount = -1;
nzMaxColumn = -1;
nzMinColumn = -1;
nzLockAspectRatio = false;
nzPreview = false;
nzDisabled = false;
nzResize = new EventEmitter();
nzResizeEnd = new EventEmitter();
nzResizeStart = new EventEmitter();
resizing = false;
elRect;
currentHandleEvent = null;
ghostElement = null;
el;
sizeCache = null;
constructor() {
this.nzResizableService.handleMouseDownOutsideAngular$.pipe(takeUntilDestroyed()).subscribe((event) => {
if (this.nzDisabled) return;
this.resizing = true;
this.nzResizableService.startResizing(event.mouseEvent);
this.currentHandleEvent = event;
if (this.nzResizeStart.observers.length) this.ngZone.run(() => this.nzResizeStart.emit({
mouseEvent: event.mouseEvent,
direction: event.direction
}));
this.elRect = this.el.getBoundingClientRect();
});
this.nzResizableService.documentMouseUpOutsideAngular$.pipe(takeUntilDestroyed(), filter(Boolean)).subscribe((event) => {
if (this.resizing) {
this.resizing = false;
this.nzResizableService.documentMouseUpOutsideAngular$.next(null);
this.endResize(event);
}
});
this.nzResizableService.documentMouseMoveOutsideAngular$.pipe(takeUntilDestroyed()).subscribe((event) => {
if (this.resizing) this.resize(event);
});
}
setPosition() {
const position = getComputedStyle(this.el).position;
if (position === "static" || !position) this.renderer.setStyle(this.el, "position", "relative");
}
calcSize(width, height, ratio) {
let newWidth;
let newHeight;
let maxWidth;
let maxHeight;
let col = 0;
let spanWidth = 0;
let minWidth = this.nzMinWidth;
let boundWidth = Infinity;
let boundHeight = Infinity;
if (this.nzBounds === "parent") {
const parent = this.renderer.parentNode(this.el);
if (parent instanceof HTMLElement) {
const parentRect = parent.getBoundingClientRect();
boundWidth = parentRect.width;
boundHeight = parentRect.height;
}
} else if (this.nzBounds === "window") {
if (typeof window !== "undefined") {
boundWidth = window.innerWidth;
boundHeight = window.innerHeight;
}
} else if (this.nzBounds && this.nzBounds.nativeElement && this.nzBounds.nativeElement instanceof HTMLElement) {
const boundsRect = this.nzBounds.nativeElement.getBoundingClientRect();
boundWidth = boundsRect.width;
boundHeight = boundsRect.height;
}
maxWidth = ensureInBounds(this.nzMaxWidth, boundWidth);
maxHeight = ensureInBounds(this.nzMaxHeight, boundHeight);
if (this.nzGridColumnCount !== -1) {
spanWidth = maxWidth / this.nzGridColumnCount;
minWidth = this.nzMinColumn !== -1 ? spanWidth * this.nzMinColumn : minWidth;
maxWidth = this.nzMaxColumn !== -1 ? spanWidth * this.nzMaxColumn : maxWidth;
}
if (ratio !== -1) {
if (/(left|right)/i.test(this.currentHandleEvent.direction)) {
newWidth = Math.min(Math.max(width, minWidth), maxWidth);
newHeight = Math.min(Math.max(newWidth / ratio, this.nzMinHeight), maxHeight);
if (newHeight >= maxHeight || newHeight <= this.nzMinHeight) newWidth = Math.min(Math.max(newHeight * ratio, minWidth), maxWidth);
} else {
newHeight = Math.min(Math.max(height, this.nzMinHeight), maxHeight);
newWidth = Math.min(Math.max(newHeight * ratio, minWidth), maxWidth);
if (newWidth >= maxWidth || newWidth <= minWidth) newHeight = Math.min(Math.max(newWidth / ratio, this.nzMinHeight), maxHeight);
}
} else {
newWidth = Math.min(Math.max(width, minWidth), maxWidth);
newHeight = Math.min(Math.max(height, this.nzMinHeight), maxHeight);
}
if (this.nzGridColumnCount !== -1) {
col = Math.round(newWidth / spanWidth);
newWidth = col * spanWidth;
}
return {
col,
width: newWidth,
height: newHeight
};
}
resize(event) {
const elRect = this.elRect;
const resizeEvent = getEventWithPoint(event);
const handleEvent = getEventWithPoint(this.currentHandleEvent.mouseEvent);
let width = elRect.width;
let height = elRect.height;
const ratio = this.nzLockAspectRatio ? width / height : -1;
switch (this.currentHandleEvent.direction) {
case "bottomRight":
width = resizeEvent.clientX - elRect.left;
height = resizeEvent.clientY - elRect.top;
break;
case "bottomLeft":
width = elRect.width + handleEvent.clientX - resizeEvent.clientX;
height = resizeEvent.clientY - elRect.top;
break;
case "topRight":
width = resizeEvent.clientX - elRect.left;
height = elRect.height + handleEvent.clientY - resizeEvent.clientY;
break;
case "topLeft":
width = elRect.width + handleEvent.clientX - resizeEvent.clientX;
height = elRect.height + handleEvent.clientY - resizeEvent.clientY;
break;
case "top":
height = elRect.height + handleEvent.clientY - resizeEvent.clientY;
break;
case "right":
width = resizeEvent.clientX - elRect.left;
break;
case "bottom":
height = resizeEvent.clientY - elRect.top;
break;
case "left": width = elRect.width + handleEvent.clientX - resizeEvent.clientX;
}
const size = this.calcSize(width, height, ratio);
this.sizeCache = { ...size };
if (this.nzResize.observers.length) this.ngZone.run(() => {
this.nzResize.emit({
...size,
mouseEvent: event,
direction: this.currentHandleEvent.direction
});
});
if (this.nzPreview) this.previewResize(size);
}
endResize(event) {
this.removeGhostElement();
const size = this.sizeCache ? { ...this.sizeCache } : {
width: this.elRect.width,
height: this.elRect.height
};
if (this.nzResizeEnd.observers.length) this.ngZone.run(() => {
this.nzResizeEnd.emit({
...size,
mouseEvent: event,
direction: this.currentHandleEvent.direction
});
});
this.sizeCache = null;
this.currentHandleEvent = null;
}
previewResize({ width, height }) {
this.createGhostElement();
this.renderer.setStyle(this.ghostElement, "width", `${width}px`);
this.renderer.setStyle(this.ghostElement, "height", `${height}px`);
}
createGhostElement() {
if (!this.ghostElement) {
this.ghostElement = this.renderer.createElement("div");
this.renderer.setAttribute(this.ghostElement, "class", "nz-resizable-preview");
}
this.renderer.appendChild(this.el, this.ghostElement);
}
removeGhostElement() {
if (this.ghostElement) this.renderer.removeChild(this.el, this.ghostElement);
}
ngAfterViewInit() {
if (!this.platform.isBrowser) return;
this.el = this.elementRef.nativeElement;
this.setPosition();
fromEventOutsideAngular(this.el, "mouseenter").pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
this.nzResizableService.mouseEnteredOutsideAngular$.next(true);
});
fromEventOutsideAngular(this.el, "mouseleave").pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
this.nzResizableService.mouseEnteredOutsideAngular$.next(false);
});
}
ngOnDestroy() {
this.ghostElement = null;
this.sizeCache = null;
}
static ɵfac = i0.ɵɵngDeclareFactory({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableDirective,
deps: [],
target: i0.ɵɵFactoryTarget.Directive
});
static ɵdir = i0.ɵɵngDeclareDirective({
minVersion: "16.1.0",
version: "22.2.2",
type: NzResizableDirective,
isStandalone: true,
selector: "[nz-resizable]",
inputs: {
nzBounds: "nzBounds",
nzMaxHeight: "nzMaxHeight",
nzMaxWidth: "nzMaxWidth",
nzMinHeight: [
"nzMinHeight",
"nzMinHeight",
numberAttribute
],
nzMinWidth: [
"nzMinWidth",
"nzMinWidth",
numberAttribute
],
nzGridColumnCount: [
"nzGridColumnCount",
"nzGridColumnCount",
numberAttribute
],
nzMaxColumn: [
"nzMaxColumn",
"nzMaxColumn",
numberAttribute
],
nzMinColumn: [
"nzMinColumn",
"nzMinColumn",
numberAttribute
],
nzLockAspectRatio: [
"nzLockAspectRatio",
"nzLockAspectRatio",
booleanAttribute
],
nzPreview: [
"nzPreview",
"nzPreview",
booleanAttribute
],
nzDisabled: [
"nzDisabled",
"nzDisabled",
booleanAttribute
]
},
outputs: {
nzResize: "nzResize",
nzResizeEnd: "nzResizeEnd",
nzResizeStart: "nzResizeStart"
},
host: {
properties: {
"class.nz-resizable-resizing": "resizing",
"class.nz-resizable-disabled": "nzDisabled"
},
classAttribute: "nz-resizable"
},
providers: [NzResizableService],
exportAs: ["nzResizable"],
ngImport: i0
});
};
i0.ɵɵngDeclareClassMetadata({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableDirective,
decorators: [{
type: Directive,
args: [{
selector: "[nz-resizable]",
exportAs: "nzResizable",
providers: [NzResizableService],
host: {
class: "nz-resizable",
"[class.nz-resizable-resizing]": "resizing",
"[class.nz-resizable-disabled]": "nzDisabled"
}
}]
}],
ctorParameters: () => [],
propDecorators: {
nzBounds: [{ type: Input }],
nzMaxHeight: [{ type: Input }],
nzMaxWidth: [{ type: Input }],
nzMinHeight: [{
type: Input,
args: [{ transform: numberAttribute }]
}],
nzMinWidth: [{
type: Input,
args: [{ transform: numberAttribute }]
}],
nzGridColumnCount: [{
type: Input,
args: [{ transform: numberAttribute }]
}],
nzMaxColumn: [{
type: Input,
args: [{ transform: numberAttribute }]
}],
nzMinColumn: [{
type: Input,
args: [{ transform: numberAttribute }]
}],
nzLockAspectRatio: [{
type: Input,
args: [{ transform: booleanAttribute }]
}],
nzPreview: [{
type: Input,
args: [{ transform: booleanAttribute }]
}],
nzDisabled: [{
type: Input,
args: [{ transform: booleanAttribute }]
}],
nzResize: [{ type: Output }],
nzResizeEnd: [{ type: Output }],
nzResizeStart: [{ type: Output }]
}
});
var NzResizeHandleMouseDownEvent = class {
direction;
mouseEvent;
constructor(direction, mouseEvent) {
this.direction = direction;
this.mouseEvent = mouseEvent;
}
};
const passiveEventListenerOptions = normalizePassiveListenerOptions({ passive: true });
var NzResizeHandleComponent = class NzResizeHandleComponent {
nzResizableService = inject(NzResizableService);
renderer = inject(Renderer2);
el = inject(ElementRef).nativeElement;
destroyRef = inject(DestroyRef);
nzDirection = "bottomRight";
nzCursorType = "window";
nzMouseDown = new EventEmitter();
ngOnInit() {
this.nzResizableService.mouseEnteredOutsideAngular$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((entered) => {
if (entered) this.renderer.addClass(this.el, "nz-resizable-handle-box-hover");
else this.renderer.removeClass(this.el, "nz-resizable-handle-box-hover");
});
merge(fromEventOutsideAngular(this.el, "mousedown", passiveEventListenerOptions), fromEventOutsideAngular(this.el, "touchstart", passiveEventListenerOptions)).pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => {
this.nzResizableService.handleMouseDownOutsideAngular$.next(new NzResizeHandleMouseDownEvent(this.nzDirection, event));
});
}
onPointerDown(event) {
event.target.setPointerCapture(event.pointerId);
}
onPointerUp(event) {
event.target.releasePointerCapture(event.pointerId);
}
static ɵfac = i0.ɵɵngDeclareFactory({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizeHandleComponent,
deps: [],
target: i0.ɵɵFactoryTarget.Component
});
static ɵcmp = i0.ɵɵngDeclareComponent({
minVersion: "14.0.0",
version: "22.2.2",
type: NzResizeHandleComponent,
isStandalone: true,
selector: "nz-resize-handle, [nz-resize-handle]",
inputs: {
nzDirection: "nzDirection",
nzCursorType: "nzCursorType"
},
outputs: { nzMouseDown: "nzMouseDown" },
host: {
listeners: {
"pointerdown": "onPointerDown($event)",
"pointerup": "onPointerUp($event)"
},
properties: {
"class.nz-resizable-handle-top": "nzDirection === 'top'",
"class.nz-resizable-handle-right": "nzDirection === 'right'",
"class.nz-resizable-handle-bottom": "nzDirection === 'bottom'",
"class.nz-resizable-handle-left": "nzDirection === 'left'",
"class.nz-resizable-handle-topRight": "nzDirection === 'topRight'",
"class.nz-resizable-handle-bottomRight": "nzDirection === 'bottomRight'",
"class.nz-resizable-handle-bottomLeft": "nzDirection === 'bottomLeft'",
"class.nz-resizable-handle-topLeft": "nzDirection === 'topLeft'",
"class.nz-resizable-handle-cursor-type-grid": "nzCursorType === 'grid'",
"class.nz-resizable-handle-cursor-type-window": "nzCursorType === 'window'"
},
classAttribute: "nz-resizable-handle"
},
exportAs: ["nzResizeHandle"],
ngImport: i0,
template: `<ng-content />`,
isInline: true
});
};
i0.ɵɵngDeclareClassMetadata({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizeHandleComponent,
decorators: [{
type: Component,
args: [{
selector: "nz-resize-handle, [nz-resize-handle]",
exportAs: "nzResizeHandle",
template: `<ng-content />`,
host: {
class: "nz-resizable-handle",
"[class.nz-resizable-handle-top]": `nzDirection === 'top'`,
"[class.nz-resizable-handle-right]": `nzDirection === 'right'`,
"[class.nz-resizable-handle-bottom]": `nzDirection === 'bottom'`,
"[class.nz-resizable-handle-left]": `nzDirection === 'left'`,
"[class.nz-resizable-handle-topRight]": `nzDirection === 'topRight'`,
"[class.nz-resizable-handle-bottomRight]": `nzDirection === 'bottomRight'`,
"[class.nz-resizable-handle-bottomLeft]": `nzDirection === 'bottomLeft'`,
"[class.nz-resizable-handle-topLeft]": `nzDirection === 'topLeft'`,
"[class.nz-resizable-handle-cursor-type-grid]": `nzCursorType === 'grid'`,
"[class.nz-resizable-handle-cursor-type-window]": `nzCursorType === 'window'`,
"(pointerdown)": "onPointerDown($event)",
"(pointerup)": "onPointerUp($event)"
}
}]
}],
propDecorators: {
nzDirection: [{ type: Input }],
nzCursorType: [{ type: Input }],
nzMouseDown: [{ type: Output }]
}
});
const DEFAULT_RESIZE_DIRECTION = [
"bottomRight",
"topRight",
"bottomLeft",
"topLeft",
"bottom",
"right",
"top",
"left"
];
function normalizeResizeHandleOptions(value) {
return value.map((val) => {
if (typeof val === "string") return {
direction: val,
cursorType: "window"
};
return val;
});
}
var NzResizeHandlesComponent = class NzResizeHandlesComponent {
nzDirections = DEFAULT_RESIZE_DIRECTION;
resizeHandleOptions = normalizeResizeHandleOptions(this.nzDirections);
ngOnChanges(changes) {
const { nzDirections } = changes;
if (nzDirections) this.resizeHandleOptions = normalizeResizeHandleOptions(nzDirections.currentValue);
}
static ɵfac = i0.ɵɵngDeclareFactory({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizeHandlesComponent,
deps: [],
target: i0.ɵɵFactoryTarget.Component
});
static ɵcmp = i0.ɵɵngDeclareComponent({
minVersion: "17.0.0",
version: "22.2.2",
type: NzResizeHandlesComponent,
isStandalone: true,
selector: "nz-resize-handles",
inputs: { nzDirections: "nzDirections" },
exportAs: ["nzResizeHandles"],
usesOnChanges: true,
ngImport: i0,
template: `
(option of resizeHandleOptions; track option) {
<nz-resize-handle [nzDirection]="option.direction" [nzCursorType]="option.cursorType" />
}
`,
isInline: true,
dependencies: [{
kind: "component",
type: NzResizeHandleComponent,
selector: "nz-resize-handle, [nz-resize-handle]",
inputs: ["nzDirection", "nzCursorType"],
outputs: ["nzMouseDown"],
exportAs: ["nzResizeHandle"]
}]
});
};
i0.ɵɵngDeclareClassMetadata({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizeHandlesComponent,
decorators: [{
type: Component,
args: [{
selector: "nz-resize-handles",
exportAs: "nzResizeHandles",
template: `
(option of resizeHandleOptions; track option) {
<nz-resize-handle [nzDirection]="option.direction" [nzCursorType]="option.cursorType" />
}
`,
imports: [NzResizeHandleComponent]
}]
}],
propDecorators: { nzDirections: [{ type: Input }] }
});
var NzResizableModule = class NzResizableModule {
static ɵfac = i0.ɵɵngDeclareFactory({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableModule,
deps: [],
target: i0.ɵɵFactoryTarget.NgModule
});
static ɵmod = i0.ɵɵngDeclareNgModule({
minVersion: "14.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableModule,
imports: [
NzResizableDirective,
NzResizeHandleComponent,
NzResizeHandlesComponent
],
exports: [
NzResizableDirective,
NzResizeHandleComponent,
NzResizeHandlesComponent
]
});
static ɵinj = i0.ɵɵngDeclareInjector({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableModule
});
};
i0.ɵɵngDeclareClassMetadata({
minVersion: "12.0.0",
version: "22.2.2",
ngImport: i0,
type: NzResizableModule,
decorators: [{
type: NgModule,
args: [{
imports: [
NzResizableDirective,
NzResizeHandleComponent,
NzResizeHandlesComponent
],
exports: [
NzResizableDirective,
NzResizeHandleComponent,
NzResizeHandlesComponent
]
}]
}]
});
export { DEFAULT_RESIZE_DIRECTION, NzResizableDirective, NzResizableModule, NzResizableService, NzResizeHandleComponent, NzResizeHandleMouseDownEvent, NzResizeHandlesComponent, getEventWithPoint };
//# sourceMappingURL=ng-zorro-antd-resizable.mjs.map