##// END OF EJS Templates
Changed selection widget API to use labels list...
Jonathan Frederic -
Show More
@@ -64,7 +64,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
64 64 this.$droplabel.text(selected_item_text);
65 65 }
66 66
67 var items = this.model.get('_values');
67 var items = this.model.get('labels');
68 68 var $replace_droplist = $('<ul />')
69 69 .addClass('dropdown-menu');
70 70 var that = this;
@@ -139,7 +139,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
139 139 // changed by another view or by a state update from the back-end.
140 140 if (options === undefined || options.updated_view != this) {
141 141 // Add missing items to the DOM.
142 var items = this.model.get('_values');
142 var items = this.model.get('labels');
143 143 var disabled = this.model.get('disabled');
144 144 var that = this;
145 145 _.each(items, function(item, index) {
@@ -230,7 +230,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
230 230 // changed by another view or by a state update from the back-end.
231 231 if (options === undefined || options.updated_view != this) {
232 232 // Add missing items to the DOM.
233 var items = this.model.get('_values');
233 var items = this.model.get('labels');
234 234 var disabled = this.model.get('disabled');
235 235 var that = this;
236 236 _.each(items, function(item, index) {
@@ -316,7 +316,7 b' define(["notebook/js/widgets/widget"], function(WidgetManager){'
316 316 // changed by another view or by a state update from the back-end.
317 317 if (options === undefined || options.updated_view != this) {
318 318 // Add missing items to the DOM.
319 var items = this.model.get('_values');
319 var items = this.model.get('labels');
320 320 var that = this;
321 321 _.each(items, function(item, index) {
322 322 var item_query = ' :contains("' + item + '")';
@@ -24,33 +24,35 b' from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict'
24 24 class _SelectionWidget(DOMWidget):
25 25 value = Any(help="Selected value")
26 26 values = List(help="List of values the user can select")
27 value_names = Dict(help="""List of string representations for each value.
27 labels = List(help="""List of string representations for each value.
28 28 These string representations are used to display the values in the
29 front-end.""")
29 front-end.""", sync=True) # Only synced to the back-end.
30 30 disabled = Bool(False, help="Enable or disable user changes", sync=True)
31 31 description = Unicode(help="Description of the value this widget represents", sync=True)
32 32
33 33 _value = Unicode(sync=True) # Bi-directionally synced.
34 _values = List(sync=True) # Only back-end to front-end synced.
35 _reverse_value_names = Dict()
36 34
37 35 def __init__(self, *pargs, **kwargs):
38 36 """Constructor"""
39 DOMWidget.__init__(self, *pargs, **kwargs)
40 37 self.value_lock = Lock()
41 38 self.on_trait_change(self._string_value_set, ['_value'])
39 DOMWidget.__init__(self, *pargs, **kwargs)
42 40
43 def _value_names_changed(self, name=None, old=None, new=None):
41 def _labels_changed(self, name=None, old=None, new=None):
44 42 """Handles when the value_names Dict has been changed.
45 43
46 44 This method sets the _reverse_value_names Dict to the inverse of the new
47 45 value for the value_names Dict."""
48 self._reverse_value_names = {v:k for k, v in self.value_names.items()}
49 self._values_changed()
46 if len(new) != len(self.values):
47 raise TypeError('Labels list must be the same size as the values list.')
50 48
51 49 def _values_changed(self, name=None, old=None, new=None):
52 """Called when values has been changed"""
53 self._values = [self._get_string_repr(v) for v in self.values]
50 """Handles when the value_names Dict has been changed.
51
52 This method sets the _reverse_value_names Dict to the inverse of the new
53 value for the value_names Dict."""
54 if len(new) != len(self.labels):
55 self.labels = [(self.labels[i] if i < len(self.labels) else str(v)) for i, v in enumerate(new)]
54 56
55 57 def _value_changed(self, name, old, new):
56 58 """Called when value has been changed"""
@@ -59,25 +61,18 b' class _SelectionWidget(DOMWidget):'
59 61 # Make sure the value is in the list of values.
60 62 if new in self.values:
61 63 # Set the string version of the value.
62 self._value = self._get_string_repr(new)
64 self._value = self.labels[self.values.index(new)]
63 65 else:
64 66 raise TypeError('Value must be a value in the values list.')
65 67 finally:
66 68 self.value_lock.release()
67 69
68 def _get_string_repr(self, value):
69 """Get the string repr of a value"""
70 if value not in self.value_names:
71 self.value_names[value] = str(value)
72 self._value_names_changed()
73 return self.value_names[value]
74
75 70 def _string_value_set(self, name, old, new):
76 71 """Called when _value has been changed."""
77 72 if self.value_lock.acquire(False):
78 73 try:
79 if new in self._reverse_value_names:
80 self.value = self._reverse_value_names[new]
74 if new in self.labels:
75 self.value = self.values[self.labels.index(new)]
81 76 else:
82 77 self.value = None
83 78 finally:
General Comments 0
You need to be logged in to leave comments. Login now