import { Connect } from "dob-react" import * as React from "react" import * as ReactDOM from "react-dom" import Icon from "../../../components/icon/src" import { TreeNode } from "../../../components/tree/src" import * as Styled from "./tree-node.style" import * as typings from "./tree-node.type" class CustomTreeNode extends React.Component { public static defaultProps = new typings.Props() public state = new typings.State() private pageInfo: IPage public render() { this.pageInfo = this.props.stores.ApplicationStore.pages.get(this.props.pageKey) if (!this.pageInfo) { return null } // 子元素 let childs: Array> = null if (this.pageInfo.type === "folder") { childs = this.pageInfo.childs.map(childKey => { return ( ) }) } const pageName = this.pageInfo.name const currentCreatedPageKey = this.props.stores.ApplicationStore.currentCreatedPageKey const currentViewportPageKey = this.props.stores.ApplicationStore.currentViewportPageKey const currentEditPageKey = this.props.stores.ApplicationStore.currentEditPageKey const childProps = { render: () => { return ( {pageName === "" ? <未命名> : pageName} {currentViewportPageKey === this.props.pageKey && 使用中 } {currentCreatedPageKey !== this.props.pageKey && } ) }, defaultExpendAll: true, toggleByArrow: true, onClick: this.handleClick } return ( {React.createElement(TreeNode, childProps, childs)} ) } private handleClick = () => { // 当前正在创建,未确定的页面不能设置为当前编辑 if (this.props.pageKey === this.props.stores.ApplicationStore.currentCreatedPageKey) { return } // 是当前使用的页面,什么都不发生 if (this.props.pageKey === this.props.stores.ApplicationStore.currentViewportPageKey) { return } if (this.pageInfo.type === "page") { // 更新前,将当前页面内容保存 const fullInformation = this.props.stores.ViewportStore.currentFullInformation this.props.stores.ApplicationStore.pageInstances.set(this.props.stores.ApplicationStore.currentViewportPageKey, fullInformation) // 重置应用 this.props.actions.ApplicationAction.resetApplication() // 自己的窗口不要关 this.props.actions.ApplicationAction.setLeftTool("page") // 设置 viewport if (this.props.stores.ApplicationStore.pageInstances.has(this.props.pageKey)) { this.props.actions.ViewportAction.resetViewport(this.props.stores.ApplicationStore.pageInstances.get(this.props.pageKey)) } else { // 清空当前视图 this.props.actions.ViewportAction.clearViewport() // 创建一个初始视图 this.props.actions.ViewportAction.initViewport() } // 设置当前 viewport 使用的页面 this.props.actions.ApplicationAction.changeCurrentViewportPageKey(this.props.pageKey) } } private handleEdit = (event: React.FormEvent) => { event.stopPropagation() this.props.actions.ApplicationAction.RemoveCreatingPage() this.props.actions.ApplicationAction.setCurrentEditPageKey(this.props.pageKey) const pageInfo = this.props.stores.ApplicationStore.pages.get(this.props.pageKey) if (pageInfo.type === "folder") { this.props.actions.ApplicationAction.setRightTool("addFolder") } else { this.props.actions.ApplicationAction.setRightTool("addPage") } } } const ConnectedTreeNode = Connect(CustomTreeNode) export default ConnectedTreeNode