Show More
@@ -0,0 +1,48 b'' | |||||
|
1 | ||||
|
2 | require(["notebook/js/widget"], function(){ | |||
|
3 | ||||
|
4 | var BoolWidgetModel = IPython.WidgetModel.extend({}); | |||
|
5 | IPython.notebook.widget_manager.register_widget_model('BoolWidgetModel', BoolWidgetModel); | |||
|
6 | ||||
|
7 | var CheckboxView = IPython.WidgetView.extend({ | |||
|
8 | ||||
|
9 | // Called when view is rendered. | |||
|
10 | render : function(){ | |||
|
11 | this.$el | |||
|
12 | .html('') | |||
|
13 | .addClass(this.model.comm.comm_id); | |||
|
14 | ||||
|
15 | var $label = $('<label />') | |||
|
16 | .addClass('checkbox') | |||
|
17 | .appendTo(this.$el); | |||
|
18 | this.$checkbox = $('<input />') | |||
|
19 | .attr('type', 'checkbox') | |||
|
20 | .appendTo($label); | |||
|
21 | this.$checkbox_label = $('<div />') | |||
|
22 | .appendTo($label); | |||
|
23 | ||||
|
24 | this.update(); // Set defaults. | |||
|
25 | }, | |||
|
26 | ||||
|
27 | // Handles: Backend -> Frontend Sync | |||
|
28 | // Frontent -> Frontend Sync | |||
|
29 | update : function(){ | |||
|
30 | if (!this.user_invoked_update) { | |||
|
31 | this.$checkbox.prop('checked', this.model.get('value')); | |||
|
32 | this.$checkbox_label.html(this.model.get('description')); | |||
|
33 | } | |||
|
34 | }, | |||
|
35 | ||||
|
36 | events: {"change input" : "handleChanged"}, | |||
|
37 | ||||
|
38 | // Handles and validates user input. | |||
|
39 | handleChanged: function(e) { | |||
|
40 | this.user_invoked_update = true; | |||
|
41 | this.model.set('value', $(e.target).prop('checked')); | |||
|
42 | this.model.apply(this); | |||
|
43 | this.user_invoked_update = false; | |||
|
44 | }, | |||
|
45 | }); | |||
|
46 | ||||
|
47 | IPython.notebook.widget_manager.register_widget_view('CheckboxView', CheckboxView); | |||
|
48 | }); |
@@ -0,0 +1,15 b'' | |||||
|
1 | import os | |||
|
2 | ||||
|
3 | from base import Widget | |||
|
4 | from IPython.utils.traitlets import Unicode, Bool, List | |||
|
5 | ||||
|
6 | class BoolWidget(Widget): | |||
|
7 | target_name = Unicode('BoolWidgetModel') | |||
|
8 | default_view_name = Unicode('CheckboxView') | |||
|
9 | js_requirements = List(["static/notebook/js/widgets/bool.js"]) | |||
|
10 | _keys = ['value', 'description', 'disabled'] | |||
|
11 | ||||
|
12 | value = Bool(False) | |||
|
13 | description = Unicode('') # Description of the boolean (label). | |||
|
14 | disabled = Bool(False) # Enable or disable user changes | |||
|
15 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now