Skip to content

Commit

Permalink
Data table data aggregation #727
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Jan 3, 2023
1 parent 5d331bb commit 5df7e6e
Show file tree
Hide file tree
Showing 21 changed files with 996 additions and 351 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,13 @@ private void renderChildColumns(
col.createColumnElement(tableConfig)
.clearElement()
.setAttribute("rowspan", diff + "");
ColumnCssRuleMeta.get(this)
.ifPresent(
meta ->
meta.cssRules()
.forEach(
columnCssRule ->
fillHeader.addCss(columnCssRule.getCssClass())));
ColumnHeaderMeta.get(col)
.ifPresent(
columnHeaderMeta -> columnHeaderMeta.addExtraHeadElement(fillHeader));
Expand Down Expand Up @@ -916,6 +923,11 @@ private DominoElement<HTMLTableCellElement> createColumnElement(TableConfig<T> t
th.addCss("dui-column-group");
}

ColumnCssRuleMeta.get(this)
.ifPresent(
meta ->
meta.cssRules().forEach(columnCssRule -> th.addCss(columnCssRule.getCssClass())));

applyScreenMedia(th.element());

setHeadElement(th.element());
Expand Down Expand Up @@ -950,32 +962,32 @@ public ColumnConfig<T> applyAndOnSubColumns(
return this;
}

public ColumnConfig<T> applyAndOnFirstSubColumn(Consumer<ColumnConfig<T>> handler) {
public ColumnConfig<T> applyAndOnEachFirstSubColumn(Consumer<ColumnConfig<T>> handler) {
handler.accept(this);
if (isColumnGroup()) {
getSubColumns().get(0).applyAndOnFirstSubColumn(handler);
getSubColumns().get(0).applyAndOnEachFirstSubColumn(handler);
}
return this;
}

public ColumnConfig<T> onFirstSubColumn(Consumer<ColumnConfig<T>> handler) {
if (isColumnGroup()) {
getSubColumns().get(0).applyAndOnFirstSubColumn(handler);
getSubColumns().get(0).applyAndOnEachFirstSubColumn(handler);
}
return this;
}

public ColumnConfig<T> applyAndOnLastSubColumn(Consumer<ColumnConfig<T>> handler) {
public ColumnConfig<T> applyAndOnEachLastSubColumn(Consumer<ColumnConfig<T>> handler) {
handler.accept(this);
if (isColumnGroup()) {
getSubColumns().get(getSubColumns().size() - 1).applyAndOnLastSubColumn(handler);
getSubColumns().get(getSubColumns().size() - 1).applyAndOnEachLastSubColumn(handler);
}
return this;
}

public ColumnConfig<T> onEachLastSubColumn(Consumer<ColumnConfig<T>> handler) {
if (isColumnGroup()) {
getSubColumns().get(getSubColumns().size() - 1).applyAndOnLastSubColumn(handler);
getSubColumns().get(getSubColumns().size() - 1).applyAndOnEachLastSubColumn(handler);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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;

import elemental2.dom.CSSRule;

public class ColumnCssRule {

private final String key;
private final String selector;
private final String cssClass;
private final CSSRule cssRule;

public ColumnCssRule(String key, String selector, String cssClass, CSSRule cssRule) {
this.key = key;
this.selector = selector;
this.cssClass = cssClass;
this.cssRule = cssRule;
}

public String getKey() {
return key;
}

public String getSelector() {
return selector;
}

public String getCssClass() {
return cssClass;
}

public CSSRule getCssRule() {
return cssRule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,56 @@
*/
package org.dominokit.domino.ui.datatable;

import elemental2.dom.CSSRule;
import elemental2.dom.HTMLDivElement;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.dominokit.domino.ui.utils.DynamicStyleSheet;

public class ColumnCssRuleMeta implements ColumnMeta {
public class ColumnCssRuleMeta<T> implements ColumnMeta {

public static final String COLUMN_CSS_RULE_META = "column-css-rule-meta";
public static final String DEFAULT_RULE = "COLUMN-DEFAULT-CSS-RULE";

private final CSSRule cssRule;
private final String selector;
private final Map<String, ColumnCssRule> cssRules = new HashMap<>();
private final DynamicStyleSheet<HTMLDivElement, DataTable<T>> dynamicStyleSheet;

public static ColumnCssRuleMeta of(CSSRule cssRule, String selector) {
return new ColumnCssRuleMeta(cssRule, selector);
static <T> ColumnCssRuleMeta<T> of(
DynamicStyleSheet<HTMLDivElement, DataTable<T>> dynamicStyleSheet) {
return new ColumnCssRuleMeta<>(dynamicStyleSheet);
}

public ColumnCssRuleMeta(CSSRule cssRule, String selector) {
this.cssRule = cssRule;
this.selector = selector;
private ColumnCssRuleMeta(DynamicStyleSheet<HTMLDivElement, DataTable<T>> dynamicStyleSheet) {
this.dynamicStyleSheet = dynamicStyleSheet;
}

public static Optional<ColumnCssRuleMeta> get(ColumnConfig<?> column) {
public static <T> Optional<ColumnCssRuleMeta<T>> get(ColumnConfig<?> column) {
return column.getMeta(COLUMN_CSS_RULE_META);
}

public CSSRule getCssRule() {
return cssRule;
public ColumnCssRuleMeta addRule(String key, String cssClass) {
DynamicStyleSheet.DynamicCssRule dynamicCssRule = dynamicStyleSheet.insertRule(cssClass);
cssRules.put(
key,
new ColumnCssRule(
key,
dynamicCssRule.getSelector(),
dynamicCssRule.getClassName(),
dynamicCssRule.getCssRule()));
return this;
}

public String getSelector() {
return selector;
public Optional<ColumnCssRule> getColumnCssRule(String key) {
return Optional.ofNullable(cssRules.get(key));
}

public Map<String, ColumnCssRule> getCssRules() {
return cssRules;
}

public Collection<ColumnCssRule> cssRules() {
return cssRules.values();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.dominokit.domino.ui.datatable;

import static elemental2.dom.DomGlobal.document;
import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.datatable.DataTableStyles.TABLE;
import static org.dominokit.domino.ui.datatable.DataTableStyles.TABLE_BORDERED;
Expand All @@ -24,10 +23,7 @@
import static org.dominokit.domino.ui.datatable.DataTableStyles.TABLE_RESPONSIVE;
import static org.dominokit.domino.ui.datatable.DataTableStyles.TABLE_ROW_FILTERED;
import static org.dominokit.domino.ui.datatable.DataTableStyles.TABLE_STRIPED;
import static org.jboss.elemento.Elements.div;
import static org.jboss.elemento.Elements.table;
import static org.jboss.elemento.Elements.tbody;
import static org.jboss.elemento.Elements.thead;
import static org.jboss.elemento.Elements.*;

import elemental2.dom.*;
import java.util.ArrayList;
Expand All @@ -41,6 +37,7 @@
import org.dominokit.domino.ui.datatable.store.DataStore;
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.DominoElement;
import org.dominokit.domino.ui.utils.DynamicStyleSheet;
import org.dominokit.domino.ui.utils.HasSelectionSupport;
import org.jboss.elemento.EventType;

Expand All @@ -57,7 +54,7 @@ public class DataTable<T> extends BaseDominoElement<HTMLDivElement, DataTable<T>
/** Use this constant as flag value to check if a row in the data tables have been filtered out */
public static final String DATA_TABLE_ROW_FILTERED = "data-table-row-filtered";

private static final String PARENT_SELECTOR_PREFIX = "dt-";
private static final String PARENT_SELECTOR_PREFIX = "dui-dt-";

private final DataStore<T> dataStore;
private DominoElement<HTMLDivElement> root = DominoElement.of(div()).css(TABLE_RESPONSIVE);
Expand All @@ -66,6 +63,7 @@ public class DataTable<T> extends BaseDominoElement<HTMLDivElement, DataTable<T>
private TableConfig<T> tableConfig;
private DominoElement<HTMLTableSectionElement> tbody = DominoElement.of(tbody());
private DominoElement<HTMLTableSectionElement> thead = DominoElement.of(thead());
private DominoElement<HTMLTableSectionElement> tfoot = DominoElement.of(tfoot());
private List<T> data = new ArrayList<>();
private boolean selectable = true;
private List<TableRow<T>> tableRows = new ArrayList<>();
Expand All @@ -89,8 +87,8 @@ public class DataTable<T> extends BaseDominoElement<HTMLDivElement, DataTable<T>
evt.stopPropagation();
}
};
private HTMLStyleElement styleElement;
private CSSStyleSheet styleSheet;

private DynamicStyleSheet<HTMLDivElement, DataTable<T>> dynamicStyleSheet;

/**
* Creates a new data table instance
Expand All @@ -101,10 +99,21 @@ public class DataTable<T> extends BaseDominoElement<HTMLDivElement, DataTable<T>
public DataTable(TableConfig<T> tableConfig, DataStore<T> dataStore) {
super.init(this);
this.tableConfig = tableConfig;

this.events.put(ANY, new ArrayList<>());
this.dataStore = dataStore;
this.addTableEventListener(ANY, dataStore);
tableElement.setAttribute("dui-data-v-scroll", 0);
tableElement.setAttribute("dui-data-h-scroll", 0);
this.addEventListener(EventType.keydown.getName(), disableKeyboardListener, true);
tableElement.addEventListener(
"scroll",
evt -> {
double scrollTop = new Double(tableElement.element().scrollTop).intValue();
double scrollLeft = new Double(tableElement.element().scrollLeft).intValue();
tableElement.setAttribute("dui-data-v-scroll", scrollTop);
tableElement.setAttribute("dui-data-h-scroll", scrollLeft);
});
this.dataStore.onDataChanged(
dataChangedEvent -> {
fireTableEvent(
Expand All @@ -125,48 +134,26 @@ public DataTable(TableConfig<T> tableConfig, DataStore<T> dataStore) {
fireTableEvent(new TableDataUpdatedEvent<>(this.data, dataChangedEvent.getTotalCount()));
});

initStyleSheet();
initDynamicStyleSheet();
init();
}

private void initStyleSheet() {
this.styleElement = (HTMLStyleElement) document.createElement("style");
this.styleElement.type = "text/css";
this.styleElement.id = tableElement.getDominoId() + "styles";
document.head.append(this.styleElement);
this.styleSheet = (CSSStyleSheet) this.styleElement.sheet;

tableElement.addCss(PARENT_SELECTOR_PREFIX + tableElement.getDominoId());
String rule = "." + PARENT_SELECTOR_PREFIX + tableElement.getDominoId() + " {" + "}";
this.styleSheet.insertRule(rule, 0);
private void initDynamicStyleSheet() {
this.dynamicStyleSheet = new DynamicStyleSheet<>(PARENT_SELECTOR_PREFIX, this);
tableConfig
.getColumnsGrouped()
.forEach(
col -> {
col.applyAndOnSubColumns(
column -> {
int index =
styleSheet.insertRule(
"."
+ PARENT_SELECTOR_PREFIX
+ tableElement.getDominoId()
+ " ."
+ PARENT_SELECTOR_PREFIX
+ "col-"
+ column.getName().replace(" ", "-")
+ "{}",
styleSheet.cssRules.length);
column.applyMeta(
ColumnCssRuleMeta.of(
styleSheet.cssRules.item(index),
PARENT_SELECTOR_PREFIX + "col-" + column.getName().replace(" ", "-")));
ColumnCssRuleMeta<T> columnCssRuleMeta =
ColumnCssRuleMeta.of(this.dynamicStyleSheet);
columnCssRuleMeta.addRule(
ColumnCssRuleMeta.DEFAULT_RULE,
"col-" + column.getName().replace(" ", "-"));
column.applyMeta(columnCssRuleMeta);
});
});

tableElement.onDetached(
mutationRecord -> {
document.head.removeChild(this.styleElement);
});
}

public DataStore<T> getDataStore() {
Expand All @@ -188,6 +175,8 @@ private DataTable<T> init() {
tableConfig.onAfterHeaders(this);
tableElement.appendChild(tbody);
tableConfig.getPlugins().forEach(plugin -> plugin.onBodyAdded(DataTable.this));
tableElement.appendChild(tfoot);
tableConfig.getPlugins().forEach(plugin -> plugin.onFooterAdded(DataTable.this));
root.appendChild(tableElement);
tableConfig.getPlugins().forEach(plugin -> plugin.onAfterAddTable(DataTable.this));
if (!tableConfig.isLazyLoad()) {
Expand Down Expand Up @@ -409,6 +398,11 @@ public DominoElement<HTMLTableSectionElement> headerElement() {
return thead;
}

/** @return the {@link HTMLTableSectionElement} -tfoot- wrapped as {@link DominoElement} */
public DominoElement<HTMLTableSectionElement> footerElement() {
return tfoot;
}

/** @return the applied {@link TableConfig} of this table */
public TableConfig<T> getTableConfig() {
return tableConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ public void drawHeaders(DataTable<T> dataTable, DominoElement<HTMLTableSectionEl
thead.appendChild(headers[i]);
}

columns.forEach(
col -> {
col.renderHeader(dataTable, this, headers);
});
columns.forEach(col -> col.renderHeader(dataTable, this, headers));
if (!thead.isAttached()) {
dataTable.tableElement().appendChild(thead);
}
Expand All @@ -156,7 +153,7 @@ public void drawRecord(DataTable<T> dataTable, TableRow<T> tableRow) {
rowAppender.appendRow(dataTable, tableRow);
}

plugins.forEach(plugin -> plugin.onRowAdded(dataTable, tableRow));
getPlugins().forEach(plugin -> plugin.onRowAdded(dataTable, tableRow));
}

private boolean isOdd(int index) {
Expand Down Expand Up @@ -323,7 +320,7 @@ public void setRowAppender(RowAppender<T> rowAppender) {

/** @return the {@link List} of plugins added to the table */
public List<DataTablePlugin<T>> getPlugins() {
return plugins;
return plugins.stream().sorted().collect(Collectors.toList());
}

/**
Expand All @@ -333,7 +330,7 @@ public List<DataTablePlugin<T>> getPlugins() {
* @param dataTable the {@link DataTable} initialized with this configuration
*/
void onBeforeHeaders(DataTable<T> dataTable) {
plugins.forEach(plugin -> plugin.onBeforeAddHeaders(dataTable));
getPlugins().forEach(plugin -> plugin.onBeforeAddHeaders(dataTable));
}

/**
Expand All @@ -343,7 +340,7 @@ void onBeforeHeaders(DataTable<T> dataTable) {
* @param dataTable the {@link DataTable} initialized with this configuration
*/
void onAfterHeaders(DataTable<T> dataTable) {
plugins.forEach(plugin -> plugin.onAfterAddHeaders(dataTable));
getPlugins().forEach(plugin -> plugin.onAfterAddHeaders(dataTable));
}

/** @return a {@link List} of all non grouping {@link ColumnConfig} added to the table */
Expand Down
Loading

0 comments on commit 5df7e6e

Please sign in to comment.