Skip to content

Commit 5c40ac5

Browse files
committed
code review
1 parent 0e4e820 commit 5c40ac5

File tree

4 files changed

+36
-46
lines changed

4 files changed

+36
-46
lines changed

addon/components/x-tree-children.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Component from '@ember/component';
22
import layout from '../templates/components/x-tree-children';
3-
import { setProperties } from '@ember/object';
3+
import { get, setProperties } from '@ember/object';
44

55
export default Component.extend({
66
layout,
@@ -10,8 +10,7 @@ export default Component.extend({
1010
actions: {
1111
updateCheckbox() {
1212
if (this.recursiveCheck) {
13-
let model = this.model;
14-
let children = model.children;
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

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Component from '@ember/component';
2-
import { computed, set, 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,20 +13,18 @@ export default Component.extend({
1313
recursiveCheck: false,
1414

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

1919
click() {
20-
let select = this.onSelect;
21-
if (select) {
22-
let model = this.model;
23-
let wasChecked = model.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.isChecked;
25+
let isChecked = get(this, 'model.isChecked');
2826
if (isChecked !== wasChecked && this.recursiveCheck) {
29-
this.setChildCheckboxesRecursively(model, isChecked);
27+
this.setChildCheckboxesRecursively(this.model, isChecked);
3028
this.updateCheckbox();
3129
}
3230
}
@@ -35,23 +33,21 @@ export default Component.extend({
3533
mouseEnter() {
3634
set(this, 'model.isSelected', true);
3735

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

4441
mouseLeave() {
4542
set(this, 'model.isSelected', false);
4643

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

5349
setChildCheckboxesRecursively(node, isChecked) {
54-
let children = node.children;
50+
let children = get(node, 'children');
5551
if (children.length) {
5652
children.forEach(child => {
5753
setProperties(child, {
@@ -69,16 +65,14 @@ export default Component.extend({
6965
event.stopPropagation();
7066

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

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

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

addon/components/x-tree.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Component from '@ember/component';
22
import layout from '../templates/components/x-tree';
33
import { getDescendents, getAncestors } from '../utils/tree';
4-
import { set } from '@ember/object';
4+
import { get, set } from '@ember/object';
55

66
export default Component.extend({
77
layout,
@@ -14,25 +14,22 @@ export default Component.extend({
1414

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

1918
// Make sure chosen item is highlighted and expanded-to in the tree
20-
let chosenId = this.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 (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.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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ export function buildTree(model, options = {}) {
3131

3232
// Defaults
3333
set(node, 'children', A());
34-
set(node, 'isVisible', node.isVisible || true);
35-
set(node, 'isExpanded', node.isExpanded || false);
34+
set(node, 'isVisible', get(node, 'isVisible') || true);
35+
set(node, 'isExpanded', get(node, 'isExpanded') || false);
3636

37-
tree[node.id] = node;
37+
tree[get(node, 'id')] = node;
3838
});
3939

4040
// Connect all children to their parent
4141
model.forEach(node => {
4242
let child = tree[get(node, options.valueKey || 'id')];
43-
let parent = node.parentId;
43+
let parent = get(node, 'parentId');
4444

4545
if (isEmpty(parent)) {
4646
roots.pushObject(child);
@@ -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

@@ -74,14 +74,14 @@ export function getDescendents(tree, depth = -1) {
7474
// Returns a flat list of ancestors, including the child
7575
export function getAncestors(tree, childNode) {
7676
let ancestors = A();
77-
let childId = childNode.id;
77+
let childId = get(childNode, 'id');
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.id));
97+
return list.find(x => x.children.find(y => y.id === get(childNode, 'id')));
9898
}

0 commit comments

Comments
 (0)