mirror of
https://github.com/thomasnordquist/MQTT-Explorer.git
synced 2026-09-11 17:13:53 +00:00
253 lines
7.0 KiB
TypeScript
253 lines
7.0 KiB
TypeScript
import * as React from 'react'
|
||
import * as q from '../../../../backend/src/Model'
|
||
|
||
import { Theme, withStyles } from '@material-ui/core/styles'
|
||
|
||
import TreeNodeSubnodes from './TreeNodeSubnodes'
|
||
import TreeNodeTitle from './TreeNodeTitle'
|
||
import { TopicOrder } from '../../reducers/Settings'
|
||
import { TopicViewModel } from '../../TopicViewModel'
|
||
const debounce = require('lodash.debounce')
|
||
|
||
declare var performance: any
|
||
|
||
const styles = (theme: Theme) => {
|
||
return {
|
||
collapsedSubnodes: {
|
||
color: theme.palette.text.secondary,
|
||
},
|
||
displayBlock: {
|
||
display: 'block',
|
||
},
|
||
node: {
|
||
display: 'block',
|
||
marginLeft: '10px',
|
||
},
|
||
topicSelect: {
|
||
float: 'right' as 'right',
|
||
opacity: 0,
|
||
cursor: 'pointer',
|
||
marginTop: '-1px',
|
||
},
|
||
selected: {
|
||
backgroundColor: 'rgba(200, 200, 200, 0.55)',
|
||
},
|
||
hover: {
|
||
backgroundColor: 'rgba(100, 100, 100, 0.55)',
|
||
},
|
||
}
|
||
}
|
||
|
||
interface Props {
|
||
animateChages: boolean
|
||
isRoot?: boolean
|
||
treeNode: q.TreeNode<TopicViewModel>
|
||
name?: string | undefined
|
||
collapsed?: boolean | undefined
|
||
performanceCallback?: ((ms: number) => void) | undefined
|
||
classes: any
|
||
className?: string
|
||
topicOrder: TopicOrder
|
||
autoExpandLimit: number
|
||
lastUpdate: number
|
||
didSelectTopic: any
|
||
}
|
||
|
||
interface State {
|
||
collapsedOverride: boolean | undefined
|
||
mouseOver: boolean
|
||
selected: boolean
|
||
}
|
||
|
||
class TreeNode extends React.Component<Props, State> {
|
||
private dirtySubnodes: boolean = true
|
||
private dirtyEdges: boolean = true
|
||
private dirtyMessage: boolean = true
|
||
private animationDirty: boolean = false
|
||
private lastRenderTime = 0
|
||
|
||
private cssAnimationWasSetAt?: number
|
||
|
||
private willUpdateTime: number = performance.now()
|
||
private titleRef?: React.RefObject<HTMLDivElement> = React.createRef<HTMLDivElement>()
|
||
private nodeRef?: React.RefObject<HTMLDivElement> = React.createRef<HTMLDivElement>()
|
||
|
||
private subnodesDidchange = () => {
|
||
this.dirtySubnodes = true
|
||
}
|
||
|
||
private messageDidChange = () => {
|
||
this.dirtyMessage = true
|
||
}
|
||
|
||
private edgesDidChange = () => {
|
||
this.dirtyMessage = true
|
||
}
|
||
|
||
constructor(props: Props) {
|
||
super(props)
|
||
|
||
this.state = {
|
||
collapsedOverride: props.collapsed,
|
||
mouseOver: false,
|
||
selected: false,
|
||
}
|
||
}
|
||
|
||
public componentDidMount() {
|
||
const { treeNode } = this.props
|
||
this.addSubscriber(treeNode)
|
||
}
|
||
|
||
private addSubscriber(treeNode: q.TreeNode<TopicViewModel>) {
|
||
treeNode.viewModel = new TopicViewModel()
|
||
treeNode.viewModel.change.subscribe(this.viewStateHasChanged)
|
||
treeNode.onMerge.subscribe(this.subnodesDidchange)
|
||
treeNode.onEdgesChange.subscribe(this.edgesDidChange)
|
||
treeNode.onMessage.subscribe(this.messageDidChange)
|
||
}
|
||
|
||
private viewStateHasChanged = (msg: void, viewModel: TopicViewModel) => {
|
||
this.setState({ selected: viewModel.isSelected() })
|
||
}
|
||
|
||
private removeSubscriber(treeNode: q.TreeNode<TopicViewModel>) {
|
||
treeNode.viewModel && treeNode.viewModel.change.unsubscribe(this.viewStateHasChanged)
|
||
treeNode.viewModel = undefined
|
||
treeNode.onMerge.unsubscribe(this.subnodesDidchange)
|
||
treeNode.onEdgesChange.unsubscribe(this.edgesDidChange)
|
||
treeNode.onMessage.unsubscribe(this.messageDidChange)
|
||
}
|
||
|
||
public componentWillReceiveProps(nextProps: Props) {
|
||
if (nextProps.treeNode !== this.props.treeNode) {
|
||
this.removeSubscriber(this.props.treeNode)
|
||
this.addSubscriber(nextProps.treeNode)
|
||
}
|
||
}
|
||
|
||
public componentWillUnmount() {
|
||
const { treeNode } = this.props
|
||
this.removeSubscriber(treeNode)
|
||
this.titleRef = undefined
|
||
this.nodeRef = undefined
|
||
}
|
||
|
||
private stateHasChanged(newState: State) {
|
||
return this.state.collapsedOverride !== newState.collapsedOverride
|
||
|| this.state.mouseOver !== newState.mouseOver
|
||
|| this.state.selected !== newState.selected
|
||
}
|
||
|
||
private propsHasChanged(newProps: Props) {
|
||
return this.props.autoExpandLimit !== newProps.autoExpandLimit
|
||
}
|
||
|
||
public shouldComponentUpdate(nextProps: Props, nextState: State) {
|
||
const shouldRenderToRemoveCssAnimation = this.cssAnimationWasSetAt !== undefined
|
||
return this.stateHasChanged(nextState)
|
||
|| this.propsHasChanged(nextProps)
|
||
|| (this.dirtyEdges || this.dirtyMessage || this.dirtySubnodes)
|
||
|| this.animationDirty
|
||
|| shouldRenderToRemoveCssAnimation
|
||
}
|
||
|
||
public componentDidUpdate() {
|
||
if (this.props.performanceCallback) {
|
||
const renderTime = performance.now() - this.willUpdateTime
|
||
this.props.performanceCallback(renderTime)
|
||
}
|
||
}
|
||
|
||
public componentWillUpdate() {
|
||
if (this.props.performanceCallback) {
|
||
this.willUpdateTime = performance.now()
|
||
}
|
||
}
|
||
|
||
private toggle() {
|
||
this.setState({ collapsedOverride: !this.collapsed() })
|
||
}
|
||
|
||
private collapsed() {
|
||
if (this.state.collapsedOverride !== undefined) {
|
||
return this.state.collapsedOverride
|
||
}
|
||
|
||
return this.props.treeNode.edgeCount() > this.props.autoExpandLimit
|
||
}
|
||
|
||
public render() {
|
||
const { classes } = this.props
|
||
const isDirty = this.dirtyEdges || this.dirtyMessage || this.dirtySubnodes
|
||
this.dirtyEdges = this.dirtyMessage = this.dirtySubnodes = false
|
||
|
||
const shouldStartAnimation = (isDirty && !this.animationDirty) && !this.props.isRoot
|
||
const animation = shouldStartAnimation ? { willChange: 'auto', translateZ: 0, animation: 'example 0.5s' } : {}
|
||
this.animationDirty = shouldStartAnimation
|
||
|
||
const highlightClass = this.state.selected ? this.props.classes.selected : (this.state.mouseOver ? this.props.classes.hover : '')
|
||
|
||
return (
|
||
<div
|
||
key={this.props.treeNode.hash()}
|
||
className={`${classes.node} ${this.props.className}`}
|
||
onClick={this.didClickNode}
|
||
onMouseOver={this.mouseOver}
|
||
onMouseOut={this.mouseOut}
|
||
ref={this.nodeRef}
|
||
>
|
||
<TreeNodeTitle
|
||
style={animation}
|
||
collapsed={this.collapsed()}
|
||
treeNode={this.props.treeNode}
|
||
name={this.props.name}
|
||
didSelectNode={this.didSelectTopic}
|
||
className={highlightClass}
|
||
/>
|
||
{this.renderNodes()}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
private didSelectTopic = () => {
|
||
this.props.didSelectTopic(this.props.treeNode)
|
||
}
|
||
|
||
private mouseOver = (event: React.MouseEvent) => {
|
||
event.stopPropagation()
|
||
this.setHover(true)
|
||
}
|
||
|
||
private mouseOut = (event: React.MouseEvent) => {
|
||
event.stopPropagation()
|
||
this.setHover(false)
|
||
}
|
||
|
||
private setHover = debounce((hover: boolean) => {
|
||
this.setState({ mouseOver: hover })
|
||
}, 45)
|
||
|
||
private didClickNode = (event: React.MouseEvent) => {
|
||
event.stopPropagation()
|
||
this.toggle()
|
||
this.didSelectTopic()
|
||
}
|
||
|
||
private renderNodes() {
|
||
return (
|
||
<TreeNodeSubnodes
|
||
animateChanges={this.props.animateChages}
|
||
collapsed={this.collapsed()}
|
||
treeNode={this.props.treeNode}
|
||
autoExpandLimit={this.props.autoExpandLimit}
|
||
topicOrder={this.props.topicOrder}
|
||
lastUpdate={this.props.treeNode.lastUpdate}
|
||
didSelectTopic={this.props.didSelectTopic}
|
||
/>
|
||
)
|
||
}
|
||
}
|
||
|
||
export default withStyles(styles)(TreeNode)
|