Skip to content

Commit 22a6f3b

Browse files
committed
add update method to Modebar:
- use it in manageModebar to update buttons based on plot type and config arguments
1 parent a320f29 commit 22a6f3b

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

src/components/modebar/index.js

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,65 @@ var buttonsConfig = require('./buttons_config');
2525
* @Param {object} opts.graphInfo primary plot object containing data and layout
2626
*/
2727
function ModeBar(opts) {
28-
var _this = this;
29-
3028
this._snapshotInProgress = false;
31-
this.graphInfo = opts.graphInfo;
29+
this.container = opts.container;
3230
this.element = document.createElement('div');
3331

34-
if(this.graphInfo._context.displayModeBar === 'hover') {
32+
this.update(opts.graphInfo, opts.buttons);
33+
34+
this.container.appendChild(this.element);
35+
}
36+
37+
var proto = ModeBar.prototype;
38+
39+
/**
40+
* Update modebar (buttons and logo)
41+
*
42+
* @param {object} graphInfo primary plot object containing data and layout
43+
* @param {array of arrays} buttons nested arrays of grouped buttons to initialize
44+
*
45+
*/
46+
proto.update = function(graphInfo, buttons) {
47+
this.graphInfo = graphInfo;
48+
49+
var context = this.graphInfo._context;
50+
51+
if(context.displayModeBar === 'hover') {
3552
this.element.className = 'modebar modebar--hover';
36-
} else {
37-
this.element.className = 'modebar';
3853
}
54+
else this.element.className = 'modebar';
55+
56+
var needsNewButtons = !this.hasButtons(buttons),
57+
needsNewLogo = (this.hasLogo !== context.displaylogo);
58+
59+
if(needsNewButtons || needsNewLogo) {
60+
this.removeAllButtons();
61+
62+
this.updateButtons(buttons);
63+
64+
if(context.displaylogo) {
65+
this.element.appendChild(this.getLogo());
66+
this.hasLogo = true;
67+
}
68+
}
69+
70+
this.updateActiveButton();
71+
};
72+
73+
proto.updateButtons = function(buttons) {
74+
var _this = this;
3975

40-
this.buttons = opts.buttons;
76+
this.buttons = buttons;
4177
this.buttonElements = [];
4278

43-
this.buttons.forEach( function (buttonGroup) {
79+
this.buttons.forEach(function(buttonGroup) {
4480
var group = _this.createGroup();
4581

46-
buttonGroup.forEach( function (buttonName) {
47-
var buttonConfig = modebarConfig[buttonName];
82+
buttonGroup.forEach(function(buttonName) {
83+
var buttonConfig = buttonsConfig[buttonName];
4884

4985
if (!buttonConfig) {
50-
throw new Error(buttonName + ' not specfied in modebar configuration');
86+
throw new Error(buttonName + 'not specfied in modebar configuration');
5187
}
5288

5389
var button = _this.createButton(buttonConfig);
@@ -58,17 +94,7 @@ function ModeBar(opts) {
5894

5995
_this.element.appendChild(group);
6096
});
61-
62-
if (this.graphInfo._context.displaylogo) {
63-
this.element.appendChild(this.getLogo());
64-
}
65-
66-
opts.container.appendChild(this.element);
67-
68-
this.updateActiveButton();
69-
}
70-
71-
var proto = ModeBar.prototype;
97+
};
7298

7399
/**
74100
* Empty div for containing a group of buttons

0 commit comments

Comments
 (0)