@@ -132,6 +132,8 @@ const Map = props => {
132132 selectedDeviceId,
133133 selectedMapLayerState,
134134 center,
135+ onDeviceClick,
136+ navigateOnClick,
135137 devices,
136138 zoom,
137139 setSelectedDeviceForSingleDeviceView,
@@ -154,8 +156,11 @@ const Map = props => {
154156 )
155157 } )
156158 leafletMarker . on ( 'click' , e => {
159+ if ( navigateOnClick ) {
160+ setSelectedDeviceForSingleDeviceView ( device_id )
161+ }
162+ onDeviceClick ( device_id )
157163 e . target . closePopup ( )
158- setSelectedDeviceForSingleDeviceView ( device_id )
159164 } )
160165 leafletMarker . on ( 'mouseout' , e => {
161166 popupTimeouts . forEach ( timeout => {
@@ -182,7 +187,7 @@ const Map = props => {
182187 deviceData . name ,
183188 deviceData . timezone ,
184189 deviceData . location . geometry . coordinates ,
185- deviceData . inactive ,
190+ deviceData . inactive
186191 )
187192 )
188193 )
@@ -239,12 +244,7 @@ const Map = props => {
239244 //generate marker to be added to relevant sublayer
240245 const getNewMarker = deviceData => {
241246 const { device_id, location } = deviceData
242- if (
243- ! (
244- location &&
245- location . geometry &&
246- location . geometry . coordinates )
247- )
247+ if ( ! ( location && location . geometry && location . geometry . coordinates ) )
248248 return null
249249 let reversedCoordinates = [ ...location . geometry . coordinates ]
250250
@@ -378,8 +378,8 @@ const Map = props => {
378378 maxZoom : 21
379379 }
380380 )
381- if ( currentLayer ) {
382- mapRef . current . removeLayer ( currentLayer )
381+ if ( currentLayer ) {
382+ mapRef . current . removeLayer ( currentLayer )
383383 mapRef . current . addLayer ( newLayer )
384384 setCurrentLayer ( newLayer )
385385 }
@@ -401,161 +401,154 @@ const Map = props => {
401401 }
402402 } , [ ] ) // eslint-disable-line
403403
404- useLayoutEffect (
405- ( ) => {
406- const device = devicesObj [ selectedDeviceId ]
407- if ( device ) {
408- const distance = mapRef . current . distance (
409- mapRef . current . getCenter ( ) ,
410- device . _latlng
411- ) // calculate distance to adjust animation time and timeout for popup
412- const duration = Math . floor ( distance > 10000 ? 4 : 1 )
413- mapRef . current . flyTo ( [ device . _latlng . lat , device . _latlng . lng ] , 18 , {
414- animate : true ,
415- duration : duration
416- } )
417- popupTimeouts . push (
418- setTimeout ( function ( ) {
419- setFollow ( { device_id : selectedDeviceId } )
420- device . openPopup ( )
421- } , duration * 1000 + 500 )
422- )
423- popupTimeouts . push ( )
424- }
425- return ( ) => {
426- popupTimeouts . forEach ( timeout => {
427- clearTimeout ( timeout )
428- } )
429- }
430- } ,
431- [ selectedDeviceId ]
432- )
404+ useLayoutEffect ( ( ) => {
405+ const device = devicesObj [ selectedDeviceId ]
406+ if ( device ) {
407+ const distance = mapRef . current . distance (
408+ mapRef . current . getCenter ( ) ,
409+ device . _latlng
410+ ) // calculate distance to adjust animation time and timeout for popup
411+ const duration = Math . floor ( distance > 10000 ? 4 : 1 )
412+ mapRef . current . flyTo ( [ device . _latlng . lat , device . _latlng . lng ] , 18 , {
413+ animate : true ,
414+ duration : duration
415+ } )
416+ popupTimeouts . push (
417+ setTimeout ( function ( ) {
418+ setFollow ( { device_id : selectedDeviceId } )
419+ device . openPopup ( )
420+ } , duration * 1000 + 500 )
421+ )
422+ popupTimeouts . push ( )
423+ }
424+ return ( ) => {
425+ popupTimeouts . forEach ( timeout => {
426+ clearTimeout ( timeout )
427+ } )
428+ }
429+ } , [ selectedDeviceId ] )
433430
434431 useLayoutEffect (
435432 ( ) => {
436- if ( subscriptionData && subscriptionData . device_id ) {
437- const subscriptionDevice = utils . parseMovementStatus (
438- subscriptionData ,
439- devicesMap [ subscriptionData . device_id ]
440- )
433+ if ( subscriptionData && subscriptionData . device_id ) {
434+ const subscriptionDevice = utils . parseMovementStatus (
435+ subscriptionData ,
436+ devicesMap [ subscriptionData . device_id ]
437+ )
441438
442- if ( devicesObj [ subscriptionDevice . device_id ] && mapRef . current ) {
443- const existingDevice = devicesObj [ subscriptionDevice . device_id ]
444- const location = subscriptionDevice . location
445- if (
446- location && location . geometry && location . geometry . coordinates
447- ) {
448- //bindPopup
449- bindPopup ( existingDevice , subscriptionDevice )
450-
451- //set new icon
452- existingDevice . setIcon ( getIcon ( subscriptionDevice ) )
453- //slide animation to new location
454- activeClusterLayer . current . removeLayer ( existingDevice )
455- activeClusterLayer . current . addLayer ( existingDevice )
456- try {
457- existingDevice . slideTo (
458- // this errors out when device_name changed through mobile SDK? Why?
459- [
460- location . geometry . coordinates [ 0 ] ,
461- location . geometry . coordinates [ 1 ]
462- ] ,
463- {
464- duration : 2000 ,
465- keepAtCenter :
466- follow . device_id &&
467- follow . device_id === subscriptionDevice . device_id
468- ? true
469- : false
470- }
471- )
472- } catch ( err ) {
473- console . log ( err )
474- }
475- }
476- } else if (
477- subscriptionDevice &&
478- subscriptionDevice . location &&
479- subscriptionDevice . location . data
480- ) {
481- const newMarker = getNewMarker ( subscriptionDevice )
482- devicesObj [ subscriptionDevice . device_id ] = newMarker
483- if (
484- subscriptionDevice . device_status === CONSTANTS . movementStatus . active
485- ) {
486- try {
487- if (
488- activeClusterLayer &&
489- activeClusterLayer . current &&
490- activeClusterLayer . current . refreshClusters &&
491- activeClusterLayer . current . addLayer
492- ) {
493- activeClusterLayer . current . addLayer ( newMarker )
494- // activeClusterLayer.current.refreshClusters(newMarker)
495- activeClusterLayer . current . refreshClusters ( )
439+ if ( devicesObj [ subscriptionDevice . device_id ] && mapRef . current ) {
440+ const existingDevice = devicesObj [ subscriptionDevice . device_id ]
441+ const location = subscriptionDevice . location
442+ if ( location && location . geometry && location . geometry . coordinates ) {
443+ //bindPopup
444+ bindPopup ( existingDevice , subscriptionDevice )
445+
446+ //set new icon
447+ existingDevice . setIcon ( getIcon ( subscriptionDevice ) )
448+ //slide animation to new location
449+ activeClusterLayer . current . removeLayer ( existingDevice )
450+ activeClusterLayer . current . addLayer ( existingDevice )
451+ try {
452+ existingDevice . slideTo (
453+ // this errors out when device_name changed through mobile SDK? Why?
454+ [
455+ location . geometry . coordinates [ 0 ] ,
456+ location . geometry . coordinates [ 1 ]
457+ ] ,
458+ {
459+ duration : 2000 ,
460+ keepAtCenter :
461+ follow . device_id &&
462+ follow . device_id === subscriptionDevice . device_id
463+ ? true
464+ : false
465+ }
466+ )
467+ } catch ( err ) {
468+ console . log ( err )
496469 }
497- if (
498- mainClusterLayer &&
499- mainClusterLayer . current &&
500- mainClusterLayer . current . refreshClusters
501- )
502- mainClusterLayer . current . refreshClusters ( )
503- if (
504- nonActiveClusterLayer &&
505- nonActiveClusterLayer . current &&
506- nonActiveClusterLayer . current . refreshClusters
507- )
508- nonActiveClusterLayer . current . refreshClusters ( )
509- } catch ( err ) {
510- console . log ( err )
511470 }
512- } else {
513- if ( newMarker ) {
514- if ( nonActiveClusterLayer . current ) {
515- nonActiveClusterLayer . current . addLayer ( newMarker )
516- if ( nonActiveClusterLayer . current . refreshClusters ) {
517- // nonActiveClusterLayer.current.refreshClusters(newMarker)
471+ } else if (
472+ subscriptionDevice &&
473+ subscriptionDevice . location &&
474+ subscriptionDevice . location . data
475+ ) {
476+ const newMarker = getNewMarker ( subscriptionDevice )
477+ devicesObj [ subscriptionDevice . device_id ] = newMarker
478+ if (
479+ subscriptionDevice . device_status === CONSTANTS . movementStatus . active
480+ ) {
481+ try {
482+ if (
483+ activeClusterLayer &&
484+ activeClusterLayer . current &&
485+ activeClusterLayer . current . refreshClusters &&
486+ activeClusterLayer . current . addLayer
487+ ) {
488+ activeClusterLayer . current . addLayer ( newMarker )
489+ // activeClusterLayer.current.refreshClusters(newMarker)
490+ activeClusterLayer . current . refreshClusters ( )
491+ }
492+ if (
493+ mainClusterLayer &&
494+ mainClusterLayer . current &&
495+ mainClusterLayer . current . refreshClusters
496+ )
497+ mainClusterLayer . current . refreshClusters ( )
498+ if (
499+ nonActiveClusterLayer &&
500+ nonActiveClusterLayer . current &&
501+ nonActiveClusterLayer . current . refreshClusters
502+ )
518503 nonActiveClusterLayer . current . refreshClusters ( )
504+ } catch ( err ) {
505+ console . log ( err )
506+ }
507+ } else {
508+ if ( newMarker ) {
509+ if ( nonActiveClusterLayer . current ) {
510+ nonActiveClusterLayer . current . addLayer ( newMarker )
511+ if ( nonActiveClusterLayer . current . refreshClusters ) {
512+ // nonActiveClusterLayer.current.refreshClusters(newMarker)
513+ nonActiveClusterLayer . current . refreshClusters ( )
514+ }
519515 }
516+ if ( mainClusterLayer . current )
517+ mainClusterLayer . current . refreshClusters ( )
520518 }
521- if ( mainClusterLayer . current )
522- mainClusterLayer . current . refreshClusters ( )
523519 }
524520 }
525- }
526- return ( ) => { }
521+ return ( ) => { }
527522 }
528523 } ,
529524 [ subscriptionData ] // eslint-disable-line
530525 )
531526
532- useEffect (
533- ( ) => {
534- for ( let x in devices ) { // eslint-disable-line
535- if ( showTooltips ) {
536- // devicesObj[devices[x].device_id].getTooltip().options.className =
537- // 'ht-tooltip-enable'
538- devicesObj [ devices [ x ] . device_id ] . openTooltip ( )
539- // .bindTooltip(
540- // devices[x].name || devices[x].device_id,
541- // {
542- // offset: new Leaflet.point(5, 15),
543- // direction: 'bottom',
544- // className: 'ht-tooltip-enable'
545- // }
546- // )
547- } else {
548- // devicesObj[devices[x].device_id].getTooltip().options.className =
549- // 'ht-tooltip-disable'
550- devicesObj [ devices [ x ] . device_id ] . closeTooltip ( )
551- }
552- // else if (devices[x].name && !showTooltips) {
553- // devicesObj[devices[x].device_id].unbindTooltip()
527+ useEffect ( ( ) => {
528+ // eslint-disable-line
529+ Object . keys ( devices ) . forEach ( x => {
530+ if ( showTooltips ) {
531+ // devicesObj[devices[x].device_id].getTooltip().options.className =
532+ // 'ht-tooltip-enable'
533+ devicesObj [ devices [ x ] . device_id ] . openTooltip ( )
534+ // .bindTooltip(
535+ // devices[x].name || devices[x].device_id,
536+ // {
537+ // offset: new Leaflet.point(5, 15),
538+ // direction: 'bottom',
539+ // className: 'ht-tooltip-enable'
554540 // }
541+ // )
542+ } else {
543+ // devicesObj[devices[x].device_id].getTooltip().options.className =
544+ // 'ht-tooltip-disable'
545+ devicesObj [ devices [ x ] . device_id ] . closeTooltip ( )
555546 }
556- } ,
557- [ showTooltips , devices ]
558- )
547+ // else if (devices[x].name && !showTooltips) {
548+ // devicesObj[devices[x].device_id].unbindTooltip()
549+ // }
550+ } )
551+ } , [ showTooltips , devices ] )
559552
560553 return < div ref = { mapRef } id = "map" className = "account-map-container" />
561554}
0 commit comments