import * as React from 'react'; // import * as ReactDOM from 'react-dom'; import './scss/index.scss'; import CountTag from '&/components/common/Tag/CountTag'; import NormalTag from '&/components/common/Tag/NormalTag'; import { CountTagProps } from '&/components/common/Tag/interface'; import { TagCloudProps, TagSize, TagPos } from './interface'; type TagCloudPropsWhithDeafult = TagCloudProps & { height: number; width: number; afterCountOperation: (id: number, praise: boolean) => Promise; addTag?: Function, afterTitleClick: (id: number) => void } interface TagCloudState { tagData: CountTagProps[]; getSizeTagData: CountTagProps[]; pageIndex: number; } type TagWithSize = CountTagProps & TagSize; type TagWithSizePos = CountTagProps & TagSize & TagPos; export default class TagCloud extends React.Component { static defaultProps = { width: 500, height: 500 } tagCounter: number = 0 tagsWidthSize: (CountTagProps & TagSize)[] = [] tagWrapWidth = 400 tagWrapHeight = 400 getSizeTagData: CountTagProps[] = [] constructor(props: TagCloudProps) { super(props); this.getSizeTagData = this.sortTagDataBySize(props.tagData); this.state = { tagData: [], getSizeTagData: this.getSizeTagData, pageIndex: 0 } } componentDidMount() { let { addTag } = this.props as TagCloudPropsWhithDeafult; addTag && addTag(this.addTag) } componentWillReceiveProps(nextProps: TagCloudProps) { } addTag = (tags: CountTagProps[]) => { this.tagCounter = 0; this.tagsWidthSize = []; tags.forEach(t => { t.afterCountOperation = this.afterCountOperation; t.afterTitleClick = this.afterTitleClick; }) this.getSizeTagData = tags; this.setState({ getSizeTagData: tags }) } afterCountOperation = (id: number, praise: boolean) => { let { afterCountOperation } = this.props as TagCloudPropsWhithDeafult; afterCountOperation && afterCountOperation(id, praise).then((response => { if (response === true) { let { pageIndex } = this.state, newThispageTags: TagWithSize[] = []; this.pagingTags[pageIndex].forEach((t: TagWithSize) => { if (t.id == id) { t.praise = !praise; if (t.count !== undefined) t.count = t.count + (praise ? 1 : -1) } if (t.count) newThispageTags.push(t) }); if (newThispageTags.length) { this.pagingTags[pageIndex] = newThispageTags; this.setState({ tagData: newThispageTags }) } else { this.pagingTags.splice(pageIndex, 1) this.setState({ tagData: this.pagingTags[0] || [], pageIndex: 0 }) } } })) } // 标签排序 sortTagDataBySize = (tagData: CountTagProps[]) => { let groups = {}, maxLen = 0, newTagData: CountTagProps[] = []; // 将标签按照size分组 for (var i = 0, len = tagData.length; i < len; i++) { let sizeName = tagData[i]['size'] || 'small'; tagData[i].afterCountOperation = this.afterCountOperation tagData[i].afterTitleClick = this.afterTitleClick if (!groups[sizeName]) groups[sizeName] = []; groups[sizeName].push(tagData[i]) } // 获取最大组的长度 for (var sizeName in groups) { let group = groups[sizeName], groupLen = group.length; if (groupLen > maxLen) maxLen = groupLen; } // 依次从没组中取数据组成新数据 for (var i = 0; i < maxLen; i++) { for (var sizeName in groups) { let group = groups[sizeName], tag = group[i]; if (tag) newTagData.push(tag); } } return newTagData; } afterTitleClick = (id: number) => { let { afterTitleClick } = this.props as TagCloudPropsWhithDeafult; afterTitleClick && afterTitleClick(id) } testArea = (tags: TagWithSize[], tag: TagWithSize) => { let { width, height } = this.props as TagCloudPropsWhithDeafult, tagsArea = 0; tags.map(t => { tagsArea += (t.width * t.height) }) return (tagsArea + tag.width * tag.height) / (width * height) * 100 > 50; } // 获取标签的尺寸 tagSize: number = 0 getTagSize = (tagProps: TagWithSize) => { let pagingTagsLen = this.pagingTags.length; if (pagingTagsLen) { if (!this.getSizeTagData.length) return; let lastPageTags = this.pagingTags[pagingTagsLen - 1]; if (this.testArea(lastPageTags, tagProps)) { this.pagingTags.push([tagProps]) } else { this.pagingTags[pagingTagsLen - 1].push(tagProps); } this.tagCounter += 1; if (this.tagCounter == this.getSizeTagData.length) { this.getSizeTagData = [] this.tagCounter = 0; this.sortTagsBySize(this.pagingTags[pagingTagsLen - 1]) } } else { this.tagsWidthSize.push(tagProps) this.tagCounter += 1; if (this.tagCounter == this.getSizeTagData.length) { this.getSizeTagData = [] this.tagCounter = 0; this.tagPaging(); } } } // 标签分页 pagingTags: TagWithSize[][] = [] tagPaging = () => { let tags: TagWithSize[] = [], { width, height } = this.props as TagCloudPropsWhithDeafult, tagsArea = 0, pageIndex = 0; this.pagingTags = []; this.tagsWidthSize.map((item: TagWithSize, index: number) => { tagsArea += (item.width * item.height); if ((tagsArea / (width * height) * 100) > 50) { tags = []; tags.push(item); pageIndex += 1; tagsArea = item.width * item.height; } else { tags.push(item) } this.pagingTags[pageIndex] = tags; }) this.tagsWidthSize = []; this.sortTagsBySize(this.pagingTags[this.state.pageIndex]) } // 标签排序 sortTagsBySize = (tags: TagWithSize[]) => { tags.sort((a: TagWithSize, b: TagWithSize) => { if (a.height < b.height) { return 1; } else if (a.height > b.height) { return -1; } else { return 0; } }) this.setTagPosition(tags) } // 计算位置 setTagPosition = (tags: TagWithSize[]) => { let step = 2, alreadyPlacedTags: (TagWithSizePos)[] = [], { width, height } = this.props as TagCloudPropsWhithDeafult, wrapRatio = width / height; tags.map((item: TagWithSizePos, index) => { let angle = 6.28 * Math.random(), radius = 0.0, tagWidth = item.width, tagHeight = item.height; item.left = (width - tagWidth) / 2, item.top = (height - tagHeight) / 2; //Helper函数来测试如果一个元素重叠 var hitTest = function (elem: TagWithSizePos, other_elems: (TagWithSizePos)[]) { // 两两重叠检测 var overlapping = function (a: TagWithSizePos, b: TagWithSizePos) { if (Math.abs(2.0 * a.left + a.width - 2.0 * b.left - b.width) < a.width + b.width + 2) { if (Math.abs(2.0 * a.top + a.height - 2.0 * b.top - b.height) < a.height + b.height + 4) { return true; } } return false; }; var i = 0; // 检查元素重叠一个接一个,停止并返回false一旦发现重叠 for (var i = 0, len = other_elems.length; i < len; i++) { if (overlapping(elem, other_elems[i])) { return true; } } return false; }; while (hitTest(item, alreadyPlacedTags)) { // 默认设置:椭圆螺旋形状 radius += step; angle += (index % 2 === 0 ? 1 : -1) * step; let maxLeft = width - item.width - 1, maxTop = height - item.top; item.left = width / 2 - (tagWidth / 2.0) + (radius * Math.cos(angle)) * wrapRatio; item.top = height / 2 + radius * Math.sin(angle) - (tagHeight / 2.0); // while(posTest()) if (item.left < 0) item.left = 0; if (item.top < 0) item.top = 0; if (item.left > maxLeft) item.left = maxLeft; if (item.top > maxTop) item.top = maxTop; } alreadyPlacedTags.push(item); }) this.setState({ tagData: alreadyPlacedTags }) } pageTurning = (type: string) => (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); let { pageIndex } = this.state; if (type == 'prev') { pageIndex -= 1; } else { pageIndex += 1 } this.setState({ pageIndex }) this.sortTagsBySize(this.pagingTags[pageIndex]) } afterClose = (id: number) => { const { onClose } = this.props if (onClose) { onClose(id).then(response => { if (response) { let { pageIndex } = this.state, newThispageTags: TagWithSize[] = []; this.pagingTags[pageIndex].forEach((t: TagWithSize) => { if (t.id != id) { newThispageTags.push(t) } }); if (newThispageTags.length) { this.pagingTags[pageIndex] = newThispageTags; this.setState({ tagData: newThispageTags }) } else { this.pagingTags.splice(pageIndex, 1) this.setState({ tagData: this.pagingTags[0] || [], pageIndex: 0 }) } } }) } } render() { let { tagData, pageIndex, getSizeTagData } = this.state, { width, height, type, close } = this.props as TagCloudPropsWhithDeafult, // type 1是other 2是self { pagingTags } = this, pageLen = pagingTags.length, showPrev = pageLen > 1 && pageIndex > 0 ? true : false, showNext = pageLen > 1 && pageIndex < pageLen - 1 ? true : false; const nodataCom =
{type === 1 ? '快来做第一个给他贴标签的人' : '还没有人给你添加标签'}
return (
{showPrev && } {showNext && }
{getSizeTagData.map((item: TagWithSizePos, index) => { return (
{ close ? : }
) }) }
{tagData.length !== 0 ? tagData && tagData.map((item: TagWithSizePos, index) => { return { close ? : } }) : nodataCom }
) } }