Skip to content

Commit 3312a45

Browse files
committed
[PoC] make frame with nulls clear items & array containers
1 parent 38aa4b3 commit 3312a45

File tree

3 files changed

+114
-4
lines changed

3 files changed

+114
-4
lines changed

src/plots/plots.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1549,8 +1549,14 @@ plots.extendObjectWithContainers = function(dest, src, containerPaths) {
15491549
for(i = 0; i < containerPaths.length; i++) {
15501550
containerProp = Lib.nestedProperty(expandedObj, containerPaths[i]);
15511551
containerVal = containerProp.get();
1552-
containerProp.set(null);
1553-
Lib.nestedProperty(containerObj, containerPaths[i]).set(containerVal);
1552+
1553+
if(!containerVal) {
1554+
Lib.nestedProperty(containerObj, containerPaths[i]).set(null);
1555+
}
1556+
else {
1557+
containerProp.set(null);
1558+
Lib.nestedProperty(containerObj, containerPaths[i]).set(containerVal);
1559+
}
15541560
}
15551561
}
15561562

@@ -1564,15 +1570,20 @@ plots.extendObjectWithContainers = function(dest, src, containerPaths) {
15641570
if(!srcContainer) continue;
15651571

15661572
destProp = Lib.nestedProperty(dest, containerPaths[i]);
1567-
15681573
destContainer = destProp.get();
1574+
15691575
if(!Array.isArray(destContainer)) {
15701576
destContainer = [];
15711577
destProp.set(destContainer);
15721578
}
15731579

15741580
for(j = 0; j < srcContainer.length; j++) {
1575-
destContainer[j] = plots.extendObjectWithContainers(destContainer[j], srcContainer[j]);
1581+
var srcObj = srcContainer[j];
1582+
1583+
if(srcObj === null) destContainer[j] = {};
1584+
else {
1585+
destContainer[j] = plots.extendObjectWithContainers(destContainer[j], srcObj);
1586+
}
15761587
}
15771588

15781589
destProp.set(destContainer);

test/jasmine/tests/lib_test.js

+14
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,20 @@ describe('Test lib.js:', function() {
590590
expect(computed).toLooseDeepEqual(expected);
591591
});
592592

593+
it('does not skip over array container set to null values', function() {
594+
var input = {title: 'clear annotations', annotations: null};
595+
var expected = {title: 'clear annotations', annotations: null};
596+
var computed = Lib.expandObjectPaths(input);
597+
expect(computed).toLooseDeepEqual(expected);
598+
});
599+
600+
it('expands array containers', function() {
601+
var input = {title: 'clear annotation 1', 'annotations[1]': { title: 'new' }};
602+
var expected = {title: 'clear annotation 1', annotations: [null, { title: 'new' }]};
603+
var computed = Lib.expandObjectPaths(input);
604+
expect(computed).toLooseDeepEqual(expected);
605+
});
606+
593607
// TODO: This test is unimplemented since it's a currently-unused corner case.
594608
// Getting the test to pass requires some extension (pun?) to extendDeepNoArrays
595609
// that's intelligent enough to only selectively merge *some* arrays, in particular

test/jasmine/tests/plots_test.js

+85
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,89 @@ describe('Test Plots', function() {
427427
expect(gd._transitioning).toBeUndefined();
428428
});
429429
});
430+
431+
describe('extendLayout', function() {
432+
433+
it('extend each container items', function() {
434+
var dest = {
435+
annotations: [{
436+
text: '1',
437+
x: 1,
438+
y: 1
439+
}, {
440+
text: '2',
441+
x: 2,
442+
y: 2
443+
}]
444+
};
445+
446+
var src = {
447+
annotations: [{
448+
text: '1-new'
449+
}, {
450+
text: '2-new'
451+
}]
452+
};
453+
454+
Plots.extendLayout(dest, src);
455+
456+
expect(dest).toEqual({
457+
annotations: [{
458+
text: '1-new',
459+
x: 1,
460+
y: 1
461+
}, {
462+
text: '2-new',
463+
x: 2,
464+
y: 2
465+
}]
466+
});
467+
});
468+
469+
it('clears container items when applying null src items', function() {
470+
var dest = {
471+
annotations: [{
472+
text: '1',
473+
x: 1,
474+
y: 1
475+
}, {
476+
text: '2',
477+
x: 2,
478+
y: 2
479+
}]
480+
};
481+
482+
var src = {
483+
annotations: [null, null]
484+
};
485+
486+
Plots.extendLayout(dest, src);
487+
488+
expect(dest).toEqual({
489+
annotations: [{}, {}]
490+
});
491+
});
492+
493+
it('clears container applying null src', function() {
494+
var dest = {
495+
annotations: [{
496+
text: '1',
497+
x: 1,
498+
y: 1
499+
}, {
500+
text: '2',
501+
x: 2,
502+
y: 2
503+
}]
504+
};
505+
506+
var src = { annotations: null };
507+
508+
Plots.extendLayout(dest, src);
509+
510+
expect(dest).toEqual({
511+
annotations: null
512+
});
513+
});
514+
});
430515
});

0 commit comments

Comments
 (0)