Skip to content

Commit 1959a45

Browse files
authored
Merge pull request #20 from Aarondorn2/master
Support plain javascript objects
2 parents e9a14e5 + 5c40ac5 commit 1959a45

File tree

4 files changed

+33
-43
lines changed

4 files changed

+33
-43
lines changed

addon/components/x-tree-children.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ export default Component.extend({
99

1010
actions: {
1111
updateCheckbox() {
12-
if (this.get('recursiveCheck')) {
13-
let model = this.get('model');
14-
let children = get(model, 'children');
12+
if (this.recursiveCheck) {
13+
let children = get(this, 'model.children');
1514

1615
if (children.length) {
1716
let isChecked = false;
@@ -23,7 +22,7 @@ export default Component.extend({
2322
isIndeterminate = true;
2423
}
2524

26-
setProperties(model, { isChecked, isIndeterminate });
25+
setProperties(this.model, { isChecked, isIndeterminate });
2726
}
2827

2928
if (this.updateCheckbox) {

addon/components/x-tree-node.js

+18-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Component from '@ember/component';
2-
import { computed, get, setProperties } from '@ember/object';
2+
import { computed, get, set, setProperties } from '@ember/object';
33
import layout from '../templates/components/x-tree-node';
44

55
export default Component.extend({
@@ -13,40 +13,36 @@ export default Component.extend({
1313
recursiveCheck: false,
1414

1515
isChosen: computed('model.id', 'chosenId', function() {
16-
return this.get('model.id') === this.get('chosenId');
16+
return get(this, 'model.id') === this.chosenId;
1717
}),
1818

1919
click() {
20-
let select = this.get('onSelect');
21-
if (select) {
22-
let model = this.get('model');
23-
let wasChecked = model.get('isChecked');
20+
if (this.onSelect) {
21+
let wasChecked = get(this, 'model.isChecked');
2422

25-
select(model);
23+
this.onSelect(this.model);
2624

27-
let isChecked = model.get('isChecked');
28-
if (isChecked !== wasChecked && this.get('recursiveCheck')) {
29-
this.setChildCheckboxesRecursively(model, isChecked);
25+
let isChecked = get(this, 'model.isChecked');
26+
if (isChecked !== wasChecked && this.recursiveCheck) {
27+
this.setChildCheckboxesRecursively(this.model, isChecked);
3028
this.updateCheckbox();
3129
}
3230
}
3331
},
3432

3533
mouseEnter() {
36-
this.set('model.isSelected', true);
34+
set(this, 'model.isSelected', true);
3735

38-
let hover = this.get('onHover');
39-
if (hover) {
40-
hover(this.get('model'));
36+
if (this.onHover) {
37+
this.onHover(this.model);
4138
}
4239
},
4340

4441
mouseLeave() {
45-
this.set('model.isSelected', false);
42+
set(this, 'model.isSelected', false);
4643

47-
let hoverOut = this.get('onHoverOut');
48-
if (hoverOut) {
49-
hoverOut(this.get('model'));
44+
if (this.onHoverOut) {
45+
this.onHoverOut(this.model);
5046
}
5147
},
5248

@@ -69,16 +65,14 @@ export default Component.extend({
6965
event.stopPropagation();
7066

7167
let isChecked = this.toggleProperty('model.isChecked');
72-
let model = this.get('model');
7368

74-
if (this.get('recursiveCheck')) {
75-
this.setChildCheckboxesRecursively(model, isChecked);
69+
if (this.recursiveCheck) {
70+
this.setChildCheckboxesRecursively(this.model, isChecked);
7671
this.updateCheckbox();
7772
}
7873

79-
let check = this.get('onCheck');
80-
if (check) {
81-
check(model);
74+
if (this.onCheck) {
75+
this.onCheck(this.model);
8276
}
8377
},
8478

addon/components/x-tree.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,22 @@ export default Component.extend({
1414

1515
init() {
1616
this._super(...arguments);
17-
let tree = this.get('model');
1817

1918
// Make sure chosen item is highlighted and expanded-to in the tree
20-
let chosenId = this.get('chosenId');
21-
if (chosenId) {
22-
let chosen = getDescendents(tree).findBy('id', chosenId);
19+
if (this.chosenId) {
20+
let chosen = getDescendents(this.model).findBy('id', this.chosenId);
2321
if (chosen) {
24-
getAncestors(tree, chosen).forEach(x => {
25-
if (get(x, 'id') !== chosenId) {
22+
getAncestors(this.model, chosen).forEach(x => {
23+
if (get(x, 'id') !== this.chosenId) {
2624
set(x, 'isExpanded', true);
2725
}
2826
});
2927
}
3028
}
3129

3230
// Expand to given depth
33-
let expandDepth = this.get('expandDepth');
34-
if (expandDepth) {
35-
getDescendents(tree, expandDepth).setEach('isExpanded', true);
31+
if (this.expandDepth) {
32+
getDescendents(this.model, this.expandDepth).setEach('isExpanded', true);
3633
}
3734
}
3835
});

addon/utils/tree.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ export function getDescendents(tree, depth = -1) {
5959
if (depth < 0) { // Unlimited depth
6060
tree.forEach(node => {
6161
descendents.pushObject(node);
62-
descendents.pushObjects(getDescendents(node.children));
62+
descendents.pushObjects(getDescendents(get(node, 'children')));
6363
});
6464
} else if (depth > 0) {
6565
tree.forEach(node => {
6666
descendents.pushObject(node);
67-
descendents.pushObjects(getDescendents(node.children, depth - 1));
67+
descendents.pushObjects(getDescendents(get(node, 'children'), depth - 1));
6868
});
6969
}
7070

@@ -78,10 +78,10 @@ export function getAncestors(tree, childNode) {
7878

7979
tree.forEach(node => {
8080
if (!ancestors.isAny('id', childId)) {
81-
if (node.id === childId) {
81+
if (get(node, 'id') === childId) {
8282
ancestors.pushObject(node);
83-
} else if (node.children.length > 0) {
84-
ancestors.pushObjects(getAncestors(node.children, childNode));
83+
} else if (get(node, 'children.length') > 0) {
84+
ancestors.pushObjects(getAncestors(get(node, 'children'), childNode));
8585
if (ancestors.length > 0) {
8686
ancestors.pushObject(node);
8787
}
@@ -94,5 +94,5 @@ export function getAncestors(tree, childNode) {
9494

9595
// Returns the parent of a child
9696
export function getParent(list, childNode) {
97-
return list.find(x => x.children.find(y => y.id === childNode.get('id')));
97+
return list.find(x => x.children.find(y => y.id === get(childNode, 'id')));
9898
}

0 commit comments

Comments
 (0)