import { showMessage } from '@/components/mixins/showMessage' ; import { debounce } from 'lodash' ; import mixins from 'vue-typed-mixins' ; export const genericHelpers = mixins ( showMessage ) . extend ( { data ( ) { return { loadingService : null as any | null , debouncedFunctions : [ ] as any [ ] , } ; } , computed : { isReadOnly ( ) { if ( [ 'NodeViewExisting' , 'NodeViewNew' ] . includes ( this . $route . name as string ) ) { return false ; } return true ; } , } , methods : { displayTimer ( msPassed , showMs = false ) { if ( msPassed < 60000 ) { if ( showMs === false ) { return `${ Math . floor ( msPassed / 1000 ) } ${ this . $locale . baseText ( 'genericHelpers.sec' ) }` ; } return `${ msPassed / 1000 } ${ this . $locale . baseText ( 'genericHelpers.sec' ) }` ; } const secondsPassed = Math . floor ( msPassed / 1000 ) ; const minutesPassed = Math . floor ( secondsPassed / 60 ) ; const secondsLeft = ( secondsPassed - ( minutesPassed * 60 ) ) . toString ( ) . padStart ( 2 , '0' ) ; return `${ minutesPassed }:${ secondsLeft } ${ this . $locale . baseText ( 'genericHelpers.min' ) }` ; } , editAllowedCheck ( ) { if ( this . isReadOnly ) { this . $showMessage ( { title : this . $locale . baseText ( 'genericHelpers.showMessage.title' ) , message : this . $locale . baseText ( 'genericHelpers.showMessage.message' ) , type : 'error' , duration : 0 , } ) ; return false ; } return true ; } , startLoading ( text ? ) { if ( this . loadingService !== null ) { return ; } this . loadingService = this . $loading ( { lock : true , text : text || this . $locale . baseText ( 'genericHelpers.loading' ) , spinner : 'el-icon-loading' , background : 'rgba(255, 255, 255, 0.8)' , } ) ; } , setLoadingText ( text ) { this . loadingService . text = text ; } , stopLoading ( ) { if ( this . loadingService !== null ) { this . loadingService . close ( ) ; this . loadingService = null ; } } , async callDebounced ( ... inputParameters ) { const functionName = inputParameters . shift ( ) as string ; const debounceTime = inputParameters . shift ( ) as number ; if ( this . debouncedFunctions [ functionName ] === undefined ) { this . debouncedFunctions [ functionName ] = debounce ( this [ functionName ] , debounceTime , { leading : true } ) ; } await this . debouncedFunctions [ functionName ] . apply ( this , inputParameters ) ; } , } , } ) ;