/
index.js
182 lines (163 loc) · 6.05 KB
/
index.js
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
import _ from 'lodash';
import React from 'react';
import classNames from 'classnames';
import KeenDataviz from 'keen-dataviz';
import Select from '../../common/select.js';
import Notice from '../../common/notice.js';
import Chart from './chart.js';
import CodeSample from './code_sample.js';
import AppDispatcher from '../../../dispatcher/AppDispatcher';
import ExplorerConstants from '../../../constants/ExplorerConstants';
import ExplorerActions from '../../../actions/ExplorerActions';
import ExplorerStore from '../../../stores/ExplorerStore';
import NoticeActions from '../../../actions/NoticeActions';
import ExplorerUtils from '../../../utils/ExplorerUtils';
import ChartTypeUtils from '../../../utils/ChartTypeUtils';
import FormatUtils from '../../../utils/FormatUtils';
import DataUtils from '../../../utils/DataUtils';
const Visualization = React.createClass({
getInitialState: function() {
return {
focusDisplayName: false,
focusQueryName: false,
blurTimeout: 100
};
},
handleDisplayNameFocus: function(){
this.setState({ focusDisplayName: true });
},
handleDisplayNameBlur: function(){
setTimeout(function(){
this.setState({ focusDisplayName: false });
}.bind(this), this.state.blurTimeout);
},
handleQueryNameFocus: function(){
this.setState({ focusQueryName: true });
},
handleQueryNameBlur: function(){
setTimeout(function(){
this.setState({ focusQueryName: false });
}.bind(this), this.state.blurTimeout);
},
noticeClosed: function() {
NoticeActions.clearAll();
},
changeChartType: function(event) {
var chartType = _.find(this.formatChartTypes(), function(type){
return type.value === event.target.value;
});
var updates = {
metadata: {
visualization: { chart_type: chartType.value }
}
};
ExplorerActions.update(this.props.model.id, updates);
},
formatChartTypes: function() {
return _.map(ChartTypeUtils.getChartTypeOptions(this.props.model.query), function(type) {
return {
name: (type !== 'JSON') ? FormatUtils.toTitleCase(type).replace('chart', '') : type,
value: type
};
});
},
chartType: function() {
if (this.props.model.metadata.visualization &&
this.props.model.metadata.visualization.chart_type) {
return this.props.model.metadata.visualization.chart_type;
}
else {
return _.first(ChartTypeUtils.getChartTypeOptions(this.props.model.query))
}
},
componentWillMount: function() {
this.dataviz = new KeenDataviz();
},
componentWillUnmount: function() {
AppDispatcher.unregister(this.dispatcherToken);
},
exportToCsv: function(dataviz) {
let data = this.dataviz.dataset.matrix;
if (dataviz && dataviz.dataset && dataviz.dataset.matrix) {
data = dataviz.dataset.matrix;
}
var filename = this.props.model.query_name || 'untitled-query';
DataUtils.exportToCsv(data, filename);
},
render: function() {
var chartTitle,
codeSample;
var chartDetailBarClasses = classNames({
'chart-detail-bar': true,
'chart-detail-bar-focus': (this.state.focusDisplayName || this.state.focusQueryName) && this.props.model.response !== null && !this.props.model.loading,
'chart-detail-active': this.props.model.response !== null && !this.props.model.loading
});
if (this.props.model.isValid) {
codeSample = ExplorerUtils.getSdkExample(this.props.model, this.props.client, this.props.config);
}
if (this.props.persistence) {
const display_name = this.props.model.metadata.display_name || '';
const query_name = this.props.model.query_name || '';
chartTitle = (
<div className="chart-title-component">
<input className="chart-display-name"
type="text"
onChange={this.props.onDisplayNameChange}
onBlur={this.handleDisplayNameBlur}
onFocus={this.handleDisplayNameFocus}
spellCheck="false"
value={display_name}
placeholder="Give your query a name..." />
<div className="chart-query-name">
<label>
Saved Query Resource Name
<a href="https://keen.io/docs/api/#saved-queries" target="_blank">
<i className="icon glyphicon glyphicon-question-sign"></i>
</a>
</label>
<input className="chart-query-name"
type="text"
onChange={this.props.onQueryNameChange}
onBlur={this.handleQueryNameBlur}
onFocus={this.handleQueryNameFocus}
spellCheck="false"
value={query_name} />
</div>
</div>
);
}
return (
<div className="visualization">
<Notice notice={this.props.notice} closeCallback={this.noticeClosed} />
<div className="visualization-wrapper">
<div className={chartDetailBarClasses}>
{chartTitle}
<div className="chart-type-component">
<Select label={false}
ref="chart-type"
name="chart_type"
classes="chart-type"
options={this.formatChartTypes()}
handleSelection={this.changeChartType}
selectedOption={this.chartType()}
emptyOption={false}
disabled={this.props.model.loading} />
</div>
</div>
<div className="chart-component">
<Chart model={this.props.model}
dataviz={this.dataviz}
config={this.props.config}
exportToCsv={this.exportToCsv}/>
</div>
<CodeSample ref="codesample"
codeSample={codeSample}
hidden={this.props.appState.codeSampleHidden}
onCloseClick={this.props.toggleCodeSample}
isValid={this.props.model.isValid} />
</div>
</div>
);
}
});
export default Visualization;