import { ElementRef , Renderer2 } from '@angular/core' ; export function isInt ( value ) { if ( isNaN ( value ) ) { return false ; } const x = parseFloat ( value ) ; return ( x | 0 ) === x ; } export function isObject ( value ) { const type = typeof value ; return ! ! value && ( type === 'object' || type === 'function' ) ; } let idCounter = 0 ; export function uniqueId ( prefix = 'uid' ) { return `ngl_${ prefix }_${ ++ idCounter }` ; } export interface IReplaceClass { renderer : Renderer2 ; element : ElementRef ; } export function replaceClass ( instance , oldClass , newClass ? ) { if ( oldClass && oldClass !== newClass ) { setClass ( instance , oldClass , false ) ; } if ( newClass ) { setClass ( instance , newClass , true ) ; } } function setClass ( instance , klasses , isAdd ) { if ( klasses ) { ( Array . isArray ( klasses ) ? klasses : [ klasses ] ) . forEach ( k => { instance . renderer [ isAdd ? 'addClass' : 'removeClass' ] ( instance . element . nativeElement , k ) ; } ) ; } } export function ngClassCombine ( ngClasses , customClasses ) { if ( ! ngClasses ) { return customClasses ; } if ( typeof ngClasses === 'string' ) { ngClasses = ngClasses . split ( /\s+/ ) ; } else if ( ngClasses instanceof Set ) { const a = [ ] ; ngClasses . forEach ( v => a . push ( v ) ) ; ngClasses = a ; } if ( Array . isArray ( ngClasses ) ) { ngClasses = ngClasses . reduce ( ( o , klass ) => { o [ klass ] = true ; return o ; } , { } ) ; } return { ... ngClasses , ... customClasses } ; } param selection param value param multiple export function isOptionSelected ( value , selection , multiple ) { if ( multiple ) { if ( ! selection ) { return false ; } return Array . isArray ( selection ) ? selection . indexOf ( value ) > - 1 : ! ! selection [ value ] ; } return value === selection ; } export function addOptionToSelection ( value , selection , multiple , clearable = false ) { let next ; if ( multiple ) { if ( ! selection ) { selection = [ ] ; } if ( Array . isArray ( selection ) ) { const index = selection . indexOf ( value ) ; next = index > - 1 ? [ ... selection . slice ( 0 , index ) , ... selection . slice ( index + 1 ) ] : [ ... selection , value ] ; } else { next = Object . assign ( { } , selection , { [ value ] : ! selection [ value ] } ) ; } } else { next = selection === value && clearable ? null : value ; } return next ; } export function menuItemScroll ( container , domItem , scrollPadding = 4 ) { if ( domItem . offsetHeight - container . scrollTop + domItem . offsetTop >= container . offsetHeight ) { container . scrollTop = domItem . offsetHeight + domItem . offsetTop - container . offsetHeight + scrollPadding ; } else if ( domItem . offsetTop <= container . scrollTop ) { container . scrollTop = domItem . offsetTop - scrollPadding ; } } export function trapEvent ( event ) { if ( ! event ) { return ; } event . preventDefault ( ) ; event . stopPropagation ( ) ; }