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

org.dominokit.domino.ui.spin.SpinSelect Maven / Gradle / Ivy

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

import elemental2.dom.HTMLAnchorElement;
import elemental2.dom.HTMLDivElement;
import org.dominokit.domino.ui.icons.BaseIcon;
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.DominoElement;
import org.dominokit.domino.ui.utils.HasSelectionHandler;
import org.dominokit.domino.ui.utils.SwipeUtil;

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

import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.spin.SpinStyles.*;
import static org.jboss.elemento.Elements.a;
import static org.jboss.elemento.Elements.div;

abstract class SpinSelect> extends BaseDominoElement implements HasSelectionHandler> {

    protected DominoElement element = DominoElement.of(div().css(getStyle()));

    private DominoElement prevAnchor = DominoElement.of(a().css(prev).css(disabled));

    private DominoElement nextAnchor = DominoElement.of(a().css(next));
    protected DominoElement contentPanel = DominoElement.of(div().css(spin_content));
    protected DominoElement main = DominoElement.of(div().add(contentPanel).css(spin_container));
    protected List> items = new ArrayList<>();
    private SpinItem activeItem;
    private List>> selectionHandlers = new ArrayList<>();
    private NavigationHandler navigationHandler = direction -> {
    };

    SpinSelect(BaseIcon backIcon, BaseIcon forwardIcon) {
        element.appendChild(
                prevAnchor.appendChild(backIcon
                        .clickable()
                        .addClickListener(evt -> {
                            moveBack();
                            navigationHandler.onNavigate(Direction.BACKWARD);
                        })))
                .appendChild(main)
                .appendChild(
                        nextAnchor.appendChild(forwardIcon
                                .clickable()
                                .addClickListener(evt -> {
                                    moveForward();
                                    navigationHandler.onNavigate(Direction.FORWARD);
                                })));
        init((S) this);
        onAttached(mutationRecord -> fixElementsWidth());
        SwipeUtil.addSwipeListener(SwipeUtil.SwipeDirection.RIGHT, main.element(), evt -> moveBack());
        SwipeUtil.addSwipeListener(SwipeUtil.SwipeDirection.LEFT, main.element(), evt -> moveForward());
    }

    public S moveForward() {
        moveToIndex(items.indexOf(this.activeItem) + 1);
        return (S) this;
    }

    public S moveBack() {
        moveToIndex(items.indexOf(this.activeItem) - 1);
        return (S) this;
    }

    public S moveToIndex(int targetIndex) {
        if (targetIndex < items.size() && targetIndex >= 0) {
            int activeIndex = items.indexOf(activeItem);
            if (targetIndex != activeIndex) {
                this.activeItem = items.get(targetIndex);
                double offset = (100d / items.size()) * (targetIndex);
                setTransformProperty(offset);
                informSelectionHandlers();
            }
        }

        updateArrowsVisibility();
        return (S) this;
    }

    public S moveToItem(SpinItem item) {
        if (items.contains(item)) {
            return moveToIndex(items.indexOf(item));
        }
        return (S) this;
    }

    private void updateArrowsVisibility() {
        if (items.indexOf(this.activeItem) == items.size() - 1) {
            nextAnchor.style().add(disabled);
        } else {
            nextAnchor.style().remove(disabled);
        }

        if (items.indexOf(this.activeItem) < 1) {
            prevAnchor.style().add(disabled);
        } else {
            prevAnchor.style().remove(disabled);
        }
    }

    public S appendChild(SpinItem spinItem) {
        if (items.isEmpty()) {
            this.activeItem = spinItem;
        }
        items.add(spinItem);
        contentPanel.appendChild(spinItem);
        return (S) this;
    }

    public SpinItem getActiveItem() {
        return activeItem;
    }

    private void informSelectionHandlers() {
        selectionHandlers.forEach(spinItemSelectionHandler -> spinItemSelectionHandler.onSelection(this.activeItem));
    }

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

    @Override
    public S removeSelectionHandler(SelectionHandler> selectionHandler) {
        selectionHandlers.remove(selectionHandler);
        return (S) this;
    }

    public List> getItems() {
        return items;
    }

    public int indexOf(SpinItem item) {
        if (items.contains(item)) {
            return items.indexOf(item);
        } else {
            return -1;
        }
    }

    public int itemsCount() {
        return items.size();
    }

    public boolean isLastItem(SpinItem item) {
        if (items.contains(item) && indexOf(item) == (itemsCount() - 1)) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isFirstItem(SpinItem item) {
        if (items.contains(item) && indexOf(item) == 0) {
            return true;
        } else {
            return false;
        }
    }

    public S gotoFirst() {
        moveToIndex(0);
        return (S) this;
    }

    public S gotoLast() {
        moveToIndex(itemsCount() - 1);
        return (S) this;
    }

    public S onNavigate(NavigationHandler navigationHandler) {
        if (nonNull(navigationHandler)) {
            this.navigationHandler = navigationHandler;
        }
        return (S) this;
    }

    public DominoElement getPrevAnchor() {
        return prevAnchor;
    }

    public DominoElement getNextAnchor() {
        return nextAnchor;
    }

    public DominoElement getContentPanel() {
        return contentPanel;
    }

    protected abstract void fixElementsWidth();

    protected abstract void setTransformProperty(double offset);

    protected abstract String getStyle();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy