import * as React from 'react' ; import _isArray from 'lodash/isArray' ; import OlLayer from 'ol/layer/Layer' ; import OlImageWMS from 'ol/source/ImageWMS' ; import OlTileWMS from 'ol/source/TileWMS' ; export type TimeLayerAwareConfig = { isWmsTime ? : boolean ; layer : OlLayer < OlImageWMS | OlTileWMS > ; customHandler ? : ( values : any ) => void ; } ; param params return const findTimeParam = ( params ) => { const keys = Object . keys ( params ) ; let foundKey = 'TIME' ; keys . some ( key => { const lcKey = key && key . toLowerCase && key . toLowerCase ( ) ; if ( lcKey === 'time' ) { foundKey = key ; return true ; } return false ; } ) ; return foundKey ; } ; param WrappedComponent param layers return export function timeLayerAware < P > ( WrappedComponent , layers ) { return class TimeLayerAware extends React . Component < Omit < P , 'onChange' > > { timeChanged = newValues => { layers . forEach ( config => { if ( config . isWmsTime ) { const parms = config . layer . getSource ( ) . getParams ( ) ; const timeParam = findTimeParam ( parms ) ; if ( _isArray ( newValues ) ) { parms [ timeParam ] = `${ newValues [ 0 ] }/${ newValues [ 1 ] }` ; } else { parms [ timeParam ] = `${ newValues }` ; } config . layer . getSource ( ) . updateParams ( parms ) ; } if ( config . customHandler ) { config . customHandler ( newValues ) ; } } ) ; } ; return render = ( ) => { return < WrappedComponent onChange = { this . timeChanged } { ... this . props as P } />; } ; } ; } export default timeLayerAware ;