Skip to content

Commit be95689

Browse files
committed
Initial implementation
1 parent 4f9d5d8 commit be95689

File tree

15 files changed

+272
-23
lines changed

15 files changed

+272
-23
lines changed

README.md

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,84 @@
1-
# Ember-simple-tree
1+
# ember-simple-tree
22

3-
This README outlines the details of collaborating on this Ember addon.
3+
Simple tree component for Ember without any dependency.
44

55
## Installation
66

7-
* `git clone <repository-url>` this repository
8-
* `cd ember-simple-tree`
9-
* `npm install`
10-
* `bower install`
7+
```bash
8+
ember install ember-simple-tree
9+
```
1110

12-
## Running
1311

14-
* `ember serve`
15-
* Visit your app at [http://localhost:4200](http://localhost:4200).
12+
## Usage
1613

17-
## Running Tests
14+
Basic example:
1815

19-
* `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions)
20-
* `ember test`
21-
* `ember test --server`
16+
```handlebars
17+
{{x-tree model=tree}}
18+
```
2219

23-
## Building
2420

25-
* `ember build`
21+
### Available actions
2622

27-
For more information on using ember-cli, visit [http://ember-cli.com/](http://ember-cli.com/).
23+
#### hover
24+
25+
Returns: `node`
26+
27+
Fired when a node is hovered.
28+
29+
30+
#### select
31+
32+
Returns: `node`
33+
34+
Fired when a node is selected.
35+
36+
37+
38+
### Model structure
39+
The component uses recursion to display the tree.
40+
The model requires specific properties to properly function:
41+
- `id` - unique identifier
42+
- `name` - `string` used to display a node
43+
- `children` - `array` of other nodes
44+
- `isChecked` - `boolean` used for a checkbox state
45+
- `isExpanded` - `boolean` used to expand a node (children)
46+
- `isSelected` - `boolean` optionally used for hover state
47+
- `isVisible` - `boolean` used to display or hide a node
48+
49+
```js
50+
{
51+
id: 0,
52+
name: 'Root',
53+
isExpanded: true,
54+
isSelected: false,
55+
isVisible: true,
56+
children: [
57+
{
58+
id: 1,
59+
name: 'First Child',
60+
isExpanded: true,
61+
isSelected: false,
62+
isVisible: true,
63+
children: []
64+
},
65+
{
66+
id: 2,
67+
name: 'Second Child',
68+
isExpanded: true,
69+
isSelected: false,
70+
isVisible: true,
71+
children: [
72+
{
73+
id: 3,
74+
name: 'First Grand Child',
75+
isExpanded: true,
76+
isSelected: true,
77+
isVisible: true,
78+
children: []
79+
}
80+
]
81+
}
82+
]
83+
}
84+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Ember from 'ember';
2+
import layout from '../templates/components/x-tree-children';
3+
4+
const {
5+
Component
6+
} = Ember;
7+
8+
export default Component.extend({
9+
layout,
10+
tagName: 'li',
11+
classNames: ['tree-node']
12+
});

addon/components/x-tree-node.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Ember from 'ember';
2+
import layout from '../templates/components/x-tree-node';
3+
4+
const {
5+
Component
6+
} = Ember;
7+
8+
export default Component.extend({
9+
layout,
10+
classNameBindings: ['model.isSelected:tree-highlight'],
11+
12+
mouseEnter(event) {
13+
let { hover } = this.attrs;
14+
if (hover) {
15+
hover(this.get('model'));
16+
}
17+
},
18+
19+
actions: {
20+
toggleCheck() {
21+
this.toggleProperty('model.isChecked');
22+
},
23+
24+
toggleExpand() {
25+
this.toggleProperty('model.isExpanded');
26+
}
27+
}
28+
});

addon/components/x-tree.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Ember from 'ember';
2+
import layout from '../templates/components/x-tree';
3+
4+
const {
5+
Component
6+
} = Ember;
7+
8+
export default Component.extend({
9+
layout,
10+
tagName: 'ul',
11+
classNames: ['tree-branch']
12+
});

addon/styles/x-tree.css

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.tree-branch {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
.tree-node .tree-branch {
7+
padding-left: 1.25em;
8+
}
9+
10+
.tree-node {
11+
line-height: 1.5em;
12+
list-style: none;
13+
}
14+
15+
.tree-node span {
16+
cursor: pointer;
17+
}
18+
19+
.tree-node .toggle-icon {
20+
color: #a5d4fd;
21+
cursor: pointer;
22+
display: inline-block;
23+
font-size: 10px;
24+
margin-right: 0;
25+
width: 1.25em;
26+
}
27+
28+
.tree-node .toggle-icon:hover {
29+
opacity: 0.8;
30+
}
31+
32+
.tree-node .tree-highlight {
33+
background-color: #f2f9fc;
34+
color: #333;
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{x-tree-node model=model select=select hover=hover}}
2+
3+
{{#if model.isExpanded}}
4+
{{x-tree model=model.children select=select hover=hover}}
5+
{{/if}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{#if model.checkable}}
2+
{{x-check value=model.id checked=model.isChecked action=(action 'toggleCheck')}}
3+
{{/if}}
4+
<span class="toggle-icon" {{action 'toggleExpand'}}>
5+
{{#if model.children.length}}
6+
{{#if model.isExpanded}}
7+
&#x25BC;
8+
{{else}}
9+
&#x25B6;
10+
{{/if}}
11+
{{else}}
12+
&nbsp;&nbsp;
13+
{{/if}}
14+
</span>
15+
<span {{action select model}}>
16+
{{model.name}}
17+
</span>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{#each model as |child|}}
2+
{{#if child.isVisible}}
3+
{{x-tree-children model=child select=select hover=hover}}
4+
{{/if}}
5+
{{/each}}

app/components/x-tree-children.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'ember-simple-tree/components/x-tree-children';

app/components/x-tree-node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'ember-simple-tree/components/x-tree-node';

0 commit comments

Comments
 (0)