Skip to content

Commit b281960

Browse files
Merge pull request #107 from martinRenou/toolbar_style
Add support for changing button style
2 parents 8a0bae1 + 87e32ba commit b281960

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

ipympl/backend_nbagg.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from ipywidgets import DOMWidget, widget_serialization
1111
from traitlets import (
12-
Unicode, Bool, Float, List, Any, Instance, Enum,
12+
Unicode, Bool, Float, List, Any, Instance, CaselessStrEnum, Enum,
1313
default
1414
)
1515

@@ -93,7 +93,9 @@ class Toolbar(DOMWidget, NavigationToolbar2WebAgg):
9393

9494
toolitems = List().tag(sync=True)
9595
orientation = Enum(['horizontal', 'vertical'], default_value='vertical').tag(sync=True)
96-
# button style?
96+
button_style = CaselessStrEnum(
97+
values=['primary', 'success', 'info', 'warning', 'danger', ''], default_value='',
98+
help="""Use a predefined styling for the button.""").tag(sync=True)
9799

98100
def __init__(self, canvas, *args, **kwargs):
99101
DOMWidget.__init__(self, *args, **kwargs)

js/src/toolbar_widget.js

+45-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ var ToolbarModel = widgets.DOMWidgetModel.extend({
1313
_model_module_version: '^'+ version,
1414
_view_module_version: '^' + version,
1515
toolitems: [],
16-
orientation: 'vertical'
16+
orientation: 'vertical',
17+
button_style: ''
1718
});
1819
}
1920
});
@@ -52,6 +53,8 @@ var ToolbarView = widgets.DOMWidgetView.extend({
5253
this.toolbar.classList = this.get_container_class();
5354
this.toolbar_container.appendChild(this.toolbar);
5455

56+
this.buttons = [this.toggle_button];
57+
5558
for(var toolbar_ind in toolbar_items) {
5659
var name = toolbar_items[toolbar_ind][0];
5760
var tooltip = toolbar_items[toolbar_ind][1];
@@ -70,8 +73,12 @@ var ToolbarView = widgets.DOMWidgetView.extend({
7073
icon.classList = 'center fa fa-' + image;
7174
button.appendChild(icon);
7275

76+
this.buttons.push(button);
77+
7378
this.toolbar.appendChild(button);
7479
}
80+
81+
this.set_buttons_style();
7582
},
7683

7784
get_container_class: function() {
@@ -84,27 +91,27 @@ var ToolbarView = widgets.DOMWidgetView.extend({
8491
},
8592

8693
toolbar_button_onclick: function(name) {
87-
var toolbar_widget = this;
94+
var that = this;
8895

8996
return function(event) {
90-
var button = event.target;
97+
var target = event.target;
9198

9299
// Special case for pan and zoom as they are toggle buttons
93100
if (name == 'pan' || name == 'zoom') {
94-
if (toolbar_widget.current_action == '') {
95-
toolbar_widget.current_action = name;
96-
button.classList.add('mod-active');
101+
if (that.current_action == '') {
102+
that.current_action = name;
103+
target.classList.add('mod-active');
97104
}
98-
else if (toolbar_widget.current_action == name) {
99-
toolbar_widget.current_action = '';
100-
button.classList.remove('mod-active');
105+
else if (that.current_action == name) {
106+
that.current_action = '';
107+
target.classList.remove('mod-active');
101108
}
102109
else {
103-
toolbar_widget.current_action = name;
104-
[].forEach.call(toolbar_widget.toolbar.children, function(child) {
105-
child.classList.remove('mod-active');
110+
that.current_action = name;
111+
that.buttons.forEach(function(button) {
112+
button.classList.remove('mod-active');
106113
});
107-
button.classList.add('mod-active');
114+
target.classList.add('mod-active');
108115
}
109116
}
110117

@@ -113,8 +120,31 @@ var ToolbarView = widgets.DOMWidgetView.extend({
113120
'name': name
114121
};
115122

116-
toolbar_widget.send(message);
123+
that.send(message);
124+
};
125+
},
126+
127+
set_buttons_style: function() {
128+
var that = this;
129+
130+
var class_map = {
131+
primary: ['mod-primary'],
132+
success: ['mod-success'],
133+
info: ['mod-info'],
134+
warning: ['mod-warning'],
135+
danger: ['mod-danger']
117136
};
137+
138+
this.buttons.forEach(function(button) {
139+
for (var class_name in class_map) {
140+
button.classList.remove(class_map[class_name]);
141+
}
142+
143+
var class_name = that.model.get('button_style');
144+
if (class_name != '') {
145+
button.classList.add(class_map[class_name]);
146+
}
147+
});
118148
},
119149

120150
toggle_interaction: function() {
@@ -125,6 +155,7 @@ var ToolbarView = widgets.DOMWidgetView.extend({
125155

126156
model_events: function() {
127157
this.model.on('change:orientation', this.update_orientation.bind(this));
158+
this.model.on('change:button_style', this.set_buttons_style.bind(this));
128159
},
129160

130161
update_orientation: function() {

0 commit comments

Comments
 (0)