Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df69584

Browse files
committed
Split method shapes.draw into smaller functions
1 parent fb87442 commit df69584

File tree

1 file changed

+89
-56
lines changed

1 file changed

+89
-56
lines changed

src/components/shapes/index.js

Lines changed: 89 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -115,86 +115,114 @@ 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+
return;
151+
};
177152

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

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);
191170
}
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;
192219

193220
// 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();
195223

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

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

264-
var options = handleShapeDefaults(optionsIn, fullLayout);
265-
fullLayout.shapes[index] = options;
292+
var options = handleShapeDefaults(optionsIn, gd._fullLayout);
293+
gd._fullLayout.shapes[index] = options;
266294

267295
var attrs = {
268296
'data-index': String(index),
@@ -273,15 +301,20 @@ shapes.draw = function(gd, index, opt, value) {
273301

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

276-
var path = fullLayout._shapelayer.append('path')
304+
var path = gd._fullLayout._shapelayer.append('path')
277305
.attr(attrs)
278306
.style('opacity', options.opacity)
279307
.call(Plotly.Color.stroke, lineColor)
280308
.call(Plotly.Color.fill, options.fillcolor)
281309
.call(Plotly.Drawing.dashLine, options.line.dash, options.line.width);
282310

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

286319
function decodeDate(convertToPx) {
287320
return function(v) { return convertToPx(v.replace('_', ' ')); };

0 commit comments

Comments
 (0)