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

org.dominokit.domino.ui.badges.Badge Maven / Gradle / Ivy

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

import elemental2.dom.HTMLElement;
import elemental2.dom.Text;
import org.dominokit.domino.ui.style.Color;
import org.dominokit.domino.ui.style.Elevation;
import org.dominokit.domino.ui.style.Styles;
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.DominoElement;
import org.dominokit.domino.ui.utils.HasBackground;
import org.dominokit.domino.ui.utils.TextNode;

import static java.util.Objects.nonNull;
import static org.jboss.elemento.Elements.span;

public class Badge extends BaseDominoElement implements HasBackground {

    private DominoElement badgeElement = DominoElement.of(span()
            .css(BadgeStyles.BADGE))
            .elevate(Elevation.LEVEL_1);
    private Color badgeBackground;
    private Text textNode = TextNode.empty();

    public Badge() {
        init(this);
        appendChild(textNode);
    }

    public static Badge create(String content) {
        Badge badge = new Badge();
        badge.setText(content);
        return badge;
    }

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

    public Badge setText(String text) {
        textNode.textContent = text;
        return this;
    }

    public Badge pullRight() {
        style().pullRight();
        return this;
    }

    @Override
    public Badge setBackground(Color badgeBackground) {
        if (nonNull(this.badgeBackground)) {
            badgeElement.style().remove(this.badgeBackground.getBackground());
        }

        this.badgeBackground = badgeBackground;
        badgeElement.style().add(this.badgeBackground.getBackground());
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy