Skip to content

Commit ca22341

Browse files
committed
fix #855 ColumnResize sending spurious events
1 parent 708f5b6 commit ca22341

File tree

3 files changed

+146
-12
lines changed

3 files changed

+146
-12
lines changed

domino-ui/src/main/java/org/dominokit/domino/ui/datatable/ColumnConfig.java

+13
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,19 @@ public ColumnConfig<T> removeChild(Node child) {
12121212
return this;
12131213
}
12141214

1215+
@Override
1216+
public boolean equals(Object o) {
1217+
if (this == o) return true;
1218+
if (o == null || getClass() != o.getClass()) return false;
1219+
ColumnConfig<?> that = (ColumnConfig<?>) o;
1220+
return Objects.equals(name, that.name);
1221+
}
1222+
1223+
@Override
1224+
public int hashCode() {
1225+
return Objects.hash(name);
1226+
}
1227+
12151228
/**
12161229
* Removes the specified child element from the header element of this column.
12171230
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright © 2019 Dominokit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.dominokit.domino.ui.datatable.events;
17+
18+
import org.dominokit.domino.ui.datatable.ColumnConfig;
19+
20+
/**
21+
* This event will be fired when a column gets resized
22+
*
23+
* <p>Event type: <b>COLUMN_RESIZED -> 'column-resized'</b>
24+
*/
25+
public class ColumnResizingEvent implements TableEvent {
26+
/** Constant <code>COLUMN_RESIZED="column-resized"</code> */
27+
public static final String COLUMN_RESIZING = "column-resizing";
28+
29+
private final ColumnConfig<?> column;
30+
private final double sizeDiff;
31+
private final boolean completed;
32+
33+
/**
34+
* Factory method to create a new Event instance
35+
*
36+
* @param column a {@link ColumnConfig}
37+
* @param sizeDiff a double
38+
* @return a {@link ColumnResizingEvent}
39+
*/
40+
public static ColumnResizingEvent of(ColumnConfig<?> column, double sizeDiff) {
41+
return new ColumnResizingEvent(column, sizeDiff);
42+
}
43+
44+
/**
45+
* Factory method to create a new Event instance
46+
*
47+
* @param column a {@link ColumnConfig}
48+
* @param sizeDiff a double
49+
* @param completed a boolean
50+
* @return a {@link ColumnResizingEvent}
51+
*/
52+
public static ColumnResizingEvent of(ColumnConfig<?> column, double sizeDiff, boolean completed) {
53+
return new ColumnResizingEvent(column, sizeDiff, completed);
54+
}
55+
56+
/**
57+
* Creates a new Event instance
58+
*
59+
* @param column a {@link ColumnConfig}
60+
* @param sizeDiff a double
61+
*/
62+
public ColumnResizingEvent(ColumnConfig<?> column, double sizeDiff) {
63+
this(column, sizeDiff, false);
64+
}
65+
66+
/**
67+
* Creates a new Event instance
68+
*
69+
* @param column a {@link ColumnConfig}
70+
* @param sizeDiff a double
71+
* @param completed a boolean
72+
*/
73+
public ColumnResizingEvent(ColumnConfig<?> column, double sizeDiff, boolean completed) {
74+
this.column = column;
75+
this.sizeDiff = sizeDiff;
76+
this.completed = completed;
77+
}
78+
79+
/**
80+
* Getter for the field <code>column</code>.
81+
*
82+
* @return a {@link ColumnConfig}
83+
*/
84+
public ColumnConfig<?> getColumn() {
85+
return column;
86+
}
87+
88+
/**
89+
* Getter for the field <code>sizeDiff</code>.
90+
*
91+
* @return a double
92+
*/
93+
public double getSizeDiff() {
94+
return sizeDiff;
95+
}
96+
97+
/**
98+
* isCompleted.
99+
*
100+
* @return a boolean
101+
*/
102+
public boolean isCompleted() {
103+
return completed;
104+
}
105+
106+
/** {@inheritDoc} */
107+
@Override
108+
public String getType() {
109+
return COLUMN_RESIZING;
110+
}
111+
}

domino-ui/src/main/java/org/dominokit/domino/ui/datatable/plugins/column/ResizeColumnsPlugin.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import jsinterop.base.Js;
2626
import org.dominokit.domino.ui.datatable.*;
2727
import org.dominokit.domino.ui.datatable.events.ColumnResizedEvent;
28+
import org.dominokit.domino.ui.datatable.events.ColumnResizingEvent;
2829
import org.dominokit.domino.ui.datatable.plugins.DataTablePlugin;
2930
import org.dominokit.domino.ui.datatable.plugins.HasPluginConfig;
3031
import org.dominokit.domino.ui.elements.DivElement;
@@ -43,6 +44,9 @@ public class ResizeColumnsPlugin<T>
4344
private ResizeColumnsConfig config = new ResizeColumnsConfig();
4445
private DataTable<T> datatable;
4546

47+
private ColumnConfig<T> resizingColumn;
48+
private boolean resizing = false;
49+
4650
/**
4751
* Initializes the plugin and prepares columns for resizing.
4852
*
@@ -126,7 +130,7 @@ public void onHeaderAdded(DataTable<T> dataTable, ColumnConfig<T> column) {
126130
double currentPosition = mouseEvent.clientX;
127131
double diff = currentPosition - meta.getStartPosition();
128132

129-
dataTable.fireTableEvent(ColumnResizedEvent.of(column, diff));
133+
dataTable.fireTableEvent(ColumnResizingEvent.of(column, diff));
130134
});
131135
};
132136

@@ -137,6 +141,8 @@ public void onHeaderAdded(DataTable<T> dataTable, ColumnConfig<T> column) {
137141
if (mouseEvent.buttons == 1) {
138142
mouseEvent.stopPropagation();
139143
mouseEvent.preventDefault();
144+
this.resizingColumn = column;
145+
this.resizing = true;
140146
column
141147
.getGrandParent()
142148
.applyAndOnSubColumns(
@@ -155,23 +161,27 @@ public void onHeaderAdded(DataTable<T> dataTable, ColumnConfig<T> column) {
155161
});
156162
EventListener stopResizing =
157163
evt -> {
158-
ResizeColumnMeta.get(column)
159-
.ifPresent(
160-
meta -> {
161-
MouseEvent mouseEvent = Js.uncheckedCast(evt);
162-
double currentPosition = mouseEvent.clientX;
163-
double diff = currentPosition - meta.getStartPosition();
164+
evt.stopPropagation();
165+
if (column.equals(this.resizingColumn) && resizing) {
166+
this.resizing = false;
167+
ResizeColumnMeta.get(column)
168+
.ifPresent(
169+
meta -> {
170+
MouseEvent mouseEvent = Js.uncheckedCast(evt);
171+
double currentPosition = mouseEvent.clientX;
172+
double diff = currentPosition - meta.getStartPosition();
164173

165-
dataTable.fireTableEvent(ColumnResizedEvent.of(column, diff, true));
166-
});
174+
dataTable.fireTableEvent(
175+
ColumnResizedEvent.of(column, diff, true));
176+
});
167177

168-
DominoDom.document.body.removeEventListener(
169-
EventType.mousemove.getName(), resizeListener);
178+
DominoDom.document.body.removeEventListener(
179+
EventType.mousemove.getName(), resizeListener);
180+
}
170181
};
171182

172183
this.datatable.onAttached(
173184
mutationRecord -> {
174-
resizeElement.addEventListener(EventType.mouseup.getName(), stopResizing);
175185
DominoDom.document.body.addEventListener(
176186
EventType.mouseup.getName(), stopResizing);
177187
});

0 commit comments

Comments
 (0)