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

org.dominokit.domino.ui.forms.SelectOptionGroup Maven / Gradle / Ivy

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

import elemental2.dom.DomGlobal;
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.gwt.elemento.core.IsElement;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import static java.util.Objects.nonNull;
import static org.jboss.gwt.elemento.core.Elements.li;

public class SelectOptionGroup extends BaseDominoElement> {

    private DominoElement element = DominoElement.of(li().css("dropdown-header"));
    private List> options = new ArrayList<>();
    private Node titleElement;
    private Consumer> addOptionConsumer = (option)->{};

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

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

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

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

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

    /**
     * @deprecated use {@link #appendChild(SelectOption)}
     */
    @Deprecated
    public SelectOptionGroup addOption(SelectOption option) {
        return appendChild(option);
    }

    public SelectOptionGroup appendChild(SelectOption option) {
        option.style().add("opt");
        options.add(option);
        if(nonNull(addOptionConsumer)){
            addOptionConsumer.accept(option);
        }
        return this;
    }

    public void setAddOptionConsumer(Consumer> addOptionConsumer) {
        this.addOptionConsumer = addOptionConsumer;
    }

    public List> getOptions() {
        return options;
    }

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

    boolean isAllHidden() {
        return options.stream().allMatch(SelectOption::isHidden);
    }

    void addOptionsTo(Select select) {
        for (SelectOption option : options) {
            option.addHideListener(this::changeVisibility);
            option.addShowListener(this::changeVisibility);
            select.appendChild(option);
        }
    }

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

    public Node getTitleElement() {
        return titleElement;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy