Skip to content

Commit f7e60fb

Browse files
committed
relinkPrivateKeys inside arrayContainerDefaults
1 parent 2e6c030 commit f7e60fb

File tree

5 files changed

+73
-51
lines changed

5 files changed

+73
-51
lines changed

src/lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ lib.isPlainObject = require('./is_plain_object');
1818
lib.isArray = require('./is_array');
1919
lib.mod = require('./mod');
2020
lib.toLogRange = require('./to_log_range');
21+
lib.relinkPrivateKeys = require('./relink_private');
2122

2223
var coerceModule = require('./coerce');
2324
lib.valObjects = coerceModule.valObjects;

src/lib/relink_private.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2012-2017, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var isArray = require('./is_array');
13+
var isPlainObject = require('./is_plain_object');
14+
15+
/**
16+
* Relink private _keys and keys with a function value from one container
17+
* to the new container.
18+
* Relink means copying if object is pass-by-value and adding a reference
19+
* if object is pass-by-ref.
20+
* This prevents deepCopying massive structures like a webgl context.
21+
*/
22+
module.exports = function relinkPrivateKeys(toContainer, fromContainer) {
23+
var keys = Object.keys(fromContainer || {});
24+
25+
for(var i = 0; i < keys.length; i++) {
26+
var k = keys[i],
27+
fromVal = fromContainer[k],
28+
toVal = toContainer[k];
29+
30+
if(k.charAt(0) === '_' || typeof fromVal === 'function') {
31+
32+
// if it already exists at this point, it's something
33+
// that we recreate each time around, so ignore it
34+
if(k in toContainer) continue;
35+
36+
toContainer[k] = fromVal;
37+
}
38+
else if(isArray(fromVal) && isArray(toVal) && isPlainObject(fromVal[0])) {
39+
40+
// recurse into arrays containers
41+
for(var j = 0; j < fromVal.length; j++) {
42+
if(isPlainObject(fromVal[j]) && isPlainObject(toVal[j])) {
43+
relinkPrivateKeys(toVal[j], fromVal[j]);
44+
}
45+
}
46+
}
47+
else if(isPlainObject(fromVal) && isPlainObject(toVal)) {
48+
49+
// recurse into objects, but only if they still exist
50+
relinkPrivateKeys(toVal, fromVal);
51+
52+
if(!Object.keys(toVal).length) delete toContainer[k];
53+
}
54+
}
55+
};

src/plot_api/plot_api.js

-3
Original file line numberDiff line numberDiff line change
@@ -2092,9 +2092,6 @@ function _relayout(gd, aobj) {
20922092
var oldWidth = gd._fullLayout.width,
20932093
oldHeight = gd._fullLayout.height;
20942094

2095-
// coerce the updated layout
2096-
Plots.supplyDefaults(gd);
2097-
20982095
// calculate autosizing
20992096
if(gd.layout.autosize) Plots.plotAutoSize(gd, gd.layout, gd._fullLayout);
21002097

src/plots/array_container_defaults.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ var Lib = require('../lib');
4444
module.exports = function handleArrayContainerDefaults(parentObjIn, parentObjOut, opts) {
4545
var name = opts.name;
4646

47-
var contIn = Array.isArray(parentObjIn[name]) ? parentObjIn[name] : [],
48-
contOut = parentObjOut[name] = [];
47+
var previousContOut = parentObjOut[name];
4948

50-
for(var i = 0; i < contIn.length; i++) {
49+
var contIn = Lib.isArray(parentObjIn[name]) ? parentObjIn[name] : [],
50+
contOut = parentObjOut[name] = [],
51+
i;
52+
53+
for(i = 0; i < contIn.length; i++) {
5154
var itemIn = contIn[i],
5255
itemOut = {},
5356
itemOpts = {};
@@ -64,4 +67,13 @@ module.exports = function handleArrayContainerDefaults(parentObjIn, parentObjOut
6467

6568
contOut.push(itemOut);
6669
}
70+
71+
// in case this array gets its defaults rebuilt independent of the whole layout,
72+
// relink the private keys just for this array.
73+
if(Lib.isArray(previousContOut)) {
74+
var len = Math.min(previousContOut.length, contOut.length);
75+
for(i = 0; i < len; i++) {
76+
Lib.relinkPrivateKeys(contOut[i], previousContOut[i]);
77+
}
78+
}
6779
};

src/plots/plots.js

+2-45
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var plots = module.exports = {};
2222
var animationAttrs = require('./animation_attributes');
2323
var frameAttrs = require('./frame_attributes');
2424

25+
var relinkPrivateKeys = Lib.relinkPrivateKeys;
26+
2527
// Expose registry methods on Plots for backward-compatibility
2628
Lib.extendFlat(plots, Registry);
2729

@@ -592,51 +594,6 @@ plots.cleanPlot = function(newFullData, newFullLayout, oldFullData, oldFullLayou
592594
}
593595
};
594596

595-
/**
596-
* Relink private _keys and keys with a function value from one container
597-
* to the new container.
598-
* Relink means copying if object is pass-by-value and adding a reference
599-
* if object is pass-by-ref.
600-
* This prevents deepCopying massive structures like a webgl context.
601-
*/
602-
function relinkPrivateKeys(toContainer, fromContainer) {
603-
var isPlainObject = Lib.isPlainObject,
604-
isArray = Array.isArray;
605-
606-
var keys = Object.keys(fromContainer || {});
607-
608-
for(var i = 0; i < keys.length; i++) {
609-
var k = keys[i],
610-
fromVal = fromContainer[k],
611-
toVal = toContainer[k];
612-
613-
if(k.charAt(0) === '_' || typeof fromVal === 'function') {
614-
615-
// if it already exists at this point, it's something
616-
// that we recreate each time around, so ignore it
617-
if(k in toContainer) continue;
618-
619-
toContainer[k] = fromVal;
620-
}
621-
else if(isArray(fromVal) && isArray(toVal) && isPlainObject(fromVal[0])) {
622-
623-
// recurse into arrays containers
624-
for(var j = 0; j < fromVal.length; j++) {
625-
if(isPlainObject(fromVal[j]) && isPlainObject(toVal[j])) {
626-
relinkPrivateKeys(toVal[j], fromVal[j]);
627-
}
628-
}
629-
}
630-
else if(isPlainObject(fromVal) && isPlainObject(toVal)) {
631-
632-
// recurse into objects, but only if they still exist
633-
relinkPrivateKeys(toVal, fromVal);
634-
635-
if(!Object.keys(toVal).length) delete toContainer[k];
636-
}
637-
}
638-
}
639-
640597
plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
641598
var oldSubplots = oldFullLayout._plots || {},
642599
newSubplots = newFullLayout._plots = {};

0 commit comments

Comments
 (0)