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