Skip to content

Commit f59750a

Browse files
committed
extend input relayout & restyle objects before looping over key-vals
- so that if key-vals need to be mapped (this happens for annotations relayout calls), the input object doesn't get mutated.
1 parent 8269ea0 commit f59750a

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/plot_api/plot_api.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
11801180
if(typeof astr === 'string') aobj[astr] = val;
11811181
else if(Lib.isPlainObject(astr)) {
11821182
// the 3-arg form
1183-
aobj = astr;
1183+
aobj = Lib.extendFlat({}, astr);
11841184
if(traces === undefined) traces = val;
11851185
}
11861186
else {
@@ -1696,7 +1696,9 @@ Plotly.relayout = function relayout(gd, astr, val) {
16961696

16971697
var aobj = {};
16981698
if(typeof astr === 'string') aobj[astr] = val;
1699-
else if(Lib.isPlainObject(astr)) aobj = astr;
1699+
else if(Lib.isPlainObject(astr)) {
1700+
aobj = Lib.extendFlat({}, astr);
1701+
}
17001702
else {
17011703
Lib.warn('Relayout fail.', astr, val);
17021704
return Promise.reject();
@@ -2085,10 +2087,10 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) {
20852087
if(Object.keys(traceUpdate).length) gd.changed = true;
20862088
if(Object.keys(layoutUpdate).length) gd.changed = true;
20872089

2088-
var restyleSpecs = _restyle(gd, traceUpdate, traces),
2090+
var restyleSpecs = _restyle(gd, Lib.extendFlat({}, traceUpdate), traces),
20892091
restyleFlags = restyleSpecs.flags;
20902092

2091-
var relayoutSpecs = _relayout(gd, layoutUpdate),
2093+
var relayoutSpecs = _relayout(gd, Lib.extendFlat({}, layoutUpdate)),
20922094
relayoutFlags = relayoutSpecs.flags;
20932095

20942096
// clear calcdata if required

test/jasmine/tests/annotations_test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ describe('annotations relayout', function() {
185185
});
186186

187187
it('should be able update annotations', function(done) {
188+
var updateObj = { 'annotations[0].text': 'hello' };
188189

189190
function assertText(index, expected) {
190191
var query = '.annotation[data-index="' + index + '"]',
@@ -193,6 +194,12 @@ describe('annotations relayout', function() {
193194
expect(actual).toEqual(expected);
194195
}
195196

197+
function assertUpdateObj() {
198+
// w/o mutating relayout update object
199+
expect(Object.keys(updateObj)).toEqual(['annotations[0].text']);
200+
expect(updateObj['annotations[0].text']).toEqual('hello');
201+
}
202+
196203
assertText(0, 'left top');
197204

198205
Plotly.relayout(gd, 'annotations[0].text', 'hello').then(function() {
@@ -202,9 +209,25 @@ describe('annotations relayout', function() {
202209
})
203210
.then(function() {
204211
assertText(0, 'new text');
212+
213+
return Plotly.relayout(gd, updateObj);
205214
})
206-
.then(done);
215+
.then(function() {
216+
assertText(0, 'hello');
217+
assertUpdateObj();
218+
219+
return Plotly.relayout(gd, 'annotations[0].text', null);
220+
})
221+
.then(function() {
222+
assertText(0, 'new text');
207223

224+
return Plotly.update(gd, {}, updateObj);
225+
})
226+
.then(function() {
227+
assertText(0, 'hello');
228+
assertUpdateObj();
229+
})
230+
.then(done);
208231
});
209232
});
210233

0 commit comments

Comments
 (0)