All files _convertObject.js

5.88% Statements 2/34
0% Branches 0/47
0% Functions 0/6
5.88% Lines 2/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                        1x                                                                     1x                                                                  
import tools from './tools';
 
// 转换 Object3D
 
/**
 * 转化为 flat Tree 数据,当没有 children 或者 children 为空时,则不设置 children
 * [key, title, children, object]
 * 不排除满足 excludeTypes 项的子节点
 * @param {Object} source
 * @param {Object} options 配置参数
 * @param {Array<String>} excludeTypes 需要排除的 types 项 如: ['Camera', 'Scene', 'Light'] `excludeTypes.includes(source.type) 则无需划归到tree中`
 */
export const toFlatTreeData = (source, options = {}, excludeTypes = []) => {
  const { key = 'uuid', title = 'displayName', childrenKey = 'children' } = options;
  const result = [];
  function loop(arr) {
    arr.forEach((a) => {
      const childrens = a[childrenKey];
      if (childrens && tools.isArray(childrens) && childrens.length > 0) {
        loop(childrens);
      } else {
        if (!excludeTypes.includes(a.type)) {
          result.push({
            key: a[key],
            title: (a.userData && a.userData[title] ? a.userData[title] : a[title]) || a.name,
            object: a
          });
        }
      }
    });
  }
  let dataList = source;
  if (!tools.isArray(source)){
    dataList = [source];
  }
 
  loop(dataList);
  return result;
};
 
/**
 * 转化为 Tree 数据,当没有 children 或者 children 为空时,则不设置 children
 * [key, title, children, object]
 * @param {Object} source
 * @param {Object} options 配置参数
 * @param {Array<String>} excludeTypes 需要排除的 types 项 如: ['Camera', 'Scene', 'Light'] `excludeTypes.includes(source.type) 则无需划归到tree中`
 */
export const toTreeData = (source, options = {}, excludeTypes = []) => {
  const { key = 'uuid', title = 'displayName', childrenKey = 'children', bindObject = true } = options;
  function loop(arr) {
    return arr.map((a) => {
      const result = {};
      if (!excludeTypes.includes(a.type)) {
        // 跳过 userData.skipObjTree
        if (a.userData && a.userData.skipTreeData){
          return false;
        }
        result.key = a[key];
        result.title = (a.userData && a.userData[title] ? a.userData[title] : a[title]) || a.name;
        // 绑定 object 自身
        if (bindObject){
          result.object = a;
        }
        const childrens = a[childrenKey];
        if (childrens && tools.isArray(childrens) && childrens.length > 0) {
          result.children = loop(childrens);
        }
        return result;
      }
 
      return false;
    }).filter(Boolean);
  }
  let dataList = source;
  if (!tools.isArray(source)){
    dataList = [source];
  }
 
  return loop(dataList);
};