import WidgetRegistrySingleton from 'ievv_jsbase/lib/widget/WidgetRegistrySingleton'
WidgetRegistrySingleton
A very lightweight widget system.
Basic example below - see AbstractWidget for more examples.
Example:
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);
}
}
<button data-ievv-jsbase-widget="open-menu-button" type="button">
Open menu
</button>
// 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 |
destroyAllWidgetsWithinElement(element: Element) Destroy all widgets within the provided element. |
|
public |
destroyWidget(element: Element) Destroy the widget on the provided element. |
|
public |
getWidgetInstanceByInstanceId(widgetInstanceId: *): AbstractWidget Get a widget instance by its widget instance id. |
|
public |
getWidgetInstanceFromElement(element: *): * |
|
public |
getWidgetInstanceIdFromElement(element: Element): null | string Get the value of the |
|
public |
initializeAllWidgetsWithinElement(element: Element) 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:
Name | Type | Attribute | Description |
element | Element | The DOM Element. |
public destroyWidget(element: Element) source
Destroy the widget on the provided element.
Params:
Name | Type | Attribute | Description |
element | Element | A DOM element that has been initialized by WidgetRegistrySingleton#initializeWidget. |
Throw:
If the element has
no |
|
If the element
has the |
public getWidgetInstanceByInstanceId(widgetInstanceId: *): AbstractWidget source
Get a widget instance by its widget instance id.
Params:
Name | Type | Attribute | Description |
widgetInstanceId | * | A widget instance id. |
public getWidgetInstanceFromElement(element: *): * source
Params:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
element | Element | A DOM element. |
public initializeAllWidgetsWithinElement(element: Element) source
Initialize all widgets within the provided element.
Params:
Name | Type | Attribute | Description |
element | Element | A DOM element. |
public initializeWidget(element: Element): * source
Initialize the provided element as a widget.
Params:
Name | Type | Attribute | Description |
element | Element | The DOM element to initalize as a widget. |
Return:
* |
Throw:
If the element does not have
the |
|
If the widget alias is not in this registry. |
public registerWidgetClass(alias: string, WidgetClass: AbstractWidget) source
Register a widget class in the registry.
Params:
Name | Type | Attribute | Description |
alias | string | The alias for the widget. This is the string that
is used as the attribute value with the |
|
WidgetClass | AbstractWidget | The widget class. |
public removeWidgetClass(alias: *) source
Remove widget class from registry.
Params:
Name | Type | Attribute | Description |
alias | * | The alias that the widget class was registered with by using WidgetRegistrySingleton#registerWidgetClass. |