Skip to content

Commit

Permalink
Add move method
Browse files Browse the repository at this point in the history
Will be the foundation of dnd #109
  • Loading branch information
okcoker committed Jun 1, 2018
1 parent 63280c9 commit 6cd5e2d
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/taggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,10 @@
return this.settings.preserveCase ? text : text.toLowerCase();
};

Taggle.prototype._isIndexInRange = function(index) {
return index >= 0 && index <= this.tag.values.length - 1;
};

Taggle.prototype.getTags = function() {
return {
elements: this.getTagElements(),
Expand Down Expand Up @@ -967,7 +971,7 @@
throw new Error('Second edit argument must be a number');
}

if (index > this.tag.values.length - 1 || index < 0) {
if (!this._isIndexInRange(index)) {
throw new Error('Edit index should be between 0 and ' + this.tag.values.length - 1);
}

Expand All @@ -985,6 +989,44 @@
return this;
};

Taggle.prototype.move = function(currentIndex, destinationIndex) {
if (typeof currentIndex !== 'number' || typeof destinationIndex !== 'number') {
throw new Error('Both arguments must be numbers');
}

if (!this._isIndexInRange(currentIndex)) {
throw new Error('First index should be between 0 and ' + this.tag.values.length - 1);
}

if (!this._isIndexInRange(destinationIndex)) {
throw new Error('Second index should be between 0 and ' + this.tag.values.length - 1);
}

if (currentIndex === destinationIndex) {
return this;
}

var value = this.tag.values[currentIndex];
var element = this.tag.elements[currentIndex];
var lastElement = this.tag.elements[destinationIndex];
var eventFn = this._closeEvents[currentIndex];
var closeButton = this._closeButtons[currentIndex];

this.tag.values.splice(currentIndex, 1);
this.tag.elements.splice(currentIndex, 1);
this._closeEvents.splice(currentIndex, 1);
this._closeButtons.splice(currentIndex, 1);

this.tag.values.splice(destinationIndex, 0, value);
this.tag.elements.splice(destinationIndex, 0, element);
this._closeEvents.splice(currentIndex, 0, eventFn);
this._closeButtons.splice(currentIndex, 0, closeButton);

this.list.insertBefore(element, lastElement.nextSibling);

return this;
};

Taggle.prototype.remove = function(text, all) {
var len = this.tag.values.length - 1;
var found = false;
Expand Down
108 changes: 108 additions & 0 deletions test/taggle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,114 @@ describe('Taggle', function() {

expect(instance.getTagValues()).to.eql([{ text: 'three', id: 1 }, { text: 'four', id: 2 }]);
});

it('should edit leave element references intact', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two'],
attachTagId: true
});
var elements = instance.getTagElements();

instance.edit('three', 0).edit('four', 1);

expect(instance.getTagElements()[0]).to.equal(elements[0]);
expect(instance.getTagElements()[1]).to.equal(elements[1]);
});
});

describe('#move', function() {
it('should be chainable', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two']
});
var container = instance.move(0, 1).getContainer();

expect(container).to.equal(this.container);
});

it('should throw if first argument is not a number', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two']
});

expect(instance.move.bind(instance, null)).to.throw();
});

it('should throw if second argument is not a number', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two']
});

expect(instance.move.bind(instance, 0, null)).to.throw();
});

it('should throw if either argument is greater than tag length', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two']
});

expect(instance.move.bind(instance, 0, 3)).to.throw();
expect(instance.move.bind(instance, 3, 0)).to.throw();
});

it('should throw if either argument is less than 0', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two']
});

expect(instance.move.bind(instance, 0, -1)).to.throw();
expect(instance.move.bind(instance, -1, 1)).to.throw();
});

it('should move tags appropriately', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two']
});

instance.move(0, 1);

expect(instance.getTagValues()).to.eql(['two', 'one']);
});

it('should edit leave tag ids intact', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two'],
attachTagId: true
});

instance.move(0, 1);

expect(instance.getTagValues()).to.eql([{ text: 'two', id: 2 }, { text: 'one', id: 1 }]);
});

it('should edit leave element references intact', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two']
});
var elements = instance.getTagElements();
var fromIndex = 0;
var destinationIndex = 1;


instance.move(fromIndex, destinationIndex);

expect(instance.getTagElements()[destinationIndex]).to.equal(elements[fromIndex]);
});

it('should edit leave element references intact with attached tag ids', function() {
var instance = new Taggle(this.container, {
tags: ['one', 'two'],
attachTagId: true
});
var elements = instance.getTagElements();
var fromIndex = 0;
var destinationIndex = 1;


instance.move(fromIndex, destinationIndex);

expect(instance.getTagElements()[destinationIndex]).to.equal(elements[fromIndex]);
});
});

describe('#remove', function() {
Expand Down

0 comments on commit 6cd5e2d

Please sign in to comment.