##// END OF EJS Templates
Tooltip on toggle button
Sylvain Corlay -
Show More
@@ -1,140 +1,141 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 "widgets/js/widget",
5 "widgets/js/widget",
6 "jquery",
6 "jquery",
7 "bootstrap",
7 "bootstrap",
8 ], function(widget, $){
8 ], function(widget, $){
9
9
10 var CheckboxView = widget.DOMWidgetView.extend({
10 var CheckboxView = widget.DOMWidgetView.extend({
11 render : function(){
11 render : function(){
12 // Called when view is rendered.
12 // Called when view is rendered.
13 this.$el
13 this.$el
14 .addClass('widget-hbox widget-checkbox');
14 .addClass('widget-hbox widget-checkbox');
15 this.$label = $('<div />')
15 this.$label = $('<div />')
16 .addClass('widget-label')
16 .addClass('widget-label')
17 .appendTo(this.$el)
17 .appendTo(this.$el)
18 .hide();
18 .hide();
19 this.$checkbox = $('<input />')
19 this.$checkbox = $('<input />')
20 .attr('type', 'checkbox')
20 .attr('type', 'checkbox')
21 .appendTo(this.$el)
21 .appendTo(this.$el)
22 .click($.proxy(this.handle_click, this));
22 .click($.proxy(this.handle_click, this));
23
23
24 this.update(); // Set defaults.
24 this.update(); // Set defaults.
25 },
25 },
26
26
27 update_attr: function(name, value) {
27 update_attr: function(name, value) {
28 // Set a css attr of the widget view.
28 // Set a css attr of the widget view.
29 this.$checkbox.css(name, value);
29 this.$checkbox.css(name, value);
30 },
30 },
31
31
32 handle_click: function() {
32 handle_click: function() {
33 // Handles when the checkbox is clicked.
33 // Handles when the checkbox is clicked.
34
34
35 // Calling model.set will trigger all of the other views of the
35 // Calling model.set will trigger all of the other views of the
36 // model to update.
36 // model to update.
37 var value = this.model.get('value');
37 var value = this.model.get('value');
38 this.model.set('value', ! value, {updated_view: this});
38 this.model.set('value', ! value, {updated_view: this});
39 this.touch();
39 this.touch();
40 },
40 },
41
41
42 update : function(options){
42 update : function(options){
43 // Update the contents of this view
43 // Update the contents of this view
44 //
44 //
45 // Called when the model is changed. The model may have been
45 // Called when the model is changed. The model may have been
46 // changed by another view or by a state update from the back-end.
46 // changed by another view or by a state update from the back-end.
47 this.$checkbox.prop('checked', this.model.get('value'));
47 this.$checkbox.prop('checked', this.model.get('value'));
48
48
49 if (options === undefined || options.updated_view != this) {
49 if (options === undefined || options.updated_view != this) {
50 var disabled = this.model.get('disabled');
50 var disabled = this.model.get('disabled');
51 this.$checkbox.prop('disabled', disabled);
51 this.$checkbox.prop('disabled', disabled);
52
52
53 var description = this.model.get('description');
53 var description = this.model.get('description');
54 if (description.trim().length === 0) {
54 if (description.trim().length === 0) {
55 this.$label.hide();
55 this.$label.hide();
56 } else {
56 } else {
57 this.$label.text(description);
57 this.$label.text(description);
58 MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]);
58 MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]);
59 this.$label.show();
59 this.$label.show();
60 }
60 }
61 }
61 }
62 return CheckboxView.__super__.update.apply(this);
62 return CheckboxView.__super__.update.apply(this);
63 },
63 },
64
64
65 });
65 });
66
66
67
67
68 var ToggleButtonView = widget.DOMWidgetView.extend({
68 var ToggleButtonView = widget.DOMWidgetView.extend({
69 render : function() {
69 render : function() {
70 // Called when view is rendered.
70 // Called when view is rendered.
71 var that = this;
71 var that = this;
72 this.setElement($('<button />')
72 this.setElement($('<button />')
73 .addClass('btn btn-default')
73 .addClass('btn btn-default')
74 .attr('type', 'button')
74 .attr('type', 'button')
75 .on('click', function (e) {
75 .on('click', function (e) {
76 e.preventDefault();
76 e.preventDefault();
77 that.handle_click();
77 that.handle_click();
78 }));
78 }));
79
79 this.$el.attr("data-toggle", "tooltip");
80 this.model.on('change:button_style', function(model, value) {
80 this.model.on('change:button_style', function(model, value) {
81 this.update_button_style();
81 this.update_button_style();
82 }, this);
82 }, this);
83 this.update_button_style('');
83 this.update_button_style('');
84
84
85 this.update(); // Set defaults.
85 this.update(); // Set defaults.
86 },
86 },
87
87
88 update_button_style: function(previous_trait_value) {
88 update_button_style: function(previous_trait_value) {
89 var class_map = {
89 var class_map = {
90 primary: ['btn-primary'],
90 primary: ['btn-primary'],
91 success: ['btn-success'],
91 success: ['btn-success'],
92 info: ['btn-info'],
92 info: ['btn-info'],
93 warning: ['btn-warning'],
93 warning: ['btn-warning'],
94 danger: ['btn-danger']
94 danger: ['btn-danger']
95 };
95 };
96 this.update_mapped_classes(class_map, 'button_style', previous_trait_value);
96 this.update_mapped_classes(class_map, 'button_style', previous_trait_value);
97 },
97 },
98
98
99 update : function(options){
99 update : function(options){
100 // Update the contents of this view
100 // Update the contents of this view
101 //
101 //
102 // Called when the model is changed. The model may have been
102 // Called when the model is changed. The model may have been
103 // changed by another view or by a state update from the back-end.
103 // changed by another view or by a state update from the back-end.
104 if (this.model.get('value')) {
104 if (this.model.get('value')) {
105 this.$el.addClass('active');
105 this.$el.addClass('active');
106 } else {
106 } else {
107 this.$el.removeClass('active');
107 this.$el.removeClass('active');
108 }
108 }
109
109
110 if (options === undefined || options.updated_view != this) {
110 if (options === undefined || options.updated_view != this) {
111
111
112 var disabled = this.model.get('disabled');
112 var disabled = this.model.get('disabled');
113 this.$el.prop('disabled', disabled);
113 this.$el.prop('disabled', disabled);
114
114
115 var description = this.model.get('description');
115 var description = this.model.get('description');
116 this.$el.attr("title", this.model.get("tooltip"));
116 if (description.trim().length === 0) {
117 if (description.trim().length === 0) {
117 this.$el.html("&nbsp;"); // Preserve button height
118 this.$el.html("&nbsp;"); // Preserve button height
118 } else {
119 } else {
119 this.$el.text(description);
120 this.$el.text(description);
120 }
121 }
121 }
122 }
122 return ToggleButtonView.__super__.update.apply(this);
123 return ToggleButtonView.__super__.update.apply(this);
123 },
124 },
124
125
125 handle_click: function(e) {
126 handle_click: function(e) {
126 // Handles and validates user input.
127 // Handles and validates user input.
127
128
128 // Calling model.set will trigger all of the other views of the
129 // Calling model.set will trigger all of the other views of the
129 // model to update.
130 // model to update.
130 var value = this.model.get('value');
131 var value = this.model.get('value');
131 this.model.set('value', ! value, {updated_view: this});
132 this.model.set('value', ! value, {updated_view: this});
132 this.touch();
133 this.touch();
133 },
134 },
134 });
135 });
135
136
136 return {
137 return {
137 'CheckboxView': CheckboxView,
138 'CheckboxView': CheckboxView,
138 'ToggleButtonView': ToggleButtonView,
139 'ToggleButtonView': ToggleButtonView,
139 };
140 };
140 });
141 });
@@ -1,50 +1,51 b''
1 """Bool class.
1 """Bool class.
2
2
3 Represents a boolean using a widget.
3 Represents a boolean using a widget.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from .widget import DOMWidget, register
16 from .widget import DOMWidget, register
17 from IPython.utils.traitlets import Unicode, Bool, CaselessStrEnum
17 from IPython.utils.traitlets import Unicode, Bool, CaselessStrEnum
18 from IPython.utils.warn import DeprecatedClass
18 from IPython.utils.warn import DeprecatedClass
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Classes
21 # Classes
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 class _Bool(DOMWidget):
23 class _Bool(DOMWidget):
24 """A base class for creating widgets that represent booleans."""
24 """A base class for creating widgets that represent booleans."""
25 value = Bool(False, help="Bool value", sync=True)
25 value = Bool(False, help="Bool value", sync=True)
26 description = Unicode('', help="Description of the boolean (label).", sync=True)
26 description = Unicode('', help="Description of the boolean (label).", sync=True)
27 disabled = Bool(False, help="Enable or disable user changes.", sync=True)
27 disabled = Bool(False, help="Enable or disable user changes.", sync=True)
28
28
29
29
30 @register('IPython.Checkbox')
30 @register('IPython.Checkbox')
31 class Checkbox(_Bool):
31 class Checkbox(_Bool):
32 """Displays a boolean `value`."""
32 """Displays a boolean `value`."""
33 _view_name = Unicode('CheckboxView', sync=True)
33 _view_name = Unicode('CheckboxView', sync=True)
34
34
35
35
36 @register('IPython.ToggleButton')
36 @register('IPython.ToggleButton')
37 class ToggleButton(_Bool):
37 class ToggleButton(_Bool):
38 """Displays a boolean `value`."""
38 """Displays a boolean `value`."""
39
39
40 _view_name = Unicode('ToggleButtonView', sync=True)
40 _view_name = Unicode('ToggleButtonView', sync=True)
41 tooltip = Unicode(help="Tooltip caption of the toggle button.", sync=True)
41
42
42 button_style = CaselessStrEnum(
43 button_style = CaselessStrEnum(
43 values=['primary', 'success', 'info', 'warning', 'danger', ''],
44 values=['primary', 'success', 'info', 'warning', 'danger', ''],
44 default_value='', allow_none=True, sync=True, help="""Use a
45 default_value='', allow_none=True, sync=True, help="""Use a
45 predefined styling for the button.""")
46 predefined styling for the button.""")
46
47
47
48
48 # Remove in IPython 4.0
49 # Remove in IPython 4.0
49 CheckboxWidget = DeprecatedClass(Checkbox, 'CheckboxWidget')
50 CheckboxWidget = DeprecatedClass(Checkbox, 'CheckboxWidget')
50 ToggleButtonWidget = DeprecatedClass(ToggleButton, 'ToggleButtonWidget')
51 ToggleButtonWidget = DeprecatedClass(ToggleButton, 'ToggleButtonWidget')
General Comments 0
You need to be logged in to leave comments. Login now