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

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

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

import elemental2.dom.HTMLDivElement;
import elemental2.dom.HTMLElement;
import elemental2.dom.Node;
import org.dominokit.domino.ui.grid.flex.FlexItem;
import org.dominokit.domino.ui.grid.flex.FlexLayout;
import org.dominokit.domino.ui.icons.Icon;
import org.dominokit.domino.ui.icons.Icons;
import org.dominokit.domino.ui.style.Color;
import org.dominokit.domino.ui.style.Styles;
import org.dominokit.domino.ui.utils.*;
import org.gwtproject.editor.client.TakesValue;
import org.jboss.elemento.IsElement;

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

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.jboss.elemento.Elements.div;
import static org.jboss.elemento.Elements.span;

public class SelectOption extends BaseDominoElement> implements HasValue,
        HasBackground, Selectable, TakesValue {

    private static final String SELECTED = "select-option-selected";
    private DominoElement element = DominoElement.of(div().css("select-option"));
    private DominoElement valueContainer = DominoElement.of(span().css(Styles.ellipsis_text));
    private Supplier checkMarkSupplier = () -> Icons.ALL.check().styler(style1 -> style1.add(Styles.pull_right)
            .add("select-option-check-mark"));
    private Icon checkMark;
    private String displayValue;
    private String key;
    private T value;
    private List> selectionHandlers = new ArrayList<>();
    private FlexItem checkMarkFlexItem;
    private boolean excludeFromSearchResults = false;
    private FlexLayout optionLayoutElement;

    public SelectOption(T value, String key, String displayValue) {
        setKey(key);
        setValue(value);
        setDisplayValue(displayValue);
        checkMarkFlexItem = FlexItem.create();
        optionLayoutElement = FlexLayout.create();
        element
                .appendChild(optionLayoutElement
                        .appendChild(FlexItem.create()
                                .css(Styles.ellipsis_text)
                                .setFlexGrow(1)
                                .appendChild(valueContainer))
                        .appendChild(checkMarkFlexItem)
                );
        init(this);
    }

    public SelectOption(T value, String key) {
        this(value, key, key);
    }

    public static  SelectOption create(T value, String key, String displayValue) {
        return new SelectOption<>(value, key, displayValue);
    }

    public static  SelectOption create(T value, String key) {
        return new SelectOption<>(value, key);
    }

    public SelectOption appendChild(Node node) {
        element.appendChild(node);
        return this;
    }

    public SelectOption appendChild(IsElement node) {
        element.appendChild(node.element());
        return this;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public T getValue() {
        return this.value;
    }

    @Override
    public void setValue(T value) {
        this.value = value;
    }

    @Override
    public void addSelectionHandler(Selectable.SelectionHandler selectionHandler) {
        selectionHandlers.add(selectionHandler);
    }

    public String getDisplayValue() {
        return displayValue;
    }

    public SelectOption setDisplayValue(String displayValue) {
        this.displayValue = displayValue;
        valueContainer.setTextContent(displayValue);
        return this;
    }

    @Override
    public SelectOption select() {
        return select(false);
    }

    @Override
    public SelectOption deselect() {
        return deselect(false);
    }

    @Override
    public SelectOption select(boolean silent) {
        style().add(SELECTED);
        if (isNull(checkMark))
            checkMark = checkMarkSupplier.get();
        checkMarkFlexItem.appendChild(checkMark);
        if (!silent) {
            selectionHandlers.forEach(handler -> handler.onSelectionChanged(this));
        }
        return this;
    }

    @Override
    public SelectOption deselect(boolean silent) {
        style().remove(SELECTED);
        if (nonNull(checkMark) && element.contains(checkMark.element()))
            checkMark.remove();
        return this;
    }

    @Override
    public boolean isSelected() {
        return style().contains(SELECTED);
    }

    @Override
    public SelectOption setBackground(Color background) {
        style().add(background.getBackground());
        return this;
    }

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

    public Icon getCheckMark() {
        return checkMark;
    }

    public DominoElement getValueContainer() {
        return valueContainer;
    }

    @Override
    public SelectOption value(T value) {
        setValue(value);
        return this;
    }

    public boolean isExcludeFromSearchResults() {
        return excludeFromSearchResults;
    }

    public SelectOption setExcludeFromSearchResults(boolean excludeFromSearchResults) {
        this.excludeFromSearchResults = excludeFromSearchResults;
        return this;
    }

    public FlexLayout getOptionLayoutElement() {
        return optionLayoutElement;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy