Skip to content

Make frame with nulls clear items & array containers #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 11, 2016
65 changes: 65 additions & 0 deletions src/plots/array_container_defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Lib = require('../lib');


/** Convenience wrapper for making array container logic DRY and consistent
*
* @param {object} parentObjIn
* user input object where the container in question is linked
* (i.e. either a user trace object or the user layout object)
*
* @param {object} parentObjOut
* full object where the coerced container will be linked
* (i.e. either a full trace object or the full layout object)
*
* @param {object} opts
* options object:
* - name {string}
* name of the key linking the container in question
* - handleItemDefaults {function}
* defaults method to be called on each item in the array container in question,
*
* N.B.
*
* - opts is passed to handleItemDefaults so it can also store
* links to supplementary data (e.g. fullData for layout components)
*
* - opts.itemIsNotPlainObject is mutated on every pass in case so logic
* in handleItemDefaults relies on that fact.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels a little odd to me. Do you ever see a case where other parts of opts would need to be accessible to the item handler (or is there one already that I didn't notice?), or could we just pass itemIsNotPlainObject by itself as the last arg to the handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, annotations and shapes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'll make this the last arg.

I've ran into some problem with our multi-argument internal function lately, but yeah you're right, mutating opts is stupid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 5de6ff0

*
*/
module.exports = function handleArrayContainerDefaults(parentObjIn, parentObjOut, opts) {
Copy link
Contributor Author

@etpinard etpinard Nov 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to plots/subplot_defaults.

The logic below is pretty trivial, but might as well 🔒 down the behavior for all array containers.

var name = opts.name;

var contIn = Array.isArray(parentObjIn[name]) ? parentObjIn[name] : [],
contOut = parentObjOut[name] = [];

for(var i = 0; i < contIn.length; i++) {
var itemIn = contIn[i],
itemOut = {};

if(!Lib.isPlainObject(itemIn)) {
opts.itemIsNotPlainObject = true;
itemIn = {};
}
else {
opts.itemIsNotPlainObject = false;
}

opts.handleItemDefaults(itemIn, itemOut, parentObjOut, opts);

itemOut._input = itemIn;
itemOut._index = i;

contOut.push(itemOut);
}
};