Skip to content

Commit

Permalink
fix #920 DataTable mixed plugins DragAndDrop with Edit mode should di…
Browse files Browse the repository at this point in the history
…sable DragAndDrop
  • Loading branch information
vegegoku committed Apr 14, 2024
1 parent d6c93af commit 24e02ec
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class TableConfig<T>
private String minWidth;

private Consumer<TableRow<T>> onRowEditHandler = (tableRow) -> {};
private Consumer<TableRow<T>> onRowFinishEditHandler = (tableRow) -> {};

private final ColumnConfig<T> pluginUtilityColumn =
ColumnConfig.<T>create("plugin-utility-column")
Expand Down Expand Up @@ -582,6 +583,26 @@ Consumer<TableRow<T>> getOnRowEditHandler() {
return onRowEditHandler;
}

/**
* Use this to set a handler that will be called when ever a row editing finished.
*
* @param handler The handler to be called.
* @return same TableConfig instance.
*/
public TableConfig<T> setOnRowFinishEditHandler(Consumer<TableRow<T>> handler) {
if (isNull(handler)) {
this.onRowFinishEditHandler = tableRow -> {};
} else {
this.onRowFinishEditHandler = handler;
}
return this;
}

/** @return the handler to be called when a row editing finished. */
Consumer<TableRow<T>> getOnRowFinishEditHandler() {
return onRowFinishEditHandler;
}

/** A functional interface defining the behavior for appending rows. */
@FunctionalInterface
public interface RowAppender<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class TableRow<T> extends BaseDominoElement<HTMLTableRowElement, TableRow
private boolean selectable;

private FieldsGrouping rowFieldsGroup = FieldsGrouping.create();
private boolean draggable = true;

/**
* Constructs a table row with the given record, index, and parent table.
Expand Down Expand Up @@ -587,6 +588,7 @@ public void save() {
this.setEditable(false);
updateRow();
rowFieldsGroup.removeAllFormElements();
this.dataTable.getTableConfig().getOnRowFinishEditHandler().accept(this);
}
}

Expand All @@ -598,6 +600,7 @@ public void cancelEditing() {
this.setEditable(false);
updateRow();
rowFieldsGroup.removeAllFormElements();
this.dataTable.getTableConfig().getOnRowFinishEditHandler().accept(this);
}

/**
Expand Down Expand Up @@ -725,6 +728,22 @@ public FieldsGrouping getRowFieldsGroup() {
return rowFieldsGroup;
}

/** @return true if this table should be allowed to be dragged. */
public boolean isDraggable() {
return draggable;
}

/**
* Sets if the table row should be allowed to be dragged. the actual execution of this flag is
* upon the implementation of the dragging operation the flag here is just to give the
* implementation if the table should be dragged or not.
*
* @param draggable true to allow row dragging, false to prevent it.
*/
public void setDraggable(boolean draggable) {
this.draggable = draggable;
}

/**
* Represents a function to render a TableRow.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ public void onHeaderAdded(DataTable<T> dataTable, ColumnConfig<T> column) {
*/
@Override
public void onRowAdded(DataTable<T> dataTable, TableRow<T> tableRow) {
dragSource.addDraggable(Draggable.of(tableRow));
Draggable<TableRow<T>> draggable = Draggable.of(tableRow);
draggable.setConfig(tableRow::isDraggable);
dragSource.addDraggable(draggable);
dropZone.addDropTarget(
tableRow, draggableId -> moveItem(dataTable, tableRow.getRecord(), draggableId));
}
Expand Down
39 changes: 33 additions & 6 deletions domino-ui/src/main/java/org/dominokit/domino/ui/dnd/Draggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.dominokit.domino.ui.dnd;

import static org.dominokit.domino.ui.utils.Domino.*;
import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.utils.ElementsFactory.elements;

import elemental2.dom.*;
Expand Down Expand Up @@ -44,6 +44,7 @@ public class Draggable<E extends IsElement<? extends HTMLElement>> {
private final E element;
private final EventListener eventListener;
private final Consumer<E> dragStartListener;
private DraggableConfig config = () -> true;

/**
* Creates a draggable instance for the specified element.
Expand Down Expand Up @@ -119,11 +120,33 @@ public static <E extends IsElement<? extends HTMLElement>> Draggable<E> of(
}

private void onDragStart(Event evt, E draggable, String id) {
DragEvent e = (DragEvent) evt;
e.dataTransfer.setData("draggable_id", id);
e.dataTransfer.dropEffect = "move";
draggable.element().classList.add(DragSource.DRAGGING);
dragStartListener.accept(draggable);
if (config.isEnabled()) {
DragEvent e = (DragEvent) evt;
e.dataTransfer.setData("draggable_id", id);
e.dataTransfer.dropEffect = "move";
draggable.element().classList.add(DragSource.DRAGGING);
dragStartListener.accept(draggable);
} else {
evt.preventDefault();
}
}

/** @return Draggble item DraggableConfig */
public DraggableConfig getConfig() {
return config;
}

/**
* Sets the configuration for the draggble item, if null use a default config
*
* @param config DraggableConfig
*/
public void setConfig(DraggableConfig config) {
if (nonNull(config)) {
this.config = config;
} else {
this.config = () -> true;
}
}

/** Detaches the draggable feature from the element. */
Expand All @@ -140,4 +163,8 @@ public void detach() {
public String getId() {
return id;
}

public interface DraggableConfig {
boolean isEnabled();
}
}

0 comments on commit 24e02ec

Please sign in to comment.