@@ -25,29 +25,65 @@ var buttonsConfig = require('./buttons_config');
25
25
* @Param {object} opts.graphInfo primary plot object containing data and layout
26
26
*/
27
27
function ModeBar ( opts ) {
28
- var _this = this ;
29
-
30
28
this . _snapshotInProgress = false ;
31
- this . graphInfo = opts . graphInfo ;
29
+ this . container = opts . container ;
32
30
this . element = document . createElement ( 'div' ) ;
33
31
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' ) {
35
52
this . element . className = 'modebar modebar--hover' ;
36
- } else {
37
- this . element . className = 'modebar' ;
38
53
}
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 ;
39
75
40
- this . buttons = opts . buttons ;
76
+ this . buttons = buttons ;
41
77
this . buttonElements = [ ] ;
42
78
43
- this . buttons . forEach ( function ( buttonGroup ) {
79
+ this . buttons . forEach ( function ( buttonGroup ) {
44
80
var group = _this . createGroup ( ) ;
45
81
46
- buttonGroup . forEach ( function ( buttonName ) {
47
- var buttonConfig = modebarConfig [ buttonName ] ;
82
+ buttonGroup . forEach ( function ( buttonName ) {
83
+ var buttonConfig = buttonsConfig [ buttonName ] ;
48
84
49
85
if ( ! buttonConfig ) {
50
- throw new Error ( buttonName + ' not specfied in modebar configuration' ) ;
86
+ throw new Error ( buttonName + 'not specfied in modebar configuration' ) ;
51
87
}
52
88
53
89
var button = _this . createButton ( buttonConfig ) ;
@@ -58,17 +94,7 @@ function ModeBar(opts) {
58
94
59
95
_this . element . appendChild ( group ) ;
60
96
} ) ;
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
+ } ;
72
98
73
99
/**
74
100
* Empty div for containing a group of buttons
0 commit comments