|
16 | 16 | package com.vaadin.flow.component.popover; |
17 | 17 |
|
18 | 18 | import java.util.Arrays; |
19 | | -import java.util.Collection; |
20 | | -import java.util.HashMap; |
21 | | -import java.util.Map; |
22 | 19 | import java.util.Objects; |
23 | 20 | import java.util.Optional; |
24 | 21 | import java.util.concurrent.atomic.AtomicBoolean; |
25 | | -import java.util.stream.Collectors; |
26 | 22 |
|
27 | | -import com.vaadin.flow.component.AttachEvent; |
28 | 23 | import com.vaadin.flow.component.Component; |
29 | 24 | import com.vaadin.flow.component.ComponentEvent; |
30 | 25 | import com.vaadin.flow.component.ComponentEventListener; |
|
40 | 35 | import com.vaadin.flow.component.shared.HasThemeVariant; |
41 | 36 | import com.vaadin.flow.component.shared.internal.OverlayClassListProxy; |
42 | 37 | import com.vaadin.flow.dom.ClassList; |
43 | | -import com.vaadin.flow.dom.Element; |
44 | | -import com.vaadin.flow.dom.ElementDetachEvent; |
45 | | -import com.vaadin.flow.dom.ElementDetachListener; |
46 | 38 | import com.vaadin.flow.dom.Style; |
47 | 39 | import com.vaadin.flow.server.VaadinService; |
48 | 40 | import com.vaadin.flow.shared.Registration; |
@@ -82,8 +74,6 @@ public class Popover extends Component implements HasAriaLabel, HasComponents, |
82 | 74 | * Constructs an empty popover. |
83 | 75 | */ |
84 | 76 | public Popover() { |
85 | | - getElement().getNode().addAttachListener(this::attachComponentRenderer); |
86 | | - |
87 | 77 | // Workaround for: https://github.com/vaadin/flow/issues/3496 |
88 | 78 | getElement().setProperty("opened", false); |
89 | 79 |
|
@@ -771,110 +761,6 @@ public void setHeight(String height) { |
771 | 761 | getElement().setProperty("contentHeight", height); |
772 | 762 | } |
773 | 763 |
|
774 | | - /** |
775 | | - * Adds the given components into this popover. |
776 | | - * <p> |
777 | | - * The elements in the DOM will not be children of the |
778 | | - * {@code <vaadin-popover>} element, but will be inserted into an overlay |
779 | | - * that is attached into the {@code <body>}. |
780 | | - * |
781 | | - * @param components |
782 | | - * the components to add |
783 | | - */ |
784 | | - @Override |
785 | | - public void add(Collection<Component> components) { |
786 | | - HasComponents.super.add(components); |
787 | | - |
788 | | - updateVirtualChildNodeIds(); |
789 | | - } |
790 | | - |
791 | | - /** |
792 | | - * Adds the given component into this popover at the given index. |
793 | | - * <p> |
794 | | - * The element in the DOM will not be child of the {@code <vaadin-popover>} |
795 | | - * element, but will be inserted into an overlay that is attached into the |
796 | | - * {@code <body>}. |
797 | | - * |
798 | | - * @param index |
799 | | - * the index, where the component will be added. |
800 | | - * |
801 | | - * @param component |
802 | | - * the component to add |
803 | | - */ |
804 | | - @Override |
805 | | - public void addComponentAtIndex(int index, Component component) { |
806 | | - HasComponents.super.addComponentAtIndex(index, component); |
807 | | - |
808 | | - updateVirtualChildNodeIds(); |
809 | | - } |
810 | | - |
811 | | - private void attachComponentRenderer() { |
812 | | - getElement().executeJs( |
813 | | - "Vaadin.FlowComponentHost.patchVirtualContainer(this)"); |
814 | | - |
815 | | - String appId = UI.getCurrent().getInternals().getAppId(); |
816 | | - |
817 | | - getElement().executeJs( |
818 | | - "this.renderer = (root) => Vaadin.FlowComponentHost.setChildNodes($0, this.virtualChildNodeIds, root)", |
819 | | - appId); |
820 | | - } |
821 | | - |
822 | | - private Map<Element, Registration> childDetachListenerMap = new HashMap<>(); |
823 | | - |
824 | | - // Must not use lambda here as that would break serialization. See |
825 | | - // https://github.com/vaadin/flow-components/issues/5597 |
826 | | - private ElementDetachListener childDetachListener = new ElementDetachListener() { |
827 | | - @Override |
828 | | - public void onDetach(ElementDetachEvent e) { |
829 | | - var child = e.getSource(); |
830 | | - var childDetachedFromContainer = !getElement().getChildren() |
831 | | - .anyMatch(containerChild -> Objects.equals(child, |
832 | | - containerChild)); |
833 | | - |
834 | | - if (childDetachedFromContainer) { |
835 | | - // The child was removed from the popover |
836 | | - |
837 | | - // Remove the registration for the child detach listener |
838 | | - childDetachListenerMap.get(child).remove(); |
839 | | - childDetachListenerMap.remove(child); |
840 | | - |
841 | | - updateVirtualChildNodeIds(); |
842 | | - } |
843 | | - } |
844 | | - }; |
845 | | - |
846 | | - @Override |
847 | | - protected void onAttach(AttachEvent attachEvent) { |
848 | | - super.onAttach(attachEvent); |
849 | | - |
850 | | - updateVirtualChildNodeIds(); |
851 | | - } |
852 | | - |
853 | | - /** |
854 | | - * Updates the virtualChildNodeIds property of the popover element. |
855 | | - * <p> |
856 | | - * This method is called whenever the popover's child components change. |
857 | | - * <p> |
858 | | - * Also calls {@code requestContentUpdate} on the popover element to trigger |
859 | | - * the content update. |
860 | | - */ |
861 | | - private void updateVirtualChildNodeIds() { |
862 | | - // Add detach listeners (child may be removed with removeFromParent()) |
863 | | - getElement().getChildren().forEach(child -> { |
864 | | - if (!childDetachListenerMap.containsKey(child)) { |
865 | | - childDetachListenerMap.put(child, |
866 | | - child.addDetachListener(childDetachListener)); |
867 | | - } |
868 | | - }); |
869 | | - |
870 | | - getElement().setPropertyList("virtualChildNodeIds", |
871 | | - getElement().getChildren() |
872 | | - .map(element -> element.getNode().getId()) |
873 | | - .collect(Collectors.toList())); |
874 | | - |
875 | | - getElement().callJsFunction("requestContentUpdate"); |
876 | | - } |
877 | | - |
878 | 764 | /** |
879 | 765 | * Sets the CSS class names of the popover overlay element. This method |
880 | 766 | * overwrites any previous set class names. |
|
0 commit comments