Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: column resize directive #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
}
<span (click)="onSort()" [class]="sortClass"> </span>
</div>
<ng-content [resize-handle]> </ng-content>
`,
host: {
class: 'datatable-header-cell'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ import {
@for (column of colGroup.columns; track column.$$id) {
<datatable-header-cell
role="columnheader"
#resizeable="resizeable"
resizeable
[resizeEnabled]="column.resizeable"
(resize)="onColumnResized($event, column)"
(resizing)="onColumnResizing($event, column)"
long-press
Expand Down Expand Up @@ -79,6 +79,12 @@ import {
(select)="select.emit($event)"
(columnContextmenu)="columnContextmenu.emit($event)"
>
<span
resize-handle
(mousedown)="column.resizeable ? resizeable.onMousedown($event) : null"
[class.resize-handle]="column.resizeable"
[class.resize-handle--not-resizable]="!column.resizeable"
></span>
Comment on lines +82 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we just include this header-cell component?
I also don't see a need for a resizable directive. Just including it in header-cell would be fine I guess

</datatable-header-cell>
}
</div>
Expand Down
64 changes: 11 additions & 53 deletions projects/ngx-datatable/src/lib/directives/resizeable.directive.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,19 @@
import {
AfterViewInit,
booleanAttribute,
Directive,
ElementRef,
EventEmitter,
HostBinding,
HostListener,
inject,
Input,
numberAttribute,
OnDestroy,
Output,
Renderer2
} from '@angular/core';
import { Directive, ElementRef, EventEmitter, inject, OnDestroy, Output } from '@angular/core';
import { fromEvent, Subscription } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Directive({
selector: '[resizeable]'
selector: '[resizeable]',
exportAs: 'resizeable'
})
export class ResizeableDirective implements OnDestroy, AfterViewInit {
private renderer = inject(Renderer2);

@HostBinding('class.resizeable') @Input({ transform: booleanAttribute }) resizeEnabled = true;
@Input({ transform: numberAttribute }) minWidth: number;
@Input({ transform: numberAttribute }) maxWidth: number;

export class ResizeableDirective implements OnDestroy {
@Output() resize: EventEmitter<any> = new EventEmitter();
@Output() resizing: EventEmitter<any> = new EventEmitter();

element = inject(ElementRef).nativeElement;
subscription: Subscription;
private resizeHandle: HTMLElement;

ngAfterViewInit(): void {
const renderer2 = this.renderer;
this.resizeHandle = renderer2.createElement('span');
if (this.resizeEnabled) {
renderer2.addClass(this.resizeHandle, 'resize-handle');
} else {
renderer2.addClass(this.resizeHandle, 'resize-handle--not-resizable');
}
renderer2.appendChild(this.element, this.resizeHandle);
}

ngOnDestroy(): void {
this._destroySubscription();
if (this.renderer.destroyNode) {
this.renderer.destroyNode(this.resizeHandle);
} else if (this.resizeHandle) {
this.renderer.removeChild(this.renderer.parentNode(this.resizeHandle), this.resizeHandle);
}
}

onMouseup(): void {
Expand All @@ -60,24 +23,19 @@ export class ResizeableDirective implements OnDestroy, AfterViewInit {
}
}

@HostListener('mousedown', ['$event'])
onMousedown(event: MouseEvent): void {
const isHandle = (event.target as HTMLElement).classList.contains('resize-handle');
const initialWidth = this.element.clientWidth;
const mouseDownScreenX = event.screenX;
event.stopPropagation();

if (isHandle) {
event.stopPropagation();
const mouseup = fromEvent(document, 'mouseup');
this.subscription = mouseup.subscribe((ev: MouseEvent) => this.onMouseup());

const mouseup = fromEvent(document, 'mouseup');
this.subscription = mouseup.subscribe((ev: MouseEvent) => this.onMouseup());
const mouseMoveSub = fromEvent(document, 'mousemove')
.pipe(takeUntil(mouseup))
.subscribe((e: MouseEvent) => this.move(e, initialWidth, mouseDownScreenX));

const mouseMoveSub = fromEvent(document, 'mousemove')
.pipe(takeUntil(mouseup))
.subscribe((e: MouseEvent) => this.move(e, initialWidth, mouseDownScreenX));

this.subscription.add(mouseMoveSub);
}
this.subscription.add(mouseMoveSub);
}

move(event: MouseEvent, initialWidth: number, mouseDownScreenX: number): void {
Expand Down