import * as React from 'react'; import { WorkInfo } from './types'; import styled from 'styled-components' import TreeNode from './TreeNode' import { taker,getApiUrl } from '&/components/helper' import { findDOMNode } from 'react-dom'; export interface MapProps { root: WorkInfo containerId: string, goBack:Function, onSelectOkr:Function } interface TreeData { data: WorkInfo, children: TreeData[] left:number, right:number, childrenWidth:number[], loffset?:number, roffset?:number } const Container = styled.div` width:100%; height: 100%; overflow: visible; position: relative; transition: transform .3s ease-in,opacity .3s ease-in; opacity: 0; ` const Wrapper = styled.div` position:absolute; top:0; right:0; bottom:0; left:0; overflow: hidden; cursor: ${(p:any) => p.dragging? 'grabbing': 'grab'}; ` const OpWrapper = styled.div` position:fixed; top: 55px; right: 20px; display:flex; ` const BtnWrapper = styled.div` display: inline-flex; background: #FAFAFA; border-radius: 4px; box-shadow: 0 1px 6px 0 rgba(0,0,0,.1); font-size: 20px; color: #666666; align-items: center; justify-content: center; margin-left: 20px; height: 40px; cursor: default; transition: all .3s ease-in; &:hover{ ${(props:any) => { return props.hover || {} }} } ` const BarOuter = styled.div` position: relative; width: 146px; height: 4px; background: #D8D8D8; border-radius: 2px; margin: 0 11px; ` const Icon = styled.div` cursor: pointer; transition: color .3s ease-in; &:hover{ color: #1985FF; } ` const BarInner = styled.div` position: absolute; left:0; top:0; height: 4px; background: #1A85FF; border-radius: 2px; transition: right .3s ease-in; &::before { position: absolute; content: ''; width: 16px; height: 16px; border-radius: 8px; background-color: #1A85FF; top: -6px; right: -6px; } &::after { position: absolute; content: ''; width: 8px; height: 8px; border-radius: 4px; background-color: #FFF; top: -2px; right: -2px; } ` export default class Map extends React.Component { container:any constructor(p:MapProps){ super(p); this.state = { scale:1, root: this.props.root, direction:'vertical', dragging:false, current: this.props.root.workId } } public getBasePointPosition = () => { const container = findDOMNode(this.container) as Element; return container.getBoundingClientRect() } contLeft:number = 0; contTop:number = 0; root: TreeNode drag:boolean = false; startX:number; startY:number; startMoveX:number=0; startMoveY:number =0; timer:any; moveTimer:any; wheelTimer:any; throttle:boolean = false; wheelThrottle:boolean = false; scaleDelta = 0; children:WorkInfo[] = []; handleMouseDown=(e:React.MouseEvent) => { if(e.button) return; this.drag = true; this.setState({ dragging:true }) this.startX = e.screenX; this.startY = e.screenY; const cont = findDOMNode(this.container) as any; this.startMoveX = cont.style.left.replace('px','') - 0; this.startMoveY = cont.style.top.replace('px','') - 0; } handleMouseUp = () =>{ this.drag = false; this.setState({ dragging:false }) } handleMouseLeave = () => { this.drag = false; this.setState({ dragging:false }) } componentWillUnmount(){ } componentDidMount(){ const okrId = this.props.root.workId; const level = this.props.root.parentId? 3:2; taker(getApiUrl(`okr/subOkrs?okrId=${okrId}&getType=1`), 'GET').then(res=>{ let datas = res && res.Data; let tree = this.parseTree(okrId,datas,level+1) this.calcuLR(tree); this.setState({ treeData:tree },()=>{ this.container.style.opacity = 1; }) }) } calcuLR = (tree:TreeData,appendData?:TreeData) => { let cWidths = tree.children.map((c) => this.calcuLR(c)); tree.childrenWidth = cWidths.map((c:number[]) => c[0]); let half = Math.floor(cWidths.length/2); if(appendData) { cWidths.push([appendData.left+appendData.right+2,appendData.left,appendData.right]); tree.childrenWidth.push(appendData.left+appendData.right+2) } if(cWidths.length < 2){ tree.left = tree.right = 0 }else if(cWidths.length % 2 == 0){ for(let i = 0; i < half; i++){ tree.left += cWidths[i][0] } for(let i= half; i< cWidths.length; i++){ tree.right += cWidths[i][0] } tree.left = tree.left - 1; tree.right = tree.right - 1; }else { for(let i = 0; i < half; i++){ tree.left += cWidths[i][0] } for(let i= half + 1; i< cWidths.length; i++){ tree.right += cWidths[i][0] } tree.left = tree.left + cWidths[half][1]; tree.right = tree.right + cWidths[half][2]; tree.loffset = cWidths[half][1]; tree.roffset = cWidths[half][2]; } return [tree.left + tree.right + 2,tree.left,tree.right] } parseTree = (rootId:number,data:{currentOkr:WorkInfo,parentOkr:WorkInfo|null,subOkrs:WorkInfo[]|null,eqOkrs:WorkInfo[]|null},level=4) => { const {currentOkr,parentOkr,subOkrs,eqOkrs} = data; let treeData:TreeData = this.initTreeData(currentOkr); if(subOkrs){ subOkrs.forEach(sub => { treeData.children.push(this.initTreeData(sub)); }) } if(parentOkr){ let root = this.initTreeData(parentOkr); if(eqOkrs && eqOkrs.length){ eqOkrs.forEach((eq,i) => { if(i == Math.floor(eqOkrs.length/2)){ root.children.push(treeData); } root.children.push(this.initTreeData(eq)) }) }else { root.children.push(treeData); } treeData = root; } return treeData; } handleMouseMove = (e:React.MouseEvent) => { if(!this.drag||this.throttle) return; const cont = findDOMNode(this.container) as any; cont.style.left=this.startMoveX + e.screenX - this.startX + 'px'; cont.style.top = this.startMoveY + e.screenY - this.startY + 'px'; this.throttle = true; this.moveTimer = setTimeout(() => { this.throttle = false; }, 20); } handleMouseWheel = (e:React.WheelEvent) => { if(this.wheelThrottle) return; clearTimeout(this.timer) this.wheelThrottle = true; this.scaleDelta += e.deltaY > 0? -0.1:0.1; this.timer = setTimeout(() => { this.doScale() }, 300); this.wheelTimer = setTimeout(() => { this.wheelThrottle = false }, 100); } doScale = (scale?:number) => { let s = scale || this.state.scale + this.scaleDelta; if(!scale) this.scaleDelta = 0; if(s>1.5){ s = 1.5 }else if(s<0.5) { s = 0.5 } this.setState({ scale:s }) } backToList = () => { this.props.goBack() } initTreeData = (data:WorkInfo):TreeData => ({data,children:[],childrenWidth:[],left:0,right:0}) changeRoot = (okrId:number) => { const td = this.root.getTreeData() taker(getApiUrl(`okr/subOkrs?okrId=${okrId}`), 'GET').then(res=>{ this.children=[]; let children:TreeData[]= []; let self=this.initTreeData(res.Data.currentOkr); let datas:WorkInfo[] = res.Data.subOkrs || []; datas.forEach((d:WorkInfo) => { if(d.workId != td.data.workId) { children.push(this.initTreeData(d)) } }) if(self==undefined) return; (self as TreeData).children = children; this.calcuLR(self,td); self.children.push(td); this.container.style.opacity = 0; setTimeout(() => { this.setState({ treeData:self },()=>{ this.container.style.opacity = 1; }) }, 300); }) } toggleDirection = () => { this.setState({ direction: this.state.direction == 'vertical'? 'horizontal': 'vertical' }) const cont = findDOMNode(this.container) as any; this.contLeft = cont.style.left.replace('px','') - 0 || 0; this.contTop = cont.style.top.replace('px','') - 0 || 0; } handleChildShow = (e:MouseEvent) => { const {scale,direction} = this.state; let value,cardSize,position,windowSize; if(direction == 'vertical'){ value = e.clientY; cardSize = 280; position = 'top'; windowSize = document.body.clientHeight; }else { value = e.clientX; cardSize = 380; position = 'left'; windowSize = document.body.clientWidth; } const root = findDOMNode(this.root) as HTMLDivElement if(windowSize - value this.container = r} style={{transform:`scale(${scale})`}}> {treeData && this.root =r as TreeNode} treeData={treeData} key={treeData.data.workId} first={false} last={false} top={rootTop} left={rootLeft} changeRoot={this.changeRoot} direction={direction} index={0} root middle current={current}/>} {/* 返回地图首页 */} 切换 ); } }