Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- setSelectedHardware = (checkedNode, selectedNodes) => {
- const { initialTree, renderedTree } = this.state;
- let treeNodes = null;
- if (renderedTree != null) {
- treeNodes = renderedTree.map(item =>
- this.handleRecursiveChecked(item, checkedNode)
- );
- } else {
- treeNodes = initialTree.map(item =>
- this.handleRecursiveChecked(item, checkedNode)
- );
- }
- this.setState(prevState => ({
- ...prevState,
- renderedTree: treeNodes,
- selected: selectedNodes
- }));
- };
- toggleHardwareLeaft = currentNode => {
- const { initialTree, renderedTree } = this.state;
- let treeNodes = null;
- if (renderedTree != null) {
- treeNodes = renderedTree.map(item =>
- this.handleRecursiveToggle(item, currentNode)
- );
- } else {
- treeNodes = initialTree.map(item =>
- this.handleRecursiveToggle(item, currentNode)
- );
- }
- this.setState(prevState => ({
- ...prevState,
- renderedTree: treeNodes
- }));
- };
- setInitialTree = tree => {
- const initial = tree.children.map(child => this.createTree(child));
- this.setState(prevState => ({
- ...prevState,
- initialTree: initial
- }));
- };
- handleRecursiveToggle = (currentItem, touchedItem, pathIndex = 0) => {
- if (
- currentItem.leafId === touchedItem.leafId &&
- currentItem.parentLeaftId === touchedItem.parentLeaftId
- ) {
- if (touchedItem.expanded) {
- return {
- ...touchedItem,
- children: currentItem.children
- };
- } else {
- return {
- ...touchedItem,
- children: currentItem.children.map(child => {
- return this.recursiveMap(child, 'expanded', false);
- })
- };
- }
- } else {
- if (touchedItem.path[pathIndex] !== currentItem.leafId) {
- return currentItem;
- } else {
- return {
- ...currentItem,
- children: currentItem.children.map(child =>
- this.handleRecursiveToggle(child, touchedItem, pathIndex + 1)
- )
- };
- }
- }
- };
- handleRecursiveChecked = (currentItem, touchedItem, pathIndex = 0) => {
- if (
- currentItem.leafId === touchedItem.leafId &&
- currentItem.parentLeaftId === touchedItem.parentLeaftId
- ) {
- if (touchedItem.checked) {
- return {
- ...touchedItem,
- children: currentItem.children.map(child => {
- return this.recursiveMap(child, 'checked', true);
- })
- };
- } else {
- return {
- ...touchedItem,
- children: currentItem.children.map(child => {
- return this.recursiveMap(child, 'checked', false);
- })
- };
- }
- } else {
- if (touchedItem.path[pathIndex] !== currentItem.leafId) {
- return currentItem;
- } else {
- if (!touchedItem.checked) {
- return {
- ...currentItem,
- checked: false,
- children: currentItem.children.map(child =>
- this.handleRecursiveChecked(child, touchedItem, pathIndex + 1)
- )
- };
- } else {
- return {
- ...currentItem,
- children: currentItem.children.map(child =>
- this.handleRecursiveChecked(child, touchedItem, pathIndex + 1)
- )
- };
- }
- }
- }
- };
- recursiveMap = (current, key, val) => {
- return {
- ...current,
- [key]: val,
- children: current.children.map(child => this.recursiveMap(child, key))
- };
- };
- createTree = (
- item,
- pathToLeaf = [],
- parentLeaftId = null,
- parentfilterString = null
- ) => {
- const leafId = item.type + ':' + item.value;
- const checked = false;
- const expanded = false;
- const value = item.value;
- const type = item.type;
- const path = pathToLeaf.concat(leafId);
- const filterString =
- (parentfilterString != null ? parentfilterString + '||' : '') +
- item.type +
- ':' +
- item.value;
- return {
- ...item,
- path,
- parentLeaftId,
- leafId,
- checked,
- expanded,
- value,
- type,
- filterString,
- children: item.children.map(child =>
- this.createTree(child, path, leafId, filterString)
- )
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment