/**
 * Copyright 2019 The Hyve B.V.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */ import { GbTreeNode } from '../models/tree-node-models/gb-tree-node' ; import { VisualAttribute } from '../models/tree-node-models/visual-attribute' ; export class TreeNodeHelper { public static VALID_TREE_NODE_TYPES = [ 'NUMERIC' , 'CATEGORICAL' , 'CATEGORICAL_OPTION' , 'DATE' , 'STUDY' , 'TEXT' , 'HIGH_DIMENSIONAL' , 'UNKNOWN' ] ; param parent param selectedNodes public static updateParentNodesSelectionRecursively ( parent , selectedNodes ) { if ( parent === undefined ) { return ; } parent . partialSelected = false ; if ( parent . children . every ( ( child ) => selectedNodes . includes ( child ) ) ) { this . addNodeToSelectedNodes ( parent , selectedNodes ) ; } else { this . removeNodeFromSelectedNodes ( parent , selectedNodes ) ; if ( parent . children . some ( ( child ) => ( selectedNodes . includes ( child ) || child . partialSelected === true ) ) ) { parent . partialSelected = true ; } } this . updateParentNodesSelectionRecursively ( parent . parent , selectedNodes ) ; } param node public static updateParentForAllChildren ( node ) { node . children . forEach ( ( child ) => { if ( child ) { child . parent = node ; } } ) ; } param node param selectedNodes public static removeNodeFromSelectedNodes ( node , selectedNodes ) { const index = selectedNodes . indexOf ( node , 0 ) ; if ( index > - 1 ) { selectedNodes . splice ( index , 1 ) ; } } param node param selectedNodes public static addNodeToSelectedNodes ( node , selectedNodes ) { selectedNodes . push ( node ) ; } param nodes public static copyTreeNodes ( nodes ) { let nodesCopy = [ ] ; for ( let node of nodes ) { let parent = node . parent ; let children = node . children ; node . parent = null ; node . children = null ; let nodeCopy = JSON . parse ( JSON . stringify ( node ) ) ; if ( children ) { nodeCopy . children = this . copyTreeNodes ( children ) ; } nodesCopy . push ( nodeCopy ) ; node . parent = parent ; node . children = children ; } return nodesCopy ; } param { TreeNode } node returns { TreeNode } public static copyTreeNodeUpward ( node ) { let nodeCopy = { } ; let parentCopy = null ; for ( let key in node ) { if ( key === 'parent' ) { parentCopy = this . copyTreeNodeUpward ( node [ key ] ) ; } else if ( key !== 'children' ) { nodeCopy [ key ] = JSON . parse ( JSON . stringify ( node [ key ] ) ) ; } } if ( parentCopy ) { nodeCopy . parent = parentCopy ; } return nodeCopy ; } param node public static depthOfTreeNode ( node ) { return node . fullName ? node . fullName . split ( '\\' ) . length - 2 : null ; } param { TreeNode } node returns { boolean } public static isVariableNode ( node ) { const type = node . type ; return ( type === 'NUMERIC' || type === 'CATEGORICAL' || type === 'CATEGORICAL_OPTION' || type === 'DATE' || type === 'HIGH_DIMENSIONAL' || type === 'TEXT' ) ; } public static flattenTreeNodes ( nodes , flattened ) { for ( let node of nodes ) { flattened . push ( node ) ; if ( node . children ) { this . flattenTreeNodes ( node . children , flattened ) ; } } } public static getAllVariablesFromTreeNode ( node , variables ) { if ( node . children ) { for ( let child of node . children ) { this . getAllVariablesFromTreeNode ( child , variables ) ; } } else if ( this . isVariableNode ( node ) ) { variables . push ( node ) ; } } param { TreeNode } node returns { boolean } public static isTreeNodeStudy ( node ) { return node . type ? node . type === 'STUDY' : false ; } public static isTreeNodeLeaf ( node ) { return node . visualAttributes ? node . visualAttributes . includes ( VisualAttribute . LEAF ) : false ; } }