Added a defered init option and a new setter to the vc client item class

This commit is contained in:
Alejandro Gallardo Escobar 2019-07-02 12:37:10 +02:00
parent 14f83fe142
commit 89b12cf717
1 changed files with 26 additions and 6 deletions

View File

@ -142,10 +142,10 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
// Metadata of the item. // Metadata of the item.
private _metadata: ItemMeta; private _metadata: ItemMeta;
// Reference to the DOM element which will contain the item. // Reference to the DOM element which will contain the item.
public elementRef: HTMLElement; public elementRef: HTMLElement = document.createElement("div");
public readonly labelElementRef: HTMLElement; public labelElementRef: HTMLElement = document.createElement("div");
// Reference to the DOM element which will contain the view of the item which extends this class. // Reference to the DOM element which will contain the view of the item which extends this class.
protected readonly childElementRef: HTMLElement; protected childElementRef: HTMLElement = document.createElement("div");
// 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 moved events. // Event manager for moved events.
@ -296,10 +296,21 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
*/ */
protected abstract createDomElement(): HTMLElement; protected abstract createDomElement(): HTMLElement;
public constructor(props: Props, metadata: ItemMeta) { public constructor(
props: Props,
metadata: ItemMeta,
deferInit: boolean = false
) {
this.itemProps = props; this.itemProps = props;
this._metadata = metadata; this._metadata = metadata;
if (!deferInit) this.init();
}
/**
* To create and append the DOM elements.
*/
protected init(): void {
/* /*
* Get a HTMLElement which represents the container box * Get a HTMLElement which represents the container box
* of the Visual Console item. This element will manage * of the Visual Console item. This element will manage
@ -320,9 +331,9 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
this.elementRef.append(this.childElementRef, this.labelElementRef); this.elementRef.append(this.childElementRef, this.labelElementRef);
// Resize element. // Resize element.
this.resizeElement(props.width, props.height); this.resizeElement(this.itemProps.width, this.itemProps.height);
// Set label position. // Set label position.
this.changeLabelPosition(props.labelPosition); this.changeLabelPosition(this.itemProps.labelPosition);
} }
/** /**
@ -482,6 +493,15 @@ abstract class VisualConsoleItem<Props extends ItemProps> {
* @param newProps * @param newProps
*/ */
public set props(newProps: Props) { public set props(newProps: Props) {
this.setProps(newProps);
}
/**
* Clasic and protected version of the setter of the `props` property.
* Useful to override it from children classes.
* @param newProps
*/
protected setProps(newProps: Props) {
const prevProps = this.props; const prevProps = this.props;
// Update the internal props. // Update the internal props.
this.itemProps = newProps; this.itemProps = newProps;