|
| 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 | + * Its arguments are: |
| 32 | + * - itemIn {object} item in user layout |
| 33 | + * - itemOut {object} item in full layout |
| 34 | + * - parentObj {object} (as in closure) |
| 35 | + * - opts {object} (as in closure) |
| 36 | + * - itemOpts {object} |
| 37 | + * - itemIsNotPlainObject {boolean} |
| 38 | + * N.B. |
| 39 | + * |
| 40 | + * - opts is passed to handleItemDefaults so it can also store |
| 41 | + * links to supplementary data (e.g. fullData for layout components) |
| 42 | + * |
| 43 | + */ |
| 44 | +module.exports = function handleArrayContainerDefaults(parentObjIn, parentObjOut, opts) { |
| 45 | + var name = opts.name; |
| 46 | + |
| 47 | + var contIn = Array.isArray(parentObjIn[name]) ? parentObjIn[name] : [], |
| 48 | + contOut = parentObjOut[name] = []; |
| 49 | + |
| 50 | + for(var i = 0; i < contIn.length; i++) { |
| 51 | + var itemIn = contIn[i], |
| 52 | + itemOut = {}, |
| 53 | + itemOpts = {}; |
| 54 | + |
| 55 | + if(!Lib.isPlainObject(itemIn)) { |
| 56 | + itemOpts.itemIsNotPlainObject = true; |
| 57 | + itemIn = {}; |
| 58 | + } |
| 59 | + |
| 60 | + opts.handleItemDefaults(itemIn, itemOut, parentObjOut, opts, itemOpts); |
| 61 | + |
| 62 | + itemOut._input = itemIn; |
| 63 | + itemOut._index = i; |
| 64 | + |
| 65 | + contOut.push(itemOut); |
| 66 | + } |
| 67 | +}; |
0 commit comments