-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathcontrols.js
294 lines (287 loc) · 10.1 KB
/
controls.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
import { numericToCalendar, calendarToNumeric, currentNumDate, currentCalDate } from "../util/dateHelpers";
import { defaultGeoResolution,
defaultColorBy,
defaultDateRange,
defaultDistanceMeasure,
defaultLayout,
defaultMutType,
controlsHiddenWidth,
twoColumnBreakpoint } from "../util/globals";
import * as types from "../actions/types";
import { calcBrowserDimensionsInitialState } from "./browserDimensions";
import { doesColorByHaveConfidence } from "../actions/recomputeReduxState";
/* defaultState is a fn so that we can re-create it
at any time, e.g. if we want to revert things (e.g. on dataset change)
*/
export const getDefaultControlsState = () => {
const defaults = {
distanceMeasure: defaultDistanceMeasure,
layout: defaultLayout,
geoResolution: defaultGeoResolution,
filters: {},
colorBy: defaultColorBy,
selectedBranchLabel: "none"
};
// a default sidebarOpen status is only set via JSON, URL query
// _or_ if certain URL keywords are triggered
const initialSidebarState = getInitialSidebarState();
if (initialSidebarState.setDefault) {
defaults.sidebarOpen = initialSidebarState.sidebarOpen;
}
const dateMin = numericToCalendar(currentNumDate() - defaultDateRange);
const dateMax = currentCalDate();
const dateMinNumeric = calendarToNumeric(dateMin);
const dateMaxNumeric = calendarToNumeric(dateMax);
return {
defaults,
available: undefined,
canTogglePanelLayout: true,
selectedBranch: null,
selectedNode: null,
region: null,
search: null,
strain: null,
geneLength: {},
mutType: defaultMutType,
temporalConfidence: { exists: false, display: false, on: false },
layout: defaults.layout,
distanceMeasure: defaults.distanceMeasure,
dateMin,
dateMinNumeric,
dateMax,
dateMaxNumeric,
absoluteDateMin: dateMin,
absoluteDateMinNumeric: dateMinNumeric,
absoluteDateMax: dateMax,
absoluteDateMaxNumeric: dateMaxNumeric,
colorBy: defaults.colorBy,
colorByConfidence: { display: false, on: false },
colorScale: undefined,
selectedBranchLabel: "none",
analysisSlider: false,
geoResolution: defaults.geoResolution,
filters: {},
showDownload: false,
quickdraw: false, // if true, components may skip expensive computes.
mapAnimationDurationInMilliseconds: 30000, // in milliseconds
mapAnimationStartDate: null, // Null so it can pull the absoluteDateMin as the default
mapAnimationCumulative: false,
mapAnimationShouldLoop: false,
animationPlayPauseButton: "Play",
panelsAvailable: [],
panelsToDisplay: [],
panelLayout: calcBrowserDimensionsInitialState().width > twoColumnBreakpoint ? "grid" : "full",
showTreeToo: undefined,
showTangle: false,
zoomMin: undefined,
zoomMax: undefined,
branchLengthsToDisplay: "divAndDate",
sidebarOpen: initialSidebarState.sidebarOpen,
treeLegendOpen: undefined,
mapLegendOpen: undefined,
showOnlyPanels: false,
showTransmissionLines: true,
normalizeFrequencies: true,
resamplingCounter: 0,
};
};
/* while this may change, div currently doesn't have CIs, so they shouldn't be displayed. */
export const shouldDisplayTemporalConfidence = (exists, distMeasure, layout) => exists && distMeasure === "num_date" && layout === "rect";
const Controls = (state = getDefaultControlsState(), action) => {
switch (action.type) {
case types.URL_QUERY_CHANGE_WITH_COMPUTED_STATE: /* fallthrough */
case types.CLEAN_START:
return action.controls;
case types.SET_AVAILABLE:
return Object.assign({}, state, { available: action.data });
case types.BRANCH_MOUSEENTER:
return Object.assign({}, state, {
selectedBranch: action.data
});
case types.BRANCH_MOUSELEAVE:
return Object.assign({}, state, {
selectedBranch: null
});
case types.NODE_MOUSEENTER:
return Object.assign({}, state, {
selectedNode: action.data
});
case types.NODE_MOUSELEAVE:
return Object.assign({}, state, {
selectedNode: null
});
case types.RESAMPLE:
return {...state, resamplingCounter: state.resamplingCounter+1};
case types.CHANGE_BRANCH_LABEL:
return Object.assign({}, state, { selectedBranchLabel: action.value });
case types.CHANGE_LAYOUT:
return Object.assign({}, state, {
layout: action.data,
/* temporal confidence can only be displayed for rectangular trees */
temporalConfidence: Object.assign({}, state.temporalConfidence, {
display: shouldDisplayTemporalConfidence(
state.temporalConfidence.exists,
state.distanceMeasure,
action.data
),
on: false
})
});
case types.CHANGE_DISTANCE_MEASURE:
const updatesToState = {
distanceMeasure: action.data,
branchLengthsToDisplay: state.branchLengthsToDisplay
};
if (
shouldDisplayTemporalConfidence(state.temporalConfidence.exists, action.data, state.layout)
) {
updatesToState.temporalConfidence = Object.assign({}, state.temporalConfidence, {
display: true
});
} else {
updatesToState.temporalConfidence = Object.assign({}, state.temporalConfidence, {
display: false,
on: false
});
}
return Object.assign({}, state, updatesToState);
case types.CHANGE_DATES_VISIBILITY_THICKNESS: {
const newDates = { quickdraw: action.quickdraw };
if (action.dateMin) {
newDates.dateMin = action.dateMin;
newDates.dateMinNumeric = action.dateMinNumeric;
}
if (action.dateMax) {
newDates.dateMax = action.dateMax;
newDates.dateMaxNumeric = action.dateMaxNumeric;
}
return Object.assign({}, state, newDates);
}
case types.CHANGE_ABSOLUTE_DATE_MIN:
return Object.assign({}, state, {
absoluteDateMin: action.data,
absoluteDateMinNumeric: calendarToNumeric(action.data)
});
case types.CHANGE_ABSOLUTE_DATE_MAX:
return Object.assign({}, state, {
absoluteDateMax: action.data,
absoluteDateMaxNumeric: calendarToNumeric(action.data)
});
case types.CHANGE_ANIMATION_TIME:
return Object.assign({}, state, {
mapAnimationDurationInMilliseconds: action.data
});
case types.CHANGE_ANIMATION_CUMULATIVE:
return Object.assign({}, state, {
mapAnimationCumulative: action.data
});
case types.CHANGE_ANIMATION_LOOP:
return Object.assign({}, state, {
mapAnimationShouldLoop: action.data
});
case types.MAP_ANIMATION_PLAY_PAUSE_BUTTON:
return Object.assign({}, state, {
quickdraw: action.data !== "Play",
animationPlayPauseButton: action.data
});
case types.CHANGE_ANIMATION_START:
return Object.assign({}, state, {
mapAnimationStartDate: action.data
});
case types.CHANGE_PANEL_LAYOUT:
return Object.assign({}, state, {
panelLayout: action.data
});
case types.TREE_TOO_DATA:
return action.controls;
case types.TOGGLE_PANEL_DISPLAY:
return Object.assign({}, state, {
panelsToDisplay: action.panelsToDisplay,
panelLayout: action.panelLayout,
canTogglePanelLayout:
action.panelsToDisplay.indexOf("tree") !== -1 &&
action.panelsToDisplay.indexOf("map") !== -1
});
case types.NEW_COLORS: {
const newState = Object.assign({}, state, {
colorBy: action.colorBy,
colorScale: action.colorScale,
colorByConfidence: doesColorByHaveConfidence(state, action.colorBy)
});
return newState;
}
case types.CHANGE_GEO_RESOLUTION:
return Object.assign({}, state, {
geoResolution: action.data
});
case types.APPLY_FILTER: {
// values arrive as array
const filters = Object.assign({}, state.filters, {});
filters[action.trait] = action.values;
return Object.assign({}, state, {
filters
});
}
case types.TOGGLE_MUT_TYPE:
return Object.assign({}, state, {
mutType: action.data
});
case types.TOGGLE_TEMPORAL_CONF:
return Object.assign({}, state, {
temporalConfidence: Object.assign({}, state.temporalConfidence, {
on: !state.temporalConfidence.on
})
});
case types.TRIGGER_DOWNLOAD_MODAL:
return Object.assign({}, state, {
showDownload: true
});
case types.DISMISS_DOWNLOAD_MODAL:
return Object.assign({}, state, {
showDownload: false
});
case types.REMOVE_TREE_TOO:
return Object.assign({}, state, {
showTreeToo: undefined,
showTangle: false,
canTogglePanelLayout: state.panelsAvailable.indexOf("map") !== -1,
panelsToDisplay: state.panelsAvailable.slice()
});
case types.TOGGLE_TANGLE:
if (state.showTreeToo) {
return Object.assign({}, state, { showTangle: !state.showTangle });
}
return state;
case types.TOGGLE_SIDEBAR:
return Object.assign({}, state, { sidebarOpen: action.value });
case types.TOGGLE_LEGEND:
return Object.assign({}, state, { legendOpen: action.value });
case types.ADD_COLOR_BYS:
for (const colorBy of Object.keys(action.newColorings)) {
state.coloringsPresentOnTree.add(colorBy);
}
return Object.assign({}, state, { coloringsPresentOnTree: state.coloringsPresentOnTree });
case types.TOGGLE_TRANSMISSION_LINES:
return Object.assign({}, state, { showTransmissionLines: action.data });
case types.FREQUENCY_MATRIX: {
if (Object.hasOwnProperty.call(action, "normalizeFrequencies")) {
return Object.assign({}, state, { normalizeFrequencies: action.normalizeFrequencies });
}
return state;
}
default:
return state;
}
};
export default Controls;
function getInitialSidebarState() {
/* The following "hack" was present when `sidebarOpen` wasn't URL customisable. It can be removed
from here once the GISAID URLs (iFrames) are updated */
if (window.location.pathname.includes("gisaid")) {
return {sidebarOpen: false, setDefault: true};
}
return {
sidebarOpen: window.innerWidth > controlsHiddenWidth,
setDefault: false
};
}