Show More
@@ -57,14 +57,14 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
57 | // changed by another view or by a state update from the back-end. |
|
57 | // changed by another view or by a state update from the back-end. | |
58 |
|
58 | |||
59 | if (options === undefined || options.updated_view != this) { |
|
59 | if (options === undefined || options.updated_view != this) { | |
60 |
var selected_item_text = this.model.get(' |
|
60 | var selected_item_text = this.model.get('value_name'); | |
61 | if (selected_item_text.length === 0) { |
|
61 | if (selected_item_text.length === 0) { | |
62 | this.$droplabel.text(' '); |
|
62 | this.$droplabel.text(' '); | |
63 | } else { |
|
63 | } else { | |
64 | this.$droplabel.text(selected_item_text); |
|
64 | this.$droplabel.text(selected_item_text); | |
65 | } |
|
65 | } | |
66 |
|
66 | |||
67 |
var items = this.model.get(' |
|
67 | var items = this.model.get('value_names'); | |
68 | var $replace_droplist = $('<ul />') |
|
68 | var $replace_droplist = $('<ul />') | |
69 | .addClass('dropdown-menu'); |
|
69 | .addClass('dropdown-menu'); | |
70 | var that = this; |
|
70 | var that = this; | |
@@ -107,7 +107,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
107 |
|
107 | |||
108 | // Calling model.set will trigger all of the other views of the |
|
108 | // Calling model.set will trigger all of the other views of the | |
109 | // model to update. |
|
109 | // model to update. | |
110 |
this.model.set(' |
|
110 | this.model.set('value_name', $(e.target).text(), {updated_view: this}); | |
111 | this.touch(); |
|
111 | this.touch(); | |
112 | }, |
|
112 | }, | |
113 |
|
113 | |||
@@ -139,7 +139,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
139 | // changed by another view or by a state update from the back-end. |
|
139 | // changed by another view or by a state update from the back-end. | |
140 | if (options === undefined || options.updated_view != this) { |
|
140 | if (options === undefined || options.updated_view != this) { | |
141 | // Add missing items to the DOM. |
|
141 | // Add missing items to the DOM. | |
142 |
var items = this.model.get(' |
|
142 | var items = this.model.get('value_names'); | |
143 | var disabled = this.model.get('disabled'); |
|
143 | var disabled = this.model.get('disabled'); | |
144 | var that = this; |
|
144 | var that = this; | |
145 | _.each(items, function(item, index) { |
|
145 | _.each(items, function(item, index) { | |
@@ -159,7 +159,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
159 | } |
|
159 | } | |
160 |
|
160 | |||
161 | var $item_element = that.$container.find(item_query); |
|
161 | var $item_element = that.$container.find(item_query); | |
162 |
if (that.model.get(' |
|
162 | if (that.model.get('value_name') == item) { | |
163 | $item_element.prop('checked', true); |
|
163 | $item_element.prop('checked', true); | |
164 | } else { |
|
164 | } else { | |
165 | $item_element.prop('checked', false); |
|
165 | $item_element.prop('checked', false); | |
@@ -199,7 +199,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
199 |
|
199 | |||
200 | // Calling model.set will trigger all of the other views of the |
|
200 | // Calling model.set will trigger all of the other views of the | |
201 | // model to update. |
|
201 | // model to update. | |
202 |
this.model.set(' |
|
202 | this.model.set('value_name', $(e.target).val(), {updated_view: this}); | |
203 | this.touch(); |
|
203 | this.touch(); | |
204 | }, |
|
204 | }, | |
205 | }); |
|
205 | }); | |
@@ -230,7 +230,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
230 | // changed by another view or by a state update from the back-end. |
|
230 | // changed by another view or by a state update from the back-end. | |
231 | if (options === undefined || options.updated_view != this) { |
|
231 | if (options === undefined || options.updated_view != this) { | |
232 | // Add missing items to the DOM. |
|
232 | // Add missing items to the DOM. | |
233 |
var items = this.model.get(' |
|
233 | var items = this.model.get('value_names'); | |
234 | var disabled = this.model.get('disabled'); |
|
234 | var disabled = this.model.get('disabled'); | |
235 | var that = this; |
|
235 | var that = this; | |
236 | _.each(items, function(item, index) { |
|
236 | _.each(items, function(item, index) { | |
@@ -245,7 +245,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
245 | } |
|
245 | } | |
246 |
|
246 | |||
247 | var $item_element = that.$buttongroup.find(item_query); |
|
247 | var $item_element = that.$buttongroup.find(item_query); | |
248 |
if (that.model.get(' |
|
248 | if (that.model.get('value_name') == item) { | |
249 | $item_element.addClass('active'); |
|
249 | $item_element.addClass('active'); | |
250 | } else { |
|
250 | } else { | |
251 | $item_element.removeClass('active'); |
|
251 | $item_element.removeClass('active'); | |
@@ -285,7 +285,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
285 |
|
285 | |||
286 | // Calling model.set will trigger all of the other views of the |
|
286 | // Calling model.set will trigger all of the other views of the | |
287 | // model to update. |
|
287 | // model to update. | |
288 |
this.model.set(' |
|
288 | this.model.set('value_name', $(e.target).text(), {updated_view: this}); | |
289 | this.touch(); |
|
289 | this.touch(); | |
290 | }, |
|
290 | }, | |
291 | }); |
|
291 | }); | |
@@ -316,21 +316,21 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
316 | // changed by another view or by a state update from the back-end. |
|
316 | // changed by another view or by a state update from the back-end. | |
317 | if (options === undefined || options.updated_view != this) { |
|
317 | if (options === undefined || options.updated_view != this) { | |
318 | // Add missing items to the DOM. |
|
318 | // Add missing items to the DOM. | |
319 |
var items = this.model.get(' |
|
319 | var items = this.model.get('value_names'); | |
320 | var that = this; |
|
320 | var that = this; | |
321 | _.each(items, function(item, index) { |
|
321 | _.each(items, function(item, index) { | |
322 | var item_query = ' :contains("' + item + '")'; |
|
322 | var item_query = ' :contains("' + item + '")'; | |
323 | if (that.$listbox.find(item_query).length === 0) { |
|
323 | if (that.$listbox.find(item_query).length === 0) { | |
324 | $('<option />') |
|
324 | $('<option />') | |
325 | .text(item) |
|
325 | .text(item) | |
326 |
.attr(' |
|
326 | .attr('value_name', item) | |
327 | .appendTo(that.$listbox) |
|
327 | .appendTo(that.$listbox) | |
328 | .on('click', $.proxy(that.handle_click, that)); |
|
328 | .on('click', $.proxy(that.handle_click, that)); | |
329 | } |
|
329 | } | |
330 | }); |
|
330 | }); | |
331 |
|
331 | |||
332 | // Select the correct element |
|
332 | // Select the correct element | |
333 |
this.$listbox.val(this.model.get(' |
|
333 | this.$listbox.val(this.model.get('value_name')); | |
334 |
|
334 | |||
335 | // Disable listbox if needed |
|
335 | // Disable listbox if needed | |
336 | var disabled = this.model.get('disabled'); |
|
336 | var disabled = this.model.get('disabled'); | |
@@ -368,7 +368,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){' | |||||
368 |
|
368 | |||
369 | // Calling model.set will trigger all of the other views of the |
|
369 | // Calling model.set will trigger all of the other views of the | |
370 | // model to update. |
|
370 | // model to update. | |
371 |
this.model.set(' |
|
371 | this.model.set('value_name', $(e.target).text(), {updated_view: this}); | |
372 | this.touch(); |
|
372 | this.touch(); | |
373 | }, |
|
373 | }, | |
374 | }); |
|
374 | }); |
@@ -76,7 +76,7 b' casper.notebook_test(function () {' | |||||
76 | 'Widget list exists.'); |
|
76 | 'Widget list exists.'); | |
77 |
|
77 | |||
78 | // Verify that no items are selected. |
|
78 | // Verify that no items are selected. | |
79 |
this.test.assert(verify_selection(this, |
|
79 | this.test.assert(verify_selection(this, 0), 'Default first item selected.'); | |
80 | }); |
|
80 | }); | |
81 |
|
81 | |||
82 | index = this.append_cell( |
|
82 | index = this.append_cell( | |
@@ -123,7 +123,9 b' casper.notebook_test(function () {' | |||||
123 |
|
123 | |||
124 | index = this.append_cell( |
|
124 | index = this.append_cell( | |
125 | 'for widget in selection:\n' + |
|
125 | 'for widget in selection:\n' + | |
126 |
' |
|
126 | ' d = widget.values.copy()\n' + | |
|
127 | ' d["z"] = "z"\n' + | |||
|
128 | ' widget.values = d\n' + | |||
127 | 'selection[0].value = "z"'); |
|
129 | 'selection[0].value = "z"'); | |
128 | this.execute_cell_then(index, function(index){ |
|
130 | this.execute_cell_then(index, function(index){ | |
129 |
|
131 |
@@ -1,4 +1,4 b'' | |||||
1 |
"""SelectionWidget class. |
|
1 | """SelectionWidget classes. | |
2 |
|
2 | |||
3 | Represents an enumeration using a widget. |
|
3 | Represents an enumeration using a widget. | |
4 | """ |
|
4 | """ | |
@@ -13,68 +13,92 b' Represents an enumeration using a widget.' | |||||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | ||||
|
17 | from collections import OrderedDict | |||
16 | from threading import Lock |
|
18 | from threading import Lock | |
17 |
|
19 | |||
18 | from .widget import DOMWidget |
|
20 | from .widget import DOMWidget | |
19 | from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict |
|
21 | from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict, TraitError | |
|
22 | from IPython.utils.py3compat import unicode_type | |||
20 |
|
23 | |||
21 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
22 | # SelectionWidget |
|
25 | # SelectionWidget | |
23 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
24 | class _SelectionWidget(DOMWidget): |
|
27 | class _SelectionWidget(DOMWidget): | |
|
28 | """Base class for Selection widgets | |||
|
29 | ||||
|
30 | ``values`` can be specified as a list or dict. If given as a list, | |||
|
31 | it will be transformed to a dict of the form ``{str(value):value}``. | |||
|
32 | """ | |||
|
33 | ||||
25 |
value = Any(help="Selected value") |
|
34 | value = Any(help="Selected value") | |
26 |
values = |
|
35 | values = Dict(help="""Dictionary of {name: value} the user can select. | |
27 | labels = List(help="""List of string representations for each value. |
|
36 | ||
28 | These string representations are used to display the values in the |
|
37 | The keys of this dictionary are the strings that will be displayed in the UI, | |
29 | front-end.""", sync=True) # Only synced to the back-end. |
|
38 | representing the actual Python choices. | |
|
39 | ||||
|
40 | The keys of this dictionary are also available as value_names. | |||
|
41 | """) | |||
|
42 | value_name = Unicode(help="The name of the selected value", sync=True) | |||
|
43 | value_names = List(Unicode, help="""Read-only list of names for each value. | |||
|
44 | ||||
|
45 | If values is specified as a list, this is the string representation of each element. | |||
|
46 | Otherwise, it is the keys of the values dictionary. | |||
|
47 | ||||
|
48 | These strings are used to display the choices in the front-end.""", sync=True) | |||
30 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
49 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
31 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
50 | description = Unicode(help="Description of the value this widget represents", sync=True) | |
32 |
|
51 | |||
33 | _value = Unicode(sync=True) # Bi-directionally synced. |
|
|||
34 |
|
52 | |||
35 |
def __init__(self, * |
|
53 | def __init__(self, *args, **kwargs): | |
36 | """Constructor""" |
|
|||
37 | self.value_lock = Lock() |
|
54 | self.value_lock = Lock() | |
38 | self.on_trait_change(self._string_value_set, ['_value']) |
|
55 | self._in_values_changed = False | |
39 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
56 | if 'values' in kwargs: | |
40 |
|
57 | values = kwargs['values'] | ||
41 | def _labels_changed(self, name=None, old=None, new=None): |
|
58 | # convert list values to an dict of {str(v):v} | |
42 | """Handles when the value_names Dict has been changed. |
|
59 | if isinstance(values, list): | |
43 |
|
60 | # preserve list order with an OrderedDict | ||
44 | This method sets the _reverse_value_names Dict to the inverse of the new |
|
61 | kwargs['values'] = OrderedDict((unicode_type(v), v) for v in values) | |
45 | value for the value_names Dict.""" |
|
62 | DOMWidget.__init__(self, *args, **kwargs) | |
46 | if len(new) != len(self.values): |
|
63 | ||
47 | raise TypeError('Labels list must be the same size as the values list.') |
|
64 | def _values_changed(self, name, old, new): | |
|
65 | """Handles when the values dict has been changed. | |||
|
66 | ||||
|
67 | Setting values implies setting value names from the keys of the dict. | |||
|
68 | """ | |||
|
69 | self._in_values_changed = True | |||
|
70 | try: | |||
|
71 | self.value_names = list(new.keys()) | |||
|
72 | finally: | |||
|
73 | self._in_values_changed = False | |||
48 |
|
74 | |||
49 | def _values_changed(self, name=None, old=None, new=None): |
|
75 | # ensure that the chosen value is one of the choices | |
50 | """Handles when the value_names Dict has been changed. |
|
76 | if self.value not in new.values(): | |
|
77 | self.value = next(iter(new.values())) | |||
51 |
|
78 | |||
52 | This method sets the _reverse_value_names Dict to the inverse of the new |
|
79 | def _value_names_changed(self, name, old, new): | |
53 | value for the value_names Dict.""" |
|
80 | if not self._in_values_changed: | |
54 | if len(new) != len(self.labels): |
|
81 | raise TraitError("value_names is a read-only proxy to values.keys(). Use the values dict instead.") | |
55 | self.labels = [(self.labels[i] if i < len(self.labels) else str(v)) for i, v in enumerate(new)] |
|
|||
56 |
|
82 | |||
57 | def _value_changed(self, name, old, new): |
|
83 | def _value_changed(self, name, old, new): | |
58 | """Called when value has been changed""" |
|
84 | """Called when value has been changed""" | |
59 | if self.value_lock.acquire(False): |
|
85 | if self.value_lock.acquire(False): | |
60 | try: |
|
86 | try: | |
61 | # Make sure the value is in the list of values. |
|
87 | # Reverse dictionary lookup for the value name | |
62 |
|
|
88 | for k,v in self.values.items(): | |
63 | # Set the string version of the value. |
|
89 | if new == v: | |
64 | self._value = self.labels[self.values.index(new)] |
|
90 | # set the selected value name | |
65 | else: |
|
91 | self.value_name = k | |
66 | raise TypeError('Value must be a value in the values list.') |
|
92 | return | |
|
93 | raise KeyError(new) | |||
67 | finally: |
|
94 | finally: | |
68 | self.value_lock.release() |
|
95 | self.value_lock.release() | |
69 |
|
96 | |||
70 |
def |
|
97 | def _value_name_changed(self, name, old, new): | |
71 |
"""Called when |
|
98 | """Called when the value name has been changed (typically by the frontend).""" | |
72 | if self.value_lock.acquire(False): |
|
99 | if self.value_lock.acquire(False): | |
73 | try: |
|
100 | try: | |
74 |
|
|
101 | self.value = self.values[new] | |
75 | self.value = self.values[self.labels.index(new)] |
|
|||
76 | else: |
|
|||
77 | self.value = None |
|
|||
78 | finally: |
|
102 | finally: | |
79 | self.value_lock.release() |
|
103 | self.value_lock.release() | |
80 |
|
104 |
General Comments 0
You need to be logged in to leave comments.
Login now