added gridstack

This commit is contained in:
Pit Friedrich
2026-07-25 20:51:53 +02:00
parent 198988da35
commit c2d29f0635
3 changed files with 425 additions and 0 deletions
@@ -0,0 +1,86 @@
package com.example.components;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.Div;
import java.util.UUID;
/**
* A single cell inside a {@link GridStackLayout}. Position/size are written as
* {@code gs-*} attributes, which is how gridstack.js reads a widget's initial
* placement when {@code makeWidget} converts it client-side (see grid-stack.ts).
*/
public class GridStackItem extends Div {
private final Div content = new Div();
public GridStackItem(Component... content) {
this(UUID.randomUUID().toString(), 0, 0, 4, 3, content);
}
public GridStackItem(String id, int x, int y, int w, int h, Component... content) {
addClassName("grid-stack-item");
getElement().setAttribute("gs-id", id);
getElement().setAttribute("gs-x", String.valueOf(x));
getElement().setAttribute("gs-y", String.valueOf(y));
getElement().setAttribute("gs-w", String.valueOf(w));
getElement().setAttribute("gs-h", String.valueOf(h));
this.content.addClassName("grid-stack-item-content");
add(this.content);
this.content.add(content);
}
public void add(Component... components) {
content.add(components);
}
public void removeAll() {
content.removeAll();
}
public String getItemId() {
return getElement().getAttribute("gs-id");
}
public GridStackItem setResizable(boolean resizable) {
setBooleanAttribute("gs-no-resize", !resizable);
return this;
}
public GridStackItem setMovable(boolean movable) {
setBooleanAttribute("gs-no-move", !movable);
return this;
}
/** Writes an explicit "true" (gridstack reads this via its own
* {@code Utils.toBool}) or removes the attribute — avoids relying on
* Vaadin's HTML5 empty-string boolean-attribute convention, which
* gridstack's own boolean parsing does not document matching. */
private void setBooleanAttribute(String name, boolean value) {
if (value) {
getElement().setAttribute(name, "true");
} else {
getElement().removeAttribute(name);
}
}
public GridStackItem setMinSize(int minWidth, int minHeight) {
getElement().setAttribute("gs-min-w", String.valueOf(minWidth));
getElement().setAttribute("gs-min-h", String.valueOf(minHeight));
return this;
}
public GridStackItem setPosition(int x, int y, int w, int h) {
getElement().setAttribute("gs-x", String.valueOf(x));
getElement().setAttribute("gs-y", String.valueOf(y));
getElement().setAttribute("gs-w", String.valueOf(w));
getElement().setAttribute("gs-h", String.valueOf(h));
return this;
}
/** Snapshot of a widget's grid placement, as reported by the client after a
* drag/resize (see {@link GridStackLayout#onLayoutChange(String)}). */
public record Position(String id, int x, int y, int w, int h) {
}
}