##// END OF EJS Templates
remove incorrect is instance check in children_changed
MinRK -
Show More
@@ -1,60 +1,60 b''
1 """ContainerWidget class.
1 """ContainerWidget class.
2
2
3 Represents a container that can be used to group other widgets.
3 Represents a container that can be used to group other widgets.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from .widget import DOMWidget
16 from .widget import DOMWidget
17 from IPython.utils.traitlets import Unicode, Tuple, Instance, TraitError
17 from IPython.utils.traitlets import Unicode, Tuple, Instance, TraitError
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Classes
20 # Classes
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 class TupleOfDOMWidgets(Tuple):
23 class TupleOfDOMWidgets(Tuple):
24 """Like Tuple(Instance(DOMWidget)), but without checking length."""
24 """Like Tuple(Instance(DOMWidget)), but without checking length."""
25 def validate_elements(self, obj, value):
25 def validate_elements(self, obj, value):
26 for v in value:
26 for v in value:
27 if not isinstance(v, DOMWidget):
27 if not isinstance(v, DOMWidget):
28 raise TraitError("Container.children must be DOMWidgets, not %r" % v)
28 raise TraitError("Container.children must be DOMWidgets, not %r" % v)
29 return value
29 return value
30
30
31 class ContainerWidget(DOMWidget):
31 class ContainerWidget(DOMWidget):
32 _view_name = Unicode('ContainerView', sync=True)
32 _view_name = Unicode('ContainerView', sync=True)
33
33
34 # Keys, all private and managed by helper methods. Flexible box model
34 # Keys, all private and managed by helper methods. Flexible box model
35 # classes...
35 # classes...
36 children = TupleOfDOMWidgets()
36 children = TupleOfDOMWidgets()
37 _children = TupleOfDOMWidgets(sync=True)
37 _children = TupleOfDOMWidgets(sync=True)
38
38
39 def _children_changed(self, name, old, new):
39 def _children_changed(self, name, old, new):
40 """Validate children list.
40 """Validate children list.
41
41
42 Makes sure only one instance of any given model can exist in the
42 Makes sure only one instance of any given model can exist in the
43 children list.
43 children list.
44 An excellent post on uniqifiers is available at
44 An excellent post on uniqifiers is available at
45 http://www.peterbe.com/plog/uniqifiers-benchmark
45 http://www.peterbe.com/plog/uniqifiers-benchmark
46 which provides the inspiration for using this implementation. Below
46 which provides the inspiration for using this implementation. Below
47 I've implemented the `f5` algorithm using Python comprehensions."""
47 I've implemented the `f5` algorithm using Python comprehensions."""
48 if new is not None and isinstance(new, list):
48 if new is not None:
49 seen = {}
49 seen = {}
50 def add_item(i):
50 def add_item(i):
51 seen[i.model_id] = True
51 seen[i.model_id] = True
52 return i
52 return i
53 self._children = [add_item(i) for i in new if not i.model_id in seen]
53 self._children = [add_item(i) for i in new if not i.model_id in seen]
54
54
55
55
56 class PopupWidget(ContainerWidget):
56 class PopupWidget(ContainerWidget):
57 _view_name = Unicode('PopupView', sync=True)
57 _view_name = Unicode('PopupView', sync=True)
58
58
59 description = Unicode(sync=True)
59 description = Unicode(sync=True)
60 button_text = Unicode(sync=True)
60 button_text = Unicode(sync=True)
General Comments 0
You need to be logged in to leave comments. Login now