@@ -89,7 +89,6 @@ function linearToData(ax) { return ax.type === 'category' ? ax.l2c : ax.l2d; }
89
89
90
90
shapes . drawAll = function ( gd ) {
91
91
var fullLayout = gd . _fullLayout ;
92
- fullLayout . _shapelayer . selectAll ( 'path' ) . remove ( ) ;
93
92
for ( var i = 0 ; i < fullLayout . shapes . length ; i ++ ) {
94
93
shapes . draw ( gd , i ) ;
95
94
}
@@ -115,86 +114,106 @@ shapes.add = function(gd) {
115
114
// if opt is blank, val can be 'add' or a full options object to add a new
116
115
// annotation at that point in the array, or 'remove' to delete this one
117
116
shapes . draw = function ( gd , index , opt , value ) {
118
- var layout = gd . layout ,
119
- fullLayout = gd . _fullLayout ,
120
- i ;
121
-
122
- // TODO: abstract out these drawAll, add, and remove blocks for shapes and annotations
123
117
if ( ! isNumeric ( index ) || index === - 1 ) {
124
118
// no index provided - we're operating on ALL shapes
125
119
if ( ! index && Array . isArray ( value ) ) {
126
- // a whole annotation array is passed in
127
- // (as in, redo of delete all)
128
- layout . shapes = value ;
129
- shapes . supplyLayoutDefaults ( layout , fullLayout ) ;
130
- shapes . drawAll ( gd ) ;
120
+ replaceAllShapes ( gd , value ) ;
131
121
return ;
132
122
}
133
123
else if ( value === 'remove' ) {
134
- // delete all
135
- delete layout . shapes ;
136
- fullLayout . shapes = [ ] ;
137
- shapes . drawAll ( gd ) ;
124
+ deleteAllShapes ( gd ) ;
138
125
return ;
139
126
}
140
127
else if ( opt && value !== 'add' ) {
141
- // make the same change to all shapes
142
- for ( i = 0 ; i < fullLayout . shapes . length ; i ++ ) {
143
- shapes . draw ( gd , i , opt , value ) ;
144
- }
128
+ updateAllShapes ( gd , opt , value ) ;
145
129
return ;
146
130
}
147
131
else {
148
132
// add a new empty annotation
149
- index = fullLayout . shapes . length ;
150
- fullLayout . shapes . push ( { } ) ;
133
+ index = gd . _fullLayout . shapes . length ;
134
+ gd . _fullLayout . shapes . push ( { } ) ;
151
135
}
152
136
}
153
137
154
138
if ( ! opt && value ) {
155
139
if ( value === 'remove' ) {
156
- fullLayout . _shapelayer . selectAll ( '[data-index="' + index + '"]' )
157
- . remove ( ) ;
158
- fullLayout . shapes . splice ( index , 1 ) ;
159
- layout . shapes . splice ( index , 1 ) ;
160
- for ( i = index ; i < fullLayout . shapes . length ; i ++ ) {
161
- fullLayout . _shapelayer
162
- . selectAll ( '[data-index="' + ( i + 1 ) + '"]' )
163
- . attr ( 'data-index' , String ( i ) ) ;
164
-
165
- // redraw all shapes past the removed one,
166
- // so they bind to the right events
167
- shapes . draw ( gd , i ) ;
168
- }
140
+ deleteShape ( gd , index ) ;
169
141
return ;
170
142
}
171
143
else if ( value === 'add' || Plotly . Lib . isPlainObject ( value ) ) {
172
- fullLayout . shapes . splice ( index , 0 , { } ) ;
144
+ insertShape ( gd , index , value ) ;
145
+ }
146
+ }
173
147
174
- var rule = Plotly . Lib . isPlainObject ( value ) ?
175
- Plotly . Lib . extendFlat ( { } , value ) :
176
- { text : 'New text' } ;
148
+ updateShape ( gd , index , opt , value ) ;
149
+ } ;
177
150
178
- if ( layout . shapes ) {
179
- layout . shapes . splice ( index , 0 , rule ) ;
180
- } else {
181
- layout . shapes = [ rule ] ;
182
- }
151
+ function replaceAllShapes ( gd , newShapes ) {
152
+ gd . layout . shapes = newShapes ;
153
+ shapes . supplyLayoutDefaults ( gd . layout , gd . _fullLayout ) ;
154
+ shapes . drawAll ( gd ) ;
155
+ }
183
156
184
- for ( i = fullLayout . shapes . length - 1 ; i > index ; i -- ) {
185
- fullLayout . _shapelayer
186
- . selectAll ( '[data-index="' + ( i - 1 ) + '"]' )
187
- . attr ( 'data-index' , String ( i ) ) ;
188
- shapes . draw ( gd , i ) ;
189
- }
190
- }
157
+ function deleteAllShapes ( gd ) {
158
+ delete gd . layout . shapes ;
159
+ gd . _fullLayout . shapes = [ ] ;
160
+ shapes . drawAll ( gd ) ;
161
+ }
162
+
163
+ function updateAllShapes ( gd , opt , value ) {
164
+ for ( var i = 0 ; i < gd . _fullLayout . shapes . length ; i ++ ) {
165
+ shapes . draw ( gd , i , opt , value ) ;
166
+ }
167
+ }
168
+
169
+ function deleteShape ( gd , index ) {
170
+ gd . _fullLayout . _shapelayer . selectAll ( '[data-index="' + index + '"]' )
171
+ . remove ( ) ;
172
+
173
+ gd . _fullLayout . shapes . splice ( index , 1 ) ;
174
+
175
+ gd . layout . shapes . splice ( index , 1 ) ;
176
+
177
+ for ( var i = index ; i < gd . _fullLayout . shapes . length ; i ++ ) {
178
+ // redraw all shapes past the removed one,
179
+ // so they bind to the right events
180
+ gd . _fullLayout . _shapelayer
181
+ . selectAll ( '[data-index="' + ( i + 1 ) + '"]' )
182
+ . attr ( 'data-index' , String ( i ) ) ;
183
+ shapes . draw ( gd , i ) ;
184
+ }
185
+ }
186
+
187
+ function insertShape ( gd , index , newShape ) {
188
+ gd . _fullLayout . shapes . splice ( index , 0 , { } ) ;
189
+
190
+ var rule = Plotly . Lib . isPlainObject ( newShape ) ?
191
+ Plotly . Lib . extendFlat ( { } , newShape ) :
192
+ { text : 'New text' } ;
193
+
194
+ if ( gd . layout . shapes ) {
195
+ gd . layout . shapes . splice ( index , 0 , rule ) ;
196
+ } else {
197
+ gd . layout . shapes = [ rule ] ;
191
198
}
192
199
200
+ for ( var i = gd . _fullLayout . shapes . length - 1 ; i > index ; i -- ) {
201
+ gd . _fullLayout . _shapelayer
202
+ . selectAll ( '[data-index="' + ( i - 1 ) + '"]' )
203
+ . attr ( 'data-index' , String ( i ) ) ;
204
+ shapes . draw ( gd , i ) ;
205
+ }
206
+ }
207
+
208
+ function updateShape ( gd , index , opt , value ) {
209
+ var i ;
210
+
193
211
// remove the existing shape if there is one
194
- fullLayout . _shapelayer . selectAll ( '[data-index="' + index + '"]' ) . remove ( ) ;
212
+ gd . _fullLayout . _shapelayer . selectAll ( '[data-index="' + index + '"]' )
213
+ . remove ( ) ;
195
214
196
215
// remember a few things about what was already there,
197
- var optionsIn = layout . shapes [ index ] ;
216
+ var optionsIn = gd . layout . shapes [ index ] ;
198
217
199
218
// (from annos...) not sure how we're getting here... but C12 is seeing a bug
200
219
// where we fail here when they add/remove annotations
@@ -261,8 +280,8 @@ shapes.draw = function(gd, index, opt, value) {
261
280
optionsIn [ posAttr ] = position ;
262
281
}
263
282
264
- var options = handleShapeDefaults ( optionsIn , fullLayout ) ;
265
- fullLayout . shapes [ index ] = options ;
283
+ var options = handleShapeDefaults ( optionsIn , gd . _fullLayout ) ;
284
+ gd . _fullLayout . shapes [ index ] = options ;
266
285
267
286
var attrs = {
268
287
'data-index' : String ( index ) ,
@@ -273,15 +292,18 @@ shapes.draw = function(gd, index, opt, value) {
273
292
274
293
var lineColor = options . line . width ? options . line . color : 'rgba(0,0,0,0)' ;
275
294
276
- var path = fullLayout . _shapelayer . append ( 'path' )
295
+ var path = gd . _fullLayout . _shapelayer . append ( 'path' )
277
296
. attr ( attrs )
278
297
. style ( 'opacity' , options . opacity )
279
298
. call ( Plotly . Color . stroke , lineColor )
280
299
. call ( Plotly . Color . fill , options . fillcolor )
281
300
. call ( Plotly . Drawing . dashLine , options . line . dash , options . line . width ) ;
282
301
283
- if ( clipAxes ) path . call ( Plotly . Drawing . setClipUrl , 'clip' + fullLayout . _uid + clipAxes ) ;
284
- } ;
302
+ if ( clipAxes ) {
303
+ path . call ( Plotly . Drawing . setClipUrl ,
304
+ 'clip' + gd . _fullLayout . _uid + clipAxes ) ;
305
+ }
306
+ }
285
307
286
308
function decodeDate ( convertToPx ) {
287
309
return function ( v ) { return convertToPx ( v . replace ( '_' , ' ' ) ) ; } ;
0 commit comments