org.dominokit.domino.ui.pagination.AdvancedPagination Maven / Gradle / Ivy
package org.dominokit.domino.ui.pagination;
import elemental2.dom.DomGlobal;
import elemental2.dom.EventListener;
import elemental2.dom.HTMLAnchorElement;
import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLLIElement;
import org.dominokit.domino.ui.forms.Select;
import org.dominokit.domino.ui.forms.SelectOption;
import org.dominokit.domino.ui.icons.Icons;
import org.dominokit.domino.ui.keyboard.KeyboardEvents;
import org.dominokit.domino.ui.utils.DominoElement;
import java.util.function.Function;
import java.util.stream.IntStream;
import static org.jboss.elemento.Elements.a;
import static org.jboss.elemento.Elements.li;
public class AdvancedPagination extends BasePagination {
private DominoElement firstPage;
private DominoElement lastPage;
private DominoElement prevAnchor;
private DominoElement firstPageAnchor;
private DominoElement nextAnchor;
private DominoElement lastPageAnchor;
private Select pagesSelect;
private Function pagesCountTextHandler = pagesCount -> " of " + pagesCount + " Pages";
public static AdvancedPagination create() {
return new AdvancedPagination();
}
public static AdvancedPagination create(int pages) {
return new AdvancedPagination(pages);
}
public static AdvancedPagination create(int pages, int pageSize) {
return new AdvancedPagination(pages, pageSize);
}
public AdvancedPagination() {
this(0, 10);
}
public AdvancedPagination(int pages) {
this(pages, 10);
}
public AdvancedPagination(int pages, int pageSize) {
this.pagesCount = pages;
this.pageSize = pageSize;
init(this);
updatePages(pages, pageSize);
}
@Override
public AdvancedPagination updatePages(int pages) {
return updatePages(pages, pageSize);
}
@Override
public AdvancedPagination updatePages(int pages, int pageSize) {
this.pageSize = pageSize;
this.pagesCount = pages;
this.index = 1;
allPages.clear();
prevAnchor = DominoElement.of(a());
prevElement = DominoElement.of(li().css("page-nav"))
.appendChild(prevAnchor
.setTooltip("Previous page")
.appendChild(Icons.ALL.chevron_left()
.clickable()));
addListenerToElement(prevAnchor, event -> moveToPage(index - 1, false));
firstPageAnchor = DominoElement.of(a());
firstPage = DominoElement.of(li().css("page-nav"))
.appendChild(firstPageAnchor
.setTooltip("First page")
.appendChild(Icons.ALL.skip_previous()
.clickable()));
addListenerToElement(firstPageAnchor, event -> moveToPage(1, false));
pagesElement.clearElement()
.appendChild(firstPage)
.appendChild(prevElement);
pagesSelect = Select.create()
.styler(style -> style.setMarginBottom("0px"))
.addSelectionHandler(option -> moveToPage(option.getValue(), false));
if (pages > 0) {
IntStream.rangeClosed(1, pages).forEach(p -> {
pagesSelect.appendChild(SelectOption.create(p, p + "")
.apply(element -> allPages.add(DominoElement.of(element.element()))));
});
}
pagesElement.appendChild(DominoElement.of(li()).appendChild(a().style("margin-left: 10px; margin-right: 10px;").add(pagesSelect)));
pagesElement.appendChild(DominoElement.of(li()).appendChild(a().css("adv-page-count").textContent(pagesCountTextHandler.apply(pages))));
nextAnchor = DominoElement.of(a());
nextElement = DominoElement.of(li().css("page-nav"))
.appendChild(nextAnchor
.setTooltip("Next page")
.appendChild(Icons.ALL.chevron_right()
.clickable()));
addListenerToElement(nextAnchor, event -> moveToPage(index + 1, false));
lastPageAnchor = DominoElement.of(a());
lastPage = DominoElement.of(li().css("page-nav"))
.appendChild(lastPageAnchor
.setTooltip("Last page")
.appendChild(Icons.ALL.skip_next()
.clickable()));
addListenerToElement(lastPageAnchor, event -> {
DomGlobal.console.info("going to last page : " + allPages.size());
moveToPage(allPages.size(), false);
});
if (pages > 0) {
moveToPage(1, true);
}
if (pages <= 0) {
nextElement.disable();
prevElement.disable();
}
pagesElement
.appendChild(nextElement)
.appendChild(lastPage);
return this;
}
private void addListenerToElement(DominoElement extends HTMLElement> element, EventListener listener) {
element.addClickListener(listener);
KeyboardEvents.listenOn(element).onEnter(listener);
}
@Override
protected void moveToPage(int page, boolean silent) {
if (page > 0 && page <= pagesCount) {
index = page;
if (!silent) {
pageChangedCallBack.onPageChanged(page);
}
if (page == pagesCount) {
nextElement.disable();
lastPage.disable();
nextAnchor.removeAttribute("tabindex");
lastPageAnchor.removeAttribute("tabindex");
} else {
nextElement.enable();
lastPage.enable();
nextAnchor.setAttribute("tabindex", "0");
lastPageAnchor.setAttribute("tabindex", "0");
}
if (page == 1) {
prevElement.disable();
firstPage.disable();
prevAnchor.removeAttribute("tabindex");
firstPageAnchor.removeAttribute("tabindex");
} else {
prevElement.enable();
firstPage.enable();
prevAnchor.setAttribute("tabindex", "0");
firstPageAnchor.setAttribute("tabindex", "0");
}
pagesSelect.selectAt(page - 1, true);
}
}
public DominoElement getPrevAnchor() {
return prevAnchor;
}
public DominoElement getFirstPageAnchor() {
return firstPageAnchor;
}
public DominoElement getNextAnchor() {
return nextAnchor;
}
public DominoElement getLastPageAnchor() {
return lastPageAnchor;
}
public void setPagesCountTextHandler(Function pagesCountTextHandler) {
this.pagesCountTextHandler = pagesCountTextHandler;
}
}