-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathvis.js
216 lines (184 loc) · 6 KB
/
vis.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
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
/**
* @name Vis
*
* @description This class consists of aggs, params, listeners, title, and type.
* - Aggs: Instances of AggConfig.
* - Params: The settings in the Options tab.
*
* Not to be confused with vislib/vis.js.
*/
import { EventEmitter } from 'events';
import _ from 'lodash';
import { VisTypesRegistryProvider } from 'ui/registry/vis_types';
import { VisAggConfigsProvider } from 'ui/vis/agg_configs';
import { PersistedState } from 'ui/persisted_state';
import { UtilsBrushEventProvider } from 'ui/utils/brush_event';
import { FilterBarQueryFilterProvider } from 'ui/filter_bar/query_filter';
import { FilterBarClickHandlerProvider } from 'ui/filter_bar/filter_bar_click_handler';
import { updateVisualizationConfig } from './vis_update';
import { SearchSourceProvider } from 'ui/courier/data_source/search_source';
import { SavedObjectsClientProvider } from 'ui/saved_objects';
import { FilterManagerProvider } from 'ui/filter_manager';
export function VisProvider(Private, indexPatterns, timefilter, getAppState) {
const visTypes = Private(VisTypesRegistryProvider);
const AggConfigs = Private(VisAggConfigsProvider);
const brushEvent = Private(UtilsBrushEventProvider);
const queryFilter = Private(FilterBarQueryFilterProvider);
const filterBarClickHandler = Private(FilterBarClickHandlerProvider);
const SearchSource = Private(SearchSourceProvider);
const savedObjectsClient = Private(SavedObjectsClientProvider);
const filterManager = Private(FilterManagerProvider);
class Vis extends EventEmitter {
constructor(indexPattern, visState, uiState) {
super();
visState = visState || {};
if (_.isString(visState)) {
visState = {
type: visState
};
}
this.indexPattern = indexPattern;
if (!uiState) {
uiState = new PersistedState();
}
this.setCurrentState(visState);
this.setState(this.getCurrentState(), false);
this.setUiState(uiState);
// Session state is for storing information that is transitory, and will not be saved with the visualization.
// For instance, map bounds, which depends on the view port, browser window size, etc.
this.sessionState = {};
this.API = {
savedObjectsClient: savedObjectsClient,
SearchSource: SearchSource,
indexPatterns: indexPatterns,
timeFilter: timefilter,
filterManager: filterManager,
queryFilter: queryFilter,
events: {
filter: (event) => {
const appState = getAppState();
filterBarClickHandler(appState)(event);
}, brush: (event) => {
const appState = getAppState();
brushEvent(appState)(event);
}
}
};
}
isEditorMode() {
return this.editorMode || false;
}
setCurrentState(state) {
this.title = state.title || '';
const type = state.type || this.type;
if (_.isString(type)) {
this.type = visTypes.byName[type];
if (!this.type) {
throw new Error(`Invalid type "${type}"`);
}
} else {
this.type = type;
}
this.params = _.defaults({},
_.cloneDeep(state.params || {}),
_.cloneDeep(this.type.visConfig.defaults || {})
);
updateVisualizationConfig(state.params, this.params);
this.aggs = new AggConfigs(this, state.aggs);
}
setState(state, updateCurrentState = true) {
this._state = _.cloneDeep(state);
if (updateCurrentState) this.resetState();
}
updateState() {
this.setState(this.getCurrentState(true));
this.emit('update');
}
resetState() {
this.setCurrentState(this._state);
}
forceReload() {
this.emit('reload');
}
getCurrentState(includeDisabled) {
return {
title: this.title,
type: this.type.name,
params: this.params,
aggs: this.aggs
.map(agg => agg.toJSON())
.filter(agg => includeDisabled || agg.enabled)
.filter(Boolean)
};
}
getStateInternal(includeDisabled) {
return {
title: this._state.title,
type: this._state.type,
params: this._state.params,
aggs: this._state.aggs
.filter(agg => includeDisabled || agg.enabled)
};
}
getEnabledState() {
return this.getStateInternal(false);
}
getAggConfig() {
return new AggConfigs(this, this.aggs.raw.filter(agg => agg.enabled));
}
getState() {
return this.getStateInternal(true);
}
clone() {
const uiJson = this.hasUiState() ? this.getUiState().toJSON() : {};
const uiState = new PersistedState(uiJson);
const clonedVis = new Vis(this.indexPattern, this.getState(), uiState);
clonedVis.editorMode = this.editorMode;
return clonedVis;
}
requesting() {
// Invoke requesting() on each agg. Aggs is an instance of AggConfigs.
_.invoke(this.aggs.getRequestAggs(), 'requesting');
}
isHierarchical() {
if (_.isFunction(this.type.hierarchicalData)) {
return !!this.type.hierarchicalData(this);
} else {
return !!this.type.hierarchicalData;
}
}
hasSchemaAgg(schemaName, aggTypeName) {
const aggs = this.aggs.bySchemaName[schemaName] || [];
return aggs.some(function (agg) {
if (!agg.type || !agg.type.name) return false;
return agg.type.name === aggTypeName;
});
}
hasUiState() {
return !!this.__uiState;
}
setUiState(uiState) {
if (uiState instanceof PersistedState) {
this.__uiState = uiState;
}
}
getUiState() {
return this.__uiState;
}
/**
* Currently this is only used to extract map-specific information
* (e.g. mapZoom, mapCenter).
*/
uiStateVal(key, val) {
if (this.hasUiState()) {
if (_.isUndefined(val)) {
return this.__uiState.get(key);
}
return this.__uiState.set(key, val);
}
return val;
}
}
Vis.prototype.type = 'histogram';
return Vis;
}