-
Notifications
You must be signed in to change notification settings - Fork 178
/
lib-voyager.tsx
287 lines (255 loc) · 7.26 KB
/
lib-voyager.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
// This module is intended to be used when embedding voyager
// in some other context than the orgiginal app.
//
// It provides factory methods for creating instances of the Voyager application
// and should eventually also export a react component if one is doing that kind
// of integration.
import * as Ajv from 'ajv';
import * as draft4Schemas from 'ajv/lib/refs/json-schema-draft-04.json';
import 'font-awesome-sass-loader'; // TODO should this move to App?
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {Store} from 'redux';
import {TopLevelSpec} from 'vega-lite';
import {Data} from 'vega-lite/build/src/data';
import {FacetedCompositeUnitSpec, isUnitSpec, TopLevel} from 'vega-lite/build/src/spec';
import * as vlSchema from 'vega-lite/build/vega-lite-schema.json';
import {isString} from 'vega-util';
import { REDO, UNDO } from './actions/index';
import {App} from './components/app';
import {State} from './models';
import {DEFAULT_VOYAGER_CONFIG, VoyagerConfig} from './models/config';
import {fromSerializable, SerializableState, toSerializable} from './models/index';
import {selectData} from './selectors/dataset';
import {selectBookmark} from './selectors/index';
import {selectMainSpec} from './selectors/result';
import {configureStore} from './store';
export type Container = string | HTMLElement;
/**
* The Voyager class encapsulates the voyager application and allows for easy
* instantiation and interaction from non-react projects.
*/
export class Voyager {
private container: HTMLElement;
private config: VoyagerConfig;
private store: Store<State>;
private data: Data;
private filename: string;
constructor(container: Container, config: VoyagerConfig, data: Data) {
if (isString(container)) {
this.container = document.querySelector(container) as HTMLElement;
// TODO throw error if not found
} else {
this.container = container;
}
this.config = {
...DEFAULT_VOYAGER_CONFIG,
...config
};
this.data = data;
this.init();
}
/**
* Update the dataset currently loaded into voyager
*
* @param {VoyagerData} data
*
* @memberof Voyager
*/
public updateData(data: Data) {
this.data = data;
this.render();
}
/**
* Update state to reflect the previous state
*
* @memberof Voyager
*/
public undo(): void {
this.store.dispatch({type: UNDO});
this.render();
}
/**
* Update state to reflect the future state
*
* @memberof Voyager
*/
public redo(): void {
this.store.dispatch({type: REDO});
this.render();
}
/**
* Update the configuration of the voyager application.
*
* @param {VoyagerConfig} config
*
* @memberof Voyager
*/
public updateConfig(config: VoyagerConfig) {
this.config = config;
this.render();
}
public setFilename(filename: string) {
this.filename = filename;
}
/**
* Apply a vega-lite spec to voyager.
*
* @param {VoyagerConfig} config
*
* @memberof Voyager
*/
public setSpec(spec: Object) {
const ajv = new Ajv({
validateSchema: true,
allErrors: true,
extendRefs: 'fail'
});
ajv.addMetaSchema(draft4Schemas, 'http://json-schema.org/draft-04/schema#');
const validateVl = ajv.compile(vlSchema);
const valid = validateVl(spec);
if (!valid) {
throw new Error("Invalid spec:" + validateVl.errors.toString());
}
// If it is validated, it is a TopLevelExtendedSpec
if (!isUnitSpec(spec as TopLevelSpec)) {
throw new Error("Voyager does not support layered or multi-view vega-lite specs");
}
// If it is unit, then we can cast to a top level unit spec
const validSpec: TopLevel<FacetedCompositeUnitSpec> = spec as TopLevel<FacetedCompositeUnitSpec>;
this.data = validSpec.data;
this.render(validSpec);
}
/**
* Sets the entire voyager application state. This is useful for restoring
* the state of the application to a previosly saved state.
*
* @param state A State object with the following keys
*
* @param state.config
* @param state.dataset
* @param state.shelf
* @param state.result
*
* @memberof Voyager
*/
public setApplicationState(state: SerializableState): void {
this.data = undefined;
this.config = undefined;
this.renderFromState(fromSerializable(state));
}
/**
*
* Gets the current application state.
*
* @returns {Readonly<State>}
*
* @memberof Voyager
*/
public getApplicationState(): SerializableState {
return toSerializable(this.store.getState());
}
/**
*
* Gets Vega-Lite spec of current specified view
*
* @returns {Readonly<Spec>}
*
* @memberof Voyager
*/
public getSpec(includeData: boolean): FacetedCompositeUnitSpec {
const spec = selectMainSpec(this.store.getState());
if (includeData) {
return {
...spec,
data: selectData(this.store.getState()),
};
}
return spec;
}
/**
*
* Gets the current bookmarked vega-lite specs.
*
* @returns {string[]}
*
* @memberof Voyager
*/
public getBookmarkedSpecs(): string[] {
return selectBookmark(this.store.getState()).list;
}
/**
* Subscribe to state changes.
*
* This is useful for taking state snapshots to persist and later restore.
*
* @param {Function} onChange callback that takes a single state parameter.
* @returns {Function} unsubscribe, call this function to remove this listener.
*
* @memberof Voyager
*/
public onStateChange(onChange: (state: SerializableState) => void): Function {
let currentState: State;
const handleChange = () => {
const nextState = this.store.getState();
if (nextState !== currentState) {
currentState = nextState;
onChange(toSerializable(currentState));
}
};
const unsubscribe = this.store.subscribe(handleChange);
return unsubscribe;
}
/**
* Initialized the application, and renders it into the target container
*
* @private
*
* @memberof Voyager
*/
private init() {
this.store = configureStore();
this.render();
}
private render(spec?: TopLevel<FacetedCompositeUnitSpec>) {
const store = this.store;
const root = this.container;
ReactDOM.render(
<Provider store={store}>
<App
dispatch={store.dispatch}
data={this.data}
config={this.config}
spec={spec}
filename={this.filename}
/>
</Provider>,
root
);
}
private renderFromState(state: Readonly<State>) {
const store = this.store;
const root = this.container;
ReactDOM.render(
<Provider store={store}>
<App
dispatch={store.dispatch}
applicationState={state}
/>
</Provider>,
root
);
}
}
/**
* Create an instance of the voyager application.
*
* @param {Container} container css selector or HTMLElement that will be the parent
* element of the application
* @param {Object} config configuration options
* @param {Array} data data object. Can be a string or an array of objects.
*/
export function CreateVoyager(container: Container, config: VoyagerConfig, data: Data): Voyager {
return new Voyager(container, config, data);
}