From ca22341abd9f09358e79b95b96d3af7d54fc7ed9 Mon Sep 17 00:00:00 2001 From: "Ahmad K. Bawaneh" Date: Sat, 28 Oct 2023 00:16:18 +0300 Subject: [PATCH] fix #855 ColumnResize sending spurious events --- .../domino/ui/datatable/ColumnConfig.java | 13 ++ .../datatable/events/ColumnResizingEvent.java | 111 ++++++++++++++++++ .../plugins/column/ResizeColumnsPlugin.java | 34 ++++-- 3 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 domino-ui/src/main/java/org/dominokit/domino/ui/datatable/events/ColumnResizingEvent.java diff --git a/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/ColumnConfig.java b/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/ColumnConfig.java index 205108141..0a205abba 100644 --- a/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/ColumnConfig.java +++ b/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/ColumnConfig.java @@ -1212,6 +1212,19 @@ public ColumnConfig removeChild(Node child) { return this; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ColumnConfig that = (ColumnConfig) o; + return Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + /** * Removes the specified child element from the header element of this column. * diff --git a/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/events/ColumnResizingEvent.java b/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/events/ColumnResizingEvent.java new file mode 100644 index 000000000..f5b6bc5da --- /dev/null +++ b/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/events/ColumnResizingEvent.java @@ -0,0 +1,111 @@ +/* + * Copyright © 2019 Dominokit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dominokit.domino.ui.datatable.events; + +import org.dominokit.domino.ui.datatable.ColumnConfig; + +/** + * This event will be fired when a column gets resized + * + *

Event type: COLUMN_RESIZED -> 'column-resized' + */ +public class ColumnResizingEvent implements TableEvent { + /** Constant COLUMN_RESIZED="column-resized" */ + public static final String COLUMN_RESIZING = "column-resizing"; + + private final ColumnConfig column; + private final double sizeDiff; + private final boolean completed; + + /** + * Factory method to create a new Event instance + * + * @param column a {@link ColumnConfig} + * @param sizeDiff a double + * @return a {@link ColumnResizingEvent} + */ + public static ColumnResizingEvent of(ColumnConfig column, double sizeDiff) { + return new ColumnResizingEvent(column, sizeDiff); + } + + /** + * Factory method to create a new Event instance + * + * @param column a {@link ColumnConfig} + * @param sizeDiff a double + * @param completed a boolean + * @return a {@link ColumnResizingEvent} + */ + public static ColumnResizingEvent of(ColumnConfig column, double sizeDiff, boolean completed) { + return new ColumnResizingEvent(column, sizeDiff, completed); + } + + /** + * Creates a new Event instance + * + * @param column a {@link ColumnConfig} + * @param sizeDiff a double + */ + public ColumnResizingEvent(ColumnConfig column, double sizeDiff) { + this(column, sizeDiff, false); + } + + /** + * Creates a new Event instance + * + * @param column a {@link ColumnConfig} + * @param sizeDiff a double + * @param completed a boolean + */ + public ColumnResizingEvent(ColumnConfig column, double sizeDiff, boolean completed) { + this.column = column; + this.sizeDiff = sizeDiff; + this.completed = completed; + } + + /** + * Getter for the field column. + * + * @return a {@link ColumnConfig} + */ + public ColumnConfig getColumn() { + return column; + } + + /** + * Getter for the field sizeDiff. + * + * @return a double + */ + public double getSizeDiff() { + return sizeDiff; + } + + /** + * isCompleted. + * + * @return a boolean + */ + public boolean isCompleted() { + return completed; + } + + /** {@inheritDoc} */ + @Override + public String getType() { + return COLUMN_RESIZING; + } +} diff --git a/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/plugins/column/ResizeColumnsPlugin.java b/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/plugins/column/ResizeColumnsPlugin.java index 3091b97a5..e41f98898 100644 --- a/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/plugins/column/ResizeColumnsPlugin.java +++ b/domino-ui/src/main/java/org/dominokit/domino/ui/datatable/plugins/column/ResizeColumnsPlugin.java @@ -25,6 +25,7 @@ import jsinterop.base.Js; import org.dominokit.domino.ui.datatable.*; import org.dominokit.domino.ui.datatable.events.ColumnResizedEvent; +import org.dominokit.domino.ui.datatable.events.ColumnResizingEvent; import org.dominokit.domino.ui.datatable.plugins.DataTablePlugin; import org.dominokit.domino.ui.datatable.plugins.HasPluginConfig; import org.dominokit.domino.ui.elements.DivElement; @@ -43,6 +44,9 @@ public class ResizeColumnsPlugin private ResizeColumnsConfig config = new ResizeColumnsConfig(); private DataTable datatable; + private ColumnConfig resizingColumn; + private boolean resizing = false; + /** * Initializes the plugin and prepares columns for resizing. * @@ -126,7 +130,7 @@ public void onHeaderAdded(DataTable dataTable, ColumnConfig column) { double currentPosition = mouseEvent.clientX; double diff = currentPosition - meta.getStartPosition(); - dataTable.fireTableEvent(ColumnResizedEvent.of(column, diff)); + dataTable.fireTableEvent(ColumnResizingEvent.of(column, diff)); }); }; @@ -137,6 +141,8 @@ public void onHeaderAdded(DataTable dataTable, ColumnConfig column) { if (mouseEvent.buttons == 1) { mouseEvent.stopPropagation(); mouseEvent.preventDefault(); + this.resizingColumn = column; + this.resizing = true; column .getGrandParent() .applyAndOnSubColumns( @@ -155,23 +161,27 @@ public void onHeaderAdded(DataTable dataTable, ColumnConfig column) { }); EventListener stopResizing = evt -> { - ResizeColumnMeta.get(column) - .ifPresent( - meta -> { - MouseEvent mouseEvent = Js.uncheckedCast(evt); - double currentPosition = mouseEvent.clientX; - double diff = currentPosition - meta.getStartPosition(); + evt.stopPropagation(); + if (column.equals(this.resizingColumn) && resizing) { + this.resizing = false; + ResizeColumnMeta.get(column) + .ifPresent( + meta -> { + MouseEvent mouseEvent = Js.uncheckedCast(evt); + double currentPosition = mouseEvent.clientX; + double diff = currentPosition - meta.getStartPosition(); - dataTable.fireTableEvent(ColumnResizedEvent.of(column, diff, true)); - }); + dataTable.fireTableEvent( + ColumnResizedEvent.of(column, diff, true)); + }); - DominoDom.document.body.removeEventListener( - EventType.mousemove.getName(), resizeListener); + DominoDom.document.body.removeEventListener( + EventType.mousemove.getName(), resizeListener); + } }; this.datatable.onAttached( mutationRecord -> { - resizeElement.addEventListener(EventType.mouseup.getName(), stopResizing); DominoDom.document.body.addEventListener( EventType.mouseup.getName(), stopResizing); });