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