@@ -2,7 +2,6 @@ import React, {Component} from 'react';
2
2
import PropTypes from 'prop-types' ;
3
3
import isNumeric from 'fast-isnumeric' ;
4
4
import objectAssign from 'object-assign' ;
5
- // import throttle from "throttle-debounce/throttle";
6
5
7
6
// The naming convention is:
8
7
// - events are attached as `'plotly_' + eventName.toLowerCase()`
@@ -56,14 +55,13 @@ export default function plotComponentFactory(Plotly) {
56
55
57
56
this . p = Promise . resolve ( ) ;
58
57
this . fitHandler = null ;
58
+ this . resizeHandler = null ;
59
59
this . handlers = { } ;
60
60
61
61
this . syncWindowResize = this . syncWindowResize . bind ( this ) ;
62
62
this . syncEventHandlers = this . syncEventHandlers . bind ( this ) ;
63
63
this . attachUpdateEvents = this . attachUpdateEvents . bind ( this ) ;
64
64
this . getRef = this . getRef . bind ( this ) ;
65
-
66
- //this.handleUpdate = throttle(0, this.handleUpdate.bind(this));
67
65
this . handleUpdate = this . handleUpdate . bind ( this ) ;
68
66
}
69
67
@@ -79,7 +77,7 @@ export default function plotComponentFactory(Plotly) {
79
77
. then ( ( ) => {
80
78
return Plotly . newPlot ( this . el , {
81
79
data : this . props . data ,
82
- layout : this . sizeAdjustedLayout ( this . props . layout ) ,
80
+ layout : this . resizedLayoutIfFit ( this . props . layout ) ,
83
81
config : this . props . config ,
84
82
frames : this . props . frames ,
85
83
} ) ;
@@ -97,30 +95,30 @@ export default function plotComponentFactory(Plotly) {
97
95
}
98
96
99
97
componentWillUpdate ( nextProps ) {
100
- let nextLayout = this . sizeAdjustedLayout ( nextProps . layout ) ;
101
-
102
98
this . p = this . p
103
99
. then ( ( ) => {
104
100
if ( hasReactAPIMethod ) {
105
101
return Plotly . react ( this . el , {
106
102
data : nextProps . data ,
107
- layout : nextLayout ,
103
+ layout : this . resizedLayoutIfFit ( nextProps . layout ) ,
108
104
config : nextProps . config ,
109
105
frames : nextProps . frames ,
110
106
} ) ;
111
107
} else {
112
- this . clearLocalEventHandlers ( ) ;
108
+ this . handlers = { } ;
113
109
return Plotly . newPlot ( this . el , {
114
110
data : nextProps . data ,
115
- layout : nextLayout ,
111
+ layout : this . resizedLayoutIfFit ( nextProps . layout ) ,
116
112
config : nextProps . config ,
117
113
frames : nextProps . frames ,
118
114
} ) ;
119
115
}
120
116
} )
121
117
. then ( ( ) => this . syncEventHandlers ( nextProps ) )
122
118
. then ( ( ) => this . syncWindowResize ( nextProps ) )
123
- . then ( this . attachUpdateEvents )
119
+ . then ( ( ) => {
120
+ if ( ! hasReactAPIMethod ) this . attachUpdateEvents ( ) ;
121
+ } )
124
122
. then ( ( ) => this . handleUpdate ( nextProps ) )
125
123
. catch ( err => {
126
124
console . error ( 'Error while plotting:' , err ) ;
@@ -147,25 +145,21 @@ export default function plotComponentFactory(Plotly) {
147
145
}
148
146
149
147
attachUpdateEvents ( ) {
148
+ if ( ! this . el || ! this . el . removeListener ) return ;
149
+
150
150
for ( let i = 0 ; i < updateEvents . length ; i ++ ) {
151
- this . el . on ( updateEvents [ i ] , ( ) => {
152
- this . handleUpdate ( ) ;
153
- } ) ;
151
+ this . el . on ( updateEvents [ i ] , this . handleUpdate ) ;
154
152
}
155
153
}
156
154
157
155
removeUpdateEvents ( ) {
158
- if ( ! this . el || ! this . el . off ) return ;
156
+ if ( ! this . el || ! this . el . removeListener ) return ;
159
157
160
158
for ( let i = 0 ; i < updateEvents . length ; i ++ ) {
161
- this . el . off ( updateEvents [ i ] , this . handleUpdate ) ;
159
+ this . el . removeListener ( updateEvents [ i ] , this . handleUpdate ) ;
162
160
}
163
161
}
164
162
165
- clearLocalEventHandlers ( ) {
166
- this . handlers = [ ] ;
167
- }
168
-
169
163
handleUpdate ( props ) {
170
164
props = props || this . props ;
171
165
if ( props . onUpdate && typeof props . onUpdate === 'function' ) {
@@ -219,11 +213,14 @@ export default function plotComponentFactory(Plotly) {
219
213
const hasHandler = ! ! this . handlers [ eventName ] ;
220
214
221
215
if ( prop && ! hasHandler ) {
222
- let handler = ( this . handlers [ eventName ] = props [ 'on' + eventName ] ) ;
223
- this . el . on ( 'plotly_' + eventName . toLowerCase ( ) , handler ) ;
216
+ this . handlers [ eventName ] = prop ;
217
+ this . el . on (
218
+ 'plotly_' + eventName . toLowerCase ( ) ,
219
+ this . handlers [ eventName ]
220
+ ) ;
224
221
} else if ( ! prop && hasHandler ) {
225
222
// Needs to be removed:
226
- this . el . off (
223
+ this . el . removeListener (
227
224
'plotly_' + eventName . toLowerCase ( ) ,
228
225
this . handlers [ eventName ]
229
226
) ;
@@ -232,17 +229,11 @@ export default function plotComponentFactory(Plotly) {
232
229
}
233
230
}
234
231
235
- sizeAdjustedLayout ( layout ) {
236
- if ( this . props . fit ) {
237
- layout = objectAssign ( { } , layout ) ;
238
- objectAssign ( layout , this . getSize ( layout ) ) ;
232
+ resizedLayoutIfFit ( layout ) {
233
+ if ( ! this . props . fit ) {
234
+ return layout ;
239
235
}
240
-
241
- return layout ;
242
- }
243
-
244
- getParentSize ( ) {
245
- return this . el . parentElement . getBoundingClientRect ( ) ;
236
+ return objectAssign ( { } , layout , this . getSize ( layout ) ) ;
246
237
}
247
238
248
239
getSize ( layout ) {
@@ -254,7 +245,7 @@ export default function plotComponentFactory(Plotly) {
254
245
const hasHeight = isNumeric ( layoutHeight ) ;
255
246
256
247
if ( ! hasWidth || ! hasHeight ) {
257
- rect = this . getParentSize ( ) ;
248
+ rect = this . el . parentElement . getBoundingClientRect ( ) ;
258
249
}
259
250
260
251
return {
0 commit comments