Visual Console Refactor: added a click event handler for elements to the vc container

Former-commit-id: f90902eabeecda6d8dda05603f1e143935c45cf0
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-09 18:21:49 +02:00
parent aa01789b19
commit 3ced9a658a
3 changed files with 34 additions and 5 deletions

View File

@ -299,6 +299,10 @@
try { try {
var visualConsole = new VisualConsole(container, props, items); var visualConsole = new VisualConsole(container, props, items);
console.log(visualConsole); console.log(visualConsole);
visualConsole.onClick(function(e) {
console.log("[CLICK]", e);
});
} catch (error) { } catch (error) {
console.log("ERROR", error.message); console.log("ERROR", error.message);
} }

View File

@ -121,7 +121,7 @@ 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 click events. // Event manager for remove events.
private readonly removeEventManager = new TypedEvent< private readonly removeEventManager = new TypedEvent<
ItemRemoveEvent<Props> ItemRemoveEvent<Props>
>(); >();

View File

@ -5,7 +5,7 @@ import {
parseIntOr, parseIntOr,
notEmptyStringOr notEmptyStringOr
} from "./lib"; } from "./lib";
import Item, { ItemType, ItemProps } from "./Item"; import Item, { ItemType, ItemProps, ItemClickEvent } 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";
import ColorCloud, { colorCloudPropsDecoder } from "./items/ColorCloud"; import ColorCloud, { colorCloudPropsDecoder } from "./items/ColorCloud";
@ -19,6 +19,7 @@ import EventsHistory, {
eventsHistoryPropsDecoder eventsHistoryPropsDecoder
} from "./items/EventsHistory"; } from "./items/EventsHistory";
import Percentile, { percentilePropsDecoder } from "./items/Percentile"; import Percentile, { percentilePropsDecoder } from "./items/Percentile";
import TypedEvent, { Disposable, Listener } from "./TypedEvent";
// Base properties. // Base properties.
export interface VisualConsoleProps extends Size { export interface VisualConsoleProps extends Size {
@ -135,6 +136,12 @@ export default class VisualConsole {
private relations: { private relations: {
[key: string]: Line | null; [key: string]: Line | null;
} = {}; } = {};
// Event manager for click events.
private readonly clickEventManager = new TypedEvent<
ItemClickEvent<ItemProps>
>();
// List of references to clean the event listeners.
private readonly disposables: Disposable[] = [];
public constructor( public constructor(
container: HTMLElement, container: HTMLElement,
@ -172,9 +179,10 @@ export default class VisualConsole {
this.elementsById[itemInstance.props.id] = itemInstance; this.elementsById[itemInstance.props.id] = itemInstance;
this.elementIds.push(itemInstance.props.id); this.elementIds.push(itemInstance.props.id);
// Item event handlers. // Item event handlers.
itemInstance.onClick(e => itemInstance.onClick(e => {
console.log(`Clicked element #${e.data.id}`, e) this.clickEventManager.emit(e);
); // console.log(`Clicked element #${e.data.id}`, e);
});
itemInstance.onRemove(e => { itemInstance.onRemove(e => {
// TODO: Remove the element from the list and its relations. // TODO: Remove the element from the list and its relations.
}); });
@ -287,6 +295,7 @@ export default class VisualConsole {
* To remove the event listeners and the elements from the DOM. * To remove the event listeners and the elements from the DOM.
*/ */
public remove(): void { public remove(): void {
this.disposables.forEach(d => d.dispose()); // Arrow function.
this.elements.forEach(e => e.remove()); // Arrow function. this.elements.forEach(e => e.remove()); // Arrow function.
this.elementsById = {}; this.elementsById = {};
this.elementIds = []; this.elementIds = [];
@ -360,4 +369,20 @@ export default class VisualConsole {
return line; return line;
} }
/**
* 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.
*/
public onClick(listener: Listener<ItemClickEvent<ItemProps>>): 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.clickEventManager.on(listener);
this.disposables.push(disposable);
return disposable;
}
} }