From 71073b9cc3c16a1ebcde06e1a52cb21dd529d1b7 Mon Sep 17 00:00:00 2001 From: "Ahmad K. Bawaneh" Date: Sun, 8 Dec 2024 15:33:22 +0300 Subject: [PATCH] fix #979 Add an Option to Delay Showing a Tooltip Upon Hovering --- .../domino/ui/config/PopoverConfig.java | 34 +++++++++++++ .../dominokit/domino/ui/config/UIConfig.java | 3 +- .../domino/ui/popover/BasePopover.java | 50 ++++++++++++++++--- 3 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 domino-ui/src/main/java/org/dominokit/domino/ui/config/PopoverConfig.java diff --git a/domino-ui/src/main/java/org/dominokit/domino/ui/config/PopoverConfig.java b/domino-ui/src/main/java/org/dominokit/domino/ui/config/PopoverConfig.java new file mode 100644 index 000000000..2095e5b97 --- /dev/null +++ b/domino-ui/src/main/java/org/dominokit/domino/ui/config/PopoverConfig.java @@ -0,0 +1,34 @@ +/* + * 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.config; + +import org.dominokit.domino.ui.menu.direction.DropDirection; +import org.dominokit.domino.ui.utils.DominoUIConfig; + +public interface PopoverConfig extends ZIndexConfig { + + default DropDirection getDefaultDropDirection() { + return DropDirection.BEST_MIDDLE_UP_DOWN; + } + + default boolean closeOnBlur() { + return DominoUIConfig.CONFIG.isClosePopupOnBlur(); + } + + default int openDelay() { + return 0; + } +} diff --git a/domino-ui/src/main/java/org/dominokit/domino/ui/config/UIConfig.java b/domino-ui/src/main/java/org/dominokit/domino/ui/config/UIConfig.java index 190efe310..fea167fd5 100644 --- a/domino-ui/src/main/java/org/dominokit/domino/ui/config/UIConfig.java +++ b/domino-ui/src/main/java/org/dominokit/domino/ui/config/UIConfig.java @@ -35,4 +35,5 @@ public interface UIConfig CarouselConfig, RichTextConfig, SlidersConfig, - MenuConfig {} + MenuConfig, + PopoverConfig {} diff --git a/domino-ui/src/main/java/org/dominokit/domino/ui/popover/BasePopover.java b/domino-ui/src/main/java/org/dominokit/domino/ui/popover/BasePopover.java index 0b0326a87..b6c1c1198 100644 --- a/domino-ui/src/main/java/org/dominokit/domino/ui/popover/BasePopover.java +++ b/domino-ui/src/main/java/org/dominokit/domino/ui/popover/BasePopover.java @@ -24,7 +24,7 @@ import java.util.function.Supplier; import jsinterop.base.Js; import org.dominokit.domino.ui.config.HasComponentConfig; -import org.dominokit.domino.ui.config.ZIndexConfig; +import org.dominokit.domino.ui.config.PopoverConfig; import org.dominokit.domino.ui.elements.DivElement; import org.dominokit.domino.ui.events.EventType; import org.dominokit.domino.ui.menu.direction.DropDirection; @@ -58,7 +58,7 @@ public abstract class BasePopover> implements IsPopup, PopoverStyles, FollowOnScroll.ScrollFollower, - HasComponentConfig { + HasComponentConfig { protected final Element targetElement; protected final EventListener closeAllListener; private DivElement root; @@ -67,14 +67,16 @@ public abstract class BasePopover> private DivElement header; private DivElement body; - private DropDirection popupPosition = DropDirection.BEST_MIDDLE_UP_DOWN; + private DropDirection popupPosition; private boolean closeOthers = true; protected final EventListener closeListener; private final FollowOnScroll followOnScroll; private Supplier openCondition = () -> true; private EventListener lostFocusListener; - private boolean closeOnBlur = DominoUIConfig.CONFIG.isClosePopupOnBlur(); + private boolean closeOnBlur; + private PopoverConfig ownConfig; + private int openDelay; /** * Constructs a new BasePopover associated with the provided target element. BasePopover is the @@ -97,6 +99,10 @@ public BasePopover(Element target) { .appendChild(body = div().addCss(dui_popover_body))); init((T) this); + this.popupPosition = getConfig().getDefaultDropDirection(); + this.closeOnBlur = getConfig().closeOnBlur(); + this.openDelay = getConfig().openDelay(); + followOnScroll = new FollowOnScroll(targetElement, this); closeListener = getCloseListener(); @@ -108,13 +114,13 @@ public BasePopover(Element target) { }); elementOf(targetElement) .onDetached( - mutationRecord -> { + (e, mutationRecord) -> { close(); DomGlobal.document.body.removeEventListener("blur", lostFocusListener, true); }); onDetached( - mutationRecord -> { + (e, mutationRecord) -> { body().removeEventListener(EventType.keydown.getName(), closeListener); DomGlobal.document.body.removeEventListener("blur", lostFocusListener, true); }); @@ -145,6 +151,30 @@ public BasePopover(Element target) { }; } + @Override + public PopoverConfig getOwnConfig() { + return ownConfig; + } + + public T setOwnConfig(PopoverConfig ownConfig) { + this.ownConfig = ownConfig; + return (T) this; + } + + /** @return delay in milliseconds before showing the popover. */ + public int getOpenDelay() { + return openDelay; + } + + /** + * @param openDelay milliseconds to wait before showing the popover. + * @return same component instance + */ + public T setOpenDelay(int openDelay) { + this.openDelay = openDelay; + return (T) this; + } + /** * Gets the EventListener responsible for closing the popover when certain events occur. * @@ -187,6 +217,14 @@ public T open() { * the body, positions the popover, and starts monitoring for scroll events (if applicable). */ protected void doOpen() { + if (getOpenDelay() > 0) { + DelayedExecution.execute(this::openPopover, getOpenDelay()); + } else { + openPopover(); + } + } + + private void openPopover() { body().appendChild(root.element()); super.expand(); doPosition();