2019-06-04 13:17:19 +02:00
|
|
|
import {
|
|
|
|
Position,
|
|
|
|
Size,
|
|
|
|
AnyObject,
|
|
|
|
WithModuleProps,
|
|
|
|
ItemMeta
|
|
|
|
} from "./lib/types";
|
2019-02-11 13:43:04 +01:00
|
|
|
import {
|
|
|
|
sizePropsDecoder,
|
|
|
|
positionPropsDecoder,
|
|
|
|
parseIntOr,
|
2019-03-08 12:51:03 +01:00
|
|
|
parseBoolean,
|
2019-04-24 15:58:33 +02:00
|
|
|
notEmptyStringOr,
|
|
|
|
replaceMacros,
|
|
|
|
humanDate,
|
2019-06-11 15:27:06 +02:00
|
|
|
humanTime,
|
|
|
|
addMovementListener,
|
|
|
|
debounce
|
2019-02-11 13:43:04 +01:00
|
|
|
} from "./lib";
|
2019-06-04 13:17:19 +02:00
|
|
|
import TypedEvent, { Listener, Disposable } from "./lib/TypedEvent";
|
2019-02-11 13:43:04 +01:00
|
|
|
|
2019-02-18 17:23:57 +01:00
|
|
|
// Enum: https://www.typescriptlang.org/docs/handbook/enums.html.
|
2019-03-08 12:51:03 +01:00
|
|
|
export const enum ItemType {
|
2019-02-18 17:23:57 +01:00
|
|
|
STATIC_GRAPH = 0,
|
|
|
|
MODULE_GRAPH = 1,
|
|
|
|
SIMPLE_VALUE = 2,
|
|
|
|
PERCENTILE_BAR = 3,
|
|
|
|
LABEL = 4,
|
|
|
|
ICON = 5,
|
|
|
|
SIMPLE_VALUE_MAX = 6,
|
|
|
|
SIMPLE_VALUE_MIN = 7,
|
|
|
|
SIMPLE_VALUE_AVG = 8,
|
|
|
|
PERCENTILE_BUBBLE = 9,
|
|
|
|
SERVICE = 10,
|
|
|
|
GROUP_ITEM = 11,
|
|
|
|
BOX_ITEM = 12,
|
|
|
|
LINE_ITEM = 13,
|
|
|
|
AUTO_SLA_GRAPH = 14,
|
|
|
|
CIRCULAR_PROGRESS_BAR = 15,
|
|
|
|
CIRCULAR_INTERIOR_PROGRESS_BAR = 16,
|
|
|
|
DONUT_GRAPH = 17,
|
|
|
|
BARS_GRAPH = 18,
|
|
|
|
CLOCK = 19,
|
|
|
|
COLOR_CLOUD = 20
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:43:04 +01:00
|
|
|
// Base item properties. This interface should be extended by the item implementations.
|
2019-03-08 12:51:03 +01:00
|
|
|
export interface ItemProps extends Position, Size {
|
2019-02-11 13:43:04 +01:00
|
|
|
readonly id: number;
|
2019-03-08 12:51:03 +01:00
|
|
|
readonly type: ItemType;
|
2019-02-11 13:43:04 +01:00
|
|
|
label: string | null;
|
2019-02-18 17:23:57 +01:00
|
|
|
labelPosition: "up" | "right" | "down" | "left";
|
2019-02-11 13:43:04 +01:00
|
|
|
isLinkEnabled: boolean;
|
2019-04-09 16:07:40 +02:00
|
|
|
link: string | null;
|
2019-02-11 13:43:04 +01:00
|
|
|
isOnTop: boolean;
|
|
|
|
parentId: number | null;
|
|
|
|
aclGroupId: number | null;
|
|
|
|
}
|
|
|
|
|
2019-02-18 17:23:57 +01:00
|
|
|
// FIXME: Fix type compatibility.
|
2019-03-08 12:51:03 +01:00
|
|
|
export interface ItemClickEvent<Props extends ItemProps> {
|
|
|
|
// data: Props;
|
2019-06-04 13:17:19 +02:00
|
|
|
data: AnyObject;
|
2019-04-10 11:54:24 +02:00
|
|
|
nativeEvent: Event;
|
2019-02-26 17:05:30 +01:00
|
|
|
}
|
2019-02-18 17:23:57 +01:00
|
|
|
|
2019-04-08 16:36:58 +02:00
|
|
|
// FIXME: Fix type compatibility.
|
|
|
|
export interface ItemRemoveEvent<Props extends ItemProps> {
|
|
|
|
// data: Props;
|
2019-06-04 13:17:19 +02:00
|
|
|
data: AnyObject;
|
2019-04-08 16:36:58 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 17:27:22 +02:00
|
|
|
export interface ItemMovedEvent {
|
2019-06-11 16:44:19 +02:00
|
|
|
item: VisualConsoleItem<ItemProps>;
|
2019-06-11 17:27:22 +02:00
|
|
|
prevPosition: Position;
|
|
|
|
newPosition: Position;
|
2019-06-11 16:44:19 +02:00
|
|
|
}
|
|
|
|
|
2019-02-18 17:23:57 +01:00
|
|
|
/**
|
2019-03-07 18:41:33 +01:00
|
|
|
* Extract a valid enum value from a raw label positi9on value.
|
2019-02-18 17:23:57 +01:00
|
|
|
* @param labelPosition Raw value.
|
|
|
|
*/
|
2019-02-26 17:05:30 +01:00
|
|
|
const parseLabelPosition = (
|
2019-05-16 11:03:11 +02:00
|
|
|
labelPosition: unknown
|
2019-03-08 12:51:03 +01:00
|
|
|
): ItemProps["labelPosition"] => {
|
2019-02-18 17:23:57 +01:00
|
|
|
switch (labelPosition) {
|
|
|
|
case "up":
|
|
|
|
case "right":
|
|
|
|
case "down":
|
|
|
|
case "left":
|
|
|
|
return labelPosition;
|
|
|
|
default:
|
|
|
|
return "down";
|
|
|
|
}
|
2019-02-11 13:43:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a valid typed object from a raw object.
|
|
|
|
* This will allow us to ensure the type safety.
|
|
|
|
*
|
|
|
|
* @param data Raw object.
|
2019-02-11 17:35:16 +01:00
|
|
|
* @return An object representing the item props.
|
2019-02-11 13:43:04 +01:00
|
|
|
* @throws Will throw a TypeError if some property
|
|
|
|
* is missing from the raw object or have an invalid type.
|
|
|
|
*/
|
2019-06-04 13:17:19 +02:00
|
|
|
export function itemBasePropsDecoder(data: AnyObject): ItemProps | never {
|
2019-02-11 13:43:04 +01:00
|
|
|
if (data.id == null || isNaN(parseInt(data.id))) {
|
|
|
|
throw new TypeError("invalid id.");
|
|
|
|
}
|
|
|
|
if (data.type == null || isNaN(parseInt(data.type))) {
|
|
|
|
throw new TypeError("invalid type.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: parseInt(data.id),
|
|
|
|
type: parseInt(data.type),
|
2019-03-08 12:51:03 +01:00
|
|
|
label: notEmptyStringOr(data.label, null),
|
2019-02-18 17:23:57 +01:00
|
|
|
labelPosition: parseLabelPosition(data.labelPosition),
|
2019-02-11 13:43:04 +01:00
|
|
|
isLinkEnabled: parseBoolean(data.isLinkEnabled),
|
2019-04-09 16:07:40 +02:00
|
|
|
link: notEmptyStringOr(data.link, null),
|
2019-02-11 13:43:04 +01:00
|
|
|
isOnTop: parseBoolean(data.isOnTop),
|
|
|
|
parentId: parseIntOr(data.parentId, null),
|
|
|
|
aclGroupId: parseIntOr(data.aclGroupId, null),
|
|
|
|
...sizePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
|
|
|
|
...positionPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:59:35 +01:00
|
|
|
/**
|
|
|
|
* Base class of the visual console items. Should be extended to use its capabilities.
|
|
|
|
*/
|
2019-03-08 12:51:03 +01:00
|
|
|
abstract class VisualConsoleItem<Props extends ItemProps> {
|
2019-02-11 13:43:04 +01:00
|
|
|
// Properties of the item.
|
2019-03-08 12:51:03 +01:00
|
|
|
private itemProps: Props;
|
2019-06-04 13:17:19 +02:00
|
|
|
// Metadata of the item.
|
|
|
|
private _metadata: ItemMeta;
|
2019-02-18 17:23:57 +01:00
|
|
|
// Reference to the DOM element which will contain the item.
|
2019-04-09 16:07:40 +02:00
|
|
|
public elementRef: HTMLElement;
|
2019-04-16 09:41:17 +02:00
|
|
|
public readonly labelElementRef: HTMLElement;
|
2019-02-18 17:23:57 +01:00
|
|
|
// Reference to the DOM element which will contain the view of the item which extends this class.
|
|
|
|
protected readonly childElementRef: HTMLElement;
|
2019-02-11 13:43:04 +01:00
|
|
|
// Event manager for click events.
|
2019-03-08 12:51:03 +01:00
|
|
|
private readonly clickEventManager = new TypedEvent<ItemClickEvent<Props>>();
|
2019-06-11 16:44:19 +02:00
|
|
|
// Event manager for moved events.
|
|
|
|
private readonly movedEventManager = new TypedEvent<ItemMovedEvent>();
|
2019-04-09 18:21:49 +02:00
|
|
|
// Event manager for remove events.
|
2019-04-08 16:36:58 +02:00
|
|
|
private readonly removeEventManager = new TypedEvent<
|
|
|
|
ItemRemoveEvent<Props>
|
|
|
|
>();
|
2019-02-11 13:43:04 +01:00
|
|
|
// List of references to clean the event listeners.
|
|
|
|
private readonly disposables: Disposable[] = [];
|
|
|
|
|
2019-06-11 15:27:06 +02:00
|
|
|
// This function will only run the 2nd arg function after the time
|
|
|
|
// of the first arg have passed after its last execution.
|
|
|
|
private debouncedMovementSave = debounce(
|
|
|
|
500, // ms.
|
|
|
|
(x: Position["x"], y: Position["y"]) => {
|
2019-06-11 17:27:22 +02:00
|
|
|
const prevPosition = {
|
|
|
|
x: this.props.x,
|
|
|
|
y: this.props.y
|
|
|
|
};
|
|
|
|
const newPosition = {
|
|
|
|
x: x,
|
|
|
|
y: y
|
|
|
|
};
|
2019-06-11 15:27:06 +02:00
|
|
|
// Save the new position to the props.
|
|
|
|
this.move(x, y);
|
2019-06-11 16:44:19 +02:00
|
|
|
// Emit the movement event.
|
2019-06-11 17:27:22 +02:00
|
|
|
this.movedEventManager.emit({
|
|
|
|
item: this,
|
|
|
|
prevPosition: prevPosition,
|
|
|
|
newPosition: newPosition
|
|
|
|
});
|
2019-06-11 15:27:06 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
// This property will store the function
|
|
|
|
// to clean the movement listener.
|
|
|
|
private removeMovement: Function | null = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the movement funtionality.
|
|
|
|
* @param element Element to move inside its container.
|
|
|
|
*/
|
|
|
|
private initMovementListener(element: HTMLElement): void {
|
|
|
|
this.removeMovement = addMovementListener(
|
|
|
|
element,
|
|
|
|
(x: Position["x"], y: Position["y"]) => {
|
|
|
|
// Move the DOM element.
|
|
|
|
this.moveElement(x, y);
|
|
|
|
// Run the save function.
|
|
|
|
this.debouncedMovementSave(x, y);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Stop the movement fun
|
|
|
|
*/
|
|
|
|
private stopMovementListener(): void {
|
|
|
|
if (this.removeMovement) {
|
|
|
|
this.removeMovement();
|
|
|
|
this.removeMovement = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:43:04 +01:00
|
|
|
/**
|
|
|
|
* To create a new element which will be inside the item box.
|
|
|
|
* @return Item.
|
|
|
|
*/
|
2019-04-15 17:00:06 +02:00
|
|
|
protected abstract createDomElement(): HTMLElement;
|
2019-02-11 13:43:04 +01:00
|
|
|
|
2019-06-04 13:17:19 +02:00
|
|
|
public constructor(props: Props, metadata: ItemMeta) {
|
2019-01-30 12:06:35 +01:00
|
|
|
this.itemProps = props;
|
2019-06-04 13:17:19 +02:00
|
|
|
this._metadata = metadata;
|
2019-02-11 13:43:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a HTMLElement which represents the container box
|
|
|
|
* of the Visual Console item. This element will manage
|
|
|
|
* all the common things like click events, show a border
|
|
|
|
* when hovered, etc.
|
|
|
|
*/
|
2019-02-18 17:23:57 +01:00
|
|
|
this.elementRef = this.createContainerDomElement();
|
2019-04-04 18:47:02 +02:00
|
|
|
this.labelElementRef = this.createLabelDomElement();
|
2019-02-11 13:43:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a HTMLElement which represents the custom view
|
|
|
|
* of the Visual Console item. This element will be
|
|
|
|
* different depending on the item implementation.
|
|
|
|
*/
|
2019-02-18 17:23:57 +01:00
|
|
|
this.childElementRef = this.createDomElement();
|
2019-02-11 13:43:04 +01:00
|
|
|
|
2019-02-18 17:23:57 +01:00
|
|
|
// Insert the elements into the container.
|
2019-03-29 11:54:01 +01:00
|
|
|
this.elementRef.append(this.childElementRef, this.labelElementRef);
|
|
|
|
|
|
|
|
// Resize element.
|
|
|
|
this.resizeElement(props.width, props.height);
|
|
|
|
// Set label position.
|
|
|
|
this.changeLabelPosition(props.labelPosition);
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To create a new box for the visual console item.
|
|
|
|
* @return Item box.
|
|
|
|
*/
|
2019-02-18 17:23:57 +01:00
|
|
|
private createContainerDomElement(): HTMLElement {
|
2019-04-09 16:07:40 +02:00
|
|
|
let box;
|
|
|
|
if (this.props.isLinkEnabled) {
|
2019-06-11 15:27:06 +02:00
|
|
|
box = document.createElement("a") as HTMLAnchorElement;
|
2019-04-09 16:07:40 +02:00
|
|
|
if (this.props.link) box.href = this.props.link;
|
|
|
|
} else {
|
2019-06-11 15:27:06 +02:00
|
|
|
box = document.createElement("div") as HTMLDivElement;
|
2019-04-09 16:07:40 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 13:43:04 +01:00
|
|
|
box.className = "visual-console-item";
|
2019-04-08 16:36:58 +02:00
|
|
|
box.style.zIndex = this.props.isOnTop ? "2" : "1";
|
2019-02-18 17:23:57 +01:00
|
|
|
box.style.left = `${this.props.x}px`;
|
|
|
|
box.style.top = `${this.props.y}px`;
|
2019-06-11 15:27:06 +02:00
|
|
|
// Init the click listener.
|
2019-06-04 13:17:19 +02:00
|
|
|
box.addEventListener("click", e => {
|
2019-06-04 17:21:54 +02:00
|
|
|
if (this.meta.editMode) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
} else {
|
2019-06-04 13:17:19 +02:00
|
|
|
this.clickEventManager.emit({ data: this.props, nativeEvent: e });
|
2019-06-04 17:21:54 +02:00
|
|
|
}
|
2019-06-04 13:17:19 +02:00
|
|
|
});
|
2019-03-28 17:59:35 +01:00
|
|
|
|
2019-06-11 15:27:06 +02:00
|
|
|
// Metadata state.
|
|
|
|
if (this.meta.editMode) {
|
|
|
|
box.classList.add("is-editing");
|
|
|
|
// Init the movement listener.
|
|
|
|
this.initMovementListener(box);
|
|
|
|
}
|
|
|
|
if (this.meta.isFetching) {
|
|
|
|
box.classList.add("is-fetching");
|
|
|
|
}
|
|
|
|
if (this.meta.isUpdating) {
|
|
|
|
box.classList.add("is-updating");
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:43:04 +01:00
|
|
|
return box;
|
2019-01-30 12:06:35 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 18:47:02 +02:00
|
|
|
/**
|
|
|
|
* To create a new label for the visual console item.
|
|
|
|
* @return Item label.
|
|
|
|
*/
|
|
|
|
protected createLabelDomElement(): HTMLElement {
|
|
|
|
const element = document.createElement("div");
|
|
|
|
element.className = "visual-console-item-label";
|
|
|
|
// Add the label if it exists.
|
2019-04-24 15:58:33 +02:00
|
|
|
const label = this.getLabelWithMacrosReplaced();
|
|
|
|
if (label.length > 0) {
|
2019-04-24 12:43:37 +02:00
|
|
|
// Ugly table we need to use to replicate the legacy style.
|
|
|
|
const table = document.createElement("table");
|
|
|
|
const row = document.createElement("tr");
|
|
|
|
const emptyRow1 = document.createElement("tr");
|
|
|
|
const emptyRow2 = document.createElement("tr");
|
|
|
|
const cell = document.createElement("td");
|
|
|
|
|
2019-04-24 15:58:33 +02:00
|
|
|
cell.innerHTML = label;
|
2019-04-24 12:43:37 +02:00
|
|
|
row.append(cell);
|
|
|
|
table.append(emptyRow1, row, emptyRow2);
|
2019-04-24 16:57:20 +02:00
|
|
|
table.style.textAlign = "center";
|
2019-04-24 12:43:37 +02:00
|
|
|
|
2019-04-25 09:13:06 +02:00
|
|
|
// Change the table size depending on its position.
|
|
|
|
switch (this.props.labelPosition) {
|
|
|
|
case "up":
|
|
|
|
case "down":
|
|
|
|
if (this.props.width > 0) {
|
|
|
|
table.style.width = `${this.props.width}px`;
|
|
|
|
table.style.height = null;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "left":
|
|
|
|
case "right":
|
|
|
|
if (this.props.height > 0) {
|
|
|
|
table.style.width = null;
|
|
|
|
table.style.height = `${this.props.height}px`;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:43:37 +02:00
|
|
|
// element.innerHTML = this.props.label;
|
|
|
|
element.append(table);
|
2019-04-04 18:47:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:58:33 +02:00
|
|
|
/**
|
|
|
|
* Return the label stored into the props with some macros replaced.
|
|
|
|
*/
|
|
|
|
protected getLabelWithMacrosReplaced(): string {
|
|
|
|
// We assert that the props may have some needed properties.
|
|
|
|
const props = this.props as Partial<WithModuleProps>;
|
|
|
|
|
|
|
|
return replaceMacros(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
macro: "_date_",
|
|
|
|
value: humanDate(new Date())
|
|
|
|
},
|
|
|
|
{
|
|
|
|
macro: "_time_",
|
|
|
|
value: humanTime(new Date())
|
|
|
|
},
|
|
|
|
{
|
|
|
|
macro: "_agent_",
|
|
|
|
value: props.agentAlias != null ? props.agentAlias : ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
macro: "_agentdescription_",
|
|
|
|
value: props.agentDescription != null ? props.agentDescription : ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
macro: "_address_",
|
|
|
|
value: props.agentAddress != null ? props.agentAddress : ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
macro: "_module_",
|
|
|
|
value: props.moduleName != null ? props.moduleName : ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
macro: "_moduledescription_",
|
|
|
|
value: props.moduleDescription != null ? props.moduleDescription : ""
|
|
|
|
}
|
|
|
|
],
|
|
|
|
this.props.label || ""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:32:08 +02:00
|
|
|
/**
|
|
|
|
* To update the content element.
|
|
|
|
* @return Item.
|
|
|
|
*/
|
|
|
|
protected updateDomElement(element: HTMLElement): void {
|
|
|
|
element.innerHTML = this.createDomElement().innerHTML;
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:43:04 +01:00
|
|
|
/**
|
|
|
|
* Public accessor of the `props` property.
|
|
|
|
* @return Properties.
|
|
|
|
*/
|
2019-03-08 12:51:03 +01:00
|
|
|
public get props(): Props {
|
2019-04-11 10:04:35 +02:00
|
|
|
return { ...this.itemProps }; // Return a copy.
|
2019-01-30 12:06:35 +01:00
|
|
|
}
|
2019-02-11 13:43:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public setter of the `props` property.
|
|
|
|
* If the new props are different enough than the
|
|
|
|
* stored props, a render would be fired.
|
|
|
|
* @param newProps
|
|
|
|
*/
|
2019-03-08 12:51:03 +01:00
|
|
|
public set props(newProps: Props) {
|
2019-02-11 13:43:04 +01:00
|
|
|
const prevProps = this.props;
|
|
|
|
// Update the internal props.
|
|
|
|
this.itemProps = newProps;
|
|
|
|
|
|
|
|
// From this point, things which rely on this.props can access to the changes.
|
|
|
|
|
|
|
|
// Check if we should re-render.
|
2019-06-04 13:17:19 +02:00
|
|
|
if (this.shouldBeUpdated(prevProps, newProps))
|
|
|
|
this.render(prevProps, this._metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public accessor of the `meta` property.
|
|
|
|
* @return Properties.
|
|
|
|
*/
|
|
|
|
public get meta(): ItemMeta {
|
|
|
|
return { ...this._metadata }; // Return a copy.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public setter of the `meta` property.
|
|
|
|
* If the new meta are different enough than the
|
|
|
|
* stored meta, a render would be fired.
|
|
|
|
* @param newProps
|
|
|
|
*/
|
|
|
|
public set meta(newMetadata: ItemMeta) {
|
|
|
|
const prevMetadata = this._metadata;
|
|
|
|
// Update the internal meta.
|
|
|
|
this._metadata = newMetadata;
|
|
|
|
|
|
|
|
// From this point, things which rely on this.props can access to the changes.
|
|
|
|
|
|
|
|
// Check if we should re-render.
|
|
|
|
// if (this.shouldBeUpdated(prevMetadata, newMetadata))
|
|
|
|
this.render(this.itemProps, prevMetadata);
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To compare the previous and the new props and returns a boolean value
|
|
|
|
* in case the difference is meaningfull enough to perform DOM changes.
|
|
|
|
*
|
|
|
|
* Here, the only comparision is done by reference.
|
|
|
|
*
|
|
|
|
* Override this function to perform a different comparision depending on the item needs.
|
|
|
|
*
|
2019-03-19 10:09:18 +01:00
|
|
|
* @param prevProps
|
2019-02-11 13:43:04 +01:00
|
|
|
* @param newProps
|
|
|
|
* @return Whether the difference is meaningful enough to perform DOM changes or not.
|
|
|
|
*/
|
2019-03-19 10:09:18 +01:00
|
|
|
protected shouldBeUpdated(prevProps: Props, newProps: Props): boolean {
|
|
|
|
return prevProps !== newProps;
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To recreate or update the HTMLElement which represents the item into the DOM.
|
2019-02-18 17:23:57 +01:00
|
|
|
* @param prevProps If exists it will be used to only perform DOM updates instead of a full replace.
|
2019-02-11 13:43:04 +01:00
|
|
|
*/
|
2019-06-04 13:17:19 +02:00
|
|
|
public render(
|
|
|
|
prevProps: Props | null = null,
|
|
|
|
prevMeta: ItemMeta | null = null
|
|
|
|
): void {
|
2019-04-17 12:32:08 +02:00
|
|
|
this.updateDomElement(this.childElementRef);
|
2019-03-29 11:54:01 +01:00
|
|
|
|
2019-02-11 13:43:04 +01:00
|
|
|
// Move box.
|
2019-03-19 10:09:18 +01:00
|
|
|
if (!prevProps || this.positionChanged(prevProps, this.props)) {
|
|
|
|
this.moveElement(this.props.x, this.props.y);
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
|
|
|
// Resize box.
|
2019-03-19 10:09:18 +01:00
|
|
|
if (!prevProps || this.sizeChanged(prevProps, this.props)) {
|
|
|
|
this.resizeElement(this.props.width, this.props.height);
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
2019-03-28 17:59:35 +01:00
|
|
|
// Change label.
|
2019-04-24 15:58:33 +02:00
|
|
|
const oldLabelHtml = this.labelElementRef.innerHTML;
|
|
|
|
const newLabelHtml = this.createLabelDomElement().innerHTML;
|
|
|
|
if (oldLabelHtml !== newLabelHtml) {
|
|
|
|
this.labelElementRef.innerHTML = newLabelHtml;
|
|
|
|
}
|
|
|
|
// Change label position.
|
|
|
|
if (!prevProps || prevProps.labelPosition !== this.props.labelPosition) {
|
|
|
|
this.changeLabelPosition(this.props.labelPosition);
|
2019-03-29 11:54:01 +01:00
|
|
|
}
|
2019-04-09 16:07:40 +02:00
|
|
|
// Change link.
|
|
|
|
if (
|
|
|
|
prevProps &&
|
|
|
|
(prevProps.isLinkEnabled !== this.props.isLinkEnabled ||
|
|
|
|
(this.props.isLinkEnabled && prevProps.link !== this.props.link))
|
|
|
|
) {
|
|
|
|
const container = this.createContainerDomElement();
|
2019-04-29 10:56:06 +02:00
|
|
|
// Add the children of the old element.
|
2019-04-09 16:07:40 +02:00
|
|
|
container.innerHTML = this.elementRef.innerHTML;
|
2019-04-29 10:56:06 +02:00
|
|
|
// Copy the attributes.
|
|
|
|
const attrs = this.elementRef.attributes;
|
|
|
|
for (let i = 0; i < attrs.length; i++) {
|
|
|
|
if (attrs[i].nodeName !== "id") {
|
|
|
|
container.setAttributeNode(attrs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Replace the reference.
|
2019-04-09 16:07:40 +02:00
|
|
|
if (this.elementRef.parentNode !== null) {
|
|
|
|
this.elementRef.parentNode.replaceChild(container, this.elementRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changed the reference to the main element. It's ugly, but needed.
|
|
|
|
this.elementRef = container;
|
|
|
|
}
|
2019-06-04 13:17:19 +02:00
|
|
|
|
|
|
|
// Change metadata related things.
|
|
|
|
if (!prevMeta || prevMeta.editMode !== this.meta.editMode) {
|
|
|
|
if (this.meta.editMode) {
|
|
|
|
this.elementRef.classList.add("is-editing");
|
2019-06-11 15:27:06 +02:00
|
|
|
this.initMovementListener(this.elementRef);
|
2019-06-04 13:17:19 +02:00
|
|
|
} else {
|
|
|
|
this.elementRef.classList.remove("is-editing");
|
2019-06-11 15:27:06 +02:00
|
|
|
this.stopMovementListener();
|
2019-06-04 13:17:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!prevMeta || prevMeta.isFetching !== this.meta.isFetching) {
|
|
|
|
if (this.meta.isFetching) {
|
|
|
|
this.elementRef.classList.add("is-fetching");
|
|
|
|
} else {
|
|
|
|
this.elementRef.classList.remove("is-fetching");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!prevMeta || prevMeta.isUpdating !== this.meta.isUpdating) {
|
|
|
|
if (this.meta.isUpdating) {
|
|
|
|
this.elementRef.classList.add("is-updating");
|
|
|
|
} else {
|
|
|
|
this.elementRef.classList.remove("is-updating");
|
|
|
|
}
|
|
|
|
}
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To remove the event listeners and the elements from the DOM.
|
|
|
|
*/
|
2019-02-26 17:05:30 +01:00
|
|
|
public remove(): void {
|
2019-04-08 16:36:58 +02:00
|
|
|
// Call the remove event.
|
|
|
|
this.removeEventManager.emit({ data: this.props });
|
2019-02-11 13:43:04 +01:00
|
|
|
// Event listeners.
|
2019-04-08 16:36:58 +02:00
|
|
|
this.disposables.forEach(disposable => {
|
|
|
|
try {
|
|
|
|
disposable.dispose();
|
|
|
|
} catch (ignored) {} // eslint-disable-line no-empty
|
|
|
|
});
|
2019-02-11 13:43:04 +01:00
|
|
|
// VisualConsoleItem DOM element.
|
2019-02-18 17:23:57 +01:00
|
|
|
this.elementRef.remove();
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-19 10:09:18 +01:00
|
|
|
* Compare the previous and the new position and return
|
|
|
|
* a boolean value in case the position changed.
|
|
|
|
* @param prevPosition
|
|
|
|
* @param newPosition
|
|
|
|
* @return Whether the position changed or not.
|
|
|
|
*/
|
|
|
|
protected positionChanged(
|
|
|
|
prevPosition: Position,
|
|
|
|
newPosition: Position
|
|
|
|
): boolean {
|
|
|
|
return prevPosition.x !== newPosition.x || prevPosition.y !== newPosition.y;
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:54:01 +01:00
|
|
|
/**
|
|
|
|
* Move the label around the item content.
|
|
|
|
* @param position Label position.
|
|
|
|
*/
|
|
|
|
protected changeLabelPosition(position: Props["labelPosition"]): void {
|
|
|
|
switch (position) {
|
|
|
|
case "up":
|
|
|
|
this.elementRef.style.flexDirection = "column-reverse";
|
|
|
|
break;
|
|
|
|
case "left":
|
|
|
|
this.elementRef.style.flexDirection = "row-reverse";
|
|
|
|
break;
|
|
|
|
case "right":
|
|
|
|
this.elementRef.style.flexDirection = "row";
|
|
|
|
break;
|
|
|
|
case "down":
|
|
|
|
default:
|
|
|
|
this.elementRef.style.flexDirection = "column";
|
|
|
|
break;
|
|
|
|
}
|
2019-04-24 14:36:59 +02:00
|
|
|
|
|
|
|
// Ugly table to show the label as its legacy counterpart.
|
|
|
|
const tables = this.labelElementRef.getElementsByTagName("table");
|
|
|
|
const table = tables.length > 0 ? tables.item(0) : null;
|
2019-04-25 09:13:06 +02:00
|
|
|
// Change the table size depending on its position.
|
2019-04-24 14:36:59 +02:00
|
|
|
if (table) {
|
|
|
|
switch (this.props.labelPosition) {
|
|
|
|
case "up":
|
|
|
|
case "down":
|
|
|
|
if (this.props.width > 0) {
|
|
|
|
table.style.width = `${this.props.width}px`;
|
|
|
|
table.style.height = null;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "left":
|
|
|
|
case "right":
|
|
|
|
if (this.props.height > 0) {
|
|
|
|
table.style.width = null;
|
|
|
|
table.style.height = `${this.props.height}px`;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-29 11:54:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-19 10:09:18 +01:00
|
|
|
/**
|
|
|
|
* Move the DOM container.
|
2019-02-11 13:43:04 +01:00
|
|
|
* @param x Horizontal axis position.
|
|
|
|
* @param y Vertical axis position.
|
|
|
|
*/
|
2019-03-19 10:09:18 +01:00
|
|
|
protected moveElement(x: number, y: number): void {
|
2019-02-18 17:23:57 +01:00
|
|
|
this.elementRef.style.left = `${x}px`;
|
|
|
|
this.elementRef.style.top = `${y}px`;
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-19 10:09:18 +01:00
|
|
|
* Update the position into the properties and move the DOM container.
|
|
|
|
* @param x Horizontal axis position.
|
|
|
|
* @param y Vertical axis position.
|
2019-02-11 13:43:04 +01:00
|
|
|
*/
|
2019-03-19 10:09:18 +01:00
|
|
|
public move(x: number, y: number): void {
|
|
|
|
this.moveElement(x, y);
|
|
|
|
this.itemProps = {
|
|
|
|
...this.props, // Object spread: http://es6-features.org/#SpreadOperator
|
|
|
|
x,
|
|
|
|
y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare the previous and the new size and return
|
|
|
|
* a boolean value in case the size changed.
|
|
|
|
* @param prevSize
|
|
|
|
* @param newSize
|
|
|
|
* @return Whether the size changed or not.
|
|
|
|
*/
|
|
|
|
protected sizeChanged(prevSize: Size, newSize: Size): boolean {
|
|
|
|
return (
|
|
|
|
prevSize.width !== newSize.width || prevSize.height !== newSize.height
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-28 17:59:35 +01:00
|
|
|
* Resize the DOM content container.
|
2019-03-19 10:09:18 +01:00
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
*/
|
|
|
|
protected resizeElement(width: number, height: number): void {
|
2019-03-29 11:54:01 +01:00
|
|
|
// The most valuable size is the content size.
|
2019-04-08 12:54:29 +02:00
|
|
|
this.childElementRef.style.width = width > 0 ? `${width}px` : null;
|
|
|
|
this.childElementRef.style.height = height > 0 ? `${height}px` : null;
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-19 10:09:18 +01:00
|
|
|
/**
|
|
|
|
* Update the size into the properties and resize the DOM container.
|
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
*/
|
|
|
|
public resize(width: number, height: number): void {
|
|
|
|
this.resizeElement(width, height);
|
|
|
|
this.itemProps = {
|
|
|
|
...this.props, // Object spread: http://es6-features.org/#SpreadOperator
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:43:04 +01:00
|
|
|
/**
|
|
|
|
* To 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.
|
|
|
|
*/
|
2019-04-08 16:36:58 +02:00
|
|
|
public onClick(listener: Listener<ItemClickEvent<Props>>): Disposable {
|
2019-02-11 13:43:04 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-04-08 16:36:58 +02:00
|
|
|
const disposable = this.clickEventManager.on(listener);
|
|
|
|
this.disposables.push(disposable);
|
|
|
|
|
|
|
|
return disposable;
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:19 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2019-04-08 16:36:58 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public onRemove(listener: Listener<ItemRemoveEvent<Props>>): 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.removeEventManager.on(listener);
|
|
|
|
this.disposables.push(disposable);
|
|
|
|
|
|
|
|
return disposable;
|
2019-02-11 13:43:04 +01:00
|
|
|
}
|
2019-01-30 12:06:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default VisualConsoleItem;
|