Home Manual Reference Source
import AbstractWidget from 'ievv_jsbase/lib/widget/AbstractWidget'
public class | source

AbstractWidget

Base class for widgets for WidgetRegistrySingleton.

Example:

Create a very simple widget
export default class OpenMenuWidget extends AbstractWidget {
    constructor(element, widgetInstanceId) {
         super(element, widgetInstanceId);
         this._onClickBound = (...args) => {
             this._onClick(...args);
         };
         this.element.addEventListener('click', this._onClickBound);
    }

    _onClick = (e) => {
         e.preventDefault();
         console.log('I should have opened the menu here');
    }

    destroy() {
         this.element.removeEventListener('click', this._onClickBound);
    }
}
Use the widget
<button type="button" data-ievv-jsbase-widget="open-menu-button">
    Open menu
</button>
A widget with configuration input
export default class OpenMenuWidget extends AbstractWidget {
    constructor(element) {
         super(element);
         this._onClickBound = (...args) => {
             this._onClick(...args);
         };
         this.element.addEventListener('click', this._onClickBound);
    }

    getDefaultConfig() {
         return {
             'menuId': 'id_main_menu'
         }
    }

    _onClick = (e) => {
         e.preventDefault();
         console.log(`I should have opened the menu with id="${this.config.menuId}" here`);
    }

    destroy() {
         this.element.removeEventListener('click', this._onClickBound);
    }
}
Use the widget with config
<!-- Using the default config -->
<button type="button" data-ievv-jsbase-widget="open-menu-button">
    Open the main menu
</button>
<!-- Override the menuId config -->
<button type="button" data-ievv-jsbase-widget="open-menu-button"
         data-ievv-jsbase-widget-config='{"menuId": "id_the_other_menu"}'>
    Open the other menu
</button>

Constructor Summary

Public Constructor
public

constructor(element: Element, widgetInstanceId: string)

Member Summary

Public Members
public get

Get the config.

public

element: *

public

Method Summary

Public Methods
public

Called after all the widgets within the element that WidgetRegistrySingleton#initializeAllWidgetsWithinElement was called with is initialized.

public

Destroy the widget.

public

Get the default config.

public

If you override AbstractWidget#afterInitializeAllWidgets, you must override this to return true.

Public Constructors

public constructor(element: Element, widgetInstanceId: string) source

Params:

NameTypeAttributeDescription
element Element

The element to load the widget in.

widgetInstanceId string

The unique ID of this widget instance in the widget registry.

Public Members

public get config: Object: * source

Get the config.

JSON decodes any config supplied via the data-ievv-jsbase-widget-config attribute of the Element and AbstractWidget#getDefaultConfig into a config object. The result of this is cached, so multiple calls to this property will only result in the config object being created once.

Return:

Object

The config object. This will be an empty object if we have no AbstractWidget#getDefaultConfig and no config is supplied via the data-ievv-jsbase-widget-config attribute of the Element.

Throw:

SyntaxError

If the data-ievv-jsbase-widget-config attribute does not contain valid JSON data. Not thrown if the element does not have a data-ievv-jsbase-widget-config attribute.

public element: * source

public widgetInstanceId: * source

Public Methods

public afterInitializeAllWidgets() source

Called after all the widgets within the element that WidgetRegistrySingleton#initializeAllWidgetsWithinElement was called with is initialized.

For performance reasons, this is only called if AbstractWidget#useAfterInitializeAllWidgets returns true, so you must also override that method if you override this method.

This is useful if you need to do something after other widgets have finished initializing, which may be the case for loosely coupled widgets.

Does nothing by default.

public destroy() source

Destroy the widget.

You should override this in subclasses if your widget sets up something that will work incorrectly if the widget disappears or is re-created (such as event listeners and signals).

public getDefaultConfig(): Object source

Get the default config.

Any config supplied via the data-ievv-jsbase-widget-config attribute is merged into this object.

Return:

Object

public useAfterInitializeAllWidgets(): boolean source

If you override AbstractWidget#afterInitializeAllWidgets, you must override this to return true.

Return:

boolean

Should return true if you want the widget registry to call AbstractWidget#afterInitializeAllWidgets.