##// 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 this.$droplabel.text(selected_item_text);
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 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;
@@ -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('_values');
142 var items = this.model.get('labels');
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) {
@@ -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('_values');
233 var items = this.model.get('labels');
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) {
@@ -316,7 +316,7 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('_values');
319 var items = this.model.get('labels');
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 + '")';
@@ -24,33 +24,35 b' from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict'
24 class _SelectionWidget(DOMWidget):
24 class _SelectionWidget(DOMWidget):
25 value = Any(help="Selected value")
25 value = Any(help="Selected value")
26 values = List(help="List of values the user can select")
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 These string representations are used to display the values in the
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 disabled = Bool(False, help="Enable or disable user changes", sync=True)
30 disabled = Bool(False, help="Enable or disable user changes", sync=True)
31 description = Unicode(help="Description of the value this widget represents", sync=True)
31 description = Unicode(help="Description of the value this widget represents", sync=True)
32
32
33 _value = Unicode(sync=True) # Bi-directionally synced.
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 def __init__(self, *pargs, **kwargs):
35 def __init__(self, *pargs, **kwargs):
38 """Constructor"""
36 """Constructor"""
39 DOMWidget.__init__(self, *pargs, **kwargs)
40 self.value_lock = Lock()
37 self.value_lock = Lock()
41 self.on_trait_change(self._string_value_set, ['_value'])
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 """Handles when the value_names Dict has been changed.
42 """Handles when the value_names Dict has been changed.
45
43
46 This method sets the _reverse_value_names Dict to the inverse of the new
44 This method sets the _reverse_value_names Dict to the inverse of the new
47 value for the value_names Dict."""
45 value for the value_names Dict."""
48 self._reverse_value_names = {v:k for k, v in self.value_names.items()}
46 if len(new) != len(self.values):
49 self._values_changed()
47 raise TypeError('Labels list must be the same size as the values list.')
50
48
51 def _values_changed(self, name=None, old=None, new=None):
49 def _values_changed(self, name=None, old=None, new=None):
52 """Called when values has been changed"""
50 """Handles when the value_names Dict has been changed.
53 self._values = [self._get_string_repr(v) for v in self.values]
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 def _value_changed(self, name, old, new):
57 def _value_changed(self, name, old, new):
56 """Called when value has been changed"""
58 """Called when value has been changed"""
@@ -59,25 +61,18 b' class _SelectionWidget(DOMWidget):'
59 # Make sure the value is in the list of values.
61 # Make sure the value is in the list of values.
60 if new in self.values:
62 if new in self.values:
61 # Set the string version of the value.
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 else:
65 else:
64 raise TypeError('Value must be a value in the values list.')
66 raise TypeError('Value must be a value in the values list.')
65 finally:
67 finally:
66 self.value_lock.release()
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 def _string_value_set(self, name, old, new):
70 def _string_value_set(self, name, old, new):
76 """Called when _value has been changed."""
71 """Called when _value has been changed."""
77 if self.value_lock.acquire(False):
72 if self.value_lock.acquire(False):
78 try:
73 try:
79 if new in self._reverse_value_names:
74 if new in self.labels:
80 self.value = self._reverse_value_names[new]
75 self.value = self.values[self.labels.index(new)]
81 else:
76 else:
82 self.value = None
77 self.value = None
83 finally:
78 finally:
General Comments 0
You need to be logged in to leave comments. Login now