containers and selectioncontainers now only allow one of any single child

This commit is contained in:
Jonathan Frederic 2014-01-16 14:48:56 +00:00
parent bfdebf9632
commit 7387f886c6
2 changed files with 24 additions and 8 deletions

View File

@ -24,11 +24,29 @@ class ContainerWidget(DOMWidget):
# Keys, all private and managed by helper methods. Flexible box model # Keys, all private and managed by helper methods. Flexible box model
# classes... # classes...
children = List(Instance(DOMWidget), sync=True) children = List(Instance(DOMWidget))
_children = List(Instance(DOMWidget), sync=True)
description = Unicode(sync=True)
button_text = Unicode(sync=True) def __init__(self, *pargs, **kwargs):
"""Constructor"""
DOMWidget.__init__(self, *pargs, **kwargs)
self.on_trait_change(self._validate, ['children'])
def _validate(self, name, old, new):
"""Validate children list.
Makes sure only one instance of any given model can exist in the
children list."""
if new is not None and isinstance(new, list):
children = []
for child in new:
if child not in children:
children.append(child)
self._children = children
class ModalWidget(ContainerWidget): class ModalWidget(ContainerWidget):
view_name = Unicode('ModalView', sync=True) view_name = Unicode('ModalView', sync=True)
description = Unicode(sync=True)
button_text = Unicode(sync=True)

View File

@ -14,21 +14,19 @@ pages.
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# Imports # Imports
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
from .widget import DOMWidget from .widget_container import ContainerWidget
from IPython.utils.traitlets import Unicode, Dict, CInt, List, Instance from IPython.utils.traitlets import Unicode, Dict, CInt, List, Instance
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# Classes # Classes
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
class AccordionWidget(DOMWidget): class AccordionWidget(ContainerWidget):
view_name = Unicode('AccordionView', sync=True) view_name = Unicode('AccordionView', sync=True)
# Keys # Keys
_titles = Dict(help="Titles of the pages", sync=True) _titles = Dict(help="Titles of the pages", sync=True)
selected_index = CInt(0, sync=True) selected_index = CInt(0, sync=True)
children = List(Instance(DOMWidget), sync=True)
# Public methods # Public methods
def set_title(self, index, title): def set_title(self, index, title):
"""Sets the title of a container page. """Sets the title of a container page.