##// END OF EJS Templates
Added toggle button view
Jonathan Frederic -
Show More
@@ -1,48 +1,92
1 1
2 2 require(["notebook/js/widget"], function(){
3 3
4 4 var BoolWidgetModel = IPython.WidgetModel.extend({});
5 5 IPython.notebook.widget_manager.register_widget_model('BoolWidgetModel', BoolWidgetModel);
6 6
7 7 var CheckboxView = IPython.WidgetView.extend({
8 8
9 9 // Called when view is rendered.
10 10 render : function(){
11 11 this.$el
12 12 .html('')
13 13 .addClass(this.model.comm.comm_id);
14 14
15 15 var $label = $('<label />')
16 16 .addClass('checkbox')
17 17 .appendTo(this.$el);
18 18 this.$checkbox = $('<input />')
19 19 .attr('type', 'checkbox')
20 20 .appendTo($label);
21 21 this.$checkbox_label = $('<div />')
22 22 .appendTo($label);
23 23
24 24 this.update(); // Set defaults.
25 25 },
26 26
27 27 // Handles: Backend -> Frontend Sync
28 28 // Frontent -> Frontend Sync
29 29 update : function(){
30 30 if (!this.user_invoked_update) {
31 31 this.$checkbox.prop('checked', this.model.get('value'));
32 32 this.$checkbox_label.html(this.model.get('description'));
33 33 }
34 34 },
35 35
36 36 events: {"change input" : "handleChanged"},
37 37
38 38 // Handles and validates user input.
39 39 handleChanged: function(e) {
40 40 this.user_invoked_update = true;
41 41 this.model.set('value', $(e.target).prop('checked'));
42 42 this.model.apply(this);
43 43 this.user_invoked_update = false;
44 44 },
45 45 });
46 46
47 47 IPython.notebook.widget_manager.register_widget_view('CheckboxView', CheckboxView);
48
49 var ToggleButtonView = IPython.WidgetView.extend({
50
51 // Called when view is rendered.
52 render : function(){
53 this.$el
54 .html('')
55 .addClass(this.model.comm.comm_id);
56
57 this.$button = $('<button />')
58 .addClass('btn')
59 .attr('type', 'button')
60 .attr('data-toggle', 'button')
61 .appendTo(this.$el);
62
63 this.update(); // Set defaults.
64 },
65
66 // Handles: Backend -> Frontend Sync
67 // Frontent -> Frontend Sync
68 update : function(){
69 if (!this.user_invoked_update) {
70 if (this.model.get('value')) {
71 this.$button.addClass('active');
72 } else {
73 this.$button.removeClass('active');
74 }
75 this.$button.html(this.model.get('description'));
76 }
77 },
78
79 events: {"click button" : "handleClick"},
80
81 // Handles and validates user input.
82 handleClick: function(e) {
83 this.user_invoked_update = true;
84 this.model.set('value', ! $(e.target).hasClass('active'));
85 this.model.apply(this);
86 this.user_invoked_update = false;
87 },
88 });
89
90 IPython.notebook.widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView);
91
48 92 });
General Comments 0
You need to be logged in to leave comments. Login now