author suo
  module tool { export class ToggleButtonGroup { private _changeHandler : Handler ; private _btnGroup : ToggleButton [ ] = [ ] ; private _lastClickBtn : ToggleButton ; private _clickSameObject : boolean = false ; public index : number = - 1 ; public constructor ( ... args ) { var len = args . length ; for ( var i = 0 ; i < len ; i ++ ) { if ( args [ i ] instanceof ToggleButton ) { var btn = args [ i ] ; btn . mouseClickHandler = Handler . create ( this , this . _onClick ) ; this . _btnGroup . push ( btn ) ; } } } /**
         * 设置是否互斥
         */ public set clickSameObject ( value ) { this . _clickSameObject = value ; } /**
         * 获得长度
         */ public get length ( ) : number { return this . _btnGroup . length ; } /**
         * 设置切页函数
         */ public set changeHandler ( value ) { this . _changeHandler = value ; } /**
         * 获得该按钮在按钮组内的索引
         */ public getIndexByBtn ( btn ) { return this . _btnGroup . indexOf ( btn ) ; } /**
         * 通过索引设置点击哪个按钮
         */ public set clickByIndex ( index ) { var btn = this . _btnGroup [ index ] ; if ( btn ) { this . _onClick ( btn ) ; } } /**
         * 根据索引获得按钮
         */ public getBtnByIndex ( index ) { return this . _btnGroup [ index ] ; } /**
         * 点击函数
         */ private _onClick ( targetBtn ) { if ( this . _lastClickBtn ) { if ( this . _lastClickBtn == targetBtn && ! this . _clickSameObject ) { return ; } this . _lastClickBtn . selected = false ; } targetBtn . selected = true ; this . _lastClickBtn = targetBtn ; this . index = this . getIndexByBtn ( targetBtn ) ; if ( this . _changeHandler ) { this . _changeHandler . runWith ( targetBtn ) ; } } /**
         * 获得上一次点击按钮
         */ public get lastClickBtn ( ) : ToggleButton { return this . _lastClickBtn ; } /**
         * 添加按钮
         */ public push ( btn ) { btn . mouseClickHandler = Handler . create ( this , this . _onClick ) ; this . _btnGroup . push ( btn ) ; } /**
         * 释放
         */ public dispose ( ) { if ( this . _changeHandler ) { this . _changeHandler . recover ( ) ; this . _changeHandler = null ; } for ( var i = 0 ; i < this . _btnGroup . length ; i ++ ) { this . _btnGroup [ i ] . dispose ( ) ; this . _btnGroup [ i ] = null ; } this . _btnGroup . length = 0 ; this . _btnGroup = null ; } /**
         * 清理
         */ public clear ( ) { if ( this . _changeHandler ) { this . _changeHandler . recover ( ) ; this . _changeHandler = null ; } for ( var i = 0 ; i < this . _btnGroup . length ; i ++ ) { this . _btnGroup [ i ] . dispose ( ) ; this . _btnGroup [ i ] = null ; } this . _btnGroup . length = 0 ; } } }