import * as React from 'react'; import QueueAnim from 'rc-queue-anim'; import AvatarImg from '../../../../../components/common/avatarImg/avatarImg.tsx'; import PropTypes from 'prop-types'; import { getBSGlobal, getUserInfo, // changeQueryString } from '../../../../../helper'; export interface User { avatarColor: string; avatarImg: string; name: string; userId: number; position: string; count: number; } export interface PeerLevelProps { users: User[], isFetching: boolean; currentUserId: number; getSubUsers: Function; canVS: boolean; addVSCallback: Function; showVS: Function; talentVSUsers: number[] } interface PeerLevelState { left: number; currentIndex: number; users: any[]; transition: boolean } const userAgent = window.navigator.userAgent.toLowerCase(), ios = /iphone|ipod|ipad/.test(userAgent); export default class PeerLevel extends React.PureComponent{ getUserTimer: any; static contextTypes = { getSelectedUsers: PropTypes.func, updateSelectedUsers: PropTypes.func, showPromptPop: PropTypes.func, handleChangeUser: PropTypes.func, }; constructor(props: PeerLevelProps) { super(props); const { currentIndex } = this.getUserListLeft(props) this.state = { left: 190 - currentIndex * 80 - 75, currentIndex, users: [], transition: true } } componentDidMount() { const { users } = this.props; this.dealUsers(users); } componentWillReceiveProps(nextProps: PeerLevelProps) { const { currentIndex } = this.getUserListLeft(nextProps); const { users } = nextProps; this.dealUsers(users); this.setState({ left: 190 - currentIndex * 80 - 75, currentIndex }) } getUserListLeft = (props: PeerLevelProps) => { const { currentUserId, users } = props; const currentIndex = users.findIndex((item: User) => item.userId == currentUserId); return { currentIndex } } handlePrev = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); if (ios) return; this.prev(); } handleNext = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); if (ios) return; this.next(); } handlePrevTouchStart = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); this.prev(); } handleNextTouchStart = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); this.next() } prev = () => { const { currentIndex } = this.state; const newCurrentIndex = currentIndex - 1 < 0 ? 0 : currentIndex - 1; this.getSubUsers(newCurrentIndex); this.setState({ currentIndex: newCurrentIndex, left: 190 - newCurrentIndex * 80 - 75 }) } next = () => { const { currentIndex } = this.state; const userLen = this.props.users.length; const newCurrentIndex = currentIndex + 1 > userLen - 1 ? userLen - 1 : currentIndex + 1; this.getSubUsers(newCurrentIndex); this.setState({ currentIndex: newCurrentIndex, left: 190 - newCurrentIndex * 80 - 75 }) } getSubUsersTimer: any getSubUsers = (index: number) => { const { getSubUsers, users } = this.props; const { userId } = users[index]; clearTimeout(this.getSubUsersTimer) this.getSubUsersTimer = setTimeout(() => { getSubUsers(userId, 'sub'); }, 300) } handleUseClick = (userId: Number, index: number) => (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); this.dealUserToogle(userId, index) } dealUserToogle = (userId: Number, index: number) => { const { currentIndex } = this.state; if (index == currentIndex) { if(this.context.handleChangeUser){ this.context.handleChangeUser(userId) return; } const webPath = getBSGlobal('webPath'); const loginId = getUserInfo()['Id']; const gotoHash = `Home#${`okr/panel?to_user_id=${userId}`}`; let iframeBoxUrl = '//www.' if (webPath.indexOf('tita') > -1) { iframeBoxUrl += `${webPath}/u/${loginId}/Home#${`okr/panel?to_user_id=${userId}`}`; } if (top == self) { location.href = iframeBoxUrl } else { location.hash = gotoHash } // changeQueryString('to_user_id', userId) } else { this.setState({ currentIndex: index, left: 190 - index * 80 - 75 }) this.getSubUsers(index); } } handleAddVS = (userId?: number) => (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); if (!userId) return; const { updateSelectedUsers, getSelectedUsers, showPromptPop } = this.context; const selectedUsers = getSelectedUsers(); if (selectedUsers.length == 5) { showPromptPop() return; } const node = e.currentTarget; const { showVS } = this.props; showVS && showVS() updateSelectedUsers(userId, node).then((newUsers: any) => { const { users } = this.props; this.dealUsers(users) }) } dealUsers = (users: any[]) => { const { getSelectedUsers, } = this.context; const selectedUsers = getSelectedUsers(); const newUsers: any[] = [] users.map((item: any) => { item.hasAddVS = false; if (selectedUsers.length < 5 && selectedUsers.find((i: any) => i.UserId == item.userId)) { item.hasAddVS = true; } newUsers.push(item) }) this.setState({ users: newUsers }) } getAddVSBtn = (userId: number, hasAddVS: boolean) => { if (this.props.talentVSUsers.find((i: number) => i == userId)) { if (hasAddVS) return 已添加 return +对比 } else { return '' } } handleUserMouseEnter = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); const { users } = this.state; this.dealUsers(users) } clientStartX = 0; userListLeft = 0; handleTouchStart = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); const clientX = e.changedTouches[0].clientX; this.clientStartX = clientX; this.userListLeft = this.state.left; } handleTouchMove = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); e.preventDefault(); const clientX = e.changedTouches[0].clientX; const disX = (clientX - this.clientStartX) / 2; const { currentIndex, users } = this.state; if (Math.abs(disX) > 40) { this.clientStartX = clientX; if (disX > 0) { if (!currentIndex) return this.userListLeft += 80; this.getSubUsers(currentIndex - 1); this.setState({ currentIndex: currentIndex - 1, left: this.userListLeft }) } else { if (currentIndex == users.length - 1) return; this.userListLeft -= 80; this.getSubUsers(currentIndex + 1); this.setState({ currentIndex: currentIndex + 1, left: this.userListLeft }) } } } handleTouchEnd = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); } userList: any render() { const { isFetching, canVS } = this.props; const { left, currentIndex, transition, users } = this.state; return (
{ currentIndex && users.length ?
: null } { currentIndex != users.length - 1 && users.length ?
: null } { isFetching ? null :
this.userList = node}> { users.map((item: any, index: number) => { return (
{ item.count ?
{item.count}
: null }
{item.name}
{item.position ? item.position : item.department}
{ canVS ?
{ this.getAddVSBtn(item.userId, item.hasAddVS) }
: null }
) }) }
}
) } }