Guest User

Untitled

a guest
Apr 5th, 2018
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. setSelectedHardware = (checkedNode, selectedNodes) => {
  2.     const { initialTree, renderedTree } = this.state;
  3.     let treeNodes = null;
  4.     if (renderedTree != null) {
  5.       treeNodes = renderedTree.map(item =>
  6.         this.handleRecursiveChecked(item, checkedNode)
  7.       );
  8.     } else {
  9.       treeNodes = initialTree.map(item =>
  10.         this.handleRecursiveChecked(item, checkedNode)
  11.       );
  12.     }
  13.     this.setState(prevState => ({
  14.       ...prevState,
  15.       renderedTree: treeNodes,
  16.       selected: selectedNodes
  17.     }));
  18.   };
  19.  
  20.   toggleHardwareLeaft = currentNode => {
  21.     const { initialTree, renderedTree } = this.state;
  22.     let treeNodes = null;
  23.     if (renderedTree != null) {
  24.       treeNodes = renderedTree.map(item =>
  25.         this.handleRecursiveToggle(item, currentNode)
  26.       );
  27.     } else {
  28.       treeNodes = initialTree.map(item =>
  29.         this.handleRecursiveToggle(item, currentNode)
  30.       );
  31.     }
  32.     this.setState(prevState => ({
  33.       ...prevState,
  34.       renderedTree: treeNodes
  35.     }));
  36.   };
  37.  
  38.   setInitialTree = tree => {
  39.     const initial = tree.children.map(child => this.createTree(child));
  40.     this.setState(prevState => ({
  41.       ...prevState,
  42.       initialTree: initial
  43.     }));
  44.   };
  45.  
  46.   handleRecursiveToggle = (currentItem, touchedItem, pathIndex = 0) => {
  47.     if (
  48.       currentItem.leafId === touchedItem.leafId &&
  49.       currentItem.parentLeaftId === touchedItem.parentLeaftId
  50.     ) {
  51.       if (touchedItem.expanded) {
  52.         return {
  53.           ...touchedItem,
  54.           children: currentItem.children
  55.         };
  56.       } else {
  57.         return {
  58.           ...touchedItem,
  59.           children: currentItem.children.map(child => {
  60.             return this.recursiveMap(child, 'expanded', false);
  61.           })
  62.         };
  63.       }
  64.     } else {
  65.       if (touchedItem.path[pathIndex] !== currentItem.leafId) {
  66.         return currentItem;
  67.       } else {
  68.         return {
  69.           ...currentItem,
  70.           children: currentItem.children.map(child =>
  71.             this.handleRecursiveToggle(child, touchedItem, pathIndex + 1)
  72.           )
  73.         };
  74.       }
  75.     }
  76.   };
  77.  
  78.   handleRecursiveChecked = (currentItem, touchedItem, pathIndex = 0) => {
  79.     if (
  80.       currentItem.leafId === touchedItem.leafId &&
  81.       currentItem.parentLeaftId === touchedItem.parentLeaftId
  82.     ) {
  83.       if (touchedItem.checked) {
  84.         return {
  85.           ...touchedItem,
  86.           children: currentItem.children.map(child => {
  87.             return this.recursiveMap(child, 'checked', true);
  88.           })
  89.         };
  90.       } else {
  91.         return {
  92.           ...touchedItem,
  93.           children: currentItem.children.map(child => {
  94.             return this.recursiveMap(child, 'checked', false);
  95.           })
  96.         };
  97.       }
  98.     } else {
  99.       if (touchedItem.path[pathIndex] !== currentItem.leafId) {
  100.         return currentItem;
  101.       } else {
  102.         if (!touchedItem.checked) {
  103.           return {
  104.             ...currentItem,
  105.             checked: false,
  106.             children: currentItem.children.map(child =>
  107.               this.handleRecursiveChecked(child, touchedItem, pathIndex + 1)
  108.             )
  109.           };
  110.         } else {
  111.           return {
  112.             ...currentItem,
  113.             children: currentItem.children.map(child =>
  114.               this.handleRecursiveChecked(child, touchedItem, pathIndex + 1)
  115.             )
  116.           };
  117.         }
  118.       }
  119.     }
  120.   };
  121.  
  122.   recursiveMap = (current, key, val) => {
  123.     return {
  124.       ...current,
  125.       [key]: val,
  126.       children: current.children.map(child => this.recursiveMap(child, key))
  127.     };
  128.   };
  129.  
  130.   createTree = (
  131.     item,
  132.     pathToLeaf = [],
  133.     parentLeaftId = null,
  134.     parentfilterString = null
  135.   ) => {
  136.     const leafId = item.type + ':' + item.value;
  137.     const checked = false;
  138.     const expanded = false;
  139.     const value = item.value;
  140.     const type = item.type;
  141.     const path = pathToLeaf.concat(leafId);
  142.     const filterString =
  143.       (parentfilterString != null ? parentfilterString + '||' : '') +
  144.       item.type +
  145.       ':' +
  146.       item.value;
  147.     return {
  148.       ...item,
  149.       path,
  150.       parentLeaftId,
  151.       leafId,
  152.       checked,
  153.       expanded,
  154.       value,
  155.       type,
  156.       filterString,
  157.       children: item.children.map(child =>
  158.         this.createTree(child, path, leafId, filterString)
  159.       )
  160.     };
  161.   };
Advertisement
Add Comment
Please, Sign In to add comment