forked from storybook-eol/react-treebeard
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
142 lines (124 loc) · 4.37 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* eslint-disable */
'use strict';
import React from 'react';
import ReactDOM from 'react-dom';
import { StyleRoot } from 'radium';
import {Treebeard, decorators} from '../src/index';
import data from './data';
import styles from './styles';
import * as filters from './filter';
import ContextMenu, { menuId } from './ContextMenu';
import Roller, { animations, themes } from './Roller';
const HELP_MSG = 'Select A Node To See Its Data Structure Here...';
// Example: Customising The Header Decorator To Include Icons
decorators.Header = (props) => {
const style = props.style;
const iconType = props.node.children ? 'folder' : 'file-text';
const iconClass = `fa fa-${iconType}`;
const iconStyle = { marginRight: '5px' };
return (
<div style={style.base}>
<div style={style.title}>
<i className={iconClass} style={iconStyle}/>
{props.node.name}
</div>
</div>
);
};
class NodeViewer extends React.Component {
constructor(props){
super(props);
}
render(){
const style = styles.viewer;
let json = JSON.stringify(this.props.node, null, 4);
if(!json){ json = HELP_MSG; }
return (
<div style={style.base}>
{json}
</div>
);
}
}
NodeViewer.propTypes = {
node: React.PropTypes.object
};
class DemoTree extends React.Component {
constructor(props){
super(props);
this.state = {
data,
ctxTheme: 'dark',
ctxAnimation: 'zoomIn'
};
this.onToggle = this.onToggle.bind(this);
this.handleAnimationChange = this.handleAnimationChange.bind(this);
this.handleThemeChange = this.handleThemeChange.bind(this);
}
onToggle(node, toggled){
if(this.state.cursor){this.state.cursor.active = false;}
node.active = true;
if(node.children){ node.toggled = toggled; }
this.setState({ cursor: node });
}
onFilterMouseUp(e){
const filter = e.target.value.trim();
if(!filter){ return this.setState({data}); }
var filtered = filters.filterTree(data, filter);
filtered = filters.expandFilteredNodes(filtered, filter);
this.setState({data: filtered});
}
handleThemeChange(e){
this.setState({ctxTheme: e.target.value});
}
handleAnimationChange(e){
this.setState({ctxAnimation: e.target.value});
}
render(){
return (
<StyleRoot>
<form className="form-inline" style={styles.searchBox}>
<Roller
name="Themes : "
options={themes}
onChange={this.handleThemeChange}
selectedOption={this.state.ctxTheme}
/>
<Roller
name="Animations : "
options={animations}
onChange={this.handleAnimationChange}
selectedOption={this.state.ctxAnimation}
/>
</form>
<div style={styles.searchBox}>
<div className="input-group">
<span className="input-group-addon">
<i className="fa fa-search"></i>
</span>
<input type="text"
className="form-control"
placeholder="Search the tree..."
onKeyUp={this.onFilterMouseUp.bind(this)}
/>
</div>
</div>
<div style={styles.component}>
<h2>Right click on a node</h2>
<Treebeard
data={this.state.data}
onToggle={this.onToggle}
decorators={decorators}
contextMenuId={menuId}
/>
</div>
<div style={styles.component}>
<NodeViewer node={this.state.cursor}/>
</div>
<ContextMenu animation={this.state.ctxAnimation} theme={this.state.ctxTheme}/>
</StyleRoot>
);
}
}
const content = document.getElementById('content');
ReactDOM.render(<DemoTree/>, content);