import { isNumber , coerceNumberValue , isUndefinedOrNull } from 'ngx-tethys/util' ; import { Inject , ElementRef , Renderer2 , NgZone , Input , EventEmitter , Component , Injectable } from '@angular/core' ; import { THY_UPLOADER_DEFAULT_OPTIONS , ThyUploaderConfig } from './uploader.config' ; import { ThyFileSelectEvent , ThySizeExceedsHandler } from './types' ; @ Component ( { template : `` } ) export class FileSelectBaseComponent { acceptType : string ; sizeThreshold : number ; sizeExceedsHandler : ThySizeExceedsHandler ; @ Input ( ) set thySizeThreshold ( value ) { const sizeThreshold = coerceNumberValue ( value ) ; if ( isNumber ( sizeThreshold ) ) { this . sizeThreshold = sizeThreshold ; } } @ Input ( ) set thySizeExceedsHandler ( value ) { if ( value ) { this . sizeExceedsHandler = value ; } } constructor ( public elementRef , @ Inject ( THY_UPLOADER_DEFAULT_OPTIONS ) public defaultConfig , public renderer ? , public ngZone ? ) { this . sizeThreshold = defaultConfig . sizeThreshold ; this . sizeExceedsHandler = defaultConfig . sizeExceedsHandler ; } handleSizeExceeds ( event , files ) { let sizeExceedsFiles = files . filter ( item => item . size / 1024 > this . sizeThreshold ) ; if ( sizeExceedsFiles . length > 0 ) { const sizeExceedContext = { files : files , exceedsFiles : sizeExceedsFiles , nativeEvent : event , sizeThreshold : this . sizeThreshold } ; return this . sizeExceedsHandler ( sizeExceedContext ) ; } return files ; } selectFiles ( event , files , eventEmitter ) { let successFiles = files ; if ( this . sizeThreshold && this . sizeThreshold > 0 ) { successFiles = this . handleSizeExceeds ( event , files ) ; } if ( successFiles ) { eventEmitter . emit ( { files : successFiles , nativeEvent : event } ) ; } } }