Added an event handler for move events of the visual console items

This commit is contained in:
Alejandro Gallardo Escobar 2019-06-11 16:44:19 +02:00
parent aaa1917fce
commit 9d15ece41a
3 changed files with 66 additions and 5 deletions

View File

@ -123,7 +123,7 @@ function createVisualConsole(
try { try {
visualConsole = new VisualConsole(container, props, items); visualConsole = new VisualConsole(container, props, items);
// VC Item clicked. // VC Item clicked.
visualConsole.onClick(function(e) { visualConsole.onItemClick(function(e) {
// Override the link to another VC if it isn't on remote console. // Override the link to another VC if it isn't on remote console.
if ( if (
e.data && e.data &&
@ -139,6 +139,14 @@ function createVisualConsole(
updateVisualConsole(e.data.linkedLayoutId, updateInterval); updateVisualConsole(e.data.linkedLayoutId, updateInterval);
} }
}); });
// VC Item moved.
visualConsole.onItemMoved(function(e) {
// TODO: Run an async task to update the position here.
// TODO: Recover the previous position if the update task fails.
console.log(
"Moved item #" + e.item.props.id + " (x: " + e.x + ", y: " + e.y + ")"
);
});
if (updateInterval != null && updateInterval > 0) { if (updateInterval != null && updateInterval > 0) {
// Start an interval to update the Visual Console. // Start an interval to update the Visual Console.

View File

@ -70,6 +70,10 @@ export interface ItemRemoveEvent<Props extends ItemProps> {
data: AnyObject; data: AnyObject;
} }
export interface ItemMovedEvent extends Position {
item: VisualConsoleItem<ItemProps>;
}
/** /**
* Extract a valid enum value from a raw label positi9on value. * Extract a valid enum value from a raw label positi9on value.
* @param labelPosition Raw value. * @param labelPosition Raw value.
@ -135,6 +139,8 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
protected readonly childElementRef: HTMLElement; protected readonly childElementRef: HTMLElement;
// Event manager for click events. // Event manager for click events.
private readonly clickEventManager = new TypedEvent<ItemClickEvent<Props>>(); private readonly clickEventManager = new TypedEvent<ItemClickEvent<Props>>();
// Event manager for moved events.
private readonly movedEventManager = new TypedEvent<ItemMovedEvent>();
// Event manager for remove events. // Event manager for remove events.
private readonly removeEventManager = new TypedEvent< private readonly removeEventManager = new TypedEvent<
ItemRemoveEvent<Props> ItemRemoveEvent<Props>
@ -149,8 +155,8 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
(x: Position["x"], y: Position["y"]) => { (x: Position["x"], y: Position["y"]) => {
// Save the new position to the props. // Save the new position to the props.
this.move(x, y); this.move(x, y);
// TODO: start an async task to persist the change. // Emit the movement event.
console.log("SAVED", x, y); this.movedEventManager.emit({ item: this, x, y });
} }
); );
// This property will store the function // This property will store the function
@ -657,6 +663,22 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
return disposable; return disposable;
} }
/**
* To add an event handler to the movement of visual console elements.
* @param listener Function which is going to be executed when a linked console is moved.
*/
public onMoved(listener: Listener<ItemMovedEvent>): Disposable {
/*
* The '.on' function returns a function which will clean the event
* listener when executed. We store all the 'dispose' functions to
* call them when the item should be cleared.
*/
const disposable = this.movedEventManager.on(listener);
this.disposables.push(disposable);
return disposable;
}
/** /**
* To add an event handler to the removal of the item. * To add an event handler to the removal of the item.
* @param listener Function which is going to be executed when a item is removed. * @param listener Function which is going to be executed when a item is removed.

View File

@ -10,7 +10,8 @@ import Item, {
ItemType, ItemType,
ItemProps, ItemProps,
ItemClickEvent, ItemClickEvent,
ItemRemoveEvent ItemRemoveEvent,
ItemMovedEvent
} from "./Item"; } from "./Item";
import StaticGraph, { staticGraphPropsDecoder } from "./items/StaticGraph"; import StaticGraph, { staticGraphPropsDecoder } from "./items/StaticGraph";
import Icon, { iconPropsDecoder } from "./items/Icon"; import Icon, { iconPropsDecoder } from "./items/Icon";
@ -204,6 +205,8 @@ export default class VisualConsole {
private readonly clickEventManager = new TypedEvent< private readonly clickEventManager = new TypedEvent<
ItemClickEvent<ItemProps> ItemClickEvent<ItemProps>
>(); >();
// Event manager for move events.
private readonly movedEventManager = new TypedEvent<ItemMovedEvent>();
// List of references to clean the event listeners. // List of references to clean the event listeners.
private readonly disposables: Disposable[] = []; private readonly disposables: Disposable[] = [];
@ -216,6 +219,15 @@ export default class VisualConsole {
// console.log(`Clicked element #${e.data.id}`, e); // console.log(`Clicked element #${e.data.id}`, e);
}; };
/**
* React to a movement on an element.
* @param e Event object.
*/
private handleElementMovement: (e: ItemMovedEvent) => void = e => {
this.movedEventManager.emit(e);
// console.log(`Moved element #${e.item.props.id}`, e);
};
/** /**
* Clear some element references. * Clear some element references.
* @param e Event object. * @param e Event object.
@ -264,6 +276,7 @@ export default class VisualConsole {
this.elementIds.push(itemInstance.props.id); this.elementIds.push(itemInstance.props.id);
// Item event handlers. // Item event handlers.
itemInstance.onClick(this.handleElementClick); itemInstance.onClick(this.handleElementClick);
itemInstance.onMoved(this.handleElementMovement);
itemInstance.onRemove(this.handleElementRemove); itemInstance.onRemove(this.handleElementRemove);
// Add the item to the DOM. // Add the item to the DOM.
this.containerRef.append(itemInstance.elementRef); this.containerRef.append(itemInstance.elementRef);
@ -552,7 +565,9 @@ export default class VisualConsole {
* Add an event handler to the click of the linked visual console elements. * Add an event handler to the click of the linked visual console elements.
* @param listener Function which is going to be executed when a linked console is clicked. * @param listener Function which is going to be executed when a linked console is clicked.
*/ */
public onClick(listener: Listener<ItemClickEvent<ItemProps>>): Disposable { public onItemClick(
listener: Listener<ItemClickEvent<ItemProps>>
): Disposable {
/* /*
* The '.on' function returns a function which will clean the event * The '.on' function returns a function which will clean the event
* listener when executed. We store all the 'dispose' functions to * listener when executed. We store all the 'dispose' functions to
@ -564,6 +579,22 @@ export default class VisualConsole {
return disposable; return disposable;
} }
/**
* Add an event handler to the movement of the visual console elements.
* @param listener Function which is going to be executed when a linked console is moved.
*/
public onItemMoved(listener: Listener<ItemMovedEvent>): Disposable {
/*
* The '.on' function returns a function which will clean the event
* listener when executed. We store all the 'dispose' functions to
* call them when the item should be cleared.
*/
const disposable = this.movedEventManager.on(listener);
this.disposables.push(disposable);
return disposable;
}
/** /**
* Enable the edition mode. * Enable the edition mode.
*/ */