From 39f1b59b84996ea3ca76b7876fb9851cb3531951 Mon Sep 17 00:00:00 2001 From: Yuriy Yevstihnyeyev Date: Sun, 20 Dec 2020 11:39:27 +0200 Subject: [PATCH] fix: remove redundant sorter renderer (#535) Fixes: vaadin/vaadin-grid#1841. Cherry-pick: #515 --- .../flow/component/grid/it/SortingPage.java | 7 +- .../flow/component/grid/AbstractColumn.java | 5 +- .../grid/GridSorterComponentRenderer.java | 129 ------------------ 3 files changed, 7 insertions(+), 134 deletions(-) delete mode 100644 vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/GridSorterComponentRenderer.java diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/SortingPage.java b/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/SortingPage.java index 1b8c19be933..03548f66580 100644 --- a/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/SortingPage.java +++ b/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/grid/it/SortingPage.java @@ -23,6 +23,7 @@ import com.vaadin.flow.component.grid.GridSortOrderBuilder; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.NativeButton; +import com.vaadin.flow.component.html.Span; import com.vaadin.flow.data.bean.Person; import com.vaadin.flow.router.Route; import com.vaadin.flow.theme.Theme; @@ -72,10 +73,14 @@ private Grid createGrid(String gridId, String sortBtnId) { grid.setItems(new Person("B", 20), new Person("A", 30)); Column nameColumn = grid.addColumn(Person::getFirstName) - .setHeader("Name"); + .setHeader(new Span("Name")); Column ageColumn = grid.addColumn(Person::getAge) .setHeader("Age"); + // Needed to check that sorter is rendered in component header after + // adding new header row + grid.appendHeaderRow(); + List> sortByName = new GridSortOrderBuilder() .thenAsc(nameColumn).build(); grid.sort(sortByName); diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/AbstractColumn.java b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/AbstractColumn.java index a8c96706126..4ba9b21bf94 100644 --- a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/AbstractColumn.java +++ b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/AbstractColumn.java @@ -175,10 +175,7 @@ protected void setFooterText(String text) { } protected void setHeaderComponent(Component component) { - /* - * Uses the special renderer to take care of the vaadin-grid-sorter. - */ - setHeaderRenderer(new GridSorterComponentRenderer<>(this, component)); + setHeaderRenderer(new ComponentRenderer<>(() -> component)); } protected void setFooterComponent(Component component) { diff --git a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/GridSorterComponentRenderer.java b/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/GridSorterComponentRenderer.java deleted file mode 100644 index 1ade3cbef26..00000000000 --- a/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/GridSorterComponentRenderer.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright 2000-2017 Vaadin Ltd. - * - * 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 com.vaadin.flow.component.grid; - -import java.util.Optional; - -import com.vaadin.flow.component.Component; -import com.vaadin.flow.component.UI; -import com.vaadin.flow.data.provider.ComponentDataGenerator; -import com.vaadin.flow.data.provider.DataGenerator; -import com.vaadin.flow.data.provider.DataKeyMapper; -import com.vaadin.flow.data.renderer.ComponentRenderer; -import com.vaadin.flow.data.renderer.Rendering; -import com.vaadin.flow.dom.Element; - -/** - * Internal component renderer for sortable headers inside Grid. - * - * @author Vaadin Ltd. - * - * @param - * the model type - */ -class GridSorterComponentRenderer - extends ComponentRenderer { - - private final AbstractColumn column; - private final Component component; - - /** - * Creates a new renderer for a specific column, using the defined - * component. - * - * @param column - * The column which header should be rendered - * @param component - * The component to be used by the renderer - */ - public GridSorterComponentRenderer(AbstractColumn column, - Component component) { - this.column = column; - this.component = component; - } - - @Override - public Rendering render(Element container, - DataKeyMapper keyMapper, Element contentTemplate) { - - GridSorterComponentRendering rendering = new GridSorterComponentRendering( - contentTemplate); - - container.getNode() - .runWhenAttached(ui -> ui.getInternals().getStateTree() - .beforeClientResponse(container.getNode(), - context -> setupTemplateWhenAttached( - context.getUI(), container, rendering, - keyMapper))); - return rendering; - } - - private void setupTemplateWhenAttached(UI ui, Element owner, - GridSorterComponentRendering rendering, - DataKeyMapper keyMapper) { - String appId = ui.getInternals().getAppId(); - Element templateElement = rendering.getTemplateElement(); - owner.appendChild(templateElement); - - Element container = new Element("div"); - owner.appendVirtualChild(container); - rendering.setContainer(container); - String templateInnerHtml; - - if (component != null) { - container.appendChild(component.getElement()); - - templateInnerHtml = String.format( - "", - appId, component.getElement().getNode().getId()); - } else { - templateInnerHtml = ""; - } - - /* - * The renderer must set the base header template back to the column, so - * if/when the sortable state is changed by the developer, the column - * knows how to add or remove the grid sorter. - */ - column.setBaseHeaderTemplate(templateInnerHtml); - if (column.hasSortingIndicators()) { - templateInnerHtml = column.addGridSorter(templateInnerHtml); - } - - templateElement.setProperty("innerHTML", templateInnerHtml); - } - - private class GridSorterComponentRendering extends - ComponentDataGenerator implements Rendering { - - private Element templateElement; - - public GridSorterComponentRendering(Element templateElement) { - super(GridSorterComponentRenderer.this, null); - this.templateElement = templateElement; - } - - @Override - public Element getTemplateElement() { - return templateElement; - } - - @Override - public Optional> getDataGenerator() { - return Optional.of(this); - } - } -}