Added Donutgraph.ts, BarsGraph.ts and ModuleGraph.ts

Former-commit-id: b7a224710e18a743a590086a21e22b5ecfe162ca
This commit is contained in:
Daniel Maya 2019-04-10 18:33:05 +02:00
parent 52007832d8
commit 8fe47a8914
5 changed files with 163 additions and 12 deletions

View File

@ -20,6 +20,9 @@ import EventsHistory, {
} from "./items/EventsHistory";
import Percentile, { percentilePropsDecoder } from "./items/Percentile";
import TypedEvent, { Disposable, Listener } from "./TypedEvent";
import DonutGraph, { donutGraphPropsDecoder } from "./items/DonutGraph";
import BarsGraph, { barsGraphPropsDecoder } from "./items/BarsGraph";
import ModuleGraph, { moduleGraphPropsDecoder } from "./items/ModuleGraph";
// Base properties.
export interface VisualConsoleProps extends Size {
@ -84,7 +87,7 @@ function itemInstanceFrom(data: UnknownObject) {
case ItemType.STATIC_GRAPH:
return new StaticGraph(staticGraphPropsDecoder(data));
case ItemType.MODULE_GRAPH:
throw new TypeError("item not found");
return new ModuleGraph(moduleGraphPropsDecoder(data));
case ItemType.SIMPLE_VALUE:
case ItemType.SIMPLE_VALUE_MAX:
case ItemType.SIMPLE_VALUE_MIN:
@ -110,8 +113,9 @@ function itemInstanceFrom(data: UnknownObject) {
case ItemType.AUTO_SLA_GRAPH:
return new EventsHistory(eventsHistoryPropsDecoder(data));
case ItemType.DONUT_GRAPH:
return new DonutGraph(donutGraphPropsDecoder(data));
case ItemType.BARS_GRAPH:
throw new TypeError("item not found");
return new BarsGraph(barsGraphPropsDecoder(data));
case ItemType.CLOCK:
return new Clock(clockPropsDecoder(data));
case ItemType.COLOR_CLOUD:

View File

@ -0,0 +1,44 @@
import { UnknownObject, WithModuleProps } from "../types";
import { modulePropsDecoder, decodeBase64, stringIsEmpty } from "../lib";
import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item";
export type BarsGraphProps = {
type: ItemType.BARS_GRAPH;
html: string;
} & ItemProps &
WithModuleProps;
/**
* Build a valid typed object from a raw object.
* This will allow us to ensure the type safety.
*
* @param data Raw object.
* @return An object representing the bars graph props.
* @throws Will throw a TypeError if some property
* is missing from the raw object or have an invalid type.
*/
export function barsGraphPropsDecoder(
data: UnknownObject
): BarsGraphProps | never {
if (stringIsEmpty(data.html) && stringIsEmpty(data.encodedHtml)) {
throw new TypeError("missing html content.");
}
return {
...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
type: ItemType.BARS_GRAPH,
html: !stringIsEmpty(data.html)
? data.html
: decodeBase64(data.encodedHtml),
...modulePropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
}
export default class BarsGraph extends Item<BarsGraphProps> {
public createDomElement(): HTMLElement {
const element = document.createElement("div");
element.innerHTML = this.props.html;
return element;
}
}

View File

@ -0,0 +1,55 @@
import {
LinkedVisualConsoleProps,
UnknownObject,
WithModuleProps
} from "../types";
import {
linkedVCPropsDecoder,
modulePropsDecoder,
decodeBase64,
stringIsEmpty
} from "../lib";
import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item";
export type DonutGraphProps = {
type: ItemType.DONUT_GRAPH;
html: string;
} & ItemProps &
WithModuleProps &
LinkedVisualConsoleProps;
/**
* Build a valid typed object from a raw object.
* This will allow us to ensure the type safety.
*
* @param data Raw object.
* @return An object representing the donut graph props.
* @throws Will throw a TypeError if some property
* is missing from the raw object or have an invalid type.
*/
export function donutGraphPropsDecoder(
data: UnknownObject
): DonutGraphProps | never {
if (stringIsEmpty(data.html) && stringIsEmpty(data.encodedHtml)) {
throw new TypeError("missing html content.");
}
return {
...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
type: ItemType.DONUT_GRAPH,
html: !stringIsEmpty(data.html)
? data.html
: decodeBase64(data.encodedHtml),
...modulePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
...linkedVCPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
}
export default class DonutGraph extends Item<DonutGraphProps> {
public createDomElement(): HTMLElement {
const element = document.createElement("div");
element.innerHTML = this.props.html;
return element;
}
}

View File

@ -1,10 +1,5 @@
import { UnknownObject, WithModuleProps } from "../types";
import {
LinkedVisualConsoleProps,
UnknownObject,
WithModuleProps
} from "../types";
import {
linkedVCPropsDecoder,
modulePropsDecoder,
parseIntOr,
decodeBase64,
@ -17,8 +12,7 @@ export type EventsHistoryProps = {
maxTime: number | null;
html: string;
} & ItemProps &
WithModuleProps &
LinkedVisualConsoleProps;
WithModuleProps;
/**
* Build a valid typed object from a raw object.
@ -43,8 +37,7 @@ export function eventsHistoryPropsDecoder(
html: !stringIsEmpty(data.html)
? data.html
: decodeBase64(data.encodedHtml),
...modulePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
...linkedVCPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
...modulePropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
}

View File

@ -0,0 +1,55 @@
import {
LinkedVisualConsoleProps,
UnknownObject,
WithModuleProps
} from "../types";
import {
linkedVCPropsDecoder,
modulePropsDecoder,
decodeBase64,
stringIsEmpty
} from "../lib";
import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item";
export type ModuleGraphProps = {
type: ItemType.MODULE_GRAPH;
html: string;
} & ItemProps &
WithModuleProps &
LinkedVisualConsoleProps;
/**
* Build a valid typed object from a raw object.
* This will allow us to ensure the type safety.
*
* @param data Raw object.
* @return An object representing the module graph props.
* @throws Will throw a TypeError if some property
* is missing from the raw object or have an invalid type.
*/
export function moduleGraphPropsDecoder(
data: UnknownObject
): ModuleGraphProps | never {
if (stringIsEmpty(data.html) && stringIsEmpty(data.encodedHtml)) {
throw new TypeError("missing html content.");
}
return {
...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
type: ItemType.MODULE_GRAPH,
html: !stringIsEmpty(data.html)
? data.html
: decodeBase64(data.encodedHtml),
...modulePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
...linkedVCPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
}
export default class ModuleGraph extends Item<ModuleGraphProps> {
public createDomElement(): HTMLElement {
const element = document.createElement("div");
element.innerHTML = this.props.html;
return element;
}
}