/**
 * The OptionManager handles the option that are saved in the localStorage.
 */ export class OptionManager { param name returns private static getInputElement ( name ) { const el = < HTMLInputElement > document . getElementById ( "input" + name ) ; const el2 = < HTMLInputElement > document . getElementById ( "input" + name [ 0 ] . toUpperCase ( ) + name . substr ( 1 ) ) ; return el ? el : el2 ; } param name returns private static getInputElementsByName ( name ) { const el = document . getElementsByName ( "input" + name ) ; const el2 = document . getElementsByName ( "input" + name [ 0 ] . toUpperCase ( ) + name . substr ( 1 ) ) ; if ( el . length > 0 ) return < NodeListOf < HTMLInputElement > > el ; else return < NodeListOf < HTMLInputElement > > el2 ; } param name param onChangeCallBack description static boolean ( { name , defaultValue , onChange } ) { const el = OptionManager . getInputElement ( name ) ; const initialValue = ( localStorage [ name ] == undefined ) ? defaultValue : parseBoolean ( localStorage [ name ] ) ; el . checked = initialValue ; onChange ( el . checked ) ; el . onchange = ( ) => { localStorage [ name ] = el . checked ; onChange ( el . checked ) ; } ; } static toggle ( name ) { const el = OptionManager . getInputElement ( name ) ; el . click ( ) ; } static string ( { name , defaultValue , onChange } ) { const initialValue = ( localStorage [ name ] == undefined ) ? defaultValue : localStorage [ name ] ; const el = OptionManager . getInputElement ( name ) ; onChange ( initialValue ) ; if ( el != undefined ) { el . value = initialValue ; el . oninput = ( ) => { localStorage [ name ] = el . value ; onChange ( el . value ) ; } ; } else { const els = OptionManager . getInputElementsByName ( name ) ; for ( let i = 0 ; i < els . length ; i ++ ) { const radioInput = els [ i ] ; if ( radioInput . value == initialValue ) radioInput . checked = true ; radioInput . onclick = ( ) => { localStorage [ name ] = radioInput . value ; onChange ( radioInput . value ) ; } ; } } } static number ( { name , defaultValue , onChange } ) { const el = OptionManager . getInputElement ( name ) ; const initialValue = ( localStorage [ name ] == undefined ) ? defaultValue : localStorage [ name ] ; el . value = "" + initialValue ; onChange ( el . value ) ; el . oninput = ( ) => { localStorage [ name ] = el . value ; onChange ( el . value ) ; } ; } } function parseBoolean ( str ) { if ( str == "true" ) return true ; if ( str == "false" ) return false ; throw `string ${ str } does not represent a boolean value` ; }