<s> from pyjamas import DOM <EOL> from pyjamas import Factory <EOL> from pyjamas . ui . CellPanel import CellPanel <EOL> from pyjamas . ui import Event <EOL> class StackPanel ( CellPanel ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . visibleStack = - <NUM_LIT:1> <EOL> self . indices = { } <EOL> self . stackListeners = [ ] <EOL> kwargs [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> CellPanel . __init__ ( self , ** kwargs ) <EOL> DOM . sinkEvents ( self . getElement ( ) , Event . ONCLICK ) <EOL> def addStackChangeListener ( self , listener ) : <EOL> self . stackListeners . append ( listener ) <EOL> def add ( self , widget , stackText = "<STR_LIT>" , asHTML = False ) : <EOL> widget . removeFromParent ( ) <EOL> index = self . getWidgetCount ( ) <EOL> tr = DOM . createTR ( ) <EOL> td = DOM . createTD ( ) <EOL> DOM . appendChild ( self . body , tr ) <EOL> DOM . appendChild ( tr , td ) <EOL> self . setStyleName ( td , "<STR_LIT>" , True ) <EOL> self . _setIndex ( td , index ) <EOL> DOM . setAttribute ( td , "<STR_LIT>" , "<STR_LIT>" ) <EOL> tr = DOM . createTR ( ) <EOL> td = DOM . createTD ( ) <EOL> DOM . appendChild ( self . body , tr ) <EOL> DOM . appendChild ( tr , td ) <EOL> DOM . setAttribute ( td , "<STR_LIT>" , "<STR_LIT>" ) <EOL> DOM . setAttribute ( td , "<STR_LIT>" , "<STR_LIT>" ) <EOL> CellPanel . add ( self , widget , td ) <EOL> self . setStackVisible ( index , False ) <EOL> if self . visibleStack == - <NUM_LIT:1> : <EOL> self . showStack ( <NUM_LIT:0> ) <EOL> if stackText != "<STR_LIT>" : <EOL> self . setStackText ( self . getWidgetCount ( ) - <NUM_LIT:1> , stackText , asHTML ) <EOL> def onBrowserEvent ( self , event ) : <EOL> if DOM . eventGetType ( event ) == "<STR_LIT>" : <EOL> index = self . getDividerIndex ( DOM . eventGetTarget ( event ) ) <EOL> if index != - <NUM_LIT:1> : <EOL> self . showStack ( index ) <EOL> def remove ( self , child , index = None ) : <EOL> if index is None : <EOL> if isinstance ( child , int ) : <EOL> index = child <EOL> child = self . getWidget ( child ) <EOL> else : <EOL> index = self . getWidgetIndex ( child ) <EOL> if child . getParent ( ) != self : <EOL> return False <EOL> if self . visibleStack == index : <EOL> self . visibleStack = - <NUM_LIT:1> <EOL> elif self . visibleStack > index : <EOL> self . visibleStack -= <NUM_LIT:1> <EOL> rowIndex = <NUM_LIT:2> * index <EOL> tr = DOM . getChild ( self . body , rowIndex ) <EOL> DOM . removeChild ( self . body , tr ) <EOL> tr = DOM . getChild ( self . body , rowIndex ) <EOL> DOM . removeChild ( self . body , tr ) <EOL> CellPanel . remove ( self , child ) <EOL> rows = self . getWidgetCount ( ) * <NUM_LIT:2> <EOL> for i in range ( rowIndex , rows , <NUM_LIT:2> ) : <EOL> childTR = DOM . getChild ( self . body , i ) <EOL> td = DOM . getFirstChild ( childTR ) <EOL> curIndex = self . _getIndex ( td ) <EOL> self . _setIndex ( td , index ) <EOL> index += <NUM_LIT:1> <EOL> return True <EOL> def _setIndex ( self , td , index ) : <EOL> self . indices [ td ] = index <EOL> def _getIndex ( self , td ) : <EOL> return self . indices . get ( td ) <EOL> def setStackText ( self , index , text , asHTML = False ) : <EOL> if index >= self . getWidgetCount ( ) : <EOL> return <EOL> td = DOM . getChild ( DOM . getChild ( self . body , index * <NUM_LIT:2> ) , <NUM_LIT:0> ) <EOL> if asHTML : <EOL> DOM . setInnerHTML ( td , text ) <EOL> else : <EOL> DOM . setInnerText ( td , text ) <EOL> def showStack ( self , index ) : <EOL> if ( index >= self . getWidgetCount ( ) ) or ( index == self . visibleStack ) : <EOL> return <EOL> if self . visibleStack >= <NUM_LIT:0> : <EOL> self . setStackVisible ( self . visibleStack , False ) <EOL> self . visibleStack = index <EOL> self . setStackVisible ( self . visibleStack , True ) <EOL> for listener in self . stackListeners : <EOL> listener . onStackChanged ( self , index ) <EOL> def getDividerIndex ( self , elem ) : <EOL> while ( elem is not None ) and not DOM . compare ( elem , self . getElement ( ) ) : <EOL> expando = self . _getIndex ( elem ) <EOL> if expando is not None : <EOL> return int ( expando ) <EOL> elem = DOM . getParent ( elem ) <EOL> return - <NUM_LIT:1> <EOL> def setStackVisible ( self , index , visible ) : <EOL> tr = DOM . getChild ( self . body , ( index * <NUM_LIT:2> ) ) <EOL> if tr is None : <EOL> return <EOL> td = DOM . getFirstChild ( tr ) <EOL> self . setStyleName ( td , "<STR_LIT>" , visible ) <EOL> tr = DOM . getChild ( self . body , ( index * <NUM_LIT:2> ) + <NUM_LIT:1> ) <EOL> self . setVisible ( tr , visible ) <EOL> self . getWidget ( index ) . setVisible ( visible ) <EOL> def getSelectedIndex ( self ) : <EOL> return self . visibleStack <EOL> Factory . registerClass ( '<STR_LIT>' , '<STR_LIT>' , StackPanel ) </s>
