From f59750abcfd4aa50d88781005c0f8afd2e601dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Mon, 13 Feb 2017 15:16:57 -0500 Subject: [PATCH] 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. --- src/plot_api/plot_api.js | 10 ++++++---- test/jasmine/tests/annotations_test.js | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 3f5f80444d5..58db31f5400 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -1180,7 +1180,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) { if(typeof astr === 'string') aobj[astr] = val; else if(Lib.isPlainObject(astr)) { // the 3-arg form - aobj = astr; + aobj = Lib.extendFlat({}, astr); if(traces === undefined) traces = val; } else { @@ -1696,7 +1696,9 @@ Plotly.relayout = function relayout(gd, astr, val) { var aobj = {}; if(typeof astr === 'string') aobj[astr] = val; - else if(Lib.isPlainObject(astr)) aobj = astr; + else if(Lib.isPlainObject(astr)) { + aobj = Lib.extendFlat({}, astr); + } else { Lib.warn('Relayout fail.', astr, val); return Promise.reject(); @@ -2085,10 +2087,10 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) { if(Object.keys(traceUpdate).length) gd.changed = true; if(Object.keys(layoutUpdate).length) gd.changed = true; - var restyleSpecs = _restyle(gd, traceUpdate, traces), + var restyleSpecs = _restyle(gd, Lib.extendFlat({}, traceUpdate), traces), restyleFlags = restyleSpecs.flags; - var relayoutSpecs = _relayout(gd, layoutUpdate), + var relayoutSpecs = _relayout(gd, Lib.extendFlat({}, layoutUpdate)), relayoutFlags = relayoutSpecs.flags; // clear calcdata if required diff --git a/test/jasmine/tests/annotations_test.js b/test/jasmine/tests/annotations_test.js index 880f273ed70..ee155f9cf0d 100644 --- a/test/jasmine/tests/annotations_test.js +++ b/test/jasmine/tests/annotations_test.js @@ -185,6 +185,7 @@ describe('annotations relayout', function() { }); it('should be able update annotations', function(done) { + var updateObj = { 'annotations[0].text': 'hello' }; function assertText(index, expected) { var query = '.annotation[data-index="' + index + '"]', @@ -193,6 +194,12 @@ describe('annotations relayout', function() { expect(actual).toEqual(expected); } + function assertUpdateObj() { + // w/o mutating relayout update object + expect(Object.keys(updateObj)).toEqual(['annotations[0].text']); + expect(updateObj['annotations[0].text']).toEqual('hello'); + } + assertText(0, 'left top'); Plotly.relayout(gd, 'annotations[0].text', 'hello').then(function() { @@ -202,9 +209,25 @@ describe('annotations relayout', function() { }) .then(function() { assertText(0, 'new text'); + + return Plotly.relayout(gd, updateObj); }) - .then(done); + .then(function() { + assertText(0, 'hello'); + assertUpdateObj(); + + return Plotly.relayout(gd, 'annotations[0].text', null); + }) + .then(function() { + assertText(0, 'new text'); + return Plotly.update(gd, {}, updateObj); + }) + .then(function() { + assertText(0, 'hello'); + assertUpdateObj(); + }) + .then(done); }); });