import { form } from './Settings' ; import { log } from './Log' ; import { EVT } from './EVT' ; import { store } from './Store' ; interface FilterOption { id ? : number | string ; date ? : string ; fee ? : number ; ext ? : string ; } class Filter { public init ( ) { this . getIdRange ( ) ; this . getDateRange ( ) ; } private _postDateStart = 0 ; private _postDateEnd = 0 ; private getIdRange ( ) { if ( form . idRangeSwitch . checked ) { let id = parseInt ( form . idRangeInput . value ) ; if ( isNaN ( id ) ) { EVT . fire ( EVT . events . crawlError ) ; const msg = 'id is not a number!' ; window . alert ( msg ) ; log . error ( msg ) ; throw new Error ( msg ) ; } } } private getDateRange ( ) { if ( ! form . postDate . checked || form . postDateStart . value === '' || form . postDateEnd . value === '' ) { return ; } const postDateStart = new Date ( form . postDateStart . value ) ; const postDateEnd = new Date ( form . postDateEnd . value ) ; if ( isNaN ( postDateStart . getTime ( ) ) || isNaN ( postDateEnd . getTime ( ) ) ) { const msg = 'Date format error!' ; this . throwError ( msg ) ; } else { this . _postDateStart = postDateStart . getTime ( ) ; this . _postDateEnd = postDateEnd . getTime ( ) ; } } public check ( option ) { if ( ! this . checkFileType ( option . ext ) ) { return false ; } if ( ! this . checkfeeType ( option . fee ) ) { return false ; } if ( ! this . checkfeeRange ( option . fee ) ) { return false ; } if ( ! this . checkIdRange ( option . id ) ) { return false ; } if ( ! this . checkPostDate ( option . date ) ) { return false ; } return true ; } private checkFileType ( ext ) { if ( ! ext ) { return true ; } for ( const [ key , value ] of Object . entries ( store . fileType ) ) { if ( value . includes ( ext ) ) { return form [ key ] . checked ? true : false ; } } return form [ 'other' ] . checked ? true : false ; } private checkfeeType ( fee ) { if ( fee === undefined ) { return true ; } if ( fee > 0 ) { return form . pay . checked ; } else { return form . free . checked ; } } private checkfeeRange ( fee ) { if ( fee === undefined || ! form . feeSwitch . checked ) { return true ; } return fee >= parseInt ( form . fee . value ) ; } private checkIdRange ( id ) { if ( id === undefined || ! form . idRangeSwitch . checked ) { return true ; } const flag = parseInt ( form . idRange . value ) ; const nowId = parseInt ( id . toString ( ) ) ; const setId = parseInt ( form . idRangeInput . value ) ; if ( flag === 1 ) { return nowId > setId ; } else if ( flag === 2 ) { return nowId < setId ; } else { return true ; } } private checkPostDate ( date ) { if ( ! form . postDate . checked || date === undefined || ! this . _postDateStart || ! this . _postDateEnd ) { return true ; } const nowDate = new Date ( date ) ; return ( nowDate . getTime ( ) >= this . _postDateStart && nowDate . getTime ( ) <= this . _postDateEnd ) ; } private throwError ( msg ) { EVT . fire ( EVT . events . crawlError ) ; log . error ( msg , 2 ) ; window . alert ( msg ) ; throw new Error ( msg ) ; } } const filter = new Filter ( ) ; export { filter , FilterOption } ;