org.dominokit.domino.ui.datatable.TableConfig Maven / Gradle / Ivy
package org.dominokit.domino.ui.datatable;
import elemental2.dom.*;
import org.dominokit.domino.ui.datatable.plugins.DataTablePlugin;
import org.dominokit.domino.ui.popover.Tooltip;
import org.dominokit.domino.ui.style.Style;
import org.dominokit.domino.ui.utils.DominoElement;
import org.dominokit.domino.ui.utils.HasMultiSelectionSupport;
import org.jboss.elemento.HtmlContentBuilder;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.Objects.nonNull;
import static org.jboss.elemento.Elements.*;
public class TableConfig implements HasMultiSelectionSupport {
private List> columns = new LinkedList<>();
private List> plugins = new LinkedList<>();
private DataTable dataTable;
private boolean fixed = false;
private String fixedDefaultColumnWidth = "100px";
private String fixedBodyHeight = "400px";
private boolean lazyLoad = true;
private boolean multiSelect = true;
private RowAppender rowAppender = (dataTable, tableRow) -> dataTable.bodyElement().appendChild(tableRow.element());
private DirtyRecordProvider dirtyRecordProvider= original -> original;
private SaveDirtyRecordHandler saveDirtyRecordHandler = (originalRecord, dirtyRecord) -> {};
public void drawHeaders(DataTable dataTable, DominoElement thead) {
this.dataTable = dataTable;
HtmlContentBuilder tr = tr();
thead.appendChild(tr.element());
columns.forEach(columnConfig -> {
//TODO replace with FlexLayout
Node element = columnConfig.getHeaderElement().asElement(columnConfig.getTitle());
columnConfig.contextMenu = div().style("width: 15px; display: none;").element();
HtmlContentBuilder headerContent = div()
.style("display: flex;")
.add(div()
.style("width:100%")
.add(element))
.add(columnConfig.contextMenu);
HtmlContentBuilder th = th().css(DataTableStyles.TABLE_CM_HEADER).add(headerContent.element());
columnConfig.applyScreenMedia(th.element());
tr.add(th);
columnConfig.setHeadElement(th.element());
if (dataTable.getTableConfig().isFixed() || columnConfig.isFixed()) {
fixElementWidth(columnConfig, th.element());
}
if (columnConfig.isShowTooltip()) {
Tooltip.create(th.element(), columnConfig.getTooltipNode());
}
columnConfig.applyHeaderStyle();
columnConfig.addShowHideListener(DefaultColumnShowHideListener.of(th.element(), true));
DominoElement.of(th).toggleDisplay(!columnConfig.isHidden());
plugins.forEach(plugin -> plugin.onHeaderAdded(dataTable, columnConfig));
});
dataTable.tableElement().appendChild(thead);
}
private void fixElementWidth(ColumnConfig column, HTMLElement element) {
String fixedWidth = bestFitWidth(column);
Style.of(element)
.setWidth(fixedWidth)
.setMinWidth(fixedWidth)
.setMaxWidth(fixedWidth)
.add(DataTableStyles.FIXED_WIDTH);
}
public void drawRecord(DataTable dataTable, TableRow tableRow) {
columns.forEach(columnConfig -> {
HTMLTableCellElement cellElement;
if (columnConfig.isHeader()) {
cellElement = th().css("dt-th-cell").element();
} else {
cellElement = td().css("dt-td-cell").element();
}
if (dataTable.getTableConfig().isFixed() || columnConfig.isFixed()) {
fixElementWidth(columnConfig, cellElement);
}
RowCell rowCell = new RowCell<>(new CellRenderer.CellInfo<>(tableRow, cellElement), columnConfig);
rowCell.updateCell();
tableRow.addCell(rowCell);
columnConfig.applyScreenMedia(cellElement);
tableRow.element().appendChild(cellElement);
columnConfig.applyCellStyle(cellElement);
columnConfig.addShowHideListener(DefaultColumnShowHideListener.of(cellElement));
DominoElement.of(cellElement).toggleDisplay(!columnConfig.isHidden());
});
rowAppender.appendRow(dataTable, tableRow);
plugins.forEach(plugin -> plugin.onRowAdded(dataTable, tableRow));
}
public TableConfig addColumn(ColumnConfig column) {
this.columns.add(column);
return this;
}
public TableConfig insertColumnFirst(ColumnConfig column) {
this.columns.add(0, column);
return this;
}
public TableConfig insertColumnLast(ColumnConfig column) {
this.columns.add(this.columns.size() - 1, column);
return this;
}
public TableConfig addPlugin(DataTablePlugin plugin) {
this.plugins.add(plugin);
return this;
}
public boolean isFixed() {
return fixed;
}
public TableConfig setFixed(boolean fixed) {
this.fixed = fixed;
return this;
}
public boolean isLazyLoad() {
return lazyLoad;
}
public TableConfig setLazyLoad(boolean lazyLoad) {
this.lazyLoad = lazyLoad;
return this;
}
public String getFixedBodyHeight() {
return fixedBodyHeight;
}
public TableConfig setFixedBodyHeight(String fixedBodyHeight) {
this.fixedBodyHeight = fixedBodyHeight;
return this;
}
public String getFixedDefaultColumnWidth() {
return fixedDefaultColumnWidth;
}
public TableConfig setFixedDefaultColumnWidth(String fixedDefaultColumnWidth) {
this.fixedDefaultColumnWidth = fixedDefaultColumnWidth;
return this;
}
String bestFitWidth(ColumnConfig columnConfig) {
if (nonNull(columnConfig.getWidth()) && !columnConfig.getWidth().isEmpty()) {
return columnConfig.getWidth();
} else if (nonNull(columnConfig.getMinWidth()) && !columnConfig.getMinWidth().isEmpty()) {
return columnConfig.getMinWidth();
} else if (nonNull(columnConfig.getMaxWidth()) && !columnConfig.getMaxWidth().isEmpty()) {
return columnConfig.getMaxWidth();
} else {
return fixedDefaultColumnWidth;
}
}
@Override
public boolean isMultiSelect() {
return this.multiSelect;
}
@Override
public void setMultiSelect(boolean multiSelect) {
this.multiSelect = multiSelect;
}
public void setRowAppender(RowAppender rowAppender) {
if (nonNull(rowAppender)) {
this.rowAppender = rowAppender;
}
}
public List> getPlugins() {
return plugins;
}
void onBeforeHeaders(DataTable dataTable) {
plugins.forEach(plugin -> plugin.onBeforeAddHeaders(dataTable));
}
public void onAfterHeaders(DataTable dataTable) {
plugins.forEach(plugin -> plugin.onAfterAddHeaders(dataTable));
}
public List> getColumns() {
return columns;
}
public List> getVisibleColumns() {
return columns.stream().filter(column -> !column.isHidden())
.collect(Collectors.toList());
}
public ColumnConfig getColumnByName(String name) {
Optional> first = getColumns()
.stream()
.filter(columnConfig -> columnConfig.getName().equals(name))
.findFirst();
if (first.isPresent()) {
return first.get();
} else {
throw new ColumnNofFoundException(name);
}
}
public DataTable getDataTable() {
return dataTable;
}
public TableConfig setDirtyRecordHandlers(DirtyRecordProvider dirtyRecordProvider, SaveDirtyRecordHandler saveDirtyRecordHandler) {
this.dirtyRecordProvider = dirtyRecordProvider;
this.saveDirtyRecordHandler = saveDirtyRecordHandler;
return this;
}
DirtyRecordProvider getDirtyRecordProvider() {
return dirtyRecordProvider;
}
SaveDirtyRecordHandler getSaveDirtyRecordHandler() {
return saveDirtyRecordHandler;
}
@FunctionalInterface
public interface RowAppender {
void appendRow(DataTable dataTable, TableRow tableRow);
}
public static class ColumnNofFoundException extends RuntimeException {
public ColumnNofFoundException(String name) {
super(name);
}
}
}