Skip to content

Commit

Permalink
Merge pull request #20 from Aarondorn2/master
Browse files Browse the repository at this point in the history
Support plain javascript objects
  • Loading branch information
btecu authored Apr 30, 2019
2 parents e9a14e5 + 5c40ac5 commit 1959a45
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
7 changes: 3 additions & 4 deletions addon/components/x-tree-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ export default Component.extend({

actions: {
updateCheckbox() {
if (this.get('recursiveCheck')) {
let model = this.get('model');
let children = get(model, 'children');
if (this.recursiveCheck) {
let children = get(this, 'model.children');

if (children.length) {
let isChecked = false;
Expand All @@ -23,7 +22,7 @@ export default Component.extend({
isIndeterminate = true;
}

setProperties(model, { isChecked, isIndeterminate });
setProperties(this.model, { isChecked, isIndeterminate });
}

if (this.updateCheckbox) {
Expand Down
42 changes: 18 additions & 24 deletions addon/components/x-tree-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Component from '@ember/component';
import { computed, get, setProperties } from '@ember/object';
import { computed, get, set, setProperties } from '@ember/object';
import layout from '../templates/components/x-tree-node';

export default Component.extend({
Expand All @@ -13,40 +13,36 @@ export default Component.extend({
recursiveCheck: false,

isChosen: computed('model.id', 'chosenId', function() {
return this.get('model.id') === this.get('chosenId');
return get(this, 'model.id') === this.chosenId;
}),

click() {
let select = this.get('onSelect');
if (select) {
let model = this.get('model');
let wasChecked = model.get('isChecked');
if (this.onSelect) {
let wasChecked = get(this, 'model.isChecked');

select(model);
this.onSelect(this.model);

let isChecked = model.get('isChecked');
if (isChecked !== wasChecked && this.get('recursiveCheck')) {
this.setChildCheckboxesRecursively(model, isChecked);
let isChecked = get(this, 'model.isChecked');
if (isChecked !== wasChecked && this.recursiveCheck) {
this.setChildCheckboxesRecursively(this.model, isChecked);
this.updateCheckbox();
}
}
},

mouseEnter() {
this.set('model.isSelected', true);
set(this, 'model.isSelected', true);

let hover = this.get('onHover');
if (hover) {
hover(this.get('model'));
if (this.onHover) {
this.onHover(this.model);
}
},

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

let hoverOut = this.get('onHoverOut');
if (hoverOut) {
hoverOut(this.get('model'));
if (this.onHoverOut) {
this.onHoverOut(this.model);
}
},

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

let isChecked = this.toggleProperty('model.isChecked');
let model = this.get('model');

if (this.get('recursiveCheck')) {
this.setChildCheckboxesRecursively(model, isChecked);
if (this.recursiveCheck) {
this.setChildCheckboxesRecursively(this.model, isChecked);
this.updateCheckbox();
}

let check = this.get('onCheck');
if (check) {
check(model);
if (this.onCheck) {
this.onCheck(this.model);
}
},

Expand Down
15 changes: 6 additions & 9 deletions addon/components/x-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,22 @@ export default Component.extend({

init() {
this._super(...arguments);
let tree = this.get('model');

// Make sure chosen item is highlighted and expanded-to in the tree
let chosenId = this.get('chosenId');
if (chosenId) {
let chosen = getDescendents(tree).findBy('id', chosenId);
if (this.chosenId) {
let chosen = getDescendents(this.model).findBy('id', this.chosenId);
if (chosen) {
getAncestors(tree, chosen).forEach(x => {
if (get(x, 'id') !== chosenId) {
getAncestors(this.model, chosen).forEach(x => {
if (get(x, 'id') !== this.chosenId) {
set(x, 'isExpanded', true);
}
});
}
}

// Expand to given depth
let expandDepth = this.get('expandDepth');
if (expandDepth) {
getDescendents(tree, expandDepth).setEach('isExpanded', true);
if (this.expandDepth) {
getDescendents(this.model, this.expandDepth).setEach('isExpanded', true);
}
}
});
12 changes: 6 additions & 6 deletions addon/utils/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export function getDescendents(tree, depth = -1) {
if (depth < 0) { // Unlimited depth
tree.forEach(node => {
descendents.pushObject(node);
descendents.pushObjects(getDescendents(node.children));
descendents.pushObjects(getDescendents(get(node, 'children')));
});
} else if (depth > 0) {
tree.forEach(node => {
descendents.pushObject(node);
descendents.pushObjects(getDescendents(node.children, depth - 1));
descendents.pushObjects(getDescendents(get(node, 'children'), depth - 1));
});
}

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

tree.forEach(node => {
if (!ancestors.isAny('id', childId)) {
if (node.id === childId) {
if (get(node, 'id') === childId) {
ancestors.pushObject(node);
} else if (node.children.length > 0) {
ancestors.pushObjects(getAncestors(node.children, childNode));
} else if (get(node, 'children.length') > 0) {
ancestors.pushObjects(getAncestors(get(node, 'children'), childNode));
if (ancestors.length > 0) {
ancestors.pushObject(node);
}
Expand All @@ -94,5 +94,5 @@ export function getAncestors(tree, childNode) {

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

0 comments on commit 1959a45

Please sign in to comment.