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