Skip to content

Commit 189e313

Browse files
committed
mv create/update Modebar wrapper in modebar/ :
- manageModebar handles logic around which button should appear on the modebar and how to modebar buttons are grouped.
1 parent 079f747 commit 189e313

File tree

3 files changed

+116
-100
lines changed

3 files changed

+116
-100
lines changed

src/components/modebar/manage.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Copyright 2012-2015, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Plotly = require('../../plotly');
13+
14+
var createModebar = require('./');
15+
var buttonsConfig = require('./buttons_config');
16+
17+
/**
18+
* Modebar wrapper around 'create' and 'update',
19+
* chooses buttons to pass to Modebar constructor based on
20+
* plot type and plot config.
21+
*
22+
* @param {object} gd main plot object
23+
*
24+
*/
25+
module.exports = function manageModebar(gd) {
26+
var fullLayout = gd._fullLayout,
27+
context = gd._context,
28+
modebar = fullLayout._modebar;
29+
30+
if(!context.displayModeBar && modebar) {
31+
modebar.destroy();
32+
delete fullLayout._modebar;
33+
return;
34+
}
35+
36+
var buttons = chooseButtons(fullLayout, context.modebarButtons);
37+
38+
if(modebar) modebar.update(gd, buttons);
39+
else fullLayout._modebar = createModebar(gd, buttons);
40+
};
41+
42+
function chooseButtons(fullLayout) {
43+
var buttons = findButtons('all');
44+
45+
if(fullLayout._hasGL3D) buttons = buttons.concat(findButtons('gl3d'));
46+
47+
if(fullLayout._hasGeo) buttons = buttons.concat(findButtons('geo'));
48+
49+
if(fullLayout._hasCartesian && !areAllAxesFixed(fullLayout)) {
50+
buttons = buttons.concat(findButtons('2d'));
51+
buttons = buttons.concat(findButtons('cartesian'));
52+
}
53+
54+
if(fullLayout._hasGL2D) {
55+
buttons = buttons.concat(findButtons('2d'));
56+
buttons = buttons.concat(findButtons('gl2d'));
57+
}
58+
59+
if(fullLayout._hasPie) buttons = buttons.concat(findButtons('pie'));
60+
61+
buttons = groupButtons(buttons);
62+
63+
return buttons;
64+
}
65+
66+
function findButtons(category, list) {
67+
var buttonNames = Object.keys(buttonsConfig);
68+
var out = [];
69+
70+
for(var i = 0; i < buttonNames.length; i++) {
71+
var buttonName = buttonNames[i];
72+
if(buttonsConfig[buttonName].category === category) out.push(buttonName);
73+
}
74+
75+
return out;
76+
}
77+
78+
function areAllAxesFixed(fullLayout) {
79+
var axList = Plotly.Axes.list({_fullLayout: fullLayout}, null, true);
80+
var allFixed = true;
81+
82+
for(var i = 0; i < axList.length; i++) {
83+
if(!axList[i].fixedrange) {
84+
allFixed = false;
85+
break;
86+
}
87+
}
88+
89+
return allFixed;
90+
}
91+
92+
function groupButtons(buttons) {
93+
var hashObj = {};
94+
var i;
95+
96+
for(i = 0; i < buttons.length; i++) {
97+
var button = buttons[i],
98+
group = buttonsConfig[button].group;
99+
100+
if(hashObj[group] === undefined) hashObj[group] = [button];
101+
else hashObj[group].push(button);
102+
}
103+
104+
var groups = Object.keys(hashObj);
105+
var out = [];
106+
107+
for(i = 0; i < groups.length; i++) {
108+
out.push(hashObj[groups[i]]);
109+
}
110+
111+
return out;
112+
}

src/plot_api/plot_api.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var Plotly = require('../plotly');
1313
var Events = require('../lib/events');
14+
var manageModebar = require('../components/modebar/manage');
1415

1516
var d3 = require('d3');
1617
var m4FromQuat = require('gl-mat4/fromQuat');
@@ -399,11 +400,6 @@ function setPlotContext(gd, config) {
399400
}
400401
});
401402

402-
// cause a remake of the modebar any time we change context
403-
if(gd._fullLayout && gd._fullLayout._modebar) {
404-
delete gd._fullLayout._modebar;
405-
}
406-
407403
// map plot3dPixelRatio to plotGlPixelRatio for backward compatibility
408404
if(config.plot3dPixelRatio && !context.plotGlPixelRatio) {
409405
context.plotGlPixelRatio = context.plot3dPixelRatio;
@@ -2468,8 +2464,9 @@ Plotly.relayout = function relayout(gd, astr, val) {
24682464
return plots.previousPromises(gd);
24692465
});
24702466
}
2467+
24712468
// this is decoupled enough it doesn't need async regardless
2472-
if(domodebar) Plotly.Fx.modeBar(gd);
2469+
if(domodebar) manageModebar(gd);
24732470

24742471
var subplotIds;
24752472
if(doSceneDragmode || domodebar) {
@@ -3006,7 +3003,7 @@ function lsInner(gd) {
30063003

30073004
Plotly.Titles.draw(gd, 'gtitle');
30083005

3009-
Plotly.Fx.modeBar(gd);
3006+
manageModebar(gd);
30103007

30113008
return gd._promises.length && Promise.all(gd._promises);
30123009
}

src/plots/cartesian/graph_interact.js

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,99 +1279,6 @@ fx.click = function(gd,evt){
12791279
}
12801280
};
12811281

1282-
// dragmode and hovermode toolbars
1283-
fx.modeBar = function(gd){
1284-
1285-
function initModebar(){
1286-
1287-
var modebar = new Plotly.ModeBar({
1288-
buttons: buttons,
1289-
container: fullLayout._paperdiv.node(),
1290-
graphInfo: gd
1291-
});
1292-
1293-
if(fullLayout._privateplot) {
1294-
d3.select(modebar.element).append('span')
1295-
.classed('badge-private float--left', true)
1296-
.text('PRIVATE');
1297-
}
1298-
1299-
return modebar;
1300-
}
1301-
1302-
function deleteModebar() {
1303-
Plotly.Lib.removeElement(gd.querySelector('.modebar'));
1304-
}
1305-
1306-
var modebar,
1307-
fullLayout = gd._fullLayout || {};
1308-
1309-
if (!gd._context.displayModeBar) return deleteModebar();
1310-
1311-
var buttons = chooseModebarButtons(fullLayout);
1312-
1313-
if (!fullLayout._modebar){
1314-
deleteModebar();
1315-
fullLayout._modebar = initModebar();
1316-
}
1317-
1318-
modebar = fullLayout._modebar;
1319-
1320-
//if the buttons are different, clean old and init new modebar
1321-
if (!modebar.hasButtons(buttons)) {
1322-
fullLayout._modebar.cleanup();
1323-
fullLayout._modebar = initModebar();
1324-
}
1325-
};
1326-
1327-
function chooseModebarButtons(fullLayout) {
1328-
if(fullLayout._hasGL3D) {
1329-
return [
1330-
['toImage', 'sendDataToCloud'],
1331-
['orbitRotation', 'tableRotation', 'zoom3d', 'pan3d'],
1332-
['resetCameraDefault3d', 'resetCameraLastSave3d'],
1333-
['hoverClosest3d']
1334-
];
1335-
}
1336-
else if(fullLayout._hasGeo) {
1337-
return [
1338-
['toImage', 'sendDataToCloud'],
1339-
['zoomInGeo', 'zoomOutGeo', 'resetGeo'],
1340-
['hoverClosestGeo']
1341-
];
1342-
}
1343-
1344-
var axList = Plotly.Axes.list({_fullLayout: fullLayout}, null, true),
1345-
allFixed = true,
1346-
i,
1347-
buttons;
1348-
1349-
for(i = 0; i < axList.length; i++) {
1350-
if(!axList[i].fixedrange) {
1351-
allFixed = false;
1352-
break;
1353-
}
1354-
}
1355-
1356-
if(allFixed) buttons = [['toImage', 'sendDataToCloud']];
1357-
else buttons = [
1358-
['toImage', 'sendDataToCloud'],
1359-
['zoom2d', 'pan2d'],
1360-
['zoomIn2d', 'zoomOut2d', 'resetScale2d', 'autoScale2d']
1361-
];
1362-
1363-
if(fullLayout._hasCartesian) {
1364-
buttons.push(['hoverClosest2d', 'hoverCompare2d']);
1365-
}
1366-
else if(fullLayout._hasPie) {
1367-
buttons.push(['hoverClosestPie']);
1368-
}
1369-
else if(fullLayout._hasGL2D) {
1370-
buttons.push(['hoverClosestGl2d']);
1371-
}
1372-
1373-
return buttons;
1374-
}
13751282

13761283
// ----------------------------------------------------
13771284
// Axis dragging functions

0 commit comments

Comments
 (0)