This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
320 lines (289 loc) · 9.68 KB
/
test.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
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
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import ReduxThunk from 'redux-thunk';
import 'leaflet';
import { Provider, connect } from 'react-redux';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import WidgetEditor, { reducers, setConfig, Tooltip, Modal, Icons, SaveWidgetModal, EmbedTableModal, modalActions, VegaChart, WidgetService, getVegaTheme } from 'dist/bundle';
import 'leaflet/dist/leaflet.css';
import 'dist/styles.css';
const root = document.createElement('div');
document.body.appendChild(root);
// eslint-disable-next-line no-underscore-dangle
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const enhancer = composeEnhancers(applyMiddleware(ReduxThunk));
const store = createStore(combineReducers(reducers), enhancer);
// We set the config of the library
setConfig({
url: 'https://api.resourcewatch.org/v1',
env: 'production,preproduction',
applications: 'rw',
authUrl: 'https://api.resourcewatch.org/auth',
assetsPath: '/images/',
userToken: undefined
});
class App extends React.Component {
static injectStyles() {
const styles = `
*,
*:before,
*:after {
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
`;
const node = document.createElement('style');
node.innerHTML = styles;
document.body.appendChild(node);
}
constructor(props) {
super(props);
this.state = {
datasetId: 'a86d906d-9862-4783-9e30-cdb68cd808b8',
widgetId: 'bfa2bece-9b79-4842-9d83-1c3079f6d812',
// Widget with custom theme and new chart:
// widgetId: '7c60887a-aee9-4925-83fd-5c7673521443',
previewWidgetId: undefined,
previewConfig: undefined,
widgetTitle: '',
widgetCaption: '',
defaultWidgetTheme: JSON.stringify(getVegaTheme(), null, 2),
_defaultTheme: getVegaTheme(), // Internal
widgetTheme: '',
_theme: undefined, // Internal
previewWidgetTheme: JSON.stringify(getVegaTheme(true), null, 2),
_previewWidgetTheme: getVegaTheme(true) // Internal
};
this.onSave = this.onSave.bind(this);
this.onEmbed = this.onEmbed.bind(this);
this.onChangeWidgetTitle = this.onChangeWidgetTitle.bind(this);
this.onChangeWidgetCaption = this.onChangeWidgetCaption.bind(this);
this.onProvideWidgetConfig = this.onProvideWidgetConfig.bind(this);
this.onProvideLayer = this.onProvideLayer.bind(this);
this.onChangeDefaultTheme = this.onChangeDefaultTheme.bind(this);
this.onChangeTheme = this.onChangeTheme.bind(this);
this.onChangePreviewWidgetTheme = this.onChangePreviewWidgetTheme.bind(this);
}
componentWillMount() {
// We inject basic styles so the test page
// renders correctly
App.injectStyles();
}
async onSave() {
if (this.getWidgetConfig && this.getLayer) {
this.props.toggleModal(true, {
children: SaveWidgetModal,
childrenProps: {
datasetId: this.state.datasetId,
getWidgetConfig: this.getWidgetConfig,
getLayer: this.getLayer,
onClickCheckWidgets: () => alert('Check my widgets'),
onChangeWidgetTitle: title => this.setState({ widgetTitle: title })
}
});
}
}
async onEmbed() {
const { protocol, hostname, port } = location;
const baseUrl = `${protocol}//${hostname}${port !== '' ? `:${port}` : port}`;
this.props.toggleModal(true, {
children: EmbedTableModal,
childrenProps: { baseUrl }
});
}
onChangePreviewWidgetId(previewWidgetId) {
this.setState({ previewWidgetId });
new WidgetService(previewWidgetId)
.fetchData()
.then(data => this.setState({ previewConfig: data.attributes.widgetConfig }));
}
onChangeWidgetTitle(title) {
this.setState({ widgetTitle: title });
}
onChangeWidgetCaption(caption) {
this.setState({ widgetCaption: caption });
}
onProvideWidgetConfig(func) {
this.getWidgetConfig = func;
}
onProvideLayer(func) {
this.getLayer = func;
}
onChangeDefaultTheme({ target }) {
const theme = target.value;
let unserializedTheme;
try {
unserializedTheme = JSON.parse(theme);
} catch (err) {
unserializedTheme = getVegaTheme();
}
this.setState({
defaultWidgetTheme: theme,
_defaultTheme: unserializedTheme
});
}
onChangeTheme({ target }) {
const theme = target.value;
let unserializedTheme;
try {
unserializedTheme = JSON.parse(theme);
} catch (err) {
unserializedTheme = getVegaTheme();
}
this.setState({
widgetTheme: theme,
_theme: unserializedTheme
});
}
onChangePreviewWidgetTheme({ target }) {
const theme = target.value;
let unserializedTheme;
try {
unserializedTheme = JSON.parse(theme);
} catch (err) {
unserializedTheme = getVegaTheme(true);
}
this.setState({
previewWidgetTheme: theme,
_previewWidgetTheme: unserializedTheme
});
}
render() {
return (
<div>
<h1>Test WidgetEditor</h1>
<div style={{ border: '1px solid black', margin: '20px 0 40px', padding: '0 10px' }}>
<p>
Change here the params of the editor:
</p>
<p>
<label htmlFor="dataset">Dataset ID:</label>
<input
type="text"
id="dataset"
value={this.state.datasetId}
onChange={({ target }) => this.setState({ datasetId: target.value })}
/>
<br />
<label htmlFor="widget">Widget ID (optional):</label>
<input
type="text"
placeholder="Widget to restore"
id="widget"
value={this.state.widgetId}
onChange={({ target }) => this.setState({ widgetId: target.value })}
/>
<br />
<label htmlFor="title">Widget title (optional):</label>
<input
type="text"
placeholder="Title of the widget"
id="title"
value={this.state.widgetTitle}
onChange={({ target }) => this.setState({ widgetTitle: target.value })}
/>
<br />
<label htmlFor="caption">Widget caption (optional):</label>
<input
type="text"
placeholder="Caption of the widget"
id="caption"
value={this.state.widgetCaption}
onChange={({ target }) => this.setState({ widgetCaption: target.value })}
/>
<br />
<label htmlFor="default-theme">Default theme (optional):</label>
<textarea
placeholder="Default theme of the widget"
id="default-theme"
value={this.state.defaultWidgetTheme}
onChange={this.onChangeDefaultTheme}
/>
<br />
<label htmlFor="theme">Theme (optional):</label>
<textarea
placeholder="Theme of the widget"
id="theme"
value={this.state.widgetTheme}
onChange={this.onChangeTheme}
/>
</p>
</div>
<Icons />
<Tooltip />
<Modal />
<WidgetEditor
datasetId={this.state.datasetId}
widgetId={this.state.widgetId}
widgetTitle={this.state.widgetTitle}
widgetCaption={this.state.widgetCaption}
saveButtonMode="always"
embedButtonMode="always"
titleMode="always"
// eslint-disable-next-line no-underscore-dangle
// defaultTheme={this.state._defaultTheme}
// theme={this.state._theme}
useLayerEditor
allowBoundsCopyPaste
onSave={this.onSave}
onEmbed={this.onEmbed}
onChangeWidgetTitle={this.onChangeWidgetTitle}
onChangeWidgetCaption={this.onChangeWidgetCaption}
onChangeTheme={theme => this.setState({ widgetTheme: JSON.stringify(theme, null, 2), _theme: theme })}
provideWidgetConfig={this.onProvideWidgetConfig}
provideLayer={this.onProvideLayer}
/>
<div style={{ border: '1px solid black', margin: '40px 0', padding: '0 10px' }}>
<p>
Change here the params of the VegaChart component:
</p>
<p>
<label htmlFor="preview_widget">Widget ID:</label>
<input
type="text"
placeholder="Widget to preview"
id="preview_widget"
value={this.state.previewWidgetId}
onChange={({ target }) => this.onChangePreviewWidgetId(target.value)}
/>
<br />
<label htmlFor="preview_widget_theme">Theme (optional):</label>
<textarea
placeholder="Theme of the widget"
id="preview_widget_theme"
value={this.state.previewWidgetTheme}
onChange={this.onChangePreviewWidgetTheme}
/>
</p>
</div>
{this.state.previewWidgetId && this.state.previewConfig && (
<VegaChart
width={250}
height={180}
data={this.state.previewConfig}
// eslint-disable-next-line no-underscore-dangle
theme={this.state._previewWidgetTheme}
reloadOnResize
/>
)}
</div>
);
}
}
App.propTypes = {
toggleModal: PropTypes.func
};
const mapStateToProps = () => ({});
const mapDispatchToProps = dispatch => ({
toggleModal: (...params) => dispatch(modalActions.toggleModal(...params))
});
const Container = connect(mapStateToProps, mapDispatchToProps)(App);
ReactDOM.render(
<Provider store={store}>
<Container />
</Provider>,
root
);