Skip to content

feat: add touch support for reorderable and resizable features #181

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

Open
wants to merge 2 commits 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 @@ -49,6 +49,19 @@
visibility: visible;
}
}
:host {
@media (hover: none) {
touch-action: none;

.resize-handle {
visibility: visible;
}

.datatable-header-cell-label.draggable {
user-select: none;
}
}
}

.resize-handle--not-resizable {
:host(:hover) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { NgTemplateOutlet } from '@angular/common';
import { InnerSortEvent, TableColumnInternal } from '../../types/internal.types';
import { fromEvent, Subscription, takeUntil } from 'rxjs';
import { getPositionFromEvent } from '../../utils/events';

@Component({
selector: 'datatable-header-cell',
Expand Down Expand Up @@ -57,7 +58,11 @@ import { fromEvent, Subscription, takeUntil } from 'rxjs';
<span (click)="onSort()" [class]="sortClass"> </span>
</div>
@if (column.resizeable) {
<span class="resize-handle" (mousedown)="onMousedown($event)"></span>
<span
class="resize-handle"
(mousedown)="onMousedown($event)"
(touchstart)="onMousedown($event)"
></span>
}
`,
host: {
Expand Down Expand Up @@ -219,6 +224,9 @@ export class DataTableHeaderCellComponent implements OnInit, OnDestroy {
@HostListener('contextmenu', ['$event'])
onContextmenu($event: MouseEvent): void {
this.columnContextmenu.emit({ event: $event, column: this.column });
if (this.column.draggable) {
$event.preventDefault();
}
}

@HostListener('keydown.enter')
Expand Down Expand Up @@ -281,17 +289,18 @@ export class DataTableHeaderCellComponent implements OnInit, OnDestroy {
}
}

protected onMousedown(event: MouseEvent): void {
protected onMousedown(event: MouseEvent | TouchEvent): void {
const isMouse = event instanceof MouseEvent;
const initialWidth = this.element.clientWidth;
const mouseDownScreenX = event.screenX;
const { screenX } = getPositionFromEvent(event);
event.stopPropagation();

const mouseup = fromEvent(document, 'mouseup');
const mouseup = fromEvent<MouseEvent | TouchEvent>(document, isMouse ? 'mouseup' : 'touchend');
this.subscription = mouseup.subscribe(() => this.onMouseup());

const mouseMoveSub = fromEvent(document, 'mousemove')
const mouseMoveSub = fromEvent<MouseEvent | TouchEvent>(document, isMouse ? 'mousemove' : 'touchmove')
.pipe(takeUntil(mouseup))
.subscribe((e: Event) => this.move(e, initialWidth, mouseDownScreenX));
.subscribe((e: MouseEvent | TouchEvent) => this.move(e, initialWidth, screenX));

this.subscription.add(mouseMoveSub);
}
Expand All @@ -303,8 +312,8 @@ export class DataTableHeaderCellComponent implements OnInit, OnDestroy {
}
}

private move(event: Event, initialWidth: number, mouseDownScreenX: number): void {
const movementX = (event as MouseEvent).screenX - mouseDownScreenX;
private move(event: MouseEvent | TouchEvent, initialWidth: number, mouseDownScreenX: number): void {
const movementX = getPositionFromEvent(event).screenX - mouseDownScreenX;
const newWidth = initialWidth + movementX;
this.resizing.emit({ width: newWidth, column: this.column });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class DataTableHeaderComponent implements OnDestroy, OnChanges {
@Input() reorderable: boolean;
@Input() verticalScrollVisible = false;

dragEventTarget?: MouseEvent;
dragEventTarget?: MouseEvent | TouchEvent;

@HostBinding('style.height')
@Input()
Expand Down Expand Up @@ -214,7 +214,7 @@ export class DataTableHeaderComponent implements OnDestroy, OnChanges {
this.destroyed = true;
}

onLongPressStart({ event, model }: { event: MouseEvent; model: TableColumnInternal<unknown> }) {
onLongPressStart({ event, model }: { event: MouseEvent | TouchEvent; model: TableColumnInternal<unknown> }) {
model.dragging = true;
this.dragEventTarget = event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ describe('DraggableDirective', () => {

beforeEach(() => {
element.classList.add('draggable');
mouseDown = {
target: element,
// eslint-disable-next-line @typescript-eslint/no-empty-function
preventDefault: () => {}
} as MouseEvent;
mouseDown = new MouseEvent('mousedown');
Object.defineProperty(mouseDown, 'target', { value: element });
});

// or else the document:mouseup event can fire again when resizing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { fromEvent, Subscription } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { DraggableDragEvent, TableColumnInternal } from '../types/internal.types';
import { getPositionFromEvent } from '../utils/events';

/**
* Draggable Directive for Angular2
Expand Down Expand Up @@ -54,7 +55,7 @@ export class DraggableDirective implements OnDestroy, OnChanges {
this._destroySubscription();
}

onMouseup(event: MouseEvent): void {
onMouseup(event: MouseEvent | TouchEvent): void {
if (!this.isDragging) {
return;
}
Expand All @@ -72,20 +73,27 @@ export class DraggableDirective implements OnDestroy, OnChanges {
}
}

onMousedown(event: MouseEvent): void {
onMousedown(event: MouseEvent | TouchEvent): void {
const isMouse = event instanceof MouseEvent;
// we only want to drag the inner header text
const isDragElm = (event.target as HTMLElement).classList.contains('draggable');

if (isDragElm && (this.dragX || this.dragY)) {
event.preventDefault();
this.isDragging = true;

const mouseDownPos = { x: event.clientX, y: event.clientY };
const mouseDownPos = getPositionFromEvent(event);

const mouseup = fromEvent<MouseEvent>(document, 'mouseup');
const mouseup = fromEvent<MouseEvent | TouchEvent>(
document,
isMouse ? 'mouseup' : 'touchend'
);
this.subscription = mouseup.subscribe(ev => this.onMouseup(ev));

const mouseMoveSub = fromEvent<MouseEvent>(document, 'mousemove')
const mouseMoveSub = fromEvent<MouseEvent | TouchEvent>(
document,
isMouse ? 'mousemove' : 'touchmove'
)
.pipe(takeUntil(mouseup))
.subscribe(ev => this.move(ev, mouseDownPos));

Expand All @@ -99,13 +107,14 @@ export class DraggableDirective implements OnDestroy, OnChanges {
}
}

move(event: MouseEvent, mouseDownPos: { x: number; y: number }): void {
move(event: MouseEvent | TouchEvent, mouseDownPos: { clientX: number; clientY: number }): void {
if (!this.isDragging) {
return;
}

const x = event.clientX - mouseDownPos.x;
const y = event.clientY - mouseDownPos.y;
const { clientX, clientY } = getPositionFromEvent(event);
const x = clientX - mouseDownPos.clientX;
const y = clientY - mouseDownPos.clientY;

if (this.dragX) {
this.element.style.left = `${x}px`;
Expand Down
49 changes: 24 additions & 25 deletions projects/ngx-datatable/src/lib/directives/long-press.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,47 @@ import {
booleanAttribute,
Directive,
EventEmitter,
HostBinding,
HostListener,
Input,
numberAttribute,
OnDestroy,
Output
Output,
signal
} from '@angular/core';
import { fromEvent, Subscription } from 'rxjs';
import { TableColumnInternal } from '../types/internal.types';

@Directive({
selector: '[long-press]',
standalone: true
standalone: true,
host: {
'(touchstart)': 'onMouseDown($event)',
'(mousedown)': 'onMouseDown($event)',
'[class.press]': 'pressing()',
'[class.longpress]': 'isLongPressing()'
}
})
export class LongPressDirective implements OnDestroy {
@Input({ transform: booleanAttribute }) pressEnabled = true;
@Input() pressModel: TableColumnInternal;
@Input({ transform: numberAttribute }) duration = 500;

@Output() longPressStart = new EventEmitter<{ event: MouseEvent; model: TableColumnInternal }>();
@Output() longPressStart = new EventEmitter<{
event: MouseEvent | TouchEvent;
model: TableColumnInternal;
}>();
@Output() longPressEnd = new EventEmitter<{ model: TableColumnInternal }>();

pressing: boolean;
isLongPressing: boolean;
pressing = signal(false);
isLongPressing = signal(false);
timeout: any;

subscription: Subscription;

@HostBinding('class.press')
get press(): boolean {
return this.pressing;
}

@HostBinding('class.longpress')
get isLongPress(): boolean {
return this.isLongPressing;
}
onMouseDown(event: MouseEvent | TouchEvent): void {
const isMouse = event instanceof MouseEvent;

@HostListener('mousedown', ['$event'])
onMouseDown(event: MouseEvent): void {
// don't do right/middle clicks
if (event.which !== 1 || !this.pressEnabled) {
if (!this.pressEnabled || (isMouse && event.button !== 0)) {
return;
}

Expand All @@ -53,14 +52,14 @@ export class LongPressDirective implements OnDestroy {
return;
}

this.pressing = true;
this.isLongPressing = false;
this.pressing.set(true);
this.isLongPressing.set(false);

const mouseup = fromEvent(document, 'mouseup');
const mouseup = fromEvent(document, isMouse ? 'mouseup' : 'touchend');
this.subscription = mouseup.subscribe(() => this.endPress());

this.timeout = setTimeout(() => {
this.isLongPressing = true;
this.isLongPressing.set(true);
this.longPressStart.emit({
event,
model: this.pressModel
Expand All @@ -70,8 +69,8 @@ export class LongPressDirective implements OnDestroy {

endPress(): void {
clearTimeout(this.timeout);
this.isLongPressing = false;
this.pressing = false;
this.isLongPressing.set(false);
this.pressing.set(false);
this._destroySubscription();

this.longPressEnd.emit({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
TableColumnInternal,
TargetChangedEvent
} from '../types/internal.types';
import { getPositionFromEvent } from '../utils/events';

interface OrderPosition {
left: number;
Expand Down Expand Up @@ -141,11 +142,10 @@ export class OrderableDirective implements AfterContentInit, OnDestroy {
element.style.left = 'auto';
}

isTarget(model: TableColumnInternal, event: MouseEvent) {
isTarget(model: TableColumnInternal, event: MouseEvent | TouchEvent) {
let i = 0;
const x = event.x || event.clientX;
const y = event.y || event.clientY;
const targets = this.document.elementsFromPoint(x, y);
const { clientX, clientY } = getPositionFromEvent(event);
const targets = this.document.elementsFromPoint(clientX, clientY);

for (const id in this.positions) {
// current column position which throws event.
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-datatable/src/lib/types/internal.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface Page {
}

export interface DraggableDragEvent {
event: MouseEvent;
event: MouseEvent | TouchEvent;
element: HTMLElement;
model: TableColumnInternal;
}
Expand Down
14 changes: 14 additions & 0 deletions projects/ngx-datatable/src/lib/utils/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Extracts the position (x, y coordinates) from a MouseEvent or TouchEvent.
*
* @param {MouseEvent | TouchEvent} event - The event object from which to extract the position. Can be either a MouseEvent or a TouchEvent.
* @return {{ x: number, y: number }} An object containing the x and y coordinates of the event relative to the viewport.
*/
export function getPositionFromEvent(event: MouseEvent | TouchEvent): {
clientX: number;
clientY: number;
screenX: number;
screenY: number;
} {
return event instanceof MouseEvent ? event as MouseEvent : event.changedTouches[0] as Touch;
}
Loading