Skip to content

Commit

Permalink
fix #855 ColumnResize sending spurious events
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Oct 27, 2023
1 parent 708f5b6 commit ca22341
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,19 @@ public ColumnConfig<T> 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
*
* <p>Event type: <b>COLUMN_RESIZED -> 'column-resized'</b>
*/
public class ColumnResizingEvent implements TableEvent {
/** Constant <code>COLUMN_RESIZED="column-resized"</code> */
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 <code>column</code>.
*
* @return a {@link ColumnConfig}
*/
public ColumnConfig<?> getColumn() {
return column;
}

/**
* Getter for the field <code>sizeDiff</code>.
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +44,9 @@ public class ResizeColumnsPlugin<T>
private ResizeColumnsConfig config = new ResizeColumnsConfig();
private DataTable<T> datatable;

private ColumnConfig<T> resizingColumn;
private boolean resizing = false;

/**
* Initializes the plugin and prepares columns for resizing.
*
Expand Down Expand Up @@ -126,7 +130,7 @@ public void onHeaderAdded(DataTable<T> dataTable, ColumnConfig<T> column) {
double currentPosition = mouseEvent.clientX;
double diff = currentPosition - meta.getStartPosition();

dataTable.fireTableEvent(ColumnResizedEvent.of(column, diff));
dataTable.fireTableEvent(ColumnResizingEvent.of(column, diff));
});
};

Expand All @@ -137,6 +141,8 @@ public void onHeaderAdded(DataTable<T> dataTable, ColumnConfig<T> column) {
if (mouseEvent.buttons == 1) {
mouseEvent.stopPropagation();
mouseEvent.preventDefault();
this.resizingColumn = column;
this.resizing = true;
column
.getGrandParent()
.applyAndOnSubColumns(
Expand All @@ -155,23 +161,27 @@ public void onHeaderAdded(DataTable<T> dataTable, ColumnConfig<T> 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);
});
Expand Down

0 comments on commit ca22341

Please sign in to comment.