Skip to content

Commit

Permalink
feat: implement item mode changed events (#6672)
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-vaadin committed Oct 10, 2024
1 parent 789015c commit 1b12dbc
Show file tree
Hide file tree
Showing 8 changed files with 539 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,42 @@ public Registration addItemRemovedListener(
return addListener(DashboardItemRemovedEvent.class, listener);
}

/**
* Adds an item selected change listener to this dashboard.
*
* @param listener
* the listener to add, not <code>null</code>
* @return a handle that can be used for removing the listener
*/
public Registration addItemSelectedChangedListener(
ComponentEventListener<DashboardItemSelectedChangedEvent> listener) {
return addListener(DashboardItemSelectedChangedEvent.class, listener);
}

/**
* Adds an item move mode change listener to this dashboard.
*
* @param listener
* the listener to add, not <code>null</code>
* @return a handle that can be used for removing the listener
*/
public Registration addItemMoveModeChangedListener(
ComponentEventListener<DashboardItemMoveModeChangedEvent> listener) {
return addListener(DashboardItemMoveModeChangedEvent.class, listener);
}

/**
* Adds an item resize mode change listener to this dashboard.
*
* @param listener
* the listener to add, not <code>null</code>
* @return a handle that can be used for removing the listener
*/
public Registration addItemResizeModeChangedListener(
ComponentEventListener<DashboardItemResizeModeChangedEvent> listener) {
return addListener(DashboardItemResizeModeChangedEvent.class, listener);
}

/**
* Gets the internationalization object previously set for this component.
* <p>
Expand Down Expand Up @@ -406,6 +442,21 @@ protected void onAttach(AttachEvent attachEvent) {
doUpdateClient();
}

Component getItem(int nodeId) {
return getChildren().map(item -> {
if (nodeId == item.getElement().getNode().getId()) {
return item;
}
if (item instanceof DashboardSection section) {
return section.getWidgets().stream()
.filter(sectionItem -> nodeId == sectionItem
.getElement().getNode().getId())
.findAny().orElse(null);
}
return null;
}).filter(Objects::nonNull).findAny().orElseThrow();
}

void updateClient() {
if (pendingUpdate) {
return;
Expand Down Expand Up @@ -616,27 +667,12 @@ private void initItemRemovedClientEventListener() {

private void handleItemRemovedClientEvent(DomEvent e, String nodeIdKey) {
int nodeId = (int) e.getEventData().getNumber(nodeIdKey);
Component removedItem = getRemovedItem(nodeId);
Component removedItem = getItem(nodeId);
removedItem.removeFromParent();
fireEvent(new DashboardItemRemovedEvent(this, true, removedItem,
getChildren().toList()));
}

private Component getRemovedItem(int nodeId) {
return getChildren().map(item -> {
if (nodeId == item.getElement().getNode().getId()) {
return item;
}
if (item instanceof DashboardSection section) {
return section.getWidgets().stream()
.filter(sectionItem -> nodeId == sectionItem
.getElement().getNode().getId())
.findAny().orElse(null);
}
return null;
}).filter(Objects::nonNull).findAny().orElseThrow();
}

private void customizeItemMovedEvent() {
getElement().executeJs(
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2000-2024 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full
* license.
*/
package com.vaadin.flow.component.dashboard;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.DomEvent;
import com.vaadin.flow.component.EventData;

/**
* Widget or section move mode state changed event of {@link Dashboard}.
*
* @author Vaadin Ltd.
* @see Dashboard#addItemMoveModeChangedListener(ComponentEventListener)
*/
@DomEvent("dashboard-item-move-mode-changed")
public class DashboardItemMoveModeChangedEvent
extends ComponentEvent<Dashboard> {

private final Component item;

private final boolean moveMode;

/**
* Creates a dashboard item move mode changed event.
*
* @param source
* Dashboard that contains the item of which the move mode state
* has changed
* @param fromClient
* {@code true} if the event originated from the client side,
* {@code false} otherwise
* @param itemNodeId
* The node ID of the item of which the move mode state has
* changed
* @param moveMode
* Whether the item is in move mode
*/
public DashboardItemMoveModeChangedEvent(Dashboard source,
boolean fromClient,
@EventData("event.detail.item.nodeid") int itemNodeId,
@EventData("event.detail.value") boolean moveMode) {
super(source, fromClient);
this.item = source.getItem(itemNodeId);
this.moveMode = moveMode;
}

/**
* Returns the item of which the move mode state has changed
*
* @return the item of which the move mode state has changed
*/
public Component getItem() {
return item;
}

/**
* Returns whether the item is in move mode
*
* @return whether the item is in move mode
*/
public boolean isMoveMode() {
return moveMode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright 2000-2024 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full
* license.
*/
package com.vaadin.flow.component.dashboard;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.DomEvent;
import com.vaadin.flow.component.EventData;

/**
* Widget resize mode state changed event of {@link Dashboard}.
*
* @author Vaadin Ltd.
* @see Dashboard#addItemResizeModeChangedListener(ComponentEventListener)
*/
@DomEvent("dashboard-item-resize-mode-changed")
public class DashboardItemResizeModeChangedEvent
extends ComponentEvent<Dashboard> {

private final Component item;

private final boolean resizeMode;

/**
* Creates a dashboard item resize mode changed event.
*
* @param source
* Dashboard that contains the item of which the resize mode
* state has changed
* @param fromClient
* {@code true} if the event originated from the client side,
* {@code false} otherwise
* @param itemNodeId
* The node ID of the item of which the resize mode state has
* changed
* @param resizeMode
* Whether the item is in resize mode
*/
public DashboardItemResizeModeChangedEvent(Dashboard source,
boolean fromClient,
@EventData("event.detail.item.nodeid") int itemNodeId,
@EventData("event.detail.value") boolean resizeMode) {
super(source, fromClient);
this.item = source.getWidgets().stream().filter(
widget -> itemNodeId == widget.getElement().getNode().getId())
.findAny().orElseThrow();
this.resizeMode = resizeMode;
}

/**
* Returns the item of which the resize mode state has changed;
*
* @return the item of which the resize mode state has changed
*/
public Component getItem() {
return item;
}

/**
* Returns whether the item is in resize mode
*
* @return whether the item is in resize mode
*/
public boolean isResizeMode() {
return resizeMode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2000-2024 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full
* license.
*/
package com.vaadin.flow.component.dashboard;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.DomEvent;
import com.vaadin.flow.component.EventData;

/**
* Widget or section selected state changed event of {@link Dashboard}.
*
* @author Vaadin Ltd.
* @see Dashboard#addItemSelectedChangedListener(ComponentEventListener)
*/
@DomEvent("dashboard-item-selected-changed")
public class DashboardItemSelectedChangedEvent
extends ComponentEvent<Dashboard> {

private final Component item;

private final boolean selected;

/**
* Creates a dashboard item selected changed event.
*
* @param source
* Dashboard that contains the item of which the selected state
* has changed
* @param fromClient
* {@code true} if the event originated from the client side,
* {@code false} otherwise
* @param itemNodeId
* The node ID of the item of which the selected state has
* changed
* @param selected
* Whether the item is selected
*/
public DashboardItemSelectedChangedEvent(Dashboard source,
boolean fromClient,
@EventData("event.detail.item.nodeid") int itemNodeId,
@EventData("event.detail.value") boolean selected) {
super(source, fromClient);
this.item = source.getItem(itemNodeId);
this.selected = selected;
}

/**
* Returns the item of which the selected state has changed
*
* @return the item of which the selected state has changed
*/
public Component getItem() {
return item;
}

/**
* Returns whether the item is selected
*
* @return whether the item is selected
*/
public boolean isSelected() {
return selected;
}
}
Loading

0 comments on commit 1b12dbc

Please sign in to comment.