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

WidgetRegistrySingleton

A very lightweight widget system.

Basic example below - see AbstractWidget for more examples.

Example:

Create a very simple widget
export default class OpenMenuWidget extends AbstractWidget {
    constructor(element) {
         super(element);
         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 data-ievv-jsbase-widget="open-menu-button" type="button">
    Open menu
</button>
Register and load widgets
// Somewhere that is called after all the widgets are rendered
// - typically at the end of the <body>
import WidgetRegistrySingleton from 'ievv_jsbase/widget/WidgetRegistrySingleton';
import OpenMenuWidget from 'path/to/OpenMenuWidget';
const widgetRegistry = new WidgetRegistrySingleton();
widgetRegistry.registerWidgetClass('open-menu-button', OpenMenuWidget);
widgetRegistry.initializeAllWidgetsWithinElement(document.body);

Constructor Summary

Public Constructor
public

Method Summary

Public Methods
public

clear()

public

Destroy all widgets within the provided element.

public

Destroy the widget on the provided element.

public

Get a widget instance by its widget instance id.

public
public

Get the value of the data-ievv-jsbase-widget-instanceid attribute of the provided element.

public

Initialize all widgets within the provided element.

public

initializeWidget(element: Element): *

Initialize the provided element as a widget.

public

registerWidgetClass(alias: string, WidgetClass: AbstractWidget)

Register a widget class in the registry.

public

removeWidgetClass(alias: *)

Remove widget class from registry.

Public Constructors

public constructor source

Public Methods

public clear() source

public destroyAllWidgetsWithinElement(element: Element) source

Destroy all widgets within the provided element. Only destroys widgets on elements that is a child of the element.

Params:

NameTypeAttributeDescription
element Element

The DOM Element.

public destroyWidget(element: Element) source

Destroy the widget on the provided element.

Params:

NameTypeAttributeDescription
element Element

A DOM element that has been initialized by WidgetRegistrySingleton#initializeWidget.

Throw:

ElementHasNoWidgetInstanceIdError

If the element has no data-ievv-jsbase-widget-instanceid attribute or the attribute value is empty. This normally means that the element is not a widget, or that the widget is not initialized.

ElementIsNotInitializedAsWidget

If the element has the data-ievv-jsbase-widget-instanceid attribute but the value of the attribute is not a valid widget instance id. This should not happen unless you manipulate the attribute manually or use the private members of this registry.

public getWidgetInstanceByInstanceId(widgetInstanceId: *): AbstractWidget source

Get a widget instance by its widget instance id.

Params:

NameTypeAttributeDescription
widgetInstanceId *

A widget instance id.

Return:

AbstractWidget

A widget instance or null.

public getWidgetInstanceFromElement(element: *): * source

Params:

NameTypeAttributeDescription
element *

Return:

*

public getWidgetInstanceIdFromElement(element: Element): null | string source

Get the value of the data-ievv-jsbase-widget-instanceid attribute of the provided element.

Params:

NameTypeAttributeDescription
element Element

A DOM element.

Return:

null | string

public initializeAllWidgetsWithinElement(element: Element) source

Initialize all widgets within the provided element.

Params:

NameTypeAttributeDescription
element Element

A DOM element.

public initializeWidget(element: Element): * source

Initialize the provided element as a widget.

Params:

NameTypeAttributeDescription
element Element

The DOM element to initalize as a widget.

Return:

*

Throw:

ElementIsNotWidgetError

If the element does not have the data-ievv-jsbase-widget attribute.

InvalidWidgetAliasError

If the widget alias is not in this registry.

public registerWidgetClass(alias: string, WidgetClass: AbstractWidget) source

Register a widget class in the registry.

Params:

NameTypeAttributeDescription
alias string

The alias for the widget. This is the string that is used as the attribute value with the data-ievv-jsbase-widget DOM element attribute.

WidgetClass AbstractWidget

The widget class.

public removeWidgetClass(alias: *) source

Remove widget class from registry.

Params:

NameTypeAttributeDescription
alias *

The alias that the widget class was registered with by using WidgetRegistrySingleton#registerWidgetClass.