Skip to content

Commit

Permalink
feat(trigger): trigger only if different direction
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Sep 6, 2015
1 parent c82c580 commit 9e334ba
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/nipplejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,49 @@ Nipple.prototype.computeDirection = function (evt, obj) {
}

if (obj.force > this.options.threshold) {
obj.direction = {
var oldDirection = {};
for (var i in this.direction) {
if (this.direction.hasOwnProperty(i)) {
oldDirection[i] = this.direction[i];
}
}
var same = true;

this.direction = {
x: directionX,
y: directionY,
angle: direction
};

this.trigger('dir', obj);
this.trigger('plain', obj);
this.trigger('dir:' + direction, obj);
this.trigger('plain:' + directionX, obj);
this.trigger('plain:' + directionY, obj);
obj.direction = this.direction;

for (var i in oldDirection) {
if (oldDirection[i] !== this.direction[i]) {
same = false;
}
}

if (same) {
return;
}

if (oldDirection.x !== this.direction.x ||
oldDirection.y !== this.direction.y) {
this.trigger('plain', obj);
}

if (oldDirection.x !== this.direction.x) {
this.trigger('plain:' + directionX, obj);
}

if (oldDirection.y !== this.direction.y) {
this.trigger('plain:' + directionY, obj);
}

if (oldDirection.angle !== this.direction.angle) {
this.trigger('dir', obj);
this.trigger('dir:' + direction, obj);
}
}
};

Expand Down

0 comments on commit 9e334ba

Please sign in to comment.