Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/peterheard01/tree-model-js
Browse files Browse the repository at this point in the history
…into peterheard01-master
  • Loading branch information
joaonuno committed Sep 18, 2014
2 parents 83fad7f + 59c3477 commit da4d8f0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
74 changes: 46 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,52 @@ module.exports = (function () {
return this.children.length > 0;
};

Node.prototype.addChild = function (child) {
var index;

if (!(child instanceof Node)) {
throw new TypeError('Child must be of type Node.');
}

child.parent = this;
if (!(this.model[this.config.childrenPropertyName] instanceof Array)) {
this.model[this.config.childrenPropertyName] = [];
}

if (this.config.modelComparatorFn) {
// Find the index to insert the child
index = findInsertIndex(
this.config.modelComparatorFn,
this.model[this.config.childrenPropertyName],
child.model);
// Add to the model children
this.model[this.config.childrenPropertyName].splice(index, 0, child.model);
// Add to the node children
this.children.splice(index, 0, child);
} else {
this.model[this.config.childrenPropertyName].push(child.model);
this.children.push(child);
}
return child;
};
Node.prototype.addChild = function (child) {
return addChild(this, child);
};

Node.prototype.addChildAtIndex = function (child, index) {
return addChild(this, child, index);
};

function addChild(self, child, insertIndex) {
var index;

if (!(child instanceof Node)) {
throw new TypeError('Child must be of type Node.');
}

child.parent = self;
if (!(self.model[self.config.childrenPropertyName] instanceof Array)) {
self.model[self.config.childrenPropertyName] = [];
}

if (self.config.modelComparatorFn) {
// Find the index to insert the child
index = findInsertIndex(
self.config.modelComparatorFn,
self.model[self.config.childrenPropertyName],
child.model);

// Add to the model children
self.model[self.config.childrenPropertyName].splice(index, 0, child.model);

// Add to the node children
self.children.splice(index, 0, child);
} else {

if (insertIndex == undefined) {
self.model[self.config.childrenPropertyName].push(child.model);
self.children.push(child);
} else {

self.model[self.config.childrenPropertyName].splice(insertIndex, 0, child.model);
self.children.splice(index, 0, child);
}

}
return child;
}

Node.prototype.getPath = function () {
var path = [];
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ describe('TreeModel', function () {
it('should throw an error when child is not a Node', function () {
assert.throws(root.addChild.bind(root, {children: []}), TypeError, 'Child must be of type Node.');
});

it('should add child at index', function () {
root.addChildAtIndex(treeModel.parse({ id: 13 }), 1);
assert.deepEqual(root.model.children, [{ id: 11 }, { id: 13 }, { id: 12 }]);
});

});

describe('getPath()', function () {
Expand Down

0 comments on commit da4d8f0

Please sign in to comment.