Skip to content

Commit d21d782

Browse files
committed
add general array container defaults handler
1 parent 4c49755 commit d21d782

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/plots/array_container_defaults.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright 2012-2016, 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+
'use strict';
10+
11+
var Lib = require('../lib');
12+
13+
14+
/** Convenience wrapper for making array container logic DRY and consistent
15+
*
16+
* @param {object} parentObjIn
17+
* user input object where the container in question is linked
18+
* (i.e. either a user trace object or the user layout object)
19+
*
20+
* @param {object} parentObjOut
21+
* full object where the coerced container will be linked
22+
* (i.e. either a full trace object or the full layout object)
23+
*
24+
* @param {object} opts
25+
* options object:
26+
* - name {string}
27+
* name of the key linking the container in question
28+
* - handleItemDefaults {function}
29+
* defaults method to be called on each item in the array container in question,
30+
*
31+
* N.B.
32+
*
33+
* - opts is passed to handleItemDefaults so it can also store
34+
* links to supplementary data (e.g. fullData for layout components)
35+
*
36+
* - opts.itemIsNotPlainObject is mutated on every pass in case so logic
37+
* in handleItemDefaults relies on that fact.
38+
*
39+
*/
40+
module.exports = function handleArrayContainerDefaults(parentObjIn, parentObjOut, opts) {
41+
var name = opts.name;
42+
43+
var contIn = Array.isArray(parentObjIn[name]) ? parentObjIn[name] : [],
44+
contOut = parentObjOut[name] = [];
45+
46+
for(var i = 0; i < contIn.length; i++) {
47+
var itemIn = contIn[i],
48+
itemOut = {};
49+
50+
if(!Lib.isPlainObject(itemIn)) {
51+
opts.itemIsNotPlainObject = true;
52+
itemIn = {};
53+
}
54+
else {
55+
opts.itemIsNotPlainObject = false;
56+
}
57+
58+
opts.handleItemDefaults(itemIn, itemOut, parentObjOut, opts);
59+
60+
itemOut._input = itemIn;
61+
itemOut._index = i;
62+
63+
contOut.push(itemOut);
64+
}
65+
};

0 commit comments

Comments
 (0)