<commit_before>'use strict';

var app = angular.module('LDT.directives');

app.directive('appVersion', ['version', function(version) {
    return function(scope, elm, attrs) {
      elm.text(version);
    };
  }]);

app.directive('catchInput',function() {
  return {
    link: function(scope, element, iAttrs, ctrl) {
      function stopit(e) {
        e.stopPropagation();
      };

      //Don't allow clicks to change mode
      element.click(stopit);

      //Don't allow renaming keypresses to change mode
      element.keypress(stopit);
    }
  };
});


//Don't allow dragging that begins on this element to cause anything else to move
app.directive('noDrag',function() {
  return {
    link: function(scope, element, iAttrs, ctrl) {
      function stopit(e) {
        e.stopPropagation();
      };
      element.mousemove(stopit);

      element.mousedown(stopit);
    }
  };
});


app.directive('autoFocus',function() {
  return {
    link: function(scope, element, iAttrs, ctrl) {
      var regex = /([-.#\w\d]+) on ([\w]+)/;
      var matches = regex.exec(iAttrs.autoFocus);
      var params = _.object(['targetSelector', 'eventName'], matches.slice(1,3));

      element.bind(params.eventName, function(e) {
        // Select the whole entity title for fast rename
        $(element.find(params.targetSelector)[0]).select();
      });
    }
  };
});


// Would be nice to automatically highlight the attribute that was double-clicked.
/*
app.directive('forwardEvent',function() {
  return {
    link: function(scope, element, iAttrs, ctrl) {
      var regex = /([\w]+) to ([.#-\w\d]+)/
      var matches = regex.exec(iAttrs.forwardEvent);
      var params = _.object(['eventName', 'targetSelector'], matches.slice(1,3));

      element.bind(params.eventName, function(e) {
        // Trigger another event on child when parent event occurs (danger... recursive event firing)
        console.log(e)
        var child = element.find(params.targetSelector)
        child.bind(params.eventName, function(e) { e.stopPropagation(); });
        child.trigger(e);
      })
    }
  }
});
*/

// Setup a rectangular element to be movable and resizable, and bind its position to the scope
app.directive('moveAndResize',function() {
  return {
    link: function (scope, element, iAttrs, ctrl) {
      var subject = scope[iAttrs.moveAndResize];
      var dragging = false;
      var dragType,
          dragStartX, dragStartY,
          dragStartMouseX, dragStartMouseY,
          dragStartWidth, dragStartHeight;

      var GRIDSIZE  = 10;
      var BORDER_HANDLE_SIZE = 10;

      function grid(n) {
        return Math.floor((n + GRIDSIZE/2.0)/GRIDSIZE)*GRIDSIZE
      }

      // 0: move, 1: resize-left, 2: resize-right, 3: resize-top: 4: resize-bottom
      function boxBorderArea(x,y) {
               if (x < parseInt(element.offset().left) + BORDER_HANDLE_SIZE) {
          return 1;
        } else if (x > parseInt(element.offset().left) + parseInt(element.width()) - BORDER_HANDLE_SIZE) {
          return 2;
        } else if (y < parseInt(element.offset().top) + BORDER_HANDLE_SIZE) {
          return 3;
        } else if (y > parseInt(element.offset().top) + parseInt(element.height()) - BORDER_HANDLE_SIZE) {
          return 4;
        } else {
          return 0;
        }
      }

      function actionByDragType(ev) {
        switch(dragType) {
          case 0:
            subject.x      = grid(dragStartX      + ev.pageX - dragStartMouseX);
            subject.y      = grid(dragStartY      + ev.pageY - dragStartMouseY);
            break;
          case 1:
            subject.x      = grid(dragStartX      + ev.pageX - dragStartMouseX);
            subject.width  = grid(dragStartWidth  - ev.pageX + dragStartMouseX);
            break;
          case 2:
            subject.width  = grid(dragStartWidth  + ev.pageX - dragStartMouseX);
            break;
          case 3:
            subject.y      = grid(dragStartY      + ev.pageY - dragStartMouseY);
            subject.height = grid(dragStartHeight - ev.pageY + dragStartMouseY);
            break;
          case 4:
            subject.height = grid(dragStartHeight + ev.pageY - dragStartMouseY);
            break;
        }
      }

      function setCursor(ev) {
        switch(boxBorderArea(ev.pageX, ev.pageY)) {
          case 0: element.css('cursor', 'default' ); break;
          case 1: element.css('cursor', 'w-resize'); break;
          case 2: element.css('cursor', 'e-resize'); break;
          case 3: element.css('cursor', 'n-resize'); break;
          case 4: element.css('cursor', 's-resize'); break;
        }
      }

      function startDrag(ev) {
        dragging = true;
        dragStartX = subject.x;
        dragStartY = subject.y;
        dragStartWidth = subject.width;
        dragStartHeight = subject.height;
        dragStartMouseX = ev.pageX;
        dragStartMouseY = ev.pageY;
        dragType = boxBorderArea(ev.pageX, ev.pageY);
        ev.stopPropagation();
      };

      function stopDrag(ev) {
        dragging = false;
      };

      function moveDrag(ev) {
        if (dragging) {
          scope.$apply(actionByDragType(ev));
        }
        // prevent highlighting action (annoying)
        ev.preventDefault();
        ev.stopPropagation();
      };

      $(element).mousedown(startDrag);
      $(document).mouseup(stopDrag);
      $(document).mouseleave(stopDrag);
      $(document).mousemove(moveDrag);
      $(element).mousemove(setCursor);
    }
  };
});

// Setup entities to be draggable and resizable, and bind to the scope
app.directive('selectWith',function() {
  return {<commit_msg>Allow drag only with primary (left) click<commit_after>'use strict';

var app = angular.module('LDT.directives');

app.directive('appVersion', ['version', function(version) {
    return function(scope, elm, attrs) {
      elm.text(version);
    };
  }]);

app.directive('catchInput',function() {
  return {
    link: function(scope, element, iAttrs, ctrl) {
      function stopit(e) {
        e.stopPropagation();
      };

      //Don't allow clicks to change mode
      element.click(stopit);

      //Don't allow renaming keypresses to change mode
      element.keypress(stopit);
    }
  };
});


//Don't allow dragging that begins on this element to cause anything else to move
app.directive('noDrag',function() {
  return {
    link: function(scope, element, iAttrs, ctrl) {
      function stopit(e) {
        e.stopPropagation();
      };
      element.mousemove(stopit);

      element.mousedown(stopit);
    }
  };
});


app.directive('autoFocus',function() {
  return {
    link: function(scope, element, iAttrs, ctrl) {
      var regex = /([-.#\w\d]+) on ([\w]+)/;
      var matches = regex.exec(iAttrs.autoFocus);
      var params = _.object(['targetSelector', 'eventName'], matches.slice(1,3));

      element.bind(params.eventName, function(e) {
        // Select the whole entity title for fast rename
        $(element.find(params.targetSelector)[0]).select();
      });
    }
  };
});


// Would be nice to automatically highlight the attribute that was double-clicked.
/*
app.directive('forwardEvent',function() {
  return {
    link: function(scope, element, iAttrs, ctrl) {
      var regex = /([\w]+) to ([.#-\w\d]+)/
      var matches = regex.exec(iAttrs.forwardEvent);
      var params = _.object(['eventName', 'targetSelector'], matches.slice(1,3));

      element.bind(params.eventName, function(e) {
        // Trigger another event on child when parent event occurs (danger... recursive event firing)
        console.log(e)
        var child = element.find(params.targetSelector)
        child.bind(params.eventName, function(e) { e.stopPropagation(); });
        child.trigger(e);
      })
    }
  }
});
*/

// Setup a rectangular element to be movable and resizable, and bind its position to the scope
app.directive('moveAndResize',function() {
  return {
    link: function (scope, element, iAttrs, ctrl) {
      var subject = scope[iAttrs.moveAndResize];
      var dragging = false;
      var dragType,
          dragStartX, dragStartY,
          dragStartMouseX, dragStartMouseY,
          dragStartWidth, dragStartHeight;

      var GRIDSIZE  = 10;
      var BORDER_HANDLE_SIZE = 10;

      function grid(n) {
        return Math.floor((n + GRIDSIZE/2.0)/GRIDSIZE)*GRIDSIZE
      }

      // 0: move, 1: resize-left, 2: resize-right, 3: resize-top: 4: resize-bottom
      function boxBorderArea(x,y) {
               if (x < parseInt(element.offset().left) + BORDER_HANDLE_SIZE) {
          return 1;
        } else if (x > parseInt(element.offset().left) + parseInt(element.width()) - BORDER_HANDLE_SIZE) {
          return 2;
        } else if (y < parseInt(element.offset().top) + BORDER_HANDLE_SIZE) {
          return 3;
        } else if (y > parseInt(element.offset().top) + parseInt(element.height()) - BORDER_HANDLE_SIZE) {
          return 4;
        } else {
          return 0;
        }
      }

      function actionByDragType(ev) {
        switch(dragType) {
          case 0:
            subject.x      = grid(dragStartX      + ev.pageX - dragStartMouseX);
            subject.y      = grid(dragStartY      + ev.pageY - dragStartMouseY);
            break;
          case 1:
            subject.x      = grid(dragStartX      + ev.pageX - dragStartMouseX);
            subject.width  = grid(dragStartWidth  - ev.pageX + dragStartMouseX);
            break;
          case 2:
            subject.width  = grid(dragStartWidth  + ev.pageX - dragStartMouseX);
            break;
          case 3:
            subject.y      = grid(dragStartY      + ev.pageY - dragStartMouseY);
            subject.height = grid(dragStartHeight - ev.pageY + dragStartMouseY);
            break;
          case 4:
            subject.height = grid(dragStartHeight + ev.pageY - dragStartMouseY);
            break;
        }
      }

      function setCursor(ev) {
        switch(boxBorderArea(ev.pageX, ev.pageY)) {
          case 0: element.css('cursor', 'default' ); break;
          case 1: element.css('cursor', 'w-resize'); break;
          case 2: element.css('cursor', 'e-resize'); break;
          case 3: element.css('cursor', 'n-resize'); break;
          case 4: element.css('cursor', 's-resize'); break;
        }
      }

      function startDrag(ev) {
				// left click only (otherwise right click has issues)
				if (ev.button == 0) {
					dragging = true;
					dragStartX = subject.x;
					dragStartY = subject.y;
					dragStartWidth = subject.width;
					dragStartHeight = subject.height;
					dragStartMouseX = ev.pageX;
					dragStartMouseY = ev.pageY;
					dragType = boxBorderArea(ev.pageX, ev.pageY);
					ev.stopPropagation();
				}
      };

      function stopDrag(ev) {
        dragging = false;
      };

      function moveDrag(ev) {
        if (dragging) {
          scope.$apply(actionByDragType(ev));
        }
        // prevent highlighting action (annoying)
        ev.preventDefault();
        ev.stopPropagation();
      };

      $(element).mousedown(startDrag);
      $(document).mouseup(stopDrag);
      $(document).mouseleave(stopDrag);
      $(document).mousemove(moveDrag);
      $(element).mousemove(setCursor);
    }
  };
});

// Setup entities to be draggable and resizable, and bind to the scope
app.directive('selectWith',function() {
  return {
{'fraction_non_alphanumeric': 0.13288069835111543, 'fraction_numerical': 0.005555065690856185, 'mean_word_length': 2.289443155452436, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '912690', 'n_tokens_mistral': 3551, 'n_tokens_neox': 3411, 'n_words': 767}