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

com.metaeffekt.artifact.analysis.dashboard.elements.ContentCard Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.metaeffekt.artifact.analysis.dashboard.elements;

import com.metaeffekt.artifact.analysis.utils.StringUtils;
import j2html.tags.ContainerTag;
import j2html.tags.DomContent;
import j2html.tags.specialized.DivTag;
import j2html.tags.specialized.H5Tag;

import static j2html.TagCreator.*;

public class ContentCard {

    private final DivTag content = new DivTag().withStyle("padding:0px;padding-left:6px;margin:0px;");
    private final H5Tag title = new H5Tag().withStyle("margin-bottom:2px");
    private ContentCardType color;

    private int characterCollapseThreshold = Integer.MAX_VALUE;

    public ContentCard() {
        withType(ContentCardType.DEFAULT);
    }

    public ContentCard(ContentCardType type) {
        withType(type);
    }

    public ContentCard withCharacterCollapseThreshold(int characterCollapseThreshold) {
        this.characterCollapseThreshold = characterCollapseThreshold;
        return this;
    }

    public ContentCard withType(ContentCardType type) {
        if (type == null || type == ContentCardType.DEFAULT) this.color = ContentCardType.GRAY;
        else this.color = type;
        return this;
    }

    public ContentCard with(String... content) {
        for (String s : content) {
            this.content.with(text(s));
        }
        return this;
    }

    public ContentCard with(DomContent... content) {
        this.content.with(content);
        return this;
    }

    public ContentCard withText(String... content) {
        for (String s : content) {
            if (StringUtils.isEmpty(s)) continue;
            if (!s.contains("

")) { this.content.with(p(s)); } else { this.content.with(rawHtml(s)); } } return this; } public ContentCard withTitle(String title) { this.title.with(text(title)); return this; } public DivTag generate() { final DivTag generated = div().withClasses("data-sheet-content-card", color.name().toLowerCase().replace("_", "-")); if (title.getNumChildren() > 0) { generated.with(title); } if (content.getNumChildren() > 0) { if (htmlTextContentLength(content.render()) > characterCollapseThreshold) { generated.withStyle("overflow: hidden; max-height: 180px; position: relative;"); generated.with(content); generated.with( span("Show more") .withClasses("badge", "badge-primary") .withStyle("position: absolute; bottom: 8px; left: 14px; cursor: pointer;") .attr("onclick", "this.parentElement.style.maxHeight = 'none'; this.style.display = 'none';") ); } else { generated.with(content); } } return generated; } public void generate(ContainerTag append) { append.with(generate()); } private int htmlTextContentLength(String html) { return html.replaceAll("<[^>]*>", "").length(); } public int getNumChildren() { return content.getNumChildren(); } public enum ContentCardType { DEFAULT, LIGHT_GRAY, PASTEL_BLUE, GRAY, GREEN, RED } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy