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

fi.evolver.ai.vaadin.component.form.SideFormChatContainer Maven / Gradle / Ivy

There is a newer version: 1.5.5
Show newest version
package fi.evolver.ai.vaadin.component.form;

import com.vaadin.flow.component.*;
import com.vaadin.flow.component.details.Details;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H3;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.page.Page;
import com.vaadin.flow.dom.Style.BoxSizing;
import com.vaadin.flow.shared.Registration;
import com.vaadin.flow.theme.lumo.LumoUtility;

import fi.evolver.ai.vaadin.component.ChatMessageContainer;
import fi.evolver.ai.vaadin.component.i18n.MessageInputI18nFactory;
import fi.evolver.ai.vaadin.entity.HasValueGetterSetter;
import fi.evolver.utils.string.StringUtils;

public class SideFormChatContainer & HasEnabled, TVal>
		extends ChatMessageContainer implements HasValueGetterSetter {
	private final int breakpoint;
	private final String formWidth;
	private boolean isLayoutMobile = false;

	protected final Div inputContainer = new Div();
	protected final VerticalLayout messageContainer = new VerticalLayout();
	protected final VerticalLayout formContainer = new VerticalLayout();

	protected final C form;
	private final String formTitle;

	protected Registration listener;

	public SideFormChatContainer(C form, String formTitleTranslationKey, int breakpoint, String formWidth) {
		super(false);
		this.form = form;
		this.breakpoint = breakpoint;
		this.formWidth = formWidth;
		this.formTitle = getTranslation(formTitleTranslationKey);
		addComponents();
	}

	public SideFormChatContainer(C form, String formTitleTranslationKey) {
		this(form, formTitleTranslationKey, 1200, "400px");
	}

	@Override
	public TVal getValue() {
		return form.getValue();
	}

	@Override
	public void setValue(TVal value) {
		form.setValue(value);
	}

	@Override
	protected void onAttach(AttachEvent attachEvent) {
		super.onAttach(attachEvent);
		Page page = attachEvent.getUI().getPage();
		listener = page.addBrowserWindowResizeListener(event -> setInputWidths(event.getWidth()));
		page.retrieveExtendedClientDetails(receiver -> {
			int width = receiver.getBodyClientWidth();
			isLayoutMobile = width > breakpoint; // Set the initial width so that the layout is build always
			setInputWidths(receiver.getBodyClientWidth());
		});
	}

	@Override
	protected void onDetach(DetachEvent detachEvent) {
		listener.remove();
		super.onDetach(detachEvent);
	}

	protected void setInputWidths(int width) {
		if (isLayoutMobile != width > breakpoint)
			return;
		if (width > breakpoint) {
			inputContainer.removeClassName(LumoUtility.FlexDirection.COLUMN);
			inputContainer.addClassName(LumoUtility.FlexDirection.ROW);
			inputContainer.removeAll();
			formContainer.setWidth(formWidth);
			formContainer.setMaxHeight(null);
			messageContainer.setWidthFull();
			inputContainer.add(messageContainer, formContainer);
			isLayoutMobile = false;
		}
		else {
			inputContainer.removeClassName(LumoUtility.FlexDirection.ROW);
			inputContainer.addClassName(LumoUtility.FlexDirection.COLUMN);
			inputContainer.removeAll();
			formContainer.setWidthFull();
			formContainer.setMaxHeight(80, Unit.VH);
			Details details = new Details(formTitle);
			details.setWidthFull();
			details.add(formContainer);
			inputContainer.add(details, messageContainer);
			messageContainer.setWidthFull();
			isLayoutMobile = true;
		}
	}

	@Override
	protected void addComponents() {
		super.addComponents();
		setPadding(false);
		addLayout();
		reset();
	}

	protected void addLayout() {
		inputContainer.setSizeFull();
		inputContainer.addClassName(LumoUtility.Display.FLEX);
		messageContainer.setSizeFull();
		messageContainer.setPadding(false);
		messageContainer.addClassName(LumoUtility.Overflow.HIDDEN);
		formContainer.setHeightFull();
		formContainer.setPadding(false);
		formContainer.addClassName(LumoUtility.Overflow.SCROLL);
		if (StringUtils.hasText(formTitle))
			formContainer.add(new H3(formTitle));
		form.setSizeFull();
		formContainer.add(form);
		add(inputContainer);
	}

	@Override
	protected void addMessageList() {
		chatMessageList.setId("chat-message-list-container");
		chatMessageList.setSizeFull();
		chatMessageList.getStyle().setBoxSizing(BoxSizing.BORDER_BOX);
		messageContainer.add(chatMessageList);
		expand(chatMessageList);
	}

	@Override
	protected void addInputs() {
		chatMessageInput.setI18n(MessageInputI18nFactory.getI18n(this::getTranslation));
		chatMessageInput.setWidthFull();
		messageContainer.add(chatMessageInput);
	}

	@Override
	public void reset() {
		chatMessageList.reset();
		form.setValue(null);
	}

	@Override
	public void setInputEnabled(Boolean enabled) {
		chatMessageInput.setEnabled(enabled);
		form.setEnabled(enabled);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy