import * as React from 'react'; import Card from './components/Card' import { WorkInfo } from './types'; import { taker,getApiUrl } from '&/components/helper' import styled from 'styled-components' //展开时需要计算同胞子元素占据的位置 //需要知道每一层级占据的宽度用来拖拽 interface TreeData { data: WorkInfo, children: TreeData[] left:number, right:number childrenWidth:number[] loffset?:number, roffset?:number, } export interface TreeNodeProps { treeData: TreeData, first: boolean, last: boolean, top?: number, left?: number, root?:boolean, childToggle?: Function, //getBasePointPosition:Function, index:number, middle?: boolean changeRoot?:Function direction:"vertical"|"horizontal" current: number onChildShow:Function onSelectOkr:Function } export interface TreeNodeState { childNodes?: TreeData[], expand:boolean, leftWidth: number, rightWidth: number, leftOffset:number, rightOffset: number, pending:boolean } const Middle = styled.div` ${(props:any) => props.direction == 'vertical'? 'width:100%':'height:220px'} ${(props:any) => (props.direction == 'vertical'? 'height':'width' )+ ':30px;'} position: relative; ` const Switch = styled.div` position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); width: 15px; height: 15px; border-radius: 50%; font-size: 12px; line-height: 15px; cursor: pointer; color: #FFF; text-align: center; background: #4384FF; z-index:10; ` const Path = styled.div` position: absolute; ${(p:any) => p.direction == 'vertical'? 'top' : 'left'}: 0; box-sizing: border-box; ${(p:any) => p.direction == 'vertical'? 'height' : 'width'}: 20px; border-color: #979797; border-style: solid; ${(p:any) => p.direction == 'vertical'? 'border-top-width' : 'border-left-width'}: 1px; ${(p:any) => p.direction == 'vertical'? 'border-bottom' : 'border-right'}: none; ` const ChildsWrapper = styled.div` position: absolute; bottom: ${-210 -30 -20 + 'px'}; white-space: nowrap; ` const LittleBar = styled.div` position: absolute; ${(p:any) => p.direction == 'vertical'? 'left' : 'top'}: 50%; ${(p:any) => p.direction == 'vertical'? 'width' : 'height'}: 1px; ${(p:any) => p.direction == 'vertical'? 'height' : 'width'}: 30px; background-color: #979797; ` const PlaceHolder = styled.div` display:${(p:any) => p.direction == 'vertical'? 'inline-block' : 'block'}; transition:all .3s ease; &::before { position: absolute; content: ''; ${(p:any) => p.direction == 'vertical'? 'top:30px;' : 'left:0'}; ${(p:any) => p.direction == 'horizontal'? '' : p.position == 'left' ? 'right:0;' : 'left:0;'} ${(p:any) => p.direction == 'vertical'? 'height' : 'width'}: 1px; ${(p:any) => (p.direction == 'vertical'? 'width:' : 'height:')+p.width+'px'}; background-color: #979797; } ` const cardSpaceUnit = {vertical: 196,horizontal: 120}; const cardPosition = { vertical:['left','right'], horizontal: ['top','bottom'] } export default class TreeNode extends React.Component { constructor(p:TreeNodeProps) { super(p); const {treeData} = this.props; this.state={ expand: !!treeData.children && !!treeData.children.length, leftWidth: treeData.left, rightWidth: treeData.right, leftOffset:treeData.loffset||0, rightOffset: treeData.roffset||0, childNodes:this.props.treeData.children, pending: false } this.childsWidth = treeData.childrenWidth } childsWidth:number[]=[]; leftHand?:any rightHand?:any childNodes:TreeNode[] = []; childrenContainer:{[propName:string]:HTMLDivElement} = {}; click:boolean = true timer:any toggleSwitch = async (e:MouseEvent) => { const {childToggle,index,treeData,first,last,onChildShow} = this.props; const {expand} = this.state; if(expand){ await this.doAnimate(!expand) } this.setState({ expand: !expand }) if(!expand){ this.setState({ pending: true }) onChildShow(e) // alert(getApiUrl(`okr/subOkrs`)) taker(getApiUrl(`okr/subOkrs?okrId=${treeData.data.workId}&getType=0`), 'GET').then(res=>{ let Data = res.Data || {}; let {subOkrs} = Data; let childs = subOkrs || []; childs = childs.filter((c:WorkInfo) => { return c.workId != treeData.data.workId }) if(!childs.length) return; let treeChilds:TreeData[] = [] this.childsWidth = childs.map((c:WorkInfo) => { treeChilds.push({data:c,children:[],left:0,right:0,childrenWidth:[]}) return 2 }); const marginWidth = (childs.length * 2 - 2) / 2; this.setState({ childNodes:treeChilds, leftWidth: first? 0 : marginWidth, rightWidth: last? 0 : marginWidth, pending: false },()=>{ this.doAnimate(true) }) if(childToggle) childToggle(index,childs.length * 2); }).catch(err => { console.error(err) }) }else { if(childToggle) childToggle(index,2); this.setState({ childNodes:[], leftWidth:0, rightWidth:0, leftOffset:0, rightOffset:0 }) } } childToggle = (i:number,width:number,pwidth?:number[]) =>{ const {childToggle,index,root,first,last,middle} = this.props; this.childsWidth[i] = width; const childsNum = this.childsWidth.length; const half = Math.floor(childsNum/2); let leftWidth = 0,rightWidth = 0; if(childsNum % 2 == 1 && i == half){ this.setState({ leftOffset: pwidth && pwidth[0] || (width - 2) / 2, rightOffset: pwidth && pwidth[1] || (width - 2) / 2, }) } if(root) return; for(let i = 0;i= childsNum - half; i--){ rightWidth += this.childsWidth[i]; } if(childsNum % 2 == 1){ leftWidth += this.childsWidth[half] / 2; rightWidth += this.childsWidth[half] / 2; } childToggle && childToggle(index,leftWidth+rightWidth || 2,[leftWidth - 1,rightWidth -1]) if(middle) return; this.setState({ leftWidth: first ? 0 : leftWidth - 1, rightWidth: last ? 0 : rightWidth - 1 }) } getStyle = () => { const {first,last,root,direction} = this.props; if(root) return {}; if(first && last) { return { border: 'none' } } if(first){ return { [cardPosition[direction][0]]: cardSpaceUnit[direction] + 'px', [cardPosition[direction][1]]: 0, borderTopLeftRadius: '5px', [`border${direction == 'vertical'?'Left':'Top'}Width`]: '1px', [`border${direction == 'vertical'?'Right':'Bottom'}Width`]: 0 } } if(last){ return { [cardPosition[direction][1]]: cardSpaceUnit[direction] + 'px', [cardPosition[direction][0]]: 0, borderTopRightRadius: '5px', [`border${direction == 'vertical'?'Right':'Bottom'}Width`]: '1px', [`border${direction == 'vertical'?'Left':'Top'}Width`]: 0 } } return { [cardPosition[direction][0]]: 0, [cardPosition[direction][1]]: 0, [`border${direction == 'vertical'?'Left':'Top'}Width`]: 0, [`border${direction == 'vertical'?'Right':'Bottom'}Width`]: 0 } } changeRoot = (e:MouseEvent) => { const {changeRoot,treeData} = this.props; if(!treeData.data.parentId || !changeRoot) return; changeRoot(treeData.data.parentId,e); } getTreeData = () => { const {treeData} = this.props; const {leftWidth,rightWidth,leftOffset,rightOffset} = this.state; const children = this.childNodes.map(child => child.getTreeData()); let tree:TreeData = { data: treeData.data, children, left:leftWidth, right:rightWidth, loffset:leftOffset, roffset:rightOffset, childrenWidth: this.childsWidth } return tree; } doAnimate = (show:boolean) => { const {direction} = this.props; //@ts-ignore // this.childrenContainer[direction].style[direction == 'vertical'? 'top' : 'left'] = show? '0' : '-200px'; this.childrenContainer[direction].style.opacity = show? '1' : '0'; this.childrenContainer[direction].style.transform = show? 'scale(1)':'scale(0)'; return new Promise((resolve,reject) => { setTimeout(() => { resolve() }, 300); }) } renderChildNodes = () => { const {childNodes,expand,leftOffset,rightOffset} = this.state; const {direction,onChildShow,onSelectOkr} = this.props; if(!expand || !childNodes || !childNodes.length) return null; let nodes:any[] = []; if(childNodes.length == 1){ nodes.push( this.childNodes[0] = r as TreeNode} current={this.props.current} key={childNodes[0].data.workId} index={0} treeData={childNodes[0]} first last childToggle={this.childToggle} middle/> ) }else if(childNodes.length%2 == 0){ const leftNum = childNodes.length / 2; nodes.push( { childNodes.slice(0,leftNum).map((node,index) => { return this.childNodes[index] = r as TreeNode} index={index} treeData={node} first={index==0} last={false} childToggle={this.childToggle}/> }) } ) nodes.push( { childNodes.slice(leftNum,childNodes.length).map((node,index) => { return this.childNodes[index+leftNum] = r as TreeNode} index={index+leftNum} treeData={node} first={false} last={index+leftNum==childNodes.length -1} childToggle={this.childToggle}/> }) } ) }else { const leftNum = Math.floor(childNodes.length / 2); nodes.push( this.leftHand = r} style={{[cardPosition[direction][1]]: '100%'}}> { childNodes.slice(0,leftNum).map((node,index) => { return this.childNodes[index] = r as TreeNode} key={node.data.workId} index={index} treeData={node} first={index==0} last={false} childToggle={this.childToggle}/> }) } ) nodes.push( this.childNodes[leftNum] = r as TreeNode} key={childNodes[leftNum].data.workId} index={leftNum} treeData={childNodes[leftNum]} first={false} last={false} childToggle={this.childToggle} middle/> ) nodes.push( this.rightHand = r} style={{[cardPosition[direction][0]]: '100%'}}> { childNodes.slice(leftNum+1,childNodes.length).map((node,index) => { return this.childNodes[index + 1 + leftNum] = r as TreeNode} index={leftNum + 1 + index} treeData={node} first={false} last={index+leftNum+1==childNodes.length-1} childToggle={this.childToggle}/> }) } ) } return nodes; } handleMouseDown = () => { this.click = true; this.timer = setTimeout(() => { this.click = false; }, 300); } handleClickCard = () => { clearTimeout(this.timer); if(!this.click) return; this.props.onSelectOkr(this.props.treeData.data) } public render():any { const {treeData,top,left,root,first,last,middle,direction,current} = this.props; const {expand,leftWidth,rightWidth} = this.state; let childrenContainerStyle = {} if(!expand){ childrenContainerStyle = { opacity: 0, transform: 'scale(0)' } } let wrapStyle = {}; if(root){ wrapStyle = { position:'absolute', top:top+'px', left:left+'px', transition: 'all .3s ease-out' } } if(direction == 'horizontal'){ return (
{root && treeData.data.parentId != 0 && ||
} {!root &&
{((!first&&!last) || (first&&last))?
:null}
}
{treeData.data.hasSub && {expand&&} ||
}
this.childrenContainer.horizontal = r as HTMLDivElement}> {this.renderChildNodes()}
) } return (
{root && treeData.data.parentId != 0 && this.changeRoot(e)} className={'tu-icon-add'}/> ||
} {!root &&
{((!first&&!last) || (first&&last))?
:null}
}
{treeData.data.hasSub && {expand&&} {/* {pending && } */} ||
}
this.childrenContainer.vertical = r as HTMLDivElement}> {this.renderChildNodes()}
); } }