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

org.dominokit.domino.ui.dropdown.DropdownActionsGroup Maven / Gradle / Ivy

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

import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLLIElement;
import elemental2.dom.Node;
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.DominoElement;
import org.jboss.elemento.IsElement;

import java.util.ArrayList;
import java.util.List;

import static elemental2.dom.DomGlobal.document;
import static java.util.Objects.nonNull;
import static org.jboss.elemento.Elements.li;

public class DropdownActionsGroup extends BaseDominoElement> {
    private DominoElement element = DominoElement.of(li().css(DropDownStyles.DROPDOWN_HEADER));
    private List> actions = new ArrayList<>();
    private DropDownMenu menu;

    public DropdownActionsGroup(Node titleElement) {
        element.addEventListener("click", evt -> {
            evt.preventDefault();
            evt.stopPropagation();
        });
        element.appendChild(titleElement);
        init(this);
    }

    public static  DropdownActionsGroup create(String title) {
        return create(document.createTextNode(title));
    }

    public static  DropdownActionsGroup create(Node titleElement) {
        return new DropdownActionsGroup<>(titleElement);
    }

    public static  DropdownActionsGroup create(HTMLElement titleElement) {
        return create((Node) titleElement);
    }

    public static  DropdownActionsGroup create(IsElement titleElement) {
        return create(titleElement.element());
    }


    public DropdownActionsGroup appendChild(DropdownAction action) {
        actions.add(action);
        addActionToMenu(action);
        return this;
    }

    public List> getActions() {
        return actions;
    }

    @Override
    public HTMLLIElement element() {
        return element.element();
    }

    boolean isAllHidden() {
        return actions.stream().allMatch(DropdownAction::isHidden);
    }

    public void bindTo(DropDownMenu menu) {
        this.menu = menu;
        for (DropdownAction action : actions) {
            addActionToMenu(action);
        }
    }

    private void addActionToMenu(DropdownAction action) {
        if (nonNull(menu)) {
            action.addHideListener(this::changeVisibility);
            action.addShowListener(this::changeVisibility);
            menu.appendChild(action);
        }
    }

    void changeVisibility() {
        if (isAllHidden()) {
            hide();
        } else {
            show();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy