import { preventDefault } from './dom-event' ; export type GenericHash = { [ key ] : any ; } ; let guidNumber = 0 ; export function guid ( ) { guidNumber += 1 ; return String ( guidNumber ) ; } export function disableCursor ( ) { document . body . classList . add ( 'fc-not-allowed' ) ; } export function enableCursor ( ) { document . body . classList . remove ( 'fc-not-allowed' ) ; } export function preventSelection ( el ) { el . classList . add ( 'fc-unselectable' ) ; el . addEventListener ( 'selectstart' , preventDefault ) ; } export function allowSelection ( el ) { el . classList . remove ( 'fc-unselectable' ) ; el . removeEventListener ( 'selectstart' , preventDefault ) ; } export function preventContextMenu ( el ) { el . addEventListener ( 'contextmenu' , preventDefault ) ; } export function allowContextMenu ( el ) { el . removeEventListener ( 'contextmenu' , preventDefault ) ; } export interface OrderSpec < Subject > { field ? : string ; order ? : number ; func ? : FieldSpecInputFunc < Subject > ; } export type FieldSpecInput < Subject > = string | string [ ] | FieldSpecInputFunc < Subject > | FieldSpecInputFunc < Subject > [ ] ; export type FieldSpecInputFunc < Subject > = ( a : Subject , b : Subject ) => number ; export function parseFieldSpecs < Subject > ( input ) { let specs = [ ] ; let tokens = [ ] ; let i ; let token ; if ( typeof input === 'string' ) { tokens = input . split ( /\s*,\s*/ ) ; } else if ( typeof input === 'function' ) { tokens = [ input ] ; } else if ( Array . isArray ( input ) ) { tokens = input ; } for ( i = 0 ; i < tokens . length ; i += 1 ) { token = tokens [ i ] ; if ( typeof token === 'string' ) { specs . push ( token . charAt ( 0 ) === '-' ? { field : token . substring ( 1 ) , order : - 1 } : { field : token , order : 1 } ) ; } else if ( typeof token === 'function' ) { specs . push ( { func : token } ) ; } } return specs ; } export function compareByFieldSpecs < Subject > ( obj0 , obj1 , fieldSpecs ) { let i ; let cmp ; for ( i = 0 ; i < fieldSpecs . length ; i += 1 ) { cmp = compareByFieldSpec ( obj0 , obj1 , fieldSpecs [ i ] ) ; if ( cmp ) { return cmp ; } } return 0 ; } export function compareByFieldSpec < Subject > ( obj0 , obj1 , fieldSpec ) { if ( fieldSpec . func ) { return fieldSpec . func ( obj0 , obj1 ) ; } return flexibleCompare ( obj0 [ fieldSpec . field ] , obj1 [ fieldSpec . field ] ) * ( fieldSpec . order || 1 ) ; } export function flexibleCompare ( a , b ) { if ( ! a && ! b ) { return 0 ; } if ( b == null ) { return - 1 ; } if ( a == null ) { return 1 ; } if ( typeof a === 'string' || typeof b === 'string' ) { return String ( a ) . localeCompare ( String ( b ) ) ; } return a - b ; } export function padStart ( val , len ) { let s = String ( val ) ; return '000' . substr ( 0 , len - s . length ) + s ; } export function formatWithOrdinals < Args extends any [ ] > ( formatter , args , fallbackText ) { if ( typeof formatter === 'function' ) { return formatter ( ... args ) ; } if ( typeof formatter === 'string' ) { return args . reduce ( ( str , arg , index ) => ( str . replace ( '$' + index , arg || '' ) ) , formatter ) ; } return fallbackText ; } export function compareNumbers ( a , b ) { return a - b ; } export function isInt ( n ) { return n % 1 === 0 ; } export function firstDefined ( ... args ) { for ( let i = 0 ; i < args . length ; i += 1 ) { if ( args [ i ] !== undefined ) { return args [ i ] ; } } return undefined ; } export function computeSmallestCellWidth ( cellEl ) { let allWidthEl = cellEl . querySelector ( '.fc-scrollgrid-shrink-frame' ) ; let contentWidthEl = cellEl . querySelector ( '.fc-scrollgrid-shrink-cushion' ) ; if ( ! allWidthEl ) { throw new Error ( 'needs fc-scrollgrid-shrink-frame className' ) ; } if ( ! contentWidthEl ) { throw new Error ( 'needs fc-scrollgrid-shrink-cushion className' ) ; } return cellEl . getBoundingClientRect ( ) . width - allWidthEl . getBoundingClientRect ( ) . width + contentWidthEl . getBoundingClientRect ( ) . width ; }