contextMenu.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. angular.module("robware").directive('contextMenu', function() {
  2. return {
  3. restrict: 'E',
  4. templateUrl: '/scripts/directives/templates/contextMenu.html',
  5. scope: {
  6. actions: '=',
  7. isChild: '=?'
  8. },
  9. link: function(scope, element) {
  10. scope.isFunction = angular.isFunction;
  11. scope.show=false;
  12. scope.selectedElement=null;
  13. scope.subMenuShowKey="";
  14. scope.changeSubMenuShowKey=function(name){
  15. scope.subMenuShowKey=name;
  16. };
  17. function show(x, y, target){
  18. scope.changeSubMenuShowKey("");
  19. scope.menuX=x+"px";
  20. scope.menuY=y+"px";
  21. scope.selectedElement=target;
  22. scope.show=true;
  23. };
  24. function hide(){
  25. scope.show=false;
  26. }
  27. scope.performAction=function(func){
  28. if (!angular.isFunction(func))
  29. return;
  30. func($(scope.selectedElement).scope());
  31. hide();
  32. };
  33. $(document).on("click", function(e){
  34. hide();
  35. scope.$apply();
  36. });
  37. var parent=element.parent()[0];
  38. if (scope.isChild){
  39. show(parent.clientWidth,parent.clientTop);
  40. }else
  41. $(parent).css("position","relative");
  42. element.parent().on("contextmenu", function(e){
  43. var offset=$(element).offset();
  44. show(e.pageX-offset.left, e.pageY-offset.top, e.target);
  45. scope.$apply();
  46. e.preventDefault();
  47. });
  48. }
  49. };
  50. });