-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathmanage.js
354 lines (306 loc) · 11.8 KB
/
manage.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'use strict';
var axisIds = require('../../plots/cartesian/axis_ids');
var scatterSubTypes = require('../../traces/scatter/subtypes');
var Registry = require('../../registry');
var isUnifiedHover = require('../fx/helpers').isUnifiedHover;
var createModeBar = require('./modebar');
var modeBarButtons = require('./buttons');
var DRAW_MODES = require('./constants').DRAW_MODES;
/**
* ModeBar wrapper around 'create' and 'update',
* chooses buttons to pass to ModeBar constructor based on
* plot type and plot config.
*
* @param {object} gd main plot object
*
*/
module.exports = function manageModeBar(gd) {
var fullLayout = gd._fullLayout;
var context = gd._context;
var modeBar = fullLayout._modeBar;
if(!context.displayModeBar && !context.watermark) {
if(modeBar) {
modeBar.destroy();
delete fullLayout._modeBar;
}
return;
}
if(!Array.isArray(context.modeBarButtonsToRemove)) {
throw new Error([
'*modeBarButtonsToRemove* configuration options',
'must be an array.'
].join(' '));
}
if(!Array.isArray(context.modeBarButtonsToAdd)) {
throw new Error([
'*modeBarButtonsToAdd* configuration options',
'must be an array.'
].join(' '));
}
var customButtons = context.modeBarButtons;
var buttonGroups;
if(Array.isArray(customButtons) && customButtons.length) {
buttonGroups = fillCustomButton(customButtons);
} else if(!context.displayModeBar && context.watermark) {
buttonGroups = [];
} else {
buttonGroups = getButtonGroups(gd);
}
if(modeBar) modeBar.update(gd, buttonGroups);
else fullLayout._modeBar = createModeBar(gd, buttonGroups);
};
// logic behind which buttons are displayed by default
function getButtonGroups(gd) {
var fullLayout = gd._fullLayout;
var fullData = gd._fullData;
var context = gd._context;
function match(name, B) {
if(typeof B === 'string') {
if(B.toLowerCase() === name.toLowerCase()) return true;
} else {
var v0 = B.name;
var v1 = (B._cat || B.name);
if(v0 === name || v1 === name.toLowerCase()) return true;
}
return false;
}
var layoutAdd = fullLayout.modebar.add;
if(typeof layoutAdd === 'string') layoutAdd = [layoutAdd];
var layoutRemove = fullLayout.modebar.remove;
if(typeof layoutRemove === 'string') layoutRemove = [layoutRemove];
var buttonsToAdd = context.modeBarButtonsToAdd.concat(
layoutAdd.filter(function(e) {
for(var i = 0; i < context.modeBarButtonsToRemove.length; i++) {
if(match(e, context.modeBarButtonsToRemove[i])) return false;
}
return true;
})
);
var buttonsToRemove = context.modeBarButtonsToRemove.concat(
layoutRemove.filter(function(e) {
for(var i = 0; i < context.modeBarButtonsToAdd.length; i++) {
if(match(e, context.modeBarButtonsToAdd[i])) return false;
}
return true;
})
);
var hasCartesian = fullLayout._has('cartesian');
var hasGL3D = fullLayout._has('gl3d');
var hasGeo = fullLayout._has('geo');
var hasPie = fullLayout._has('pie');
var hasFunnelarea = fullLayout._has('funnelarea');
var hasGL2D = fullLayout._has('gl2d');
var hasTernary = fullLayout._has('ternary');
var hasMapbox = fullLayout._has('mapbox');
var hasPolar = fullLayout._has('polar');
var hasSmith = fullLayout._has('smith');
var hasSankey = fullLayout._has('sankey');
var allAxesFixed = areAllAxesFixed(fullLayout);
var hasUnifiedHoverLabel = isUnifiedHover(fullLayout.hovermode);
var groups = [];
function addGroup(newGroup) {
if(!newGroup.length) return;
var out = [];
for(var i = 0; i < newGroup.length; i++) {
var name = newGroup[i];
var B = modeBarButtons[name];
var v0 = B.name.toLowerCase();
var v1 = (B._cat || B.name).toLowerCase();
var found = false;
for(var q = 0; q < buttonsToRemove.length; q++) {
var t = buttonsToRemove[q].toLowerCase();
if(t === v0 || t === v1) {
found = true;
break;
}
}
if(found) continue;
out.push(modeBarButtons[name]);
}
groups.push(out);
}
// buttons common to all plot types
var commonGroup = ['toImage'];
if(context.showEditInChartStudio) commonGroup.push('editInChartStudio');
else if(context.showSendToCloud) commonGroup.push('sendDataToCloud');
addGroup(commonGroup);
var zoomGroup = [];
var hoverGroup = [];
var resetGroup = [];
var dragModeGroup = [];
if((hasCartesian || hasGL2D || hasPie || hasFunnelarea || hasTernary) + hasGeo + hasGL3D + hasMapbox + hasPolar + hasSmith > 1) {
// graphs with more than one plot types get 'union buttons'
// which reset the view or toggle hover labels across all subplots.
hoverGroup = ['toggleHover'];
resetGroup = ['resetViews'];
} else if(hasGeo) {
zoomGroup = ['zoomInGeo', 'zoomOutGeo'];
hoverGroup = ['hoverClosestGeo'];
resetGroup = ['resetGeo'];
} else if(hasGL3D) {
hoverGroup = ['hoverClosest3d'];
resetGroup = ['resetCameraDefault3d', 'resetCameraLastSave3d'];
} else if(hasMapbox) {
zoomGroup = ['zoomInMapbox', 'zoomOutMapbox'];
hoverGroup = ['toggleHover'];
resetGroup = ['resetViewMapbox'];
} else if(hasGL2D) {
hoverGroup = ['hoverClosestGl2d'];
} else if(hasPie) {
hoverGroup = ['hoverClosestPie'];
} else if(hasSankey) {
hoverGroup = ['hoverClosestCartesian', 'hoverCompareCartesian'];
resetGroup = ['resetViewSankey'];
} else { // hasPolar, hasSmith, hasTernary
// always show at least one hover icon.
hoverGroup = ['toggleHover'];
}
// if we have cartesian, allow switching between closest and compare
// regardless of what other types are on the plot, since they'll all
// just treat any truthy hovermode as 'closest'
if(hasCartesian) {
hoverGroup = ['toggleSpikelines', 'hoverClosestCartesian', 'hoverCompareCartesian'];
}
if(hasNoHover(fullData) || hasUnifiedHoverLabel) {
hoverGroup = [];
}
if((hasCartesian || hasGL2D) && !allAxesFixed) {
zoomGroup = ['zoomIn2d', 'zoomOut2d', 'autoScale2d'];
if(resetGroup[0] !== 'resetViews') resetGroup = ['resetScale2d'];
}
if(hasGL3D) {
dragModeGroup = ['zoom3d', 'pan3d', 'orbitRotation', 'tableRotation'];
} else if(((hasCartesian || hasGL2D) && !allAxesFixed) || hasTernary) {
dragModeGroup = ['zoom2d', 'pan2d'];
} else if(hasMapbox || hasGeo) {
dragModeGroup = ['pan2d'];
} else if(hasPolar) {
dragModeGroup = ['zoom2d'];
}
if(isSelectable(fullData)) {
dragModeGroup.push('select2d', 'lasso2d');
}
var enabledHoverGroup = [];
var enableHover = function(a) {
// return if already added
if(enabledHoverGroup.indexOf(a) !== -1) return;
// should be in hoverGroup
if(hoverGroup.indexOf(a) !== -1) {
enabledHoverGroup.push(a);
}
};
if(Array.isArray(buttonsToAdd)) {
var newList = [];
for(var i = 0; i < buttonsToAdd.length; i++) {
var b = buttonsToAdd[i];
if(typeof b === 'string') {
b = b.toLowerCase();
if(DRAW_MODES.indexOf(b) !== -1) {
// accept pre-defined drag modes i.e. shape drawing features as string
if(
fullLayout._has('mapbox') || // draw shapes in paper coordinate (could be improved in future to support data coordinate, when there is no pitch)
fullLayout._has('cartesian') // draw shapes in data coordinate
) {
dragModeGroup.push(b);
}
} else if(b === 'togglespikelines') {
enableHover('toggleSpikelines');
} else if(b === 'togglehover') {
enableHover('toggleHover');
} else if(b === 'hovercompare') {
enableHover('hoverCompareCartesian');
} else if(b === 'hoverclosest') {
enableHover('hoverClosestCartesian');
enableHover('hoverClosestGeo');
enableHover('hoverClosest3d');
enableHover('hoverClosestGl2d');
enableHover('hoverClosestPie');
} else if(b === 'v1hovermode') {
enableHover('toggleHover');
enableHover('hoverClosestCartesian');
enableHover('hoverCompareCartesian');
enableHover('hoverClosestGeo');
enableHover('hoverClosest3d');
enableHover('hoverClosestGl2d');
enableHover('hoverClosestPie');
}
} else newList.push(b);
}
buttonsToAdd = newList;
}
addGroup(dragModeGroup);
addGroup(zoomGroup.concat(resetGroup));
addGroup(enabledHoverGroup);
return appendButtonsToGroups(groups, buttonsToAdd);
}
function areAllAxesFixed(fullLayout) {
var axList = axisIds.list({_fullLayout: fullLayout}, null, true);
for(var i = 0; i < axList.length; i++) {
if(!axList[i].fixedrange) {
return false;
}
}
return true;
}
// look for traces that support selection
// to be updated as we add more selectPoints handlers
function isSelectable(fullData) {
var selectable = false;
for(var i = 0; i < fullData.length; i++) {
if(selectable) break;
var trace = fullData[i];
if(!trace._module || !trace._module.selectPoints) continue;
if(Registry.traceIs(trace, 'scatter-like')) {
if(scatterSubTypes.hasMarkers(trace) || scatterSubTypes.hasText(trace)) {
selectable = true;
}
} else if(Registry.traceIs(trace, 'box-violin')) {
if(trace.boxpoints === 'all' || trace.points === 'all') {
selectable = true;
}
} else {
// assume that in general if the trace module has selectPoints,
// then it's selectable. Scatter is an exception to this because it must
// have markers or text, not just be a scatter type.
selectable = true;
}
}
return selectable;
}
// check whether all trace are 'noHover'
function hasNoHover(fullData) {
for(var i = 0; i < fullData.length; i++) {
if(!Registry.traceIs(fullData[i], 'noHover')) return false;
}
return true;
}
function appendButtonsToGroups(groups, buttons) {
if(buttons.length) {
if(Array.isArray(buttons[0])) {
for(var i = 0; i < buttons.length; i++) {
groups.push(buttons[i]);
}
} else groups.push(buttons);
}
return groups;
}
// fill in custom buttons referring to default mode bar buttons
function fillCustomButton(customButtons) {
for(var i = 0; i < customButtons.length; i++) {
var buttonGroup = customButtons[i];
for(var j = 0; j < buttonGroup.length; j++) {
var button = buttonGroup[j];
if(typeof button === 'string') {
if(modeBarButtons[button] !== undefined) {
customButtons[i][j] = modeBarButtons[button];
} else {
throw new Error([
'*modeBarButtons* configuration options',
'invalid button name'
].join(' '));
}
}
}
}
return customButtons;
}