@@ -14,7 +14,8 @@ var ToolbarModel = widgets.DOMWidgetModel.extend({
1414 _view_module_version : '^' + version ,
1515 toolitems : [ ] ,
1616 orientation : 'vertical' ,
17- button_style : ''
17+ button_style : '' ,
18+ _current_action : '' ,
1819 } ) ;
1920 }
2021} ) ;
@@ -30,8 +31,6 @@ var ToolbarView = widgets.DOMWidgetView.extend({
3031 create_toolbar : function ( ) {
3132 var toolbar_items = this . model . get ( 'toolitems' ) ;
3233
33- this . current_action = '' ;
34-
3534 this . toggle_button = document . createElement ( 'button' ) ;
3635
3736 this . toggle_button . classList = 'jupyter-matplotlib-button jupyter-widgets jupyter-button' ;
@@ -48,7 +47,7 @@ var ToolbarView = widgets.DOMWidgetView.extend({
4847 this . toolbar = document . createElement ( 'div' ) ;
4948 this . toolbar . classList = 'widget-container widget-box' ;
5049 this . el . appendChild ( this . toolbar ) ;
51- this . buttons = [ this . toggle_button ] ;
50+ this . buttons = { 'toggle_button' : this . toggle_button } ;
5251
5352 for ( var toolbar_ind in toolbar_items ) {
5453 var name = toolbar_items [ toolbar_ind ] [ 0 ] ;
@@ -68,7 +67,7 @@ var ToolbarView = widgets.DOMWidgetView.extend({
6867 icon . classList = 'center fa fa-' + image ;
6968 button . appendChild ( icon ) ;
7069
71- this . buttons . push ( button ) ;
70+ this . buttons [ method_name ] = button ;
7271
7372 this . toolbar . appendChild ( button ) ;
7473 }
@@ -93,25 +92,15 @@ var ToolbarView = widgets.DOMWidgetView.extend({
9392 var that = this ;
9493
9594 return function ( event ) {
96- var target = event . target ;
97-
9895 // Special case for pan and zoom as they are toggle buttons
9996 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' , '' ) ;
10799 }
108100 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 ) ;
114102 }
103+ that . model . save_changes ( ) ;
115104 }
116105
117106 var message = {
@@ -124,8 +113,6 @@ var ToolbarView = widgets.DOMWidgetView.extend({
124113 } ,
125114
126115 set_buttons_style : function ( ) {
127- var that = this ;
128-
129116 var class_map = {
130117 primary : [ 'mod-primary' ] ,
131118 success : [ 'mod-success' ] ,
@@ -134,16 +121,23 @@ var ToolbarView = widgets.DOMWidgetView.extend({
134121 danger : [ 'mod-danger' ]
135122 } ;
136123
137- this . buttons . forEach ( function ( button ) {
124+ for ( var name in this . buttons ) {
125+ var button = this . buttons [ name ] ;
126+
138127 for ( var class_name in class_map ) {
139128 button . classList . remove ( class_map [ class_name ] ) ;
140129 }
130+ button . classList . remove ( 'mod-active' ) ;
141131
142- var class_name = that . model . get ( 'button_style' ) ;
132+ var class_name = this . model . get ( 'button_style' ) ;
143133 if ( class_name != '' ) {
144134 button . classList . add ( class_map [ class_name ] ) ;
145135 }
146- } ) ;
136+
137+ if ( name == this . model . get ( '_current_action' ) ) {
138+ button . classList . add ( 'mod-active' ) ;
139+ }
140+ }
147141 } ,
148142
149143 toggle_interaction : function ( ) {
@@ -154,7 +148,7 @@ var ToolbarView = widgets.DOMWidgetView.extend({
154148
155149 model_events : function ( ) {
156150 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 ) ) ;
158152 } ,
159153
160154 update_orientation : function ( ) {
0 commit comments