2019-02-10 13:06:05 +00:00
|
|
|
/***
|
|
|
|
|
* Widget are responsible for accepting the abstraction layer inputs, interpretting them into rederable props and
|
|
|
|
|
* spawing components based on those props
|
|
|
|
|
* Widgets are also responsible for dispatching actions and updating the state tree
|
|
|
|
|
*/
|
2019-03-16 13:08:45 +00:00
|
|
|
import { WidgetType } from "../constants/WidgetConstants";
|
|
|
|
|
import { Component } from "react";
|
2019-02-11 18:22:23 +00:00
|
|
|
|
|
|
|
|
abstract class BaseWidget<
|
|
|
|
|
T extends IWidgetProps,
|
|
|
|
|
K extends IWidgetState
|
|
|
|
|
> extends Component<T, K> {
|
|
|
|
|
componentDidMount(): void {
|
|
|
|
|
this.calculateWidgetBounds(
|
|
|
|
|
this.props.rightColumn,
|
|
|
|
|
this.props.leftColumn,
|
|
|
|
|
this.props.topRow,
|
|
|
|
|
this.props.bottomRow,
|
|
|
|
|
this.props.parentColumnSpace,
|
|
|
|
|
this.props.parentRowSpace
|
2019-03-16 13:08:45 +00:00
|
|
|
);
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-02-11 18:22:23 +00:00
|
|
|
componentWillReceiveProps(prevProps: T, nextProps: T) {
|
|
|
|
|
this.calculateWidgetBounds(
|
|
|
|
|
nextProps.rightColumn,
|
|
|
|
|
nextProps.leftColumn,
|
|
|
|
|
nextProps.topRow,
|
|
|
|
|
nextProps.bottomRow,
|
|
|
|
|
nextProps.parentColumnSpace,
|
|
|
|
|
nextProps.parentRowSpace
|
2019-03-16 13:08:45 +00:00
|
|
|
);
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-11 18:22:23 +00:00
|
|
|
calculateWidgetBounds(
|
|
|
|
|
rightColumn: number,
|
|
|
|
|
leftColumn: number,
|
|
|
|
|
topRow: number,
|
|
|
|
|
bottomRow: number,
|
|
|
|
|
parentColumnSpace: number,
|
|
|
|
|
parentRowSpace: number
|
|
|
|
|
) {
|
|
|
|
|
const widgetState: IWidgetState = {
|
|
|
|
|
width: (rightColumn - leftColumn) * parentColumnSpace,
|
|
|
|
|
height: (bottomRow - topRow) * parentRowSpace
|
2019-03-16 13:08:45 +00:00
|
|
|
};
|
|
|
|
|
this.setState(widgetState);
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-03-16 13:08:45 +00:00
|
|
|
return this.getWidgetView();
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-03-16 13:08:45 +00:00
|
|
|
abstract getWidgetView(): JSX.Element;
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-03-16 13:08:45 +00:00
|
|
|
abstract getWidgetType(): WidgetType;
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-02-11 18:22:23 +00:00
|
|
|
export interface IWidgetState {
|
2019-03-16 13:08:45 +00:00
|
|
|
height: number;
|
|
|
|
|
width: number;
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-11 18:22:23 +00:00
|
|
|
export interface IWidgetBuilder<T extends IWidgetProps> {
|
2019-03-16 13:08:45 +00:00
|
|
|
buildWidget(data: T): JSX.Element;
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IWidgetProps {
|
2019-03-16 13:08:45 +00:00
|
|
|
widgetType: WidgetType;
|
|
|
|
|
key?: string;
|
|
|
|
|
widgetId: string;
|
|
|
|
|
topRow: number;
|
|
|
|
|
leftColumn: number;
|
|
|
|
|
bottomRow: number;
|
|
|
|
|
rightColumn: number;
|
|
|
|
|
parentColumnSpace: number;
|
|
|
|
|
parentRowSpace: number;
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-16 13:08:45 +00:00
|
|
|
export default BaseWidget;
|