import { EVT } from './EVT' ; import { store } from './Store' ; import { SettingsForm } from './Settings.d' ; interface fanboxSetting { image : boolean ; music : boolean ; video : boolean ; compressed : boolean ; ps : boolean ; other : boolean ; free : boolean ; pay : boolean ; feeSwitch : boolean ; fee : number ; idRangeSwitch : boolean ; idRangeInput : number ; postDate : boolean ; postDateStart : string ; postDateEnd : string ; saveLink : boolean ; saveText : boolean ; userSetName : string ; quietDownload : boolean ; downloadThread : number ; dateFormat : string ; savePostCover : boolean ; } interface SettingChangeData { name : keyof fanboxSetting ; value : string | number | boolean ; } class SaveSettings { constructor ( form ) { this . form = form ; this . bindOptionEvent ( ) ; window . addEventListener ( EVT . events . settingChange , ( event ) => { const data = event . detail . data as SettingChangeData ; if ( Reflect . has ( this . optionDefault , data . name ) ) { if ( ( this . options [ data . name ] as any ) !== data . value ) { ; ( this . options [ data . name ] as any ) = data . value ; localStorage . setItem ( this . storeName , JSON . stringify ( this . options ) ) ; } } } ) ; this . restoreOption ( ) ; } private form : SettingsForm ; private readonly storeName = 'fanboxSetting' ; private readonly optionDefault : fanboxSetting = { image : true , music : true , video : true , compressed : true , ps : true , other : true , free : true , pay : true , feeSwitch : false , fee : 500 , idRangeSwitch : false , idRangeInput : 0 , postDate : false , postDateStart : '' , postDateEnd : '' , saveLink : true , saveText : false , userSetName : store . defaultFileName , quietDownload : true , downloadThread : 3 , dateFormat : 'YYYY-MM-DD hh-mm' , savePostCover : false , } ; private options : fanboxSetting = Object . assign ( { } , this . optionDefault ) ; private restoreBoolean ( name ) { if ( typeof this . options [ name ] === 'boolean' ) { this . form [ name ] . checked = Boolean ( this . options [ name ] ) ; } else { this . form [ name ] . checked = Boolean ( this . optionDefault [ name ] ) ; } } private restoreString ( name ) { if ( this . options [ name ] !== undefined ) { this . form [ name ] . value = this . options [ name ] . toString ( ) ; } else { this . form [ name ] . value = this . optionDefault [ name ] . toString ( ) ; } } private restoreOption ( ) { const savedOption = localStorage . getItem ( this . storeName ) ; if ( savedOption ) { Object . assign ( this . options , JSON . parse ( savedOption ) ) ; } else { return ; } this . restoreString ( 'fee' ) ; this . restoreString ( 'idRangeInput' ) ; this . restoreString ( 'postDateStart' ) ; this . restoreString ( 'postDateEnd' ) ; this . restoreString ( 'userSetName' ) ; this . restoreString ( 'downloadThread' ) ; this . restoreBoolean ( 'image' ) ; this . restoreBoolean ( 'music' ) ; this . restoreBoolean ( 'video' ) ; this . restoreBoolean ( 'compressed' ) ; this . restoreBoolean ( 'ps' ) ; this . restoreBoolean ( 'other' ) ; this . restoreBoolean ( 'free' ) ; this . restoreBoolean ( 'pay' ) ; this . restoreBoolean ( 'feeSwitch' ) ; this . restoreBoolean ( 'idRangeSwitch' ) ; this . restoreBoolean ( 'postDate' ) ; this . restoreBoolean ( 'saveLink' ) ; this . restoreBoolean ( 'saveText' ) ; this . restoreBoolean ( 'quietDownload' ) ; this . restoreString ( 'dateFormat' ) ; this . restoreBoolean ( 'savePostCover' ) ; } private saveTextInput ( name ) { const el = this . form [ name ] as HTMLInputElement ; el . addEventListener ( 'change' , ( ) => { this . emitChange ( name , el . value ) ; } ) ; } private saveCheckBox ( name ) { const el = this . form [ name ] as HTMLInputElement ; el . addEventListener ( 'click' , ( ) => { this . emitChange ( name , el . checked ) ; } ) ; } private saveRadio ( name ) { const radios = this . form [ name ] ; for ( const radio of radios ) { radio . addEventListener ( 'click' , ( ) => { this . emitChange ( name , radio . value ) ; } ) ; } } private bindOptionEvent ( ) { this . saveTextInput ( 'fee' ) ; this . saveTextInput ( 'idRangeInput' ) ; this . saveTextInput ( 'postDateStart' ) ; this . saveTextInput ( 'postDateEnd' ) ; this . saveTextInput ( 'downloadThread' ) ; this . saveCheckBox ( 'image' ) ; this . saveCheckBox ( 'music' ) ; this . saveCheckBox ( 'video' ) ; this . saveCheckBox ( 'compressed' ) ; this . saveCheckBox ( 'ps' ) ; this . saveCheckBox ( 'other' ) ; this . saveCheckBox ( 'free' ) ; this . saveCheckBox ( 'pay' ) ; this . saveCheckBox ( 'feeSwitch' ) ; this . saveCheckBox ( 'idRangeSwitch' ) ; this . saveRadio ( 'idRange' ) ; this . saveCheckBox ( 'postDate' ) ; this . saveCheckBox ( 'saveLink' ) ; this . saveCheckBox ( 'saveText' ) ; this . saveCheckBox ( 'quietDownload' ) ; this . saveTextInput ( 'dateFormat' ) ; this . saveCheckBox ( 'savePostCover' ) ; const userSetNameInput = this . form . userSetName ; [ 'change' , 'focus' ] . forEach ( ( ev ) => { userSetNameInput . addEventListener ( ev , ( ) => { this . emitChange ( 'userSetName' , userSetNameInput . value ) ; } ) ; } ) ; window . addEventListener ( EVT . events . resetOption , ( ) => { this . form . reset ( ) ; this . reset ( ) ; } ) ; } private emitChange ( name , value ) { EVT . fire ( EVT . events . settingChange , { name : name , value : value } ) ; } private reset ( ) { Object . assign ( this . options , this . optionDefault ) ; localStorage . setItem ( this . storeName , JSON . stringify ( this . options ) ) ; this . restoreOption ( ) ; EVT . fire ( EVT . events . settingChange ) ; } } export { SaveSettings } ;