module powerbi . extensibility . visual { declare var turf ; declare var MapboxDraw ; export class DrawControl implements mapboxgl . IControl { private draw : any ; private filter : Filter ; constructor ( filter ) { this . filter = filter ; MapboxDraw . modes . draw_line_string = LassoDraw . create ( this . filter ) ; this . draw = new MapboxDraw ( { displayControlsDefault : false , controls : { 'polygon' : true , 'line_string' : true } , } ) ; } manageHandlers ( mapVisual ) { const map = mapVisual . getMap ( ) ; map . on ( 'draw.create' , ( e ) => { map . doubleClickZoom . disable ( ) ; const selection_poly = e . features [ 0 ] ; const selectFeature = function ( sel_pol , feature ) { if ( feature . geometry . type === 'Point' && turf . booleanContains ( sel_pol , feature ) ) { return true ; } if ( ( feature . geometry . type === 'Polygon' || feature . geometry . type === 'Linestring' ) && ( turf . booleanOverlap ( feature , sel_pol ) || turf . booleanContains ( sel_pol , feature ) || turf . booleanContains ( feature , sel_pol ) ) ) { return true ; } return false ; } ; const polygonBoundingBox = turf . bbox ( selection_poly ) ; const southWest = map . project ( [ polygonBoundingBox [ 0 ] , polygonBoundingBox [ 1 ] ] ) ; const northEast = map . project ( [ polygonBoundingBox [ 2 ] , polygonBoundingBox [ 3 ] ] ) ; const layers = mapVisual . getExistingLayers ( ) ; const layerIDs = layers . map ( layer => layer . getId ( ) ) ; const bbox_features = map . queryRenderedFeatures ( [ southWest , northEast ] , { layers : layerIDs } ) ; let selectedFeatures = bbox_features . reduce ( function ( acc , feature ) { if ( selectFeature ( selection_poly , feature ) ) { acc . push ( feature ) ; return acc ; } if ( feature . geometry . type === 'MultiPolygon' ) { for ( let polygon of feature . geometry . coordinates ) { if ( selectFeature ( selection_poly , turf . helpers . polygon ( polygon ) ) ) { acc . push ( feature ) ; return acc ; } } ; } return acc ; } , [ ] ) ; if ( layers && layers . length > 0 ) { const roleMap = mapVisual . getRoleMap ( ) ; if ( selectedFeatures . length > constants . MAX_SELECTION_COUNT ) { selectedFeatures = selectedFeatures . slice ( 0 , constants . MAX_SELECTION_COUNT ) ; } layers . map ( layer => { this . filter . updateSelection ( layer , selectedFeatures , roleMap ) ; } ) ; } this . draw . deleteAll ( ) ; } ) ; } onAdd ( map ) { const drawControlHTML = this . draw . onAdd ( map ) ; const drawLineGroup = drawControlHTML . getElementsByClassName ( MapboxDrawConstants . classes . CONTROL_BUTTON_LINE ) ; const drawLineControl = drawLineGroup [ 0 ] as HTMLElement ; LassoDraw . makeIcon ( drawLineControl ) ; return drawControlHTML ; } onRemove ( map ) { this . draw . onRemove ( map ) ; } getDefaultPosition ( ) { return 'top-left' ; } } }