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-18 15:10:30 +00:00
|
|
|
import {
|
|
|
|
|
WidgetType,
|
|
|
|
|
RenderMode,
|
2019-03-19 14:05:48 +00:00
|
|
|
RenderModes,
|
|
|
|
|
CSSUnits
|
2019-03-18 15:10:30 +00:00
|
|
|
} from "../constants/WidgetConstants"
|
|
|
|
|
import { Component } from "react"
|
2019-03-19 14:47:18 +00:00
|
|
|
import { BaseStyle } from "../editorComponents/BaseComponent"
|
|
|
|
|
import _ from "lodash"
|
2019-04-01 07:08:00 +00:00
|
|
|
import * as React from "react"
|
2019-04-02 16:12:08 +00:00
|
|
|
import ContainerWidget from "./ContainerWidget"
|
|
|
|
|
import ContainerComponent from "../editorComponents/ContainerComponent"
|
|
|
|
|
import DraggableComponent from "../editorComponents/DraggableComponent"
|
2019-02-11 18:22:23 +00:00
|
|
|
|
|
|
|
|
abstract class BaseWidget<
|
|
|
|
|
T extends IWidgetProps,
|
|
|
|
|
K extends IWidgetState
|
2019-04-02 16:12:08 +00:00
|
|
|
> extends Component<T, K> {
|
2019-03-19 14:47:18 +00:00
|
|
|
constructor(props: T) {
|
|
|
|
|
super(props)
|
2019-04-02 16:12:08 +00:00
|
|
|
const initialState: IWidgetState = {
|
|
|
|
|
height: 0,
|
|
|
|
|
width: 0
|
|
|
|
|
}
|
2019-03-19 15:21:27 +00:00
|
|
|
initialState.height = 0
|
|
|
|
|
initialState.width = 0
|
2019-04-02 16:12:08 +00:00
|
|
|
this.state = initialState as K
|
2019-03-19 14:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-11 18:22:23 +00:00
|
|
|
componentDidMount(): void {
|
|
|
|
|
this.calculateWidgetBounds(
|
|
|
|
|
this.props.rightColumn,
|
|
|
|
|
this.props.leftColumn,
|
|
|
|
|
this.props.topRow,
|
|
|
|
|
this.props.bottomRow,
|
|
|
|
|
this.props.parentColumnSpace,
|
|
|
|
|
this.props.parentRowSpace
|
2019-03-18 15:10:30 +00:00
|
|
|
)
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-03-19 14:47:18 +00:00
|
|
|
componentDidUpdate(prevProps: T) {
|
2019-02-11 18:22:23 +00:00
|
|
|
this.calculateWidgetBounds(
|
2019-03-19 14:47:18 +00:00
|
|
|
this.props.rightColumn,
|
|
|
|
|
this.props.leftColumn,
|
|
|
|
|
this.props.topRow,
|
|
|
|
|
this.props.bottomRow,
|
|
|
|
|
this.props.parentColumnSpace,
|
|
|
|
|
this.props.parentRowSpace
|
2019-03-18 15:10:30 +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-18 15:10:30 +00:00
|
|
|
}
|
2019-03-19 14:47:18 +00:00
|
|
|
if (
|
|
|
|
|
_.isNil(this.state) ||
|
|
|
|
|
widgetState.height !== this.state.height ||
|
|
|
|
|
widgetState.width !== this.state.width
|
|
|
|
|
) {
|
2019-08-26 12:41:21 +00:00
|
|
|
// console.log("*** " + this.props.widgetId + " " + JSON.stringify(widgetState))
|
2019-03-19 14:47:18 +00:00
|
|
|
this.setState(widgetState)
|
|
|
|
|
}
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-03-18 15:10:30 +00:00
|
|
|
return this.getWidgetView()
|
2019-02-11 18:22:23 +00:00
|
|
|
}
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-03-18 15:10:30 +00:00
|
|
|
getWidgetView(): JSX.Element {
|
|
|
|
|
switch (this.props.renderMode) {
|
|
|
|
|
case RenderModes.CANVAS:
|
|
|
|
|
return this.getCanvasView()
|
|
|
|
|
case RenderModes.COMPONENT_PANE:
|
|
|
|
|
return this.getComponentPaneView()
|
|
|
|
|
case RenderModes.PAGE:
|
|
|
|
|
return this.getPageView()
|
|
|
|
|
default:
|
|
|
|
|
return this.getPageView()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract getPageView(): JSX.Element
|
|
|
|
|
|
|
|
|
|
getCanvasView(): JSX.Element {
|
|
|
|
|
return this.getPageView()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getComponentPaneView(): JSX.Element {
|
2019-04-01 07:08:00 +00:00
|
|
|
return (
|
2019-04-02 16:12:08 +00:00
|
|
|
<DraggableComponent
|
2019-04-01 07:08:00 +00:00
|
|
|
{...this.props}
|
|
|
|
|
style={{
|
|
|
|
|
...this.getPositionStyle()
|
|
|
|
|
}}
|
|
|
|
|
orientation={"VERTICAL"}
|
|
|
|
|
>
|
|
|
|
|
{this.getPageView()}
|
2019-04-02 16:12:08 +00:00
|
|
|
</DraggableComponent>
|
2019-04-01 07:08:00 +00:00
|
|
|
)
|
2019-03-18 15:10:30 +00:00
|
|
|
}
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-03-18 15:10:30 +00:00
|
|
|
abstract getWidgetType(): WidgetType
|
2019-03-19 14:05:48 +00:00
|
|
|
|
|
|
|
|
getPositionStyle(): BaseStyle {
|
|
|
|
|
return {
|
2019-03-19 14:47:18 +00:00
|
|
|
positionType:
|
2019-04-02 16:12:08 +00:00
|
|
|
this.props.renderMode !== RenderModes.PAGE
|
2019-03-19 14:47:18 +00:00
|
|
|
? "CONTAINER_DIRECTION"
|
|
|
|
|
: "ABSOLUTE",
|
2019-03-19 15:21:27 +00:00
|
|
|
height: this.state.height,
|
|
|
|
|
width: this.state.width,
|
2019-03-19 14:05:48 +00:00
|
|
|
yPosition: this.props.topRow * this.props.parentRowSpace,
|
|
|
|
|
xPosition: this.props.leftColumn * this.props.parentColumnSpace,
|
|
|
|
|
xPositionUnit: CSSUnits.PIXEL,
|
|
|
|
|
yPositionUnit: CSSUnits.PIXEL
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-19 14:47:18 +00:00
|
|
|
static defaultProps: Partial<IWidgetProps> = {
|
|
|
|
|
parentRowSpace: 1,
|
|
|
|
|
parentColumnSpace: 1,
|
|
|
|
|
topRow: 0,
|
|
|
|
|
leftColumn: 0
|
|
|
|
|
}
|
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-18 15:10:30 +00:00
|
|
|
height: number
|
|
|
|
|
width: number
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-26 12:41:21 +00:00
|
|
|
export interface DraggableWidget {
|
|
|
|
|
type: string,
|
|
|
|
|
widget: IWidgetProps
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 18:22:23 +00:00
|
|
|
export interface IWidgetBuilder<T extends IWidgetProps> {
|
2019-03-18 15:10:30 +00:00
|
|
|
buildWidget(data: T): JSX.Element
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IWidgetProps {
|
2019-03-18 15:10:30 +00:00
|
|
|
widgetType: WidgetType
|
|
|
|
|
key?: string
|
|
|
|
|
widgetId: string
|
|
|
|
|
topRow: number
|
|
|
|
|
leftColumn: number
|
|
|
|
|
bottomRow: number
|
|
|
|
|
rightColumn: number
|
|
|
|
|
parentColumnSpace: number
|
|
|
|
|
parentRowSpace: number
|
|
|
|
|
renderMode: RenderMode
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-21 12:49:16 +00:00
|
|
|
export interface IWidgetCardProps {
|
|
|
|
|
widgetType: WidgetType
|
|
|
|
|
key?: string
|
|
|
|
|
label: string
|
|
|
|
|
icon: string
|
|
|
|
|
groups: string[]
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-18 15:10:30 +00:00
|
|
|
export default BaseWidget
|