All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dominokit.domino.ui.modals.ModalBackDrop Maven / Gradle / Ivy

There is a newer version: 1.0.139
Show newest version
package org.dominokit.domino.ui.modals;

import elemental2.dom.Event;
import elemental2.dom.HTMLDivElement;
import jsinterop.base.Js;
import org.dominokit.domino.ui.popover.Popover;
import org.jboss.elemento.Elements;
import org.jboss.elemento.EventType;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class ModalBackDrop {
    public static final int INCREMENT = 10;
    private static Deque openedModals = new LinkedList<>();
    private static Deque openedPopOvers = new LinkedList<>();
    private static Integer NEXT_Z_INDEX = 1040;
    public static final HTMLDivElement INSTANCE = Elements.div()
            .css(ModalStyles.MODAL_BACKDROP)
            .css(ModalStyles.FADE)
            .css(ModalStyles.IN)
            .on(EventType.click, event -> {
                if (ModalBackDrop.INSTANCE.isEqualNode(Js.uncheckedCast(event.target))) {
                    closeCurrentOpen();
                }
            })
            .on(EventType.keypress, event -> {
                if (ModalBackDrop.INSTANCE.isEqualNode(Js.uncheckedCast(event.target))) {
                    closeCurrentOpen();
                }
            })
            .on(EventType.scroll, Event::stopPropagation)
            .element();

    private static void closeCurrentOpen() {
        if (!ModalBackDrop.openedModals.isEmpty()) {
            BaseModal modal = ModalBackDrop.openedModals.peek();
            if (modal.isAutoClose()) {
                modal.close();
            }
        }
    }

    public static boolean contains(BaseModal modal) {
        return openedModals.contains(modal);
    }

    public static void push(BaseModal modal) {
        openedModals.push(modal);
        NEXT_Z_INDEX += INCREMENT;
    }

    public static void popModal(BaseModal modal) {
        openedModals.remove(modal);
        NEXT_Z_INDEX -= INCREMENT;
    }

    public static boolean contains(Popover popover) {
        return openedPopOvers.contains(popover);
    }

    public static void push(Popover popover) {
        openedPopOvers.push(popover);
        NEXT_Z_INDEX += INCREMENT;
    }

    public static void popPopOver() {
        if (!openedPopOvers.isEmpty()) {
            openedPopOvers.pop();
            NEXT_Z_INDEX -= INCREMENT;
        }
    }

    public static Integer getNextZIndex() {
        return NEXT_Z_INDEX;
    }

    public static void toNextZIndex() {
        NEXT_Z_INDEX += INCREMENT;
    }

    public static void closePopovers() {
        List opened = new ArrayList<>(openedPopOvers);
        opened.forEach(Popover::close);
    }

    public static void onScrollClosePopovers() {
        List shouldClose = openedPopOvers.stream().filter(Popover::isCloseOnScroll).collect(Collectors.toList());
        for (Popover popover : shouldClose) {
            popover.close();
        }
    }

    public static int openedModalsCount() {
        return openedModals.size();
    }

    public static boolean allOpenedNotModals() {
        return openedModals.stream().noneMatch(BaseModal::isModal);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy