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