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