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

org.dominokit.domino.ui.chips.ChipsGroup Maven / Gradle / Ivy

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

import elemental2.dom.HTMLDivElement;
import org.dominokit.domino.ui.style.ColorScheme;
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.HasSelectionHandler;
import org.dominokit.domino.ui.utils.HasDeselectionHandler;
import org.dominokit.domino.ui.utils.Switchable;

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

import static org.jboss.elemento.Elements.div;

public class ChipsGroup extends BaseDominoElement implements Switchable, 
        HasSelectionHandler, HasDeselectionHandler {

    private HTMLDivElement element = div().element();
    private List chips = new ArrayList<>();
    private Chip selectedChip;
    private List> selectionHandlers = new ArrayList<>();
    private List deSelectionHandlers = new ArrayList<>();

    public ChipsGroup() {
        init(this);
    }

    public static ChipsGroup create() {
        return new ChipsGroup();
    }

    /**
     * @deprecated use {@link #appendChild(Chip)}
     */
    @Deprecated
    public ChipsGroup addChip(Chip chip) {
        return appendChild(chip);
    }


    public ChipsGroup appendChild(Chip chip) {
        chip.setSelectable(true);
        chip.addSelectionHandler(value -> {
            for (Chip c : chips) {
                if (!c.equals(chip)) {
                    c.deselect();
                }
            }
            this.selectedChip = chip;
            selectionHandlers.forEach(handler -> handler.onSelection(chip));
        });
        chip.addDeselectionHandler(() -> {
            this.selectedChip = null;
            deSelectionHandlers.forEach(handler -> handler.onDeselection());
        });
        chips.add(chip);
        element.appendChild(chip.element());
        return this;
    }

    @Override
    public ChipsGroup enable() {
        chips.forEach(Chip::enable);
        return this;
    }

    @Override
    public ChipsGroup disable() {
        chips.forEach(Chip::disable);
        return this;
    }

    @Override
    public boolean isEnabled() {
        return chips.stream().allMatch(Chip::isEnabled);
    }

    public Chip getSelectedChip() {
        return selectedChip;
    }

    @Override
    public ChipsGroup addSelectionHandler(SelectionHandler selectionHandler) {
        selectionHandlers.add(selectionHandler);
        return this;
    }

    @Override
    public ChipsGroup removeSelectionHandler(SelectionHandler selectionHandler) {
        selectionHandlers.remove(selectionHandler);
        return this;
    }
    
    @Override
    public ChipsGroup addDeselectionHandler(DeselectionHandler deselectionHandler) {
       deSelectionHandlers.add(deselectionHandler);
       return this;
    }

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

    public ChipsGroup setColorScheme(ColorScheme colorScheme) {
        chips.forEach(chip -> chip.setColorScheme(colorScheme));
        return this;
    }

    public ChipsGroup selectAt(int index) {
        if (index >= 0 && index < chips.size()) {
            chips.get(index).select();
        }
        return this;
    }

    public List getChips() {
        return chips;
    }

    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy