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

org.dominokit.domino.ui.pagination.Pager Maven / Gradle / Ivy

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

import elemental2.dom.HTMLAnchorElement;
import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLLIElement;
import elemental2.dom.HTMLUListElement;
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.DominoElement;
import org.jboss.elemento.EventType;
import org.jboss.elemento.HtmlContentBuilder;

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

public class Pager extends BaseDominoElement {

    private DominoElement pagerElement = DominoElement.of(ul().css("pager"));
    private DominoElement element = DominoElement.of(nav().add(pagerElement));

    private DominoElement nextElement;
    private DominoElement prevElement;

    private DominoElement nextAnchor;
    private DominoElement prevAnchor;

    private PagerChangeCallback onNext = () -> {
    };
    private PagerChangeCallback onPrev = () -> {
    };

    private boolean allowNext = true;
    private boolean allowPrev = true;

    public Pager() {
        HtmlContentBuilder nextAnchor = a();
        this.nextAnchor = DominoElement.of(nextAnchor);
        nextElement = DominoElement.of(li().add(nextAnchor.css("wave-effect")
                .on(EventType.click, event -> {
                    if (allowNext)
                        onNext.onChange();
                })
                .textContent("Next")
                .element()));

        HtmlContentBuilder prevAnchor = a();
        this.prevAnchor = DominoElement.of(prevAnchor);
        prevElement = DominoElement.of(li().add(prevAnchor.css("wave-effect")
                .on(EventType.click, event -> {
                    if (allowPrev)
                        onPrev.onChange();
                })
                .textContent("Previous")));
        pagerElement.appendChild(prevElement);
        pagerElement.appendChild(nextElement);

        init(this);
    }

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

    public Pager onNext(PagerChangeCallback nextCallback) {
        this.onNext = nextCallback;
        return this;
    }

    public Pager onPrevious(PagerChangeCallback previousCallback) {
        this.onPrev = previousCallback;
        return this;
    }

    public Pager disableNext() {
        this.allowNext = false;
        nextElement.style().remove("disabled");
        nextElement.style().add("disabled");

        return this;
    }

    public Pager disablePrevious() {
        this.allowPrev = false;
        prevElement.style().remove("disabled");
        prevElement.style().add("disabled");

        return this;
    }

    public Pager enableNext() {
        this.allowNext = true;
        nextElement.style().remove("disabled");

        return this;
    }

    public Pager enablePrevious() {
        this.allowPrev = true;
        prevElement.style().remove("disabled");

        return this;
    }

    public Pager nextText(String text) {
        nextAnchor.setTextContent(text);
        return this;
    }

    public Pager previousText(String text) {
        prevAnchor.setTextContent(text);
        return this;
    }

    @Override
    public Pager show() {
        nextElement.style().add("next");
        prevElement.style().add("previous");
        return this;
    }

    public Pager showArrows() {
        prevAnchor.insertFirst(span().attr("aria-hidden", "true").textContent("←"));
        nextAnchor.appendChild(span().attr("aria-hidden", "true").textContent("→"));
        return this;
    }

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


    public interface PagerChangeCallback {
        void onChange();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy