1919
2020
2121import $ from 'jquery' ;
22- import { CUSTOM_LEGEND_VIS_TYPES } from '../../../ui/public/vis/vis_types/vislib_vis_legend' ;
22+ import React from 'react' ;
23+
24+ import { CUSTOM_LEGEND_VIS_TYPES , VisLegend } from '../../../ui/public/vis/vis_types/vislib_vis_legend' ;
2325import { VislibVisProvider } from '../../../ui/public/vislib/vis' ;
2426import chrome from '../../../ui/public/chrome' ;
27+ import { mountReactNode } from '../../../../core/public/utils' ;
2528
2629const legendClassName = {
2730 top : 'visLib--legend-top' ,
@@ -30,24 +33,30 @@ const legendClassName = {
3033 right : 'visLib--legend-right' ,
3134} ;
3235
33-
3436export class vislibVisController {
3537 constructor ( el , vis ) {
3638 this . el = el ;
3739 this . vis = vis ;
38- this . $scope = null ;
40+ this . unmount = null ;
41+ this . legendRef = React . createRef ( ) ;
3942
43+ // vis mount point
4044 this . container = document . createElement ( 'div' ) ;
4145 this . container . className = 'visLib' ;
4246 this . el . appendChild ( this . container ) ;
4347
48+ // chart mount point
4449 this . chartEl = document . createElement ( 'div' ) ;
4550 this . chartEl . className = 'visLib__chart' ;
4651 this . container . appendChild ( this . chartEl ) ;
52+ // legend mount point
53+ this . legendEl = document . createElement ( 'div' ) ;
54+ this . legendEl . className = 'visLib__legend' ;
55+ this . container . appendChild ( this . legendEl ) ;
4756 }
4857
4958 render ( esResponse , visParams ) {
50- if ( this . vis . vislibVis ) {
59+ if ( this . vislibVis ) {
5160 this . destroy ( ) ;
5261 }
5362
@@ -56,62 +65,69 @@ export class vislibVisController {
5665 const $injector = await chrome . dangerouslyGetActiveInjector ( ) ;
5766 const Private = $injector . get ( 'Private' ) ;
5867 this . Vislib = Private ( VislibVisProvider ) ;
59- this . $compile = $injector . get ( '$compile' ) ;
60- this . $rootScope = $injector . get ( '$rootScope' ) ;
6168 }
6269
6370 if ( this . el . clientWidth === 0 || this . el . clientHeight === 0 ) {
6471 return resolve ( ) ;
6572 }
6673
67- this . vis . vislibVis = new this . Vislib ( this . chartEl , visParams ) ;
68- this . vis . vislibVis . on ( 'brush' , this . vis . API . events . brush ) ;
69- this . vis . vislibVis . on ( 'click' , this . vis . API . events . filter ) ;
70- this . vis . vislibVis . on ( 'renderComplete' , resolve ) ;
74+ this . vislibVis = new this . Vislib ( this . chartEl , visParams ) ;
75+ this . vislibVis . on ( 'brush' , this . vis . API . events . brush ) ;
76+ this . vislibVis . on ( 'click' , this . vis . API . events . filter ) ;
77+ this . vislibVis . on ( 'renderComplete' , resolve ) ;
7178
72- this . vis . vislibVis . initVisConfig ( esResponse , this . vis . getUiState ( ) ) ;
79+ this . vislibVis . initVisConfig ( esResponse , this . vis . getUiState ( ) ) ;
7380
7481 if ( visParams . addLegend ) {
7582 $ ( this . container ) . attr ( 'class' , ( i , cls ) => {
7683 return cls . replace ( / v i s L i b - - l e g e n d - \S + / g, '' ) ;
7784 } ) . addClass ( legendClassName [ visParams . legendPosition ] ) ;
7885
79- this . $scope = this . $rootScope . $new ( ) ;
80- this . $scope . refreshLegend = 0 ;
81- this . $scope . vis = this . vis ;
82- this . $scope . visData = esResponse ;
83- this . $scope . visParams = visParams ;
84- this . $scope . uiState = this . $scope . vis . getUiState ( ) ;
85- const legendHtml = this . $compile ( '<vislib-legend></vislib-legend>' ) ( this . $scope ) ;
86- this . container . appendChild ( legendHtml [ 0 ] ) ;
87- this . $scope . $digest ( ) ;
86+ this . mountLegend ( esResponse , visParams . legendPosition ) ;
8887 }
8988
90- this . vis . vislibVis . render ( esResponse , this . vis . getUiState ( ) ) ;
89+ this . vislibVis . render ( esResponse , this . vis . getUiState ( ) ) ;
9190
9291 // refreshing the legend after the chart is rendered.
9392 // this is necessary because some visualizations
9493 // provide data necessary for the legend only after a render cycle.
95- if ( visParams . addLegend && CUSTOM_LEGEND_VIS_TYPES . includes ( this . vis . vislibVis . visConfigArgs . type ) ) {
96- this . $scope . refreshLegend ++ ;
97- this . $scope . $digest ( ) ;
98-
99- this . vis . vislibVis . render ( esResponse , this . vis . getUiState ( ) ) ;
94+ if ( visParams . addLegend && CUSTOM_LEGEND_VIS_TYPES . includes ( this . vislibVis . visConfigArgs . type ) ) {
95+ this . unmountLegend ( ) ;
96+ this . mountLegend ( esResponse , visParams . legendPosition ) ;
97+ this . vislibVis . render ( esResponse , this . vis . getUiState ( ) ) ;
10098 }
10199 } ) ;
102100 }
103101
102+ mountLegend ( visData , position ) {
103+ this . unmount = mountReactNode (
104+ < VisLegend
105+ ref = { this . legendRef }
106+ vis = { this . vis }
107+ vislibVis = { this . vislibVis }
108+ visData = { visData }
109+ position = { position }
110+ uiState = { this . vis . getUiState ( ) }
111+ />
112+ ) ( this . legendEl ) ;
113+ }
114+
115+ unmountLegend ( ) {
116+ if ( this . unmount ) {
117+ this . unmount ( ) ;
118+ }
119+ }
120+
104121 destroy ( ) {
105- if ( this . vis . vislibVis ) {
106- this . vis . vislibVis . off ( 'brush' , this . vis . API . events . brush ) ;
107- this . vis . vislibVis . off ( 'click' , this . vis . API . events . filter ) ;
108- this . vis . vislibVis . destroy ( ) ;
109- delete this . vis . vislibVis ;
122+ if ( this . unmount ) {
123+ this . unmount ( ) ;
110124 }
111- $ ( this . container ) . find ( 'vislib-legend' ) . remove ( ) ;
112- if ( this . $scope ) {
113- this . $scope . $destroy ( ) ;
114- this . $scope = null ;
125+
126+ if ( this . vislibVis ) {
127+ this . vislibVis . off ( 'brush' , this . vis . API . events . brush ) ;
128+ this . vislibVis . off ( 'click' , this . vis . API . events . filter ) ;
129+ this . vislibVis . destroy ( ) ;
130+ delete this . vislibVis ;
115131 }
116132 }
117133}
0 commit comments