-
Notifications
You must be signed in to change notification settings - Fork 168
/
data-cube-edit.tsx
409 lines (340 loc) · 12.7 KB
/
data-cube-edit.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright 2015-2016 Imply Data, Inc.
* Copyright 2017-2018 Allegro.pl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Timezone } from "chronoshift";
import { List } from "immutable";
import { AttributeInfo } from "plywood";
import * as React from "react";
import { Dimensions } from "../../../../common/models/dimension/dimensions";
import { Cluster, DataCube, Dimension, ListItem, Measure } from "../../../../common/models/index";
import { DATA_CUBE as LABELS } from "../../../../common/models/labels";
import { ImmutableUtils } from "../../../../common/utils/immutable-utils/immutable-utils";
import { generateUniqueName } from "../../../../common/utils/string/string";
import { Button, FormLabel, ImmutableDropdown, ImmutableInput, ImmutableList } from "../../../components/index";
import { DATA_CUBES_STRATEGIES_LABELS, STRINGS } from "../../../config/constants";
import { DimensionModal } from "../../../modals/dimension-modal/dimension-modal";
import { MeasureModal } from "../../../modals/measure-modal/measure-modal";
import { SuggestionModal } from "../../../modals/suggestion-modal/suggestion-modal";
import { classNames } from "../../../utils/dom/dom";
import { ImmutableFormDelegate, ImmutableFormState } from "../../../utils/immutable-form-delegate/immutable-form-delegate";
import "./data-cube-edit.scss";
export interface DataCubeEditProps {
isNewDataCube?: boolean;
dataCube?: DataCube;
clusters?: Cluster[];
tab?: string;
onSave: (newDataCube: DataCube) => void;
onCancel?: () => void;
}
export interface DataCubeEditState extends ImmutableFormState<DataCube> {
tab?: any;
showDimensionsSuggestion?: boolean;
showMeasuresSuggestion?: boolean;
}
export interface Tab {
label: string;
value: string;
render: () => JSX.Element;
}
export class DataCubeEdit extends React.Component<DataCubeEditProps, DataCubeEditState> {
private tabs: Tab[] = [
{ label: "General", value: "general", render: this.renderGeneral },
{ label: "Attributes", value: "attributes", render: this.renderAttributes },
{ label: "Dimensions", value: "dimensions", render: this.renderDimensions },
{ label: "Measures", value: "measures", render: this.renderMeasures }
];
private delegate: ImmutableFormDelegate<DataCube>;
constructor(props: DataCubeEditProps) {
super(props);
this.delegate = new ImmutableFormDelegate<DataCube>(this);
}
componentWillReceiveProps(nextProps: DataCubeEditProps) {
if (nextProps.dataCube) {
this.initFromProps(nextProps);
}
}
componentDidMount() {
if (this.props.dataCube) this.initFromProps(this.props);
}
initFromProps(props: DataCubeEditProps) {
this.setState({
newInstance: new DataCube(props.dataCube.valueOf()),
canSave: true,
errors: {},
tab: props.isNewDataCube ? this.tabs[0] : this.tabs.filter(tab => tab.value === props.tab)[0],
showDimensionsSuggestion: false,
showMeasuresSuggestion: false
});
}
selectTab(tab: Tab) {
if (this.props.isNewDataCube) {
this.setState({ tab });
} else {
var hash = window.location.hash.split("/");
hash.splice(-1);
window.location.hash = hash.join("/") + "/" + tab.value;
}
}
renderTabs(activeTab: Tab): JSX.Element[] {
return this.tabs.map(tab => {
return <button
className={classNames({ active: activeTab.value === tab.value })}
key={tab.value}
onClick={this.selectTab.bind(this, tab)}
>{tab.label}</button>;
});
}
cancel() {
const { isNewDataCube } = this.props;
if (isNewDataCube) {
this.props.onCancel();
return;
}
// Setting newInstance to undefined resets the inputs
this.setState({ newInstance: undefined }, () => this.initFromProps(this.props));
}
save() {
if (this.props.onSave) this.props.onSave(this.state.newInstance);
}
goBack() {
const { dataCube, tab } = this.props;
var hash = window.location.hash;
window.location.hash = hash.replace(`/${dataCube.name}/${tab}`, "");
}
getIntrospectionStrategies(): ListItem[] {
const labels = DATA_CUBES_STRATEGIES_LABELS as any;
return [{
label: `Default (${labels[DataCube.DEFAULT_INTROSPECTION]})`,
value: undefined
}].concat(DataCube.INTROSPECTION_VALUES.map(value => {
return { value, label: labels[value] };
}));
}
renderGeneral(): JSX.Element {
const { clusters } = this.props;
const { newInstance, errors } = this.state;
var makeLabel = FormLabel.simpleGenerator(LABELS, errors);
var makeTextInput = ImmutableInput.simpleGenerator(newInstance, this.delegate.onChange);
var makeDropDownInput = ImmutableDropdown.simpleGenerator(newInstance, this.delegate.onChange);
var possibleClusters = [
{ value: "native", label: "Load a file and serve it natively" }
].concat(clusters.map(cluster => {
return { value: cluster.name, label: cluster.name };
}));
return <form className="general vertical">
{makeLabel("title")}
{makeTextInput("title", /.*/, true)}
{makeLabel("description")}
{makeTextInput("description")}
{makeLabel("clusterName")}
{makeDropDownInput("clusterName", possibleClusters)}
{makeLabel("source")}
{makeTextInput("source")}
{makeLabel("defaultTimezone")}
<ImmutableInput
instance={newInstance}
path={"defaultTimezone"}
onChange={this.delegate.onChange}
valueToString={(value: Timezone) => value ? value.toJS() : undefined}
stringToValue={(str: string) => str ? Timezone.fromJS(str) : undefined}
/>
</form>;
}
renderAttributes(): JSX.Element {
const { newInstance, errors } = this.state;
var makeLabel = FormLabel.simpleGenerator(LABELS, errors);
return <form className="general vertical">
{makeLabel("attributeOverrides")}
<ImmutableInput
instance={newInstance}
path={"attributeOverrides"}
onChange={this.delegate.onChange}
valueToString={(value: AttributeInfo[]) => value ? JSON.stringify(AttributeInfo.toJSs(value), null, 2) : undefined}
stringToValue={(str: string) => str ? AttributeInfo.fromJSs(JSON.parse(str)) : undefined}
type="textarea"
/>
</form>;
}
renderDimensions(): JSX.Element {
const { newInstance } = this.state;
const onChange = (newDimensions: Dimensions) => {
const newCube = newInstance.changeDimensions(newDimensions);
this.setState({
newInstance: newCube
});
};
const getModal = (item: Dimension) => <DimensionModal dimension={item} />;
const getNewItem = () => Dimension.fromJS({
name: generateUniqueName("d", name => !newInstance.dimensions.containsDimensionWithName(name)),
title: "New dimension"
});
const getRows = (items: List<Dimension>) => items.toArray().map(dimension => {
return {
title: dimension.title,
description: dimension.expression.toString(),
icon: `dim-${dimension.kind}`
};
});
const DimensionsList = ImmutableList.specialize<Dimension>();
return <DimensionsList
label={STRINGS.dimensions}
items={List()} // empty list of dimensions for now
onChange={onChange.bind(this)}
getModal={getModal}
getNewItem={getNewItem}
getRows={getRows}
toggleSuggestions={this.toggleDimensionsSuggestions.bind(this)}
/>;
}
toggleDimensionsSuggestions() {
const { showDimensionsSuggestion } = this.state;
this.setState({
showDimensionsSuggestion: !showDimensionsSuggestion
});
}
addToCube(property: string, additionalValues: Array<Dimension | Measure>) {
const { newInstance } = this.state;
var newValues = additionalValues.concat((newInstance as any)[property].toArray());
this.setState({
newInstance: ImmutableUtils.setProperty(newInstance, property, List(newValues))
});
}
renderDimensionSuggestions() {
const { newInstance } = this.state;
return <SuggestionModal
onAdd={this.addToCube.bind(this, "dimensions")}
onClose={this.toggleDimensionsSuggestions.bind(this)}
getLabel={(d: Dimension) => `${d.title} (${d.formula})`}
getOptions={newInstance.getSuggestedDimensions.bind(newInstance)}
title={`${STRINGS.dimension} ${STRINGS.suggestion}`}
/>;
}
toggleMeasuresSuggestions() {
const { showMeasuresSuggestion } = this.state;
this.setState({
showMeasuresSuggestion: !showMeasuresSuggestion
});
}
renderMeasures(): JSX.Element {
var { newInstance } = this.state;
const onChange = (newMeasures: List<Measure>) => {
var { defaultSortMeasure } = newInstance;
if (defaultSortMeasure) {
if (!newMeasures.find(measure => measure.name === defaultSortMeasure)) {
newInstance = newInstance.changeDefaultSortMeasure(newMeasures.get(0).name);
}
}
const newCube = newInstance.changeMeasures(newMeasures);
this.setState({
newInstance: newCube
});
};
const getModal = (item: Measure) => <MeasureModal measure={item} />;
const getNewItem = () => Measure.fromJS({
name: generateUniqueName("m", name => !newInstance.measures.containsMeasureWithName(name)),
title: "New measure"
});
const getRows = (items: List<Measure>) => items.toArray().map(measure => {
return {
title: measure.title,
description: measure.expression.toString(),
icon: "measure"
};
});
const MeasuresList = ImmutableList.specialize<Measure>();
return <MeasuresList
label={STRINGS.measures}
items={List()} // empty list of measures for now
onChange={onChange.bind(this)}
getModal={getModal}
getNewItem={getNewItem}
getRows={getRows}
toggleSuggestions={this.toggleMeasuresSuggestions.bind(this)}
/>;
}
renderMeasureSuggestions() {
const { newInstance } = this.state;
return <SuggestionModal
onAdd={this.addToCube.bind(this, "measures")}
onClose={this.toggleMeasuresSuggestions.bind(this)}
getLabel={(m: Measure) => `${m.title} (${m.formula})`}
getOptions={newInstance.getSuggestedMeasures.bind(newInstance)}
title={`${STRINGS.measure} ${STRINGS.suggestion}`}
/>;
}
renderButtons(): JSX.Element {
const { dataCube, isNewDataCube } = this.props;
const { canSave, newInstance } = this.state;
const hasChanged = !dataCube.equals(newInstance);
const cancelButton = <Button
className="cancel"
title={isNewDataCube ? "Cancel" : "Revert changes"}
type="secondary"
onClick={this.cancel.bind(this)}
/>;
const saveButton = <Button
className={classNames("save", { disabled: !canSave || (!isNewDataCube && !hasChanged) })}
title={isNewDataCube ? "Create cube" : "Save"}
type="primary"
onClick={this.save.bind(this)}
/>;
if (!isNewDataCube && !hasChanged) {
return <div className="button-group">
{saveButton}
</div>;
}
return <div className="button-group">
{cancelButton}
{saveButton}
</div>;
}
getTitle(): string {
const { isNewDataCube } = this.props;
const { newInstance } = this.state;
const lastBit = newInstance.title ? `: ${newInstance.title}` : "";
return (isNewDataCube ? STRINGS.createDataCube : STRINGS.editDataCube) + lastBit;
}
render() {
const { dataCube, isNewDataCube } = this.props;
const { tab, newInstance, showDimensionsSuggestion, showMeasuresSuggestion } = this.state;
if (!newInstance || !tab || !dataCube) return null;
return <div className="data-cube-edit">
<div className="title-bar">
{isNewDataCube
? null
: <Button
className="button back"
type="secondary"
svg={require("../../../icons/full-back.svg")}
onClick={this.goBack.bind(this)}
/>
}
<div className="title">{this.getTitle()}</div>
{this.renderButtons()}
</div>
<div className="content">
<div className="tabs">
{this.renderTabs(tab)}
</div>
<div className="tab-content">
{tab.render.bind(this)()}
</div>
</div>
{showDimensionsSuggestion ? this.renderDimensionSuggestions() : null}
{showMeasuresSuggestion ? this.renderMeasureSuggestions() : null}
</div>;
}
}