-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathmanage.js
177 lines (146 loc) · 4.7 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
/**
* Copyright 2012-2015, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('../../plotly');
var createModeBar = require('./');
var modeBarButtons = require('./buttons');
/**
* 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,
context = gd._context,
modeBar = fullLayout._modeBar;
if(!context.displayModeBar) {
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 {
buttonGroups = getButtonGroups(
fullLayout,
context.modeBarButtonsToRemove,
context.modeBarButtonsToAdd
);
}
if(modeBar) modeBar.update(gd, buttonGroups);
else fullLayout._modeBar = createModeBar(gd, buttonGroups);
};
// logic behind which buttons are displayed by default
function getButtonGroups(fullLayout, buttonsToRemove, buttonsToAdd) {
var groups = [];
function addGroup(newGroup) {
var out = [];
for(var i = 0; i < newGroup.length; i++) {
var button = newGroup[i];
if(buttonsToRemove.indexOf(button) !== -1) continue;
out.push(modeBarButtons[button]);
}
groups.push(out);
}
// buttons common to all plot types
addGroup(['toImage', 'sendDataToCloud']);
if(fullLayout._hasGL3D) {
addGroup(['zoom3d', 'pan3d', 'orbitRotation', 'tableRotation']);
addGroup(['resetCameraDefault3d', 'resetCameraLastSave3d']);
addGroup(['hoverClosest3d']);
}
if(fullLayout._hasGeo) {
addGroup(['zoomInGeo', 'zoomOutGeo', 'resetGeo']);
addGroup(['hoverClosestGeo']);
}
var hasCartesian = fullLayout._hasCartesian,
hasGL2D = fullLayout._hasGL2D,
allAxesFixed = areAllAxesFixed(fullLayout),
dragModeGroup = [];
if((hasCartesian || hasGL2D) && !allAxesFixed) {
dragModeGroup = ['zoom2d', 'pan2d'];
}
if(hasCartesian) {
dragModeGroup.push('select2d');
dragModeGroup.push('lasso2d');
}
if(dragModeGroup.length) addGroup(dragModeGroup);
if((hasCartesian || hasGL2D) && !allAxesFixed) {
addGroup(['zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d']);
}
if(hasCartesian) {
addGroup(['hoverClosestCartesian', 'hoverCompareCartesian']);
}
if(hasGL2D) {
addGroup(['hoverClosestGl2d']);
}
if(fullLayout._hasPie) {
addGroup(['hoverClosestPie']);
}
// append buttonsToAdd to the groups
if(buttonsToAdd.length) {
if(Array.isArray(buttonsToAdd[0])) {
for(var i = 0; i < buttonsToAdd.length; i++) {
groups.push(buttonsToAdd[i]);
}
}
else groups.push(buttonsToAdd);
}
return groups;
}
function areAllAxesFixed(fullLayout) {
var axList = Plotly.Axes.list({_fullLayout: fullLayout}, null, true);
var allFixed = true;
for(var i = 0; i < axList.length; i++) {
if(!axList[i].fixedrange) {
allFixed = false;
break;
}
}
return allFixed;
}
// 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;
}