From 3d7aa1e97e2ceeae7f08416e8a7903ac9597ac5d Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Mon, 22 Apr 2019 16:59:43 +0200 Subject: [PATCH] Visual Console Refactor: changes in the group item Former-commit-id: 06c93036ced218711c6931da831e19463d23089f --- visual_console_client/src/items/Group.ts | 34 +++++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/visual_console_client/src/items/Group.ts b/visual_console_client/src/items/Group.ts index 4a39cd8e1a..18395dd585 100644 --- a/visual_console_client/src/items/Group.ts +++ b/visual_console_client/src/items/Group.ts @@ -4,19 +4,27 @@ import { parseIntOr, notEmptyStringOr, stringIsEmpty, - decodeBase64 + decodeBase64, + parseBoolean } from "../lib"; import Item, { ItemProps, itemBasePropsDecoder, ItemType } from "../Item"; export type GroupProps = { type: ItemType.GROUP_ITEM; - imageSrc: string | null; // URL? groupId: number; - html: string | null; + imageSrc: string | null; // URL? statusImageSrc: string | null; + showStatistics: boolean; + html?: string | null; } & ItemProps & LinkedVisualConsoleProps; +function extractHtml(data: UnknownObject): string | null { + if (!stringIsEmpty(data.html)) return data.html; + if (!stringIsEmpty(data.encodedHtml)) return decodeBase64(data.encodedHtml); + return null; +} + /** * Build a valid typed object from a raw object. * This will allow us to ensure the type safety. @@ -37,29 +45,35 @@ export function groupPropsDecoder(data: UnknownObject): GroupProps | never { throw new TypeError("invalid group Id."); } + const showStatistics = parseBoolean(data.showStatistics); + const html = showStatistics ? extractHtml(data) : null; + return { ...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects. type: ItemType.GROUP_ITEM, - imageSrc: notEmptyStringOr(data.imageSrc, null), groupId: parseInt(data.groupId), - html: !stringIsEmpty(data.encodedHtml) - ? decodeBase64(data.encodedHtml) - : null, + imageSrc: notEmptyStringOr(data.imageSrc, null), statusImageSrc: notEmptyStringOr(data.statusImageSrc, null), + showStatistics, + html, ...linkedVCPropsDecoder(data) // Object spread. It will merge the properties of the two objects. }; } export default class Group extends Item { protected createDomElement(): HTMLElement { - if (this.props.html !== null) { + if (this.props.showStatistics) { const div = document.createElement("div"); - div.innerHTML = this.props.html; + div.className = "group"; + if (this.props.html != null) { + div.innerHTML = this.props.html; + } + return div; } else { const img: HTMLImageElement = document.createElement("img"); img.className = "group"; - if (this.props.statusImageSrc != null) { + if (this.props.statusImageSrc !== null) { img.src = this.props.statusImageSrc; }