Skip to content

Instantly share code, notes, and snippets.

@MeTe-30
Created November 28, 2016 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeTe-30/c6709a219465004251546ba33e4fd958 to your computer and use it in GitHub Desktop.
Save MeTe-30/c6709a219465004251546ba33e4fd958 to your computer and use it in GitHub Desktop.
ngEnter, ngEsc, ngTab, ngResize, ngDragenter, ngMouse, ngDragdiff | AngularJs
app.directive('ngEnter', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if (event.which === 13) {
scope.$apply(function() {
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
app.directive('ngEsc', function() {
return function(scope, element, attrs) {
element.bind('keydown keypress', function(event) {
if (event.which === 27) {
scope.$apply(function() {
scope.$eval(attrs.ngEsc);
});
event.preventDefault();
}
});
};
});
app.directive('ngTab', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if (event.which === 9) {
scope.$apply(function() {
scope.$eval(attrs.ngTab);
});
event.preventDefault();
}
});
};
});
app.directive('ngResize', ['$window', function ($window) {
return function(scope, element, attrs) {
angular.element($window).bind('resize', function () {
scope.$apply(function () {
scope.$eval(attrs.ngResize);
});
});
};
}]);
app.directive('ngDragenter', function() {
return function(scope, element, attrs) {
element.bind('dragenter', function(e) {
scope.$apply(function() {
scope.$eval(attrs.ngDragenter);
});
e.stopPropagation();
e.preventDefault();
});
}
});
app.directive('ngMouse', [function() {
return {
scope: {
ngMouse: '='
},
link: function(scope, element, attrs, ngModel) {
element.bind("mousemove", function(e) {
scope.$applyAsync(function() {
scope.ngMouse = { x: e.clientX, y: e.clientY };
});
});
}
};
}]);
app.directive('ngDragdiff', function() {
return function(scope, element, attrs) {
element.bind('mousedown', function(e1) {
var first = [e1.clientX, e1.clientY];
var last = first;
element.bind('mousemove', function(e2) {
scope[attrs.ngDragdiff]({
total: [e2.clientX - first[0], e2.clientY - first[1]],
last: [e2.clientX - last[0], e2.clientY - last[1]]
});
last = [e2.clientX, e2.clientY];
e2.stopPropagation();
e2.preventDefault();
});
});
element.bind('mouseup mouseleave', function() {
element.unbind('mousemove');
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment