2019-02-10 13:06:05 +00:00
|
|
|
import { WidgetType } from "../constants/WidgetConstants";
|
2019-09-09 09:08:54 +00:00
|
|
|
import { WidgetBuilder, WidgetProps } from "../widgets/BaseWidget";
|
2019-02-10 13:06:05 +00:00
|
|
|
|
|
|
|
|
class WidgetFactory {
|
2019-09-09 09:08:54 +00:00
|
|
|
static widgetMap: Map<WidgetType, WidgetBuilder<WidgetProps>> = new Map();
|
|
|
|
|
|
|
|
|
|
static registerWidgetBuilder(
|
|
|
|
|
widgetType: WidgetType,
|
|
|
|
|
widgetBuilder: WidgetBuilder<WidgetProps>,
|
|
|
|
|
) {
|
|
|
|
|
this.widgetMap.set(widgetType, widgetBuilder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static createWidget(widgetData: WidgetProps): JSX.Element {
|
|
|
|
|
widgetData.key = widgetData.widgetId;
|
|
|
|
|
const widgetBuilder = this.widgetMap.get(widgetData.widgetType);
|
|
|
|
|
if (widgetBuilder) return widgetBuilder.buildWidget(widgetData);
|
|
|
|
|
else {
|
|
|
|
|
const ex: WidgetCreationException = {
|
|
|
|
|
message:
|
|
|
|
|
"Widget Builder not registered for widget type" +
|
|
|
|
|
widgetData.widgetType,
|
|
|
|
|
};
|
|
|
|
|
throw ex;
|
2019-04-02 16:12:08 +00:00
|
|
|
}
|
2019-09-09 09:08:54 +00:00
|
|
|
}
|
2019-04-02 16:12:08 +00:00
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
static getWidgetTypes(): WidgetType[] {
|
|
|
|
|
return Array.from(this.widgetMap.keys());
|
|
|
|
|
}
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 11:22:09 +00:00
|
|
|
export interface WidgetCreationException {
|
2019-09-09 09:08:54 +00:00
|
|
|
message: string;
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
export default WidgetFactory;
|