This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
/
Dialog.tsx
157 lines (128 loc) · 4.39 KB
/
Dialog.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import * as React from 'react';
import { DataSourceConnector, IDataSourceDictionary } from '../../../data-sources';
import ElementConnector from '../../ElementConnector';
import DialogsActions from './DialogsActions';
import DialogsStore from './DialogsStore';
import MDDialog from 'react-md/lib/Dialogs';
import Button from 'react-md/lib/Buttons/Button';
import List from 'react-md/lib/Lists/List';
import ListItem from 'react-md/lib/Lists/ListItem';
import * as ReactGridLayout from 'react-grid-layout';
var ResponsiveReactGridLayout = ReactGridLayout.Responsive;
var WidthProvider = ReactGridLayout.WidthProvider;
ResponsiveReactGridLayout = WidthProvider(ResponsiveReactGridLayout);
interface IDialogProps {
dialogData: IDialog;
dashboard: IDashboardConfig;
}
interface IDialogState {
dialogId?: string;
dialogArgs?: IDictionary;
mounted?: boolean;
currentBreakpoint?: string;
layouts?: ILayouts;
}
export default class Dialog extends React.PureComponent<IDialogProps, IDialogState> {
layouts = {};
constructor(props: IDialogProps) {
super(props);
this.state = DialogsStore.getState();
this.onChange = this.onChange.bind(this);
// Create dialog data source
var dialogDS: IDataSource = {
id: 'dialog_' + this.props.dialogData.id,
type: 'Constant',
params: {
selectedValue: null
}
};
DataSourceConnector.createDataSources({ dataSources: [ dialogDS ] }, this.props.dashboard.config.connections);
// Adding other data sources
DataSourceConnector.createDataSources(this.props.dialogData, this.props.dashboard.config.connections);
var layouts = ElementConnector.loadLayoutFromDashboard(this.props.dialogData, this.props.dashboard);
this.layouts = layouts;
(this.state as any).layouts = { };
}
componentDidMount() {
this.setState({ mounted: true });
DialogsStore.listen(this.onChange);
}
componentDidUpdate() {
const { dialogData } = this.props;
var { dialogId, dialogArgs } = this.state;
if (dialogData.id === dialogId) {
let dataSourceId = 'dialog_' + dialogData.id;
let dataSource = DataSourceConnector.getDataSource(dataSourceId);
dataSource.action.updateDependencies.defer(dialogArgs);
}
}
onBreakpointChange = (breakpoint) => {
var layouts = this.state.layouts;
layouts[breakpoint] = layouts[breakpoint] || this.layouts[breakpoint];
this.setState({
currentBreakpoint: breakpoint,
layouts: layouts
});
}
onChange(state: IDialogState) {
var { dialogId, dialogArgs } = state;
this.setState({ dialogId, dialogArgs });
}
closeDialog = () => {
DialogsActions.closeDialog();
}
render() {
const { dialogData, dashboard } = this.props;
const { id } = dialogData;
const { dialogId, dialogArgs } = this.state;
let { title } = dialogArgs || { title: '' };
if (title === undefined) {
title = '';
}
var visible = id === dialogId;
if (!visible) {
return null;
}
var { currentBreakpoint } = this.state;
var layout = null;
if (!currentBreakpoint || !this.state.layouts[currentBreakpoint]) {
layout = this.layouts[currentBreakpoint || 'lg'];
} else {
layout = this.state.layouts[currentBreakpoint];
}
// Creating visual elements
var elements = ElementConnector.loadElementsFromDashboard(dialogData, layout);
let grid = {
className: 'layout',
rowHeight: dashboard.config.layout.rowHeight || 30,
cols: dashboard.config.layout.cols,
breakpoints: dashboard.config.layout.breakpoints
};
return (
<MDDialog
id={id}
visible={visible}
title={title}
focusOnMount={false}
onHide={this.closeDialog}
dialogStyle={{ width: dialogData.width || '80%', overflow: 'auto' }}
contentStyle={{ padding: '0' }}
>
<ResponsiveReactGridLayout
{...grid}
isDraggable={false}
isResizable={false}
layouts={this.layouts}
onBreakpointChange={this.onBreakpointChange}
// WidthProvider option
measureBeforeMount={false}
// I like to have it animate on mount. If you don't, delete `useCSSTransforms` (it's default `true`)
// and set `measureBeforeMount={true}`.
useCSSTransforms={this.state.mounted}
>
{elements}
</ResponsiveReactGridLayout>
</MDDialog>
);
}
}