org.dominokit.domino.ui.breadcrumbs.Breadcrumb Maven / Gradle / Ivy
package org.dominokit.domino.ui.breadcrumbs;
import elemental2.dom.EventListener;
import elemental2.dom.HTMLOListElement;
import org.dominokit.domino.ui.icons.BaseIcon;
import org.dominokit.domino.ui.style.Color;
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.DominoElement;
import org.dominokit.domino.ui.utils.HasBackground;
import java.util.LinkedList;
import java.util.List;
import static java.util.Objects.nonNull;
import static org.jboss.elemento.Elements.ol;
public class Breadcrumb extends BaseDominoElement implements HasBackground {
private DominoElement element = DominoElement.of(ol()
.css(BreadcrumbStyles.BREADCRUMB));
private List items = new LinkedList<>();
private BreadcrumbItem activeItem;
private boolean removeTail = false;
private Color activeColor;
private Color activeBackground;
private boolean allowNavigation = true;
public Breadcrumb() {
init(this);
}
public static Breadcrumb create() {
return new Breadcrumb();
}
public Breadcrumb appendChild(String text, EventListener onClick) {
BreadcrumbItem item = BreadcrumbItem.create(text);
addNewItem(item);
item.addClickListener(onClick);
return this;
}
public Breadcrumb appendChild(BaseIcon> icon, String text, EventListener onClick) {
BreadcrumbItem item = BreadcrumbItem.create(icon, text);
addNewItem(item);
item.addClickListener(onClick);
return this;
}
public Breadcrumb appendChild(BreadcrumbItem item) {
addNewItem(item);
return this;
}
public Breadcrumb setAllowNavigation(boolean allowNavigation){
this.allowNavigation = allowNavigation;
style.remove(BreadcrumbStyles.NAV_DISABLED);
if(!allowNavigation){
style.add(BreadcrumbStyles.NAV_DISABLED);
}
return this;
}
public boolean isAllowNavigation() {
return allowNavigation;
}
private void addNewItem(BreadcrumbItem item) {
items.add(item);
setActiveItem(item);
element.appendChild(item);
DominoElement.of(item.getClickableElement()).addClickListener(e -> {
if(allowNavigation) {
setActiveItem(item);
}
});
}
private Breadcrumb setActiveItem(BreadcrumbItem item) {
if (nonNull(activeItem))
activeItem.deActivate();
item.activate();
this.activeItem = item;
item.activate();
if (removeTail) {
int index = items.indexOf(item) + 1;
while (items.size() > index) {
items.get(items.size() - 1).element().remove();
items.remove(items.size() - 1);
}
}
return this;
}
public Breadcrumb setRemoveActiveTailItem(boolean removeTail) {
this.removeTail = removeTail;
return this;
}
public Breadcrumb setColor(Color color) {
if (nonNull(this.activeColor))
element.style().remove(color.getStyle());
this.activeColor = color;
element.style().add(color.getStyle());
return this;
}
public Breadcrumb alignCenter() {
style().alignCenter();
return this;
}
public Breadcrumb alignRight() {
style().alignRight();
return this;
}
public Breadcrumb removeAll() {
items.forEach(BaseDominoElement::remove);
items.clear();
this.activeItem = null;
return this;
}
@Override
public HTMLOListElement element() {
return element.element();
}
@Override
public Breadcrumb setBackground(Color background) {
if (nonNull(this.activeBackground))
element.style().remove(background.getBackground());
this.activeBackground = background;
element.style().add(background.getBackground());
return this;
}
public BreadcrumbItem getActiveItem() {
return activeItem;
}
public List getItems() {
return items;
}
}