import * as React from 'react'; import AvatarImg from '../../../../../components/common/avatarImg/avatarImg.tsx'; import PropTypes from 'prop-types'; import {getBSGlobal,getUserInfo} from '../../../../../helper'; export interface User { avatarColor: string; avatarImg: string; name: string; userId: number; position: string; count: number; } export interface SubLevelProps { users: User[], toogleCard: Function; isFetching: boolean; canVS: boolean; addVSCallback: Function; showVS: Function; talentVSUsers: number[]; drillDownUsers: number[]; RedirectToProfileUsers:any } interface SubLevelState { left: number; prev: boolean; next: boolean; users: any[]; transition: boolean; } const userAgent = window.navigator.userAgent.toLowerCase(), ios = /iphone|ipod|ipad/.test(userAgent); export default class SubLevel extends React.PureComponent{ static contextTypes = { getSelectedUsers: PropTypes.func, updateSelectedUsers: PropTypes.func, showPromptPop: PropTypes.func, handleChangeUser: PropTypes.func, }; constructor(props: SubLevelProps) { super(props); const { users } = props; const userLen = users.length; const left = (380 - userLen * 70) / 2; this.state = { left, prev: left + userLen * 70 > 380, next: left < 0, users: [], transition: true } } componentDidMount() { 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.find((i: any) => i.UserId == item.userId)) { item.hasAddVS = true; } newUsers.push(item) }) this.setState({ users: newUsers }) } 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(); e.preventDefault(); this.prev() } handleNextTouchStart = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); e.preventDefault(); this.next() } handlePrevTouchEnd = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); e.preventDefault(); } handleNextTouchEnd = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); e.preventDefault(); } prev = () => { const { left } = this.state; const { users } = this.props; const userLen = users.length; const newLeft = left + 70; this.setState({ left: newLeft, prev: newLeft < 35, //newLeft + userLen * 70 > 380, next: newLeft - 380 > - (userLen + 1) * 70, }) } next = () => { const { left } = this.state; const { users } = this.props; const userLen = users.length; const newLeft = left - 70; this.setState({ left: newLeft, prev: newLeft < 35, //newLeft + userLen * 70 > 380, next: newLeft - 380 > - (userLen + 1) * 70, }) } componentWillReceiveProps(nextProps: SubLevelProps) { const { users } = nextProps; const userLen = users.length; const left = (380 - userLen * 70) / 2; this.dealUsers(users); this.setState({ left, prev: left + userLen * 70 > 380, next: left < 0, }) } handleToogle = (userId: number) => (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); // const { toogleCard drillDownUsers} = this.props; // toogleCard('bottom', userId) const { toogleCard, drillDownUsers } = this.props; const ids = this.props.RedirectToProfileUsers.find((d:number)=> { return d==userId}); if (!!ids) { if(this.context.handleChangeUser){ this.context.handleChangeUser(userId) return; } const webPath = getBSGlobal('webPath'); const loginId = getUserInfo()['Id']; const host = getBSGlobal('titaHost'); const gotoHash = `#/view?layout=honour&to_user_id=${userId}`; const gotoHref = `${host}/${loginId}/PageBuilderForEdit${gotoHash}`; let iframeBoxUrl = '//www.' if (webPath.indexOf('tita') > -1) { // iframeBoxUrl += `${webPath}/u/${loginId}/Home#${encodeURIComponent(`widget/italent?iTalentFrameType=iframe&iTalentNavId=&iTalentFrame=${encodeURIComponent(gotoHref)}`)}`; iframeBoxUrl += `${webPath}/u/${loginId}/Home#${`okr/panel?to_user_id=${userId}`}`; } if (webPath.indexOf('italent') > -1) { iframeBoxUrl += `${webPath}/${loginId}/ITalentHome#${encodeURIComponent(`widget/italent?iTalentFrameType=iframe&iTalentNavId=&iTalentFrame=${encodeURIComponent(gotoHref)}`)}`; } if (top == self) { location.href = iframeBoxUrl } else { location.hash = gotoHash } return // changeQueryString('to_user_id', userId) } // 判断要查看的人员id是否在允许查看起组织架构的列表中 if (drillDownUsers.find(item => item == userId)) toogleCard('bottom', userId) } 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) }) } 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(); // e.preventDefault(); const clientX = e.changedTouches[0].clientX; this.clientStartX = clientX; this.userListLeft = this.state.left; this.setState({ transition: false }) } handleTouchMove = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); e.preventDefault(); const { users } = this.state if (users.length <= 5) return; const clientX = e.changedTouches[0].clientX; const disX = clientX - this.clientStartX; const userListLeft = Number(this.userList.style.left.replace(/px/, '')); this.clientStartX = clientX; this.userListLeft = userListLeft + disX; this.userList.style.left = `${userListLeft + disX}px`; } handleTouchEnd = (e: React.TouchEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); // e.preventDefault(); const { users } = this.state; const center = -(users.length * 70 - 380) / 2; const a = (this.userListLeft - center) % 70; if (Math.abs(a) >= 35) { if (this.userListLeft < -115) { this.userListLeft -= (70 - Math.abs(a)) } else { this.userListLeft += (70 - Math.abs(a)) } } if (Math.abs(a) < 35) { if (this.userListLeft < -115) { this.userListLeft += (Math.abs(a)) } else { this.userListLeft -= (Math.abs(a)) } } this.setState({ transition: true, left: this.userListLeft }) this.userList.style.left = `${this.userListLeft}px`; } userList: any render() { const { isFetching, canVS } = this.props; const { left, prev, next, transition, users } = this.state; return (
{ users.length ?
: null } { prev && users.length &&
} { next && users.length &&
} { isFetching ? null :
this.userList = node} onTouchStart={this.handleTouchStart} onTouchMove={this.handleTouchMove} onTouchEnd={this.handleTouchEnd} > { users.map((item: any, index: number) => { return (
{ item.count ?
{item.count}
: null }
{item.name}
{ canVS ?
{ this.getAddVSBtn(item.userId, item.hasAddVS) }
: null }
) }) }
}
) } }