namespace Views { export class SafetyButton extends Views . View < HTMLDivElement > { private _disabled : boolean ; private _primed : boolean ; private _primary : HTMLDivElement ; private _confirm : HTMLDivElement ; public callback : Function ; constructor ( text ? , dangerous ? ) { super ( Templates . cloneTemplate < HTMLDivElement > ( 'safety-button' ) ) ; this . _disabled = false ; this . _primed = false ; this . _primary = < HTMLDivElement > this . root . children [ 0 ] ; this . _primary . onclick = this . _onPrimaryClick ; this . _confirm = < HTMLDivElement > this . root . children [ 2 ] ; this . _confirm . onclick = this . _onConfirmClick ; if ( text ) this . text = text ; if ( dangerous ) this . dangerous = dangerous ; document . body . addEventListener ( 'click' , this . _onClickCaptured , true ) ; } private _onClickCaptured = ( event ) => { if ( ( this . _primed ) && ( event . target != this . _primary ) && ( event . target != this . _confirm ) ) { this . primed = false ; } } ; private _onPrimaryClick = ( ) => { if ( ! this . _disabled ) { this . primed = ( ! this . _primed ) ; } } ; private _onConfirmClick = ( ) => { if ( this . _primed ) { this . primed = false ; if ( ( ! this . _disabled ) && ( this . callback ) ) { this . callback ( ) ; } } } ; public get text ( ) : string { return this . _primary . innerText ; } public set text ( text ) { this . _primary . innerText = text ; this . _confirm . title = text ; if ( ! this . _primed ) this . _primary . title = text ; } public get primed ( ) : boolean { return this . _primed ; } public set primed ( value ) { if ( ! value ) { this . _primed = false ; this . root . classList . remove ( 'primed' ) ; this . _primary . title = this . _primary . innerText ; } else { this . root . classList . add ( 'primed' ) ; this . _primed = true ; this . _primary . title = "cancel" ; } } public get disabled ( ) : boolean { return this . _disabled ; } public set disabled ( value ) { if ( ! value ) { this . _disabled = false ; this . root . classList . remove ( 'disabled' ) ; } else { this . primed = false ; this . _disabled = true ; this . root . classList . add ( 'disabled' ) ; } } public get dangerous ( ) : boolean { return this . _confirm . classList . contains ( 'negative' ) ; } public set dangerous ( dangerous ) { if ( dangerous ) { this . _confirm . classList . add ( 'negative' ) ; this . _confirm . classList . remove ( 'positive' ) ; } else { this . _confirm . classList . add ( 'positive' ) ; this . _confirm . classList . remove ( 'negative' ) ; } } } }