##// END OF EJS Templates
Use regular expressions when fixing button-like captions
Jonathan Frederic -
Show More
@@ -1,56 +1,56
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // ButtonWidget
9 // ButtonWidget
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 **/
15 **/
16
16
17 define(["notebook/js/widget"], function(){
17 define(["notebook/js/widget"], function(){
18
18
19 var ButtonWidgetModel = IPython.WidgetModel.extend({});
19 var ButtonWidgetModel = IPython.WidgetModel.extend({});
20 IPython.widget_manager.register_widget_model('ButtonWidgetModel', ButtonWidgetModel);
20 IPython.widget_manager.register_widget_model('ButtonWidgetModel', ButtonWidgetModel);
21
21
22 var ButtonView = IPython.WidgetView.extend({
22 var ButtonView = IPython.WidgetView.extend({
23
23
24 // Called when view is rendered.
24 // Called when view is rendered.
25 render : function(){
25 render : function(){
26 var that = this;
26 var that = this;
27 this.$el = $("<button />")
27 this.$el = $("<button />")
28 .addClass('btn')
28 .addClass('btn')
29 .click(function() {
29 .click(function() {
30 that.model.set('clicks', that.model.get('clicks') + 1);
30 that.model.set('clicks', that.model.get('clicks') + 1);
31 that.model.update_other_views(that);
31 that.model.update_other_views(that);
32 });
32 });
33
33
34 this.update(); // Set defaults.
34 this.update(); // Set defaults.
35 },
35 },
36
36
37 // Handles: Backend -> Frontend Sync
37 // Handles: Backend -> Frontend Sync
38 // Frontent -> Frontend Sync
38 // Frontent -> Frontend Sync
39 update : function(){
39 update : function(){
40 var description = this.model.get('description');
40 var description = this.model.get('description');
41 description = description.replace(' ', '&nbsp;');
41 description = description.replace(/ /g, '&nbsp;', 'm');
42 description = description.replace('\n', '<br>\n');
42 description = description.replace(/\n/g, '<br>\n', 'm');
43 if (description.length == 0) {
43 if (description.length == 0) {
44 this.$el.html('&nbsp;'); // Preserve button height
44 this.$el.html('&nbsp;'); // Preserve button height
45 } else {
45 } else {
46 this.$el.html(description);
46 this.$el.html(description);
47 }
47 }
48
48
49 return IPython.WidgetView.prototype.update.call(this);
49 return IPython.WidgetView.prototype.update.call(this);
50 },
50 },
51
51
52 });
52 });
53
53
54 IPython.widget_manager.register_widget_view('ButtonView', ButtonView);
54 IPython.widget_manager.register_widget_view('ButtonView', ButtonView);
55
55
56 });
56 });
@@ -1,277 +1,277
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2013 The IPython Development Team
2 // Copyright (C) 2013 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // SelectionWidget
9 // SelectionWidget
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 **/
15 **/
16
16
17 define(["notebook/js/widget"], function(){
17 define(["notebook/js/widget"], function(){
18 var SelectionWidgetModel = IPython.WidgetModel.extend({});
18 var SelectionWidgetModel = IPython.WidgetModel.extend({});
19 IPython.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
19 IPython.widget_manager.register_widget_model('SelectionWidgetModel', SelectionWidgetModel);
20
20
21 var DropdownView = IPython.WidgetView.extend({
21 var DropdownView = IPython.WidgetView.extend({
22
22
23 // Called when view is rendered.
23 // Called when view is rendered.
24 render : function(){
24 render : function(){
25
25
26 this.$el
26 this.$el
27 .addClass('widget-hbox-single')
27 .addClass('widget-hbox-single')
28 .html('');
28 .html('');
29 this.$label = $('<div />')
29 this.$label = $('<div />')
30 .appendTo(this.$el)
30 .appendTo(this.$el)
31 .addClass('widget-hlabel')
31 .addClass('widget-hlabel')
32 .hide();
32 .hide();
33 this.$buttongroup = $('<div />')
33 this.$buttongroup = $('<div />')
34 .addClass('widget_item')
34 .addClass('widget_item')
35 .addClass('btn-group')
35 .addClass('btn-group')
36 .appendTo(this.$el);
36 .appendTo(this.$el);
37 this.$el_to_style = this.$buttongroup; // Set default element to style
37 this.$el_to_style = this.$buttongroup; // Set default element to style
38 this.$droplabel = $('<button />')
38 this.$droplabel = $('<button />')
39 .addClass('btn')
39 .addClass('btn')
40 .addClass('widget-combo-btn')
40 .addClass('widget-combo-btn')
41 .html('&nbsp;')
41 .html('&nbsp;')
42 .appendTo(this.$buttongroup);
42 .appendTo(this.$buttongroup);
43 this.$dropbutton = $('<button />')
43 this.$dropbutton = $('<button />')
44 .addClass('btn')
44 .addClass('btn')
45 .addClass('dropdown-toggle')
45 .addClass('dropdown-toggle')
46 .addClass('widget-combo-carrot-btn')
46 .addClass('widget-combo-carrot-btn')
47 .attr('data-toggle', 'dropdown')
47 .attr('data-toggle', 'dropdown')
48 .html('<span class="caret"></span>')
48 .html('<span class="caret"></span>')
49 .appendTo(this.$buttongroup);
49 .appendTo(this.$buttongroup);
50 this.$droplist = $('<ul />')
50 this.$droplist = $('<ul />')
51 .addClass('dropdown-menu')
51 .addClass('dropdown-menu')
52 .appendTo(this.$buttongroup);
52 .appendTo(this.$buttongroup);
53
53
54 // Set defaults.
54 // Set defaults.
55 this.update();
55 this.update();
56 },
56 },
57
57
58 // Handles: Backend -> Frontend Sync
58 // Handles: Backend -> Frontend Sync
59 // Frontent -> Frontend Sync
59 // Frontent -> Frontend Sync
60 update : function(){
60 update : function(){
61
61
62 var selected_item_text = this.model.get('value');
62 var selected_item_text = this.model.get('value');
63 selected_item_text = selected_item_text.replace(' ', '&nbsp;');
63 selected_item_text = selected_item_text.replace(/ /g, '&nbsp;');
64 selected_item_text = selected_item_text.replace('\n', '<br>\n');
64 selected_item_text = selected_item_text.replace(/\n/g, '<br>\n');
65 if (selected_item_text.length == 0) {
65 if (selected_item_text.length == 0) {
66 this.$droplabel.html('&nbsp;');
66 this.$droplabel.html('&nbsp;');
67 } else {
67 } else {
68 this.$droplabel.html(selected_item_text);
68 this.$droplabel.html(selected_item_text);
69 }
69 }
70
70
71 var items = this.model.get('values');
71 var items = this.model.get('values');
72 this.$droplist.html('');
72 this.$droplist.html('');
73 for (var index in items) {
73 for (var index in items) {
74 var that = this;
74 var that = this;
75 var item_button = $('<a href="#"/>')
75 var item_button = $('<a href="#"/>')
76 .html(items[index])
76 .html(items[index])
77 .on('click', function(e){
77 .on('click', function(e){
78 that.model.set('value', $(e.target).html(), this);
78 that.model.set('value', $(e.target).html(), this);
79 that.model.update_other_views(that);
79 that.model.update_other_views(that);
80 })
80 })
81
81
82 this.$droplist.append($('<li />').append(item_button))
82 this.$droplist.append($('<li />').append(item_button))
83 }
83 }
84
84
85 if (this.model.get('disabled')) {
85 if (this.model.get('disabled')) {
86 this.$buttongroup.attr('disabled','disabled');
86 this.$buttongroup.attr('disabled','disabled');
87 this.$droplabel.attr('disabled','disabled');
87 this.$droplabel.attr('disabled','disabled');
88 this.$dropbutton.attr('disabled','disabled');
88 this.$dropbutton.attr('disabled','disabled');
89 this.$droplist.attr('disabled','disabled');
89 this.$droplist.attr('disabled','disabled');
90 } else {
90 } else {
91 this.$buttongroup.removeAttr('disabled');
91 this.$buttongroup.removeAttr('disabled');
92 this.$droplabel.removeAttr('disabled');
92 this.$droplabel.removeAttr('disabled');
93 this.$dropbutton.removeAttr('disabled');
93 this.$dropbutton.removeAttr('disabled');
94 this.$droplist.removeAttr('disabled');
94 this.$droplist.removeAttr('disabled');
95 }
95 }
96
96
97 var description = this.model.get('description');
97 var description = this.model.get('description');
98 if (description.length == 0) {
98 if (description.length == 0) {
99 this.$label.hide();
99 this.$label.hide();
100 } else {
100 } else {
101 this.$label.html(description);
101 this.$label.html(description);
102 this.$label.show();
102 this.$label.show();
103 }
103 }
104 return IPython.WidgetView.prototype.update.call(this);
104 return IPython.WidgetView.prototype.update.call(this);
105 },
105 },
106
106
107 });
107 });
108
108
109 IPython.widget_manager.register_widget_view('DropdownView', DropdownView);
109 IPython.widget_manager.register_widget_view('DropdownView', DropdownView);
110
110
111 var RadioButtonsView = IPython.WidgetView.extend({
111 var RadioButtonsView = IPython.WidgetView.extend({
112
112
113 // Called when view is rendered.
113 // Called when view is rendered.
114 render : function(){
114 render : function(){
115 this.$el
115 this.$el
116 .addClass('widget-hbox')
116 .addClass('widget-hbox')
117 .html('');
117 .html('');
118 this.$label = $('<div />')
118 this.$label = $('<div />')
119 .appendTo(this.$el)
119 .appendTo(this.$el)
120 .addClass('widget-hlabel')
120 .addClass('widget-hlabel')
121 .hide();
121 .hide();
122 this.$container = $('<div />')
122 this.$container = $('<div />')
123 .appendTo(this.$el)
123 .appendTo(this.$el)
124 .addClass('widget-container')
124 .addClass('widget-container')
125 .addClass('vbox');
125 .addClass('vbox');
126 this.$el_to_style = this.$container; // Set default element to style
126 this.$el_to_style = this.$container; // Set default element to style
127 this.update();
127 this.update();
128 },
128 },
129
129
130 // Handles: Backend -> Frontend Sync
130 // Handles: Backend -> Frontend Sync
131 // Frontent -> Frontend Sync
131 // Frontent -> Frontend Sync
132 update : function(){
132 update : function(){
133
133
134 // Add missing items to the DOM.
134 // Add missing items to the DOM.
135 var items = this.model.get('values');
135 var items = this.model.get('values');
136 var disabled = this.model.get('disabled');
136 var disabled = this.model.get('disabled');
137 for (var index in items) {
137 for (var index in items) {
138 var item_query = ' :input[value="' + items[index] + '"]';
138 var item_query = ' :input[value="' + items[index] + '"]';
139 if (this.$el.find(item_query).length == 0) {
139 if (this.$el.find(item_query).length == 0) {
140 var $label = $('<label />')
140 var $label = $('<label />')
141 .addClass('radio')
141 .addClass('radio')
142 .html(items[index])
142 .html(items[index])
143 .appendTo(this.$container);
143 .appendTo(this.$container);
144
144
145 var that = this;
145 var that = this;
146 $('<input />')
146 $('<input />')
147 .attr('type', 'radio')
147 .attr('type', 'radio')
148 .addClass(this.model)
148 .addClass(this.model)
149 .val(items[index])
149 .val(items[index])
150 .prependTo($label)
150 .prependTo($label)
151 .on('click', function(e){
151 .on('click', function(e){
152 that.model.set('value', $(e.target).val(), this);
152 that.model.set('value', $(e.target).val(), this);
153 that.model.update_other_views(that);
153 that.model.update_other_views(that);
154 });
154 });
155 }
155 }
156
156
157 var $item_element = this.$container.find(item_query);
157 var $item_element = this.$container.find(item_query);
158 if (this.model.get('value') == items[index]) {
158 if (this.model.get('value') == items[index]) {
159 $item_element.prop('checked', true);
159 $item_element.prop('checked', true);
160 } else {
160 } else {
161 $item_element.prop('checked', false);
161 $item_element.prop('checked', false);
162 }
162 }
163 $item_element.prop('disabled', disabled);
163 $item_element.prop('disabled', disabled);
164 }
164 }
165
165
166 // Remove items that no longer exist.
166 // Remove items that no longer exist.
167 this.$container.find('input').each(function(i, obj) {
167 this.$container.find('input').each(function(i, obj) {
168 var value = $(obj).val();
168 var value = $(obj).val();
169 var found = false;
169 var found = false;
170 for (var index in items) {
170 for (var index in items) {
171 if (items[index] == value) {
171 if (items[index] == value) {
172 found = true;
172 found = true;
173 break;
173 break;
174 }
174 }
175 }
175 }
176
176
177 if (!found) {
177 if (!found) {
178 $(obj).parent().remove();
178 $(obj).parent().remove();
179 }
179 }
180 });
180 });
181
181
182 var description = this.model.get('description');
182 var description = this.model.get('description');
183 if (description.length == 0) {
183 if (description.length == 0) {
184 this.$label.hide();
184 this.$label.hide();
185 } else {
185 } else {
186 this.$label.html(description);
186 this.$label.html(description);
187 this.$label.show();
187 this.$label.show();
188 }
188 }
189 return IPython.WidgetView.prototype.update.call(this);
189 return IPython.WidgetView.prototype.update.call(this);
190 },
190 },
191
191
192 });
192 });
193
193
194 IPython.widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView);
194 IPython.widget_manager.register_widget_view('RadioButtonsView', RadioButtonsView);
195
195
196
196
197 var ToggleButtonsView = IPython.WidgetView.extend({
197 var ToggleButtonsView = IPython.WidgetView.extend({
198
198
199 // Called when view is rendered.
199 // Called when view is rendered.
200 render : function(){
200 render : function(){
201 this.$el
201 this.$el
202 .addClass('widget-hbox-single')
202 .addClass('widget-hbox-single')
203 .html('');
203 .html('');
204 this.$label = $('<div />')
204 this.$label = $('<div />')
205 .appendTo(this.$el)
205 .appendTo(this.$el)
206 .addClass('widget-hlabel')
206 .addClass('widget-hlabel')
207 .hide();
207 .hide();
208 this.$buttongroup = $('<div />')
208 this.$buttongroup = $('<div />')
209 .addClass('btn-group')
209 .addClass('btn-group')
210 .attr('data-toggle', 'buttons-radio')
210 .attr('data-toggle', 'buttons-radio')
211 .appendTo(this.$el);
211 .appendTo(this.$el);
212 this.$el_to_style = this.$buttongroup; // Set default element to style
212 this.$el_to_style = this.$buttongroup; // Set default element to style
213 this.update();
213 this.update();
214 },
214 },
215
215
216 // Handles: Backend -> Frontend Sync
216 // Handles: Backend -> Frontend Sync
217 // Frontent -> Frontend Sync
217 // Frontent -> Frontend Sync
218 update : function(){
218 update : function(){
219
219
220 // Add missing items to the DOM.
220 // Add missing items to the DOM.
221 var items = this.model.get('values');
221 var items = this.model.get('values');
222 var disabled = this.model.get('disabled');
222 var disabled = this.model.get('disabled');
223 for (var index in items) {
223 for (var index in items) {
224 var item_query = ' :contains("' + items[index] + '")';
224 var item_query = ' :contains("' + items[index] + '")';
225 if (this.$buttongroup.find(item_query).length == 0) {
225 if (this.$buttongroup.find(item_query).length == 0) {
226
226
227 var that = this;
227 var that = this;
228 $('<button />')
228 $('<button />')
229 .attr('type', 'button')
229 .attr('type', 'button')
230 .addClass('btn')
230 .addClass('btn')
231 .html(items[index])
231 .html(items[index])
232 .appendTo(this.$buttongroup)
232 .appendTo(this.$buttongroup)
233 .on('click', function(e){
233 .on('click', function(e){
234 that.model.set('value', $(e.target).html(), this);
234 that.model.set('value', $(e.target).html(), this);
235 that.model.update_other_views(that);
235 that.model.update_other_views(that);
236 });
236 });
237 }
237 }
238
238
239 var $item_element = this.$buttongroup.find(item_query);
239 var $item_element = this.$buttongroup.find(item_query);
240 if (this.model.get('value') == items[index]) {
240 if (this.model.get('value') == items[index]) {
241 $item_element.addClass('active');
241 $item_element.addClass('active');
242 } else {
242 } else {
243 $item_element.removeClass('active');
243 $item_element.removeClass('active');
244 }
244 }
245 $item_element.prop('disabled', disabled);
245 $item_element.prop('disabled', disabled);
246 }
246 }
247
247
248 // Remove items that no longer exist.
248 // Remove items that no longer exist.
249 this.$buttongroup.find('button').each(function(i, obj) {
249 this.$buttongroup.find('button').each(function(i, obj) {
250 var value = $(obj).html();
250 var value = $(obj).html();
251 var found = false;
251 var found = false;
252 for (var index in items) {
252 for (var index in items) {
253 if (items[index] == value) {
253 if (items[index] == value) {
254 found = true;
254 found = true;
255 break;
255 break;
256 }
256 }
257 }
257 }
258
258
259 if (!found) {
259 if (!found) {
260 $(obj).remove();
260 $(obj).remove();
261 }
261 }
262 });
262 });
263
263
264 var description = this.model.get('description');
264 var description = this.model.get('description');
265 if (description.length == 0) {
265 if (description.length == 0) {
266 this.$label.hide();
266 this.$label.hide();
267 } else {
267 } else {
268 this.$label.html(description);
268 this.$label.html(description);
269 this.$label.show();
269 this.$label.show();
270 }
270 }
271 return IPython.WidgetView.prototype.update.call(this);
271 return IPython.WidgetView.prototype.update.call(this);
272 },
272 },
273
273
274 });
274 });
275
275
276 IPython.widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
276 IPython.widget_manager.register_widget_view('ToggleButtonsView', ToggleButtonsView);
277 });
277 });
General Comments 0
You need to be logged in to leave comments. Login now