WIP: Visual Console Refactor

Former-commit-id: c82cb8454f12caa66e2b06668f1d486a379be838
This commit is contained in:
Alejandro Gallardo Escobar 2019-02-11 17:35:16 +01:00
parent cdd67e3637
commit 49aaa91d2e
2 changed files with 46 additions and 3 deletions

View File

@ -27,7 +27,7 @@ export type ItemClickEvent<ItemProps extends VisualConsoleItemProps> = {
* This will allow us to ensure the type safety.
*
* @param data Raw object.
* @return An object representing the size.
* @return An object representing the item props.
* @throws Will throw a TypeError if some property
* is missing from the raw object or have an invalid type.
*/

View File

@ -1,7 +1,12 @@
import { WithModuleProps, LinkedVisualConsoleProps } from "../types";
import {
WithModuleProps,
LinkedVisualConsoleProps,
UnknownObject
} from "../types";
import VisualConsoleItem, {
VisualConsoleItemProps
VisualConsoleItemProps,
itemPropsDecoder
} from "../VisualConsoleItem";
export type StaticGraphProps = {
@ -10,6 +15,44 @@ export type StaticGraphProps = {
} & VisualConsoleItemProps &
(WithModuleProps | LinkedVisualConsoleProps);
/**
* Extract a valid enum value from a raw unknown value.
* @param showLastValueTooltip Raw value.
*/
const parseShowLastValueTooltip = (showLastValueTooltip: any) => {
switch (showLastValueTooltip) {
case "default":
case "enabled":
case "disabled":
return showLastValueTooltip;
default:
return "default";
}
};
/**
* 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 static graph props.
* @throws Will throw a TypeError if some property
* is missing from the raw object or have an invalid type.
*/
export function staticGraphPropsDecoder(
data: UnknownObject
): StaticGraphProps | never {
if (typeof data.imageSrc !== "string" || data.imageSrc.length === 0) {
throw new TypeError("invalid image src.");
}
return {
imageSrc: data.imageSrc,
showLastValueTooltip: parseShowLastValueTooltip(data.showLastValueTooltip),
...itemPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
}
export default class StaticGraph extends VisualConsoleItem<StaticGraphProps> {
createDomElement(): HTMLElement {
const img: HTMLImageElement = document.createElement("img");