@@ -115,86 +115,106 @@ 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
+ } ;
177
151
178
- if ( layout . shapes ) {
179
- layout . shapes . splice ( index , 0 , rule ) ;
180
- } else {
181
- layout . shapes = [ rule ] ;
182
- }
152
+ function replaceAllShapes ( gd , newShapes ) {
153
+ gd . layout . shapes = newShapes ;
154
+ shapes . supplyLayoutDefaults ( gd . layout , gd . _fullLayout ) ;
155
+ shapes . drawAll ( gd ) ;
156
+ }
183
157
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
- }
158
+ function deleteAllShapes ( gd ) {
159
+ delete gd . layout . shapes ;
160
+ gd . _fullLayout . shapes = [ ] ;
161
+ shapes . drawAll ( gd ) ;
162
+ }
163
+
164
+ function updateAllShapes ( gd , opt , value ) {
165
+ for ( var i = 0 ; i < gd . _fullLayout . shapes . length ; i ++ ) {
166
+ shapes . draw ( gd , i , opt , value ) ;
167
+ }
168
+ }
169
+
170
+ function deleteShape ( gd , index ) {
171
+ gd . _fullLayout . _shapelayer . selectAll ( '[data-index="' + index + '"]' )
172
+ . remove ( ) ;
173
+
174
+ gd . _fullLayout . shapes . splice ( index , 1 ) ;
175
+
176
+ gd . layout . shapes . splice ( index , 1 ) ;
177
+
178
+ for ( var i = index ; i < gd . _fullLayout . shapes . length ; i ++ ) {
179
+ // redraw all shapes past the removed one,
180
+ // so they bind to the right events
181
+ gd . _fullLayout . _shapelayer
182
+ . selectAll ( '[data-index="' + ( i + 1 ) + '"]' )
183
+ . attr ( 'data-index' , String ( i ) ) ;
184
+ shapes . draw ( gd , i ) ;
185
+ }
186
+ }
187
+
188
+ function insertShape ( gd , index , newShape ) {
189
+ gd . _fullLayout . shapes . splice ( index , 0 , { } ) ;
190
+
191
+ var rule = Plotly . Lib . isPlainObject ( newShape ) ?
192
+ Plotly . Lib . extendFlat ( { } , newShape ) :
193
+ { text : 'New text' } ;
194
+
195
+ if ( gd . layout . shapes ) {
196
+ gd . layout . shapes . splice ( index , 0 , rule ) ;
197
+ } else {
198
+ gd . layout . shapes = [ rule ] ;
191
199
}
192
200
201
+ for ( var i = gd . _fullLayout . shapes . length - 1 ; i > index ; i -- ) {
202
+ gd . _fullLayout . _shapelayer
203
+ . selectAll ( '[data-index="' + ( i - 1 ) + '"]' )
204
+ . attr ( 'data-index' , String ( i ) ) ;
205
+ shapes . draw ( gd , i ) ;
206
+ }
207
+ }
208
+
209
+ function updateShape ( gd , index , opt , value ) {
210
+ var i ;
211
+
193
212
// remove the existing shape if there is one
194
- fullLayout . _shapelayer . selectAll ( '[data-index="' + index + '"]' ) . remove ( ) ;
213
+ gd . _fullLayout . _shapelayer . selectAll ( '[data-index="' + index + '"]' )
214
+ . remove ( ) ;
195
215
196
216
// remember a few things about what was already there,
197
- var optionsIn = layout . shapes [ index ] ;
217
+ var optionsIn = gd . layout . shapes [ index ] ;
198
218
199
219
// (from annos...) not sure how we're getting here... but C12 is seeing a bug
200
220
// where we fail here when they add/remove annotations
@@ -261,8 +281,8 @@ shapes.draw = function(gd, index, opt, value) {
261
281
optionsIn [ posAttr ] = position ;
262
282
}
263
283
264
- var options = handleShapeDefaults ( optionsIn , fullLayout ) ;
265
- fullLayout . shapes [ index ] = options ;
284
+ var options = handleShapeDefaults ( optionsIn , gd . _fullLayout ) ;
285
+ gd . _fullLayout . shapes [ index ] = options ;
266
286
267
287
var attrs = {
268
288
'data-index' : String ( index ) ,
@@ -273,15 +293,18 @@ shapes.draw = function(gd, index, opt, value) {
273
293
274
294
var lineColor = options . line . width ? options . line . color : 'rgba(0,0,0,0)' ;
275
295
276
- var path = fullLayout . _shapelayer . append ( 'path' )
296
+ var path = gd . _fullLayout . _shapelayer . append ( 'path' )
277
297
. attr ( attrs )
278
298
. style ( 'opacity' , options . opacity )
279
299
. call ( Plotly . Color . stroke , lineColor )
280
300
. call ( Plotly . Color . fill , options . fillcolor )
281
301
. call ( Plotly . Drawing . dashLine , options . line . dash , options . line . width ) ;
282
302
283
- if ( clipAxes ) path . call ( Plotly . Drawing . setClipUrl , 'clip' + fullLayout . _uid + clipAxes ) ;
284
- } ;
303
+ if ( clipAxes ) {
304
+ path . call ( Plotly . Drawing . setClipUrl ,
305
+ 'clip' + gd . _fullLayout . _uid + clipAxes ) ;
306
+ }
307
+ }
285
308
286
309
function decodeDate ( convertToPx ) {
287
310
return function ( v ) { return convertToPx ( v . replace ( '_' , ' ' ) ) ; } ;
0 commit comments