import React from 'react';
import { Drawer, Space, Button, Dropdown, Menu, Table } from 'antd';
import { OrgChartComponent } from '@/components/orgChart';
import * as d3 from 'd3';
import qs from 'qs';
import { message } from 'antd';
import jsPDF from 'jspdf';
import styles from './index.less';
let addNodeChildFunc = null;
let orgChart = null;
let active = 'top';
export default class DrawerComponents extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
data: [],
detailType: 'chart',
rootId: '',
};
}
componentDidMount() {
//this.getDeatilDatas();
}
// 点击节点
onNodeClick(node) {}
onButtonClick(event, d) {
if (d.children) {
let idsList = [];
d.children.forEach((item) => {
if (item.data.hasChildren && !item._children) {
idsList.push(item.data.id);
}
});
if (idsList.length == 0) {
return;
}
let idsStr = idsList.join(',');
let api = '/api/bs/hrmorganization/orgchart/asyncUserData?ids=' + idsStr;
fetch(api)
.then((res) => res.json())
.then((data) => {
if (data.data) {
data.data.forEach((item) => {
window.chart.addNode(item);
});
}
});
}
}
// 获取部门图片
getDepartmentImage() {
let index = Math.floor(Math.random() * 8) + 1;
return `./img/department/${index}.png`;
}
//获取数据
getDeatilDatas(rootId) {
const { detailType } = this.state;
d3.json(
'/api/bs/hrmorganization/orgchart/userData?' +
qs.stringify({ detauleType: detailType, rootId: rootId }),
).then((data) => {
this.setState(data.data);
});
}
// ButtonContent渲染
buttonContentRender = ({ node, state }) => {
return `
`;
};
// 节点宽度渲染
nodeWidthRender = (d) => {
return 280;
};
nodeHeightRender = (d) => {
return 160;
};
// tool bar start
handleTopLayoutClick = (progressBtn) => {
progressBtn.current.style.top = 50 + 'px';
orgChart &&
orgChart
.setCentered(orgChart.getChartState().root.id)
.layout('top')
.render();
active = 'top';
};
handleLeftLayoutClick = (progressBtn) => {
progressBtn.current.style.top = 50 + 'px';
orgChart &&
orgChart
.layout('left')
.setCentered(orgChart.getChartState().root.id)
.render();
active = 'left';
};
handleZoomIn = (progressBtn) => {
if (progressBtn) {
let top = parseInt(progressBtn.current.style.top) - 10;
if (top >= 0) {
progressBtn.current.style.top = top + 'px';
} else {
return;
}
}
orgChart && orgChart.zoomIn();
};
handleZoomOut = (progressBtn) => {
if (progressBtn) {
let top = parseInt(progressBtn.current.style.top) + 10;
if (top <= 100) {
progressBtn.current.style.top = top + 'px';
} else {
return;
}
}
orgChart && orgChart.zoomOut();
};
downloadPdf(chart) {
chart.exportImg({
save: false,
full: true,
onLoad: (base64) => {
var pdf = new jsPDF();
var img = new Image();
img.src = base64;
img.onload = function () {
pdf.addImage(
img,
'JPEG',
5,
5,
595 / 3,
((img.height / img.width) * 595) / 3,
);
pdf.save('chart.pdf');
};
},
});
}
handleExport = (e) => {
let type = e.key == '1' ? 'png' : 'pdf';
if (type == 'png') {
orgChart && orgChart.exportImg({ full: true });
} else {
orgChart && downloadPdf(orgChart);
}
};
/**
* 节点渲染
*/
nodeContentRender = (d, i, arr, state) => {
if (d.data.ftype == 2) {
return `
${
d.data.fleadername
}
${d.data.fname}${
d.data.fleaderjob ? `/${d.data.fleaderjob}` : ''
}
`;
} else if (d.data.ftype == 3) {
return `
${d.data.fname}
在岗:${d.data.fonjob}
`;
} else if (d.data.ftype == 4) {
return ``;
}
};
showDrawer = (rootId) => {
this.getDeatilDatas(rootId);
this.setState({ open: true, rootId: rootId });
};
onClose = () => {
this.setState({ open: false });
};
changeDetail = () => {
const { detailType, rootId } = this.state;
this.getDeatilDatas(rootId);
this.setState({
detailType: detailType == 'chart' ? 'table' : 'chart',
});
};
render() {
const { open, data, detailType } = this.state;
const menu = (
);
const dataSource = [
{
key: '1',
name: '胡彦斌',
age: 32,
address: '西湖区湖底公园1号',
},
{
key: '2',
name: '胡彦祖',
age: 42,
address: '西湖区湖底公园1号',
},
];
const columns = [
{
title: '姓名',
dataIndex: 'name',
key: 'name',
},
{
title: '年龄',
dataIndex: 'age',
key: 'age',
},
{
title: '住址',
dataIndex: 'address',
key: 'address',
},
];
return (
}
>
{detailType == 'chart' ? (
(orgChart = chart)}
setClick={(click) => (addNodeChildFunc = click)}
onNodeClick={this.onNodeClick}
onButtonClick={this.onButtonClick}
data={data}
buttonContent={this.buttonContentRender}
nodeWidth={this.nodeWidthRender}
nodeHeight={this.nodeHeightRender}
nodeContent={this.nodeContentRender}
/>
) : (
)}
);
}
}