##// END OF EJS Templates
Added togglebutton group
Jonathan Frederic -
Show More
@@ -1,128 +1,194
1 var SelectionWidgetModel = IPython.WidgetModel.extend({});
1 var SelectionWidgetModel = IPython.WidgetModel.extend({});
2 IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
2 IPython.notebook.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
3
3
4 var DropdownView = IPython.WidgetView.extend({
4 var DropdownView = IPython.WidgetView.extend({
5
5
6 // Called when view is rendered.
6 // Called when view is rendered.
7 render : function(){
7 render : function(){
8
8
9 this.$el
9 this.$el
10 .html('')
10 .html('')
11 .addClass(this.model.comm.comm_id);
11 .addClass(this.model.comm.comm_id);
12 this.$buttongroup = $('<div />')
12 this.$buttongroup = $('<div />')
13 .addClass('btn-group')
13 .addClass('btn-group')
14 .appendTo(this.$el);
14 .appendTo(this.$el);
15 this.$droplabel = $('<button />')
15 this.$droplabel = $('<button />')
16 .addClass('btn')
16 .addClass('btn')
17 .appendTo(this.$buttongroup);
17 .appendTo(this.$buttongroup);
18 this.$dropbutton = $('<button />')
18 this.$dropbutton = $('<button />')
19 .addClass('btn')
19 .addClass('btn')
20 .addClass('dropdown-toggle')
20 .addClass('dropdown-toggle')
21 .attr('data-toggle', 'dropdown')
21 .attr('data-toggle', 'dropdown')
22 .html('<span class="caret"></span>')
22 .html('<span class="caret"></span>')
23 .appendTo(this.$buttongroup);
23 .appendTo(this.$buttongroup);
24 this.$droplist = $('<ul />')
24 this.$droplist = $('<ul />')
25 .addClass('dropdown-menu')
25 .addClass('dropdown-menu')
26 .appendTo(this.$buttongroup);
26 .appendTo(this.$buttongroup);
27
27
28 // Set defaults.
28 // Set defaults.
29 this.update();
29 this.update();
30 },
30 },
31
31
32 // Handles: Backend -> Frontend Sync
32 // Handles: Backend -> Frontend Sync
33 // Frontent -> Frontend Sync
33 // Frontent -> Frontend Sync
34 update : function(){
34 update : function(){
35 this.$droplabel.html(this.model.get('value'));
35 this.$droplabel.html(this.model.get('value'));
36
36
37 var items = this.model.get('values');
37 var items = this.model.get('values');
38 this.$droplist.html('');
38 this.$droplist.html('');
39 for (var index in items) {
39 for (var index in items) {
40 var that = this;
40 var that = this;
41 var item_button = $('<a href="#"/>')
41 var item_button = $('<a href="#"/>')
42 .html(items[index])
42 .html(items[index])
43 .on('click', function(e){
43 .on('click', function(e){
44 that.model.set('value', $(e.target).html(), this );
44 that.model.set('value', $(e.target).html(), this );
45 })
45 })
46
46
47 this.$droplist.append($('<li />').append(item_button))
47 this.$droplist.append($('<li />').append(item_button))
48 }
48 }
49
49
50 if (this.model.get('disabled')) {
50 if (this.model.get('disabled')) {
51 this.$buttongroup.attr('disabled','disabled');
51 this.$buttongroup.attr('disabled','disabled');
52 this.$droplabel.attr('disabled','disabled');
52 this.$droplabel.attr('disabled','disabled');
53 this.$dropbutton.attr('disabled','disabled');
53 this.$dropbutton.attr('disabled','disabled');
54 this.$droplist.attr('disabled','disabled');
54 this.$droplist.attr('disabled','disabled');
55 } else {
55 } else {
56 this.$buttongroup.removeAttr('disabled');
56 this.$buttongroup.removeAttr('disabled');
57 this.$droplabel.removeAttr('disabled');
57 this.$droplabel.removeAttr('disabled');
58 this.$dropbutton.removeAttr('disabled');
58 this.$dropbutton.removeAttr('disabled');
59 this.$droplist.removeAttr('disabled');
59 this.$droplist.removeAttr('disabled');
60 }
60 }
61 },
61 },
62
62
63 });
63 });
64
64
65 IPython.notebook.widget_manager.register_widget_view('DropdownView', DropdownView);
65 IPython.notebook.widget_manager.register_widget_view('DropdownView', DropdownView);
66
66 var RadioButtonView = IPython.WidgetView.extend({
67 var RadioButtonView = IPython.WidgetView.extend({
67
68
68 // Called when view is rendered.
69 // Called when view is rendered.
69 render : function(){
70 render : function(){
70 this.$el
71 this.$el
71 .html('')
72 .html('')
72 .addClass(this.model.comm.comm_id);
73 .addClass(this.model.comm.comm_id);
73 this.update();
74 this.update();
74 },
75 },
75
76
76 // Handles: Backend -> Frontend Sync
77 // Handles: Backend -> Frontend Sync
77 // Frontent -> Frontend Sync
78 // Frontent -> Frontend Sync
78 update : function(){
79 update : function(){
79
80
80 // Add missing items to the DOM.
81 // Add missing items to the DOM.
81 var items = this.model.get('values');
82 var items = this.model.get('values');
82 for (var index in items) {
83 for (var index in items) {
83 var item_query = ' :input[value="' + items[index] + '"]';
84 var item_query = ' :input[value="' + items[index] + '"]';
84 if (this.$el.find(item_query).length == 0) {
85 if (this.$el.find(item_query).length == 0) {
85 var $label = $('<label />')
86 var $label = $('<label />')
86 .addClass('radio')
87 .addClass('radio')
87 .html(items[index])
88 .html(items[index])
88 .appendTo(this.$el);
89 .appendTo(this.$el);
89
90
90 var that = this;
91 var that = this;
91 $('<input />')
92 $('<input />')
92 .attr('type', 'radio')
93 .attr('type', 'radio')
93 .addClass(this.model)
94 .addClass(this.model)
94 .val(items[index])
95 .val(items[index])
95 .prependTo($label)
96 .prependTo($label)
96 .on('click', function(e){
97 .on('click', function(e){
97 that.model.set('value', $(e.target).val(), this);
98 that.model.set('value', $(e.target).val(), this);
98 that.model.apply();
99 that.model.apply();
99 });
100 });
100 }
101 }
101
102
102 if (this.model.get('value') == items[index]) {
103 if (this.model.get('value') == items[index]) {
103 this.$el.find(item_query).prop('checked', true);
104 this.$el.find(item_query).prop('checked', true);
104 } else {
105 } else {
105 this.$el.find(item_query).prop('checked', false);
106 this.$el.find(item_query).prop('checked', false);
106 }
107 }
107 }
108 }
108
109
109 // Remove items that no longer exist.
110 // Remove items that no longer exist.
110 this.$el.find('input').each(function(i, obj) {
111 this.$el.find('input').each(function(i, obj) {
111 var value = $(obj).val();
112 var value = $(obj).val();
112 var found = false;
113 var found = false;
113 for (var index in items) {
114 for (var index in items) {
114 if (items[index] == value) {
115 if (items[index] == value) {
115 found = true;
116 found = true;
116 break;
117 break;
117 }
118 }
118 }
119 }
119
120
120 if (!found) {
121 if (!found) {
121 $(obj).parent().remove();
122 $(obj).parent().remove();
122 }
123 }
123 });
124 });
124 },
125 },
125
126
126 });
127 });
127
128
128 IPython.notebook.widget_manager.register_widget_view('RadioButtonView', RadioButtonView);
129 IPython.notebook.widget_manager.register_widget_view('RadioButtonView', RadioButtonView);
130
131
132 var ToggleButtonView = IPython.WidgetView.extend({
133
134 // Called when view is rendered.
135 render : function(){
136 this.$el
137 .html('')
138 .addClass(this.model.comm.comm_id);
139 this.$buttongroup = $('<div />')
140 .addClass('btn-group')
141 .attr('data-toggle', 'buttons-radio')
142 .appendTo(this.$el);
143 this.update();
144 },
145
146 // Handles: Backend -> Frontend Sync
147 // Frontent -> Frontend Sync
148 update : function(){
149
150 // Add missing items to the DOM.
151 var items = this.model.get('values');
152 for (var index in items) {
153 var item_query = ' :contains("' + items[index] + '")';
154 if (this.$buttongroup.find(item_query).length == 0) {
155
156 var that = this;
157 $('<button />')
158 .attr('type', 'button')
159 .addClass('btn')
160 .html(items[index])
161 .appendTo(this.$buttongroup)
162 .on('click', function(e){
163 that.model.set('value', $(e.target).html(), this);
164 that.model.apply();
165 });
166 }
167
168 if (this.model.get('value') == items[index]) {
169 this.$buttongroup.find(item_query).addClass('active');
170 } else {
171 this.$buttongroup.find(item_query).removeClass('active');
172 }
173 }
174
175 // Remove items that no longer exist.
176 this.$buttongroup.find('button').each(function(i, obj) {
177 var value = $(obj).html();
178 var found = false;
179 for (var index in items) {
180 if (items[index] == value) {
181 found = true;
182 break;
183 }
184 }
185
186 if (!found) {
187 $(obj).remove();
188 }
189 });
190 },
191
192 });
193
194 IPython.notebook.widget_manager.register_widget_view('ToggleButtonView', ToggleButtonView);
General Comments 0
You need to be logged in to leave comments. Login now