@@ -14,7 +14,8 @@ var ToolbarModel = widgets.DOMWidgetModel.extend({
14
14
_view_module_version : '^' + version ,
15
15
toolitems : [ ] ,
16
16
orientation : 'vertical' ,
17
- button_style : ''
17
+ button_style : '' ,
18
+ _current_action : '' ,
18
19
} ) ;
19
20
}
20
21
} ) ;
@@ -30,8 +31,6 @@ var ToolbarView = widgets.DOMWidgetView.extend({
30
31
create_toolbar : function ( ) {
31
32
var toolbar_items = this . model . get ( 'toolitems' ) ;
32
33
33
- this . current_action = '' ;
34
-
35
34
this . toggle_button = document . createElement ( 'button' ) ;
36
35
37
36
this . toggle_button . classList = 'jupyter-matplotlib-button jupyter-widgets jupyter-button' ;
@@ -48,7 +47,7 @@ var ToolbarView = widgets.DOMWidgetView.extend({
48
47
this . toolbar = document . createElement ( 'div' ) ;
49
48
this . toolbar . classList = 'widget-container widget-box' ;
50
49
this . el . appendChild ( this . toolbar ) ;
51
- this . buttons = [ this . toggle_button ] ;
50
+ this . buttons = { 'toggle_button' : this . toggle_button } ;
52
51
53
52
for ( var toolbar_ind in toolbar_items ) {
54
53
var name = toolbar_items [ toolbar_ind ] [ 0 ] ;
@@ -68,7 +67,7 @@ var ToolbarView = widgets.DOMWidgetView.extend({
68
67
icon . classList = 'center fa fa-' + image ;
69
68
button . appendChild ( icon ) ;
70
69
71
- this . buttons . push ( button ) ;
70
+ this . buttons [ method_name ] = button ;
72
71
73
72
this . toolbar . appendChild ( button ) ;
74
73
}
@@ -93,25 +92,15 @@ var ToolbarView = widgets.DOMWidgetView.extend({
93
92
var that = this ;
94
93
95
94
return function ( event ) {
96
- var target = event . target ;
97
-
98
95
// Special case for pan and zoom as they are toggle buttons
99
96
if ( name == 'pan' || name == 'zoom' ) {
100
- if ( that . current_action == '' ) {
101
- that . current_action = name ;
102
- target . classList . add ( 'mod-active' ) ;
103
- }
104
- else if ( that . current_action == name ) {
105
- that . current_action = '' ;
106
- target . classList . remove ( 'mod-active' ) ;
97
+ if ( that . model . get ( '_current_action' ) == name ) {
98
+ that . model . set ( '_current_action' , '' ) ;
107
99
}
108
100
else {
109
- that . current_action = name ;
110
- that . buttons . forEach ( function ( button ) {
111
- button . classList . remove ( 'mod-active' ) ;
112
- } ) ;
113
- target . classList . add ( 'mod-active' ) ;
101
+ that . model . set ( '_current_action' , name ) ;
114
102
}
103
+ that . model . save_changes ( ) ;
115
104
}
116
105
117
106
var message = {
@@ -124,8 +113,6 @@ var ToolbarView = widgets.DOMWidgetView.extend({
124
113
} ,
125
114
126
115
set_buttons_style : function ( ) {
127
- var that = this ;
128
-
129
116
var class_map = {
130
117
primary : [ 'mod-primary' ] ,
131
118
success : [ 'mod-success' ] ,
@@ -134,16 +121,23 @@ var ToolbarView = widgets.DOMWidgetView.extend({
134
121
danger : [ 'mod-danger' ]
135
122
} ;
136
123
137
- this . buttons . forEach ( function ( button ) {
124
+ for ( var name in this . buttons ) {
125
+ var button = this . buttons [ name ] ;
126
+
138
127
for ( var class_name in class_map ) {
139
128
button . classList . remove ( class_map [ class_name ] ) ;
140
129
}
130
+ button . classList . remove ( 'mod-active' ) ;
141
131
142
- var class_name = that . model . get ( 'button_style' ) ;
132
+ var class_name = this . model . get ( 'button_style' ) ;
143
133
if ( class_name != '' ) {
144
134
button . classList . add ( class_map [ class_name ] ) ;
145
135
}
146
- } ) ;
136
+
137
+ if ( name == this . model . get ( '_current_action' ) ) {
138
+ button . classList . add ( 'mod-active' ) ;
139
+ }
140
+ }
147
141
} ,
148
142
149
143
toggle_interaction : function ( ) {
@@ -154,7 +148,7 @@ var ToolbarView = widgets.DOMWidgetView.extend({
154
148
155
149
model_events : function ( ) {
156
150
this . model . on ( 'change:orientation' , this . update_orientation . bind ( this ) ) ;
157
- this . model . on ( 'change: button_style', this . set_buttons_style . bind ( this ) ) ;
151
+ this . model . on_some_change ( [ ' button_style', '_current_action' ] , this . set_buttons_style . bind ( this ) ) ;
158
152
} ,
159
153
160
154
update_orientation : function ( ) {
0 commit comments