import TreeModel from 'tree-model' ; import { ButtonM , CheckboxM , FileUploadM , FolderM , InputM , RadioButtonM , SelectMenuM , SwitchM , TextAreaM , TransformM , } from '@synerise/ds-icon' ; import { TreeData , TreeNode } from './TreeMenu.types' ; param items export function generateTree ( items ) { const treeModel = new TreeModel ( ) ; const model = treeModel . parse ( { key : 'root' , name : 'root' , children : items , } ) ; return model ; } param items export function getCount ( items ) { let count = items . length ; items . forEach ( item => { if ( item . children ) count += getCount ( item . children ) ; } ) ; return count ; } param items param searchQuery export function childrenHaveString ( items , searchQuery ) { let haveString = false ; items . forEach ( item => { if ( item . model . name . toLowerCase ( ) . indexOf ( searchQuery . toLowerCase ( ) ) > - 1 ) { haveString = true ; return ; } if ( ! haveString && item . children ) haveString = childrenHaveString ( item . children , searchQuery ) ; } ) ; return haveString ; } /**
 * Flattens the the tree to array
 */ export function getItemsToRender ( items , depth = 0 , expandedKeys , searchQuery ) { const newItems = [ ] ; items . forEach ( ( rawItem , index ) => { const thisItem = rawItem ; const { children } = thisItem ; if ( ! thisItem . model ) return ; const { name , key } = thisItem . model ; thisItem . depth = depth ; thisItem . index = index ; if ( name && searchQuery && ! ( name . toLowerCase ( ) . indexOf ( searchQuery . toLowerCase ( ) ) > - 1 || ( children && childrenHaveString ( children , searchQuery ) ) ) ) return ; newItems . push ( thisItem ) ; if ( children && ( expandedKeys . includes ( key ) || searchQuery ) ) { const childrenItems = getItemsToRender ( thisItem . children , depth + 1 , expandedKeys , searchQuery ) ; childrenItems . forEach ( item => newItems . push ( item ) ) ; } } ) ; return newItems ; } export function getKeyFromItem ( item ) { let itemKey = '' ; if ( item . parent ?. index !== undefined ) itemKey = `${ getKeyFromItem ( item . parent ) }-` ; itemKey = `${ itemKey }${ item . index }-${ item . model . name }` ; return itemKey ; } export const validateItemName = ( name ) : boolean => { const trimmedName = name . trim ( ) ; const CONTAINS_ONLY_ALLOWED_CHARACTERS = /^[^\\\/\?\*\"\>\<\:\|]*$/im ; if ( ! trimmedName || ! trimmedName . match ( CONTAINS_ONLY_ALLOWED_CHARACTERS ) ) { return false ; } return true ; } ; export const typeIcons = { folder : FolderM , text : InputM , textArea : TextAreaM , relation : TransformM , switch : SwitchM , radio : RadioButtonM , checkbox : CheckboxM , select : SelectMenuM , upload : FileUploadM , button : ButtonM , } ;