Skip to content

Commit c15a0f3

Browse files
author
jasonford
committed
cleaner focusing
1 parent 6efc055 commit c15a0f3

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/BreadCrumbs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import ReactFireMixin from 'reactfire';
55
let BreadCrumbs = React.createClass({
66
render() {
77
let crumbs = [<BreadCrumb key='/' path='/' root={true} focus={this.props.focus}/>];
8-
let pathParts = this.props.path.replace(/^\/|\/$/, '').split('/');
8+
let pathParts = this.props.path.replace(/^\/+|\/+$/, '').split('/');
99
for (let i=0; i<pathParts.length-1; i+=2) {
10-
let currentPath = pathParts.slice(0, i+2).join('/');
10+
let currentPath = '/'+pathParts.slice(0, i+2).join('/');
1111
crumbs.push(<BreadCrumb key={currentPath} path={currentPath} focus={this.props.focus}/>);
1212
}
1313
return <div>{crumbs}</div>;
@@ -24,7 +24,7 @@ let BreadCrumb = React.createClass({
2424
componentDidMount: function () {
2525
let self = this;
2626
self.refs.root.addEventListener('tap', (event)=>{
27-
self.props.focus(self.props.path);
27+
self.props.focus('/' + self.props.path);
2828
});
2929
},
3030
render() {

src/TreeChart.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let TreeChart = React.createClass({
1212
componentWillMount() {
1313
this.setState({
1414
path : this.props.path,
15-
focus : false,
15+
focus : this.props.path,
1616
element : { // start with empty element designed to show loading
1717
elements : {}
1818
}
@@ -61,20 +61,14 @@ let TreeChart = React.createClass({
6161
self.addElement(0);
6262
}
6363
});
64-
this.refs.children.addEventListener('tap', function (event) {
65-
event.stopPropagation();
66-
if (event.dataPath) {
67-
self.setState({focus : event.dataPath});
68-
}
69-
});
7064
// stop propagation between layers
7165
this.refs.root.addEventListener('dropped', function (event) {
7266
event.stopPropagation();
7367
});
7468
},
7569
addElement(index) {
7670
this.firebaseRefs.element.child('elements').push({
77-
title : "",
71+
title : "New Element",
7872
index : index,
7973
importance : 1,
8074
elements : []
@@ -114,7 +108,10 @@ let TreeChart = React.createClass({
114108
firebase.database().ref(this.props.path + '/elements/' + key).remove();
115109
},
116110
focus(path) {
117-
this.setState({focus : path});
111+
if (path) {
112+
this.setState({focus : path});
113+
}
114+
return this.state.focus;
118115
},
119116
render() {
120117
let self = this;
@@ -132,7 +129,7 @@ let TreeChart = React.createClass({
132129

133130
let childElements = [];
134131

135-
let focusedChild = self.state.focus;
132+
let focusedChild = self.props.focus ? self.props.focus() : self.focus();
136133

137134
rows.forEach((row, index)=>{
138135
childElements.push(
@@ -145,11 +142,10 @@ let TreeChart = React.createClass({
145142
self.removeChild(column.key);
146143
}
147144
let path = self.props.path + '/elements/' + column.key;
148-
let visible = focusedChild === false
149-
|| focusedChild === path
150-
|| focusedChild === self.props.path; // focusing on self
145+
let visible = focusedChild === self.props.path || focusedChild.indexOf(path) === 0; // this one parent of focused child
146+
151147
let height = row.height;
152-
if (focusedChild === path) {
148+
if (focusedChild && focusedChild.indexOf(path) === 0) {
153149
height = '100%';
154150
}
155151

@@ -159,7 +155,8 @@ let TreeChart = React.createClass({
159155
key={column.key}
160156
visible={visible}
161157
height={height}
162-
focused={focusedChild === path}
158+
focus={self.props.focus || self.focus} // pass back focus
159+
focused={focusedChild && focusedChild.indexOf(path) === 0}
163160
move={self.moveChild}
164161
remove={remove}/>);
165162
});

src/TreeChartChild.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ let TreeChartChild = React.createClass({
3535
self.refs.root.style.transform = 'translate(' + event.tx + 'px,'+ event.ty + 'px)'
3636
});
3737
this.refs.root.addEventListener("tap", (event)=>{
38-
event.dataPath = self.props.path;
38+
event.stopPropagation();
39+
self.props.focus(self.props.path);
3940
});
4041
this.refs.root.addEventListener("drop", (event)=>{
4142
event.stopPropagation();
@@ -79,7 +80,7 @@ let TreeChartChild = React.createClass({
7980
let content;
8081

8182
if (this.props.focused) {
82-
content = <TreeChart path={this.props.path} isChild={true}/>
83+
content = <TreeChart path={this.props.path} isChild={true} focus={this.props.focus}/>
8384
}
8485

8586
return <div ref="root" className="TreeChartChild" style={style}>

0 commit comments

Comments
 (0)