Skip to content

Commit c53303b

Browse files
committed
Split method shapes.draw into smaller functions
1 parent 013e778 commit c53303b

File tree

1 file changed

+79
-56
lines changed

1 file changed

+79
-56
lines changed

src/components/shapes/index.js

+79-56
Original file line numberDiff line numberDiff line change
@@ -115,86 +115,106 @@ shapes.add = function(gd) {
115115
// if opt is blank, val can be 'add' or a full options object to add a new
116116
// annotation at that point in the array, or 'remove' to delete this one
117117
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
123118
if(!isNumeric(index) || index===-1) {
124119
// no index provided - we're operating on ALL shapes
125120
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);
131122
return;
132123
}
133124
else if(value==='remove') {
134-
// delete all
135-
delete layout.shapes;
136-
fullLayout.shapes = [];
137-
shapes.drawAll(gd);
125+
deleteAllShapes(gd);
138126
return;
139127
}
140128
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);
145130
return;
146131
}
147132
else {
148133
// 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({});
151136
}
152137
}
153138

154139
if(!opt && value) {
155140
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);
169142
return;
170143
}
171144
else if(value==='add' || Plotly.Lib.isPlainObject(value)) {
172-
fullLayout.shapes.splice(index,0,{});
145+
insertShape(gd, index, value);
146+
}
147+
}
173148

174-
var rule = Plotly.Lib.isPlainObject(value) ?
175-
Plotly.Lib.extendFlat({}, value) :
176-
{text: 'New text'};
149+
updateShape(gd, index, opt, value);
150+
};
177151

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+
}
183157

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];
191199
}
192200

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+
193212
// 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();
195215

196216
// remember a few things about what was already there,
197-
var optionsIn = layout.shapes[index];
217+
var optionsIn = gd.layout.shapes[index];
198218

199219
// (from annos...) not sure how we're getting here... but C12 is seeing a bug
200220
// where we fail here when they add/remove annotations
@@ -261,8 +281,8 @@ shapes.draw = function(gd, index, opt, value) {
261281
optionsIn[posAttr] = position;
262282
}
263283

264-
var options = handleShapeDefaults(optionsIn, fullLayout);
265-
fullLayout.shapes[index] = options;
284+
var options = handleShapeDefaults(optionsIn, gd._fullLayout);
285+
gd._fullLayout.shapes[index] = options;
266286

267287
var attrs = {
268288
'data-index': String(index),
@@ -273,15 +293,18 @@ shapes.draw = function(gd, index, opt, value) {
273293

274294
var lineColor = options.line.width ? options.line.color : 'rgba(0,0,0,0)';
275295

276-
var path = fullLayout._shapelayer.append('path')
296+
var path = gd._fullLayout._shapelayer.append('path')
277297
.attr(attrs)
278298
.style('opacity', options.opacity)
279299
.call(Plotly.Color.stroke, lineColor)
280300
.call(Plotly.Color.fill, options.fillcolor)
281301
.call(Plotly.Drawing.dashLine, options.line.dash, options.line.width);
282302

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+
}
285308

286309
function decodeDate(convertToPx) {
287310
return function(v) { return convertToPx(v.replace('_', ' ')); };

0 commit comments

Comments
 (0)