Code coverage report for src/canvas-view/canvas/dragging-service.js

Statements: 9.09% (3 / 33)      Branches: 0% (0 / 18)      Functions: 16.67% (1 / 6)      Lines: 9.09% (3 / 33)      Ignored: none     

All files » src/canvas-view/canvas/ » dragging-service.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 971       1     1                                                                                                                                                                                  
(function () {
  'use strict';
 
  // Service used to help with dragging and clicking on elements.
  angular.module('dragging', ['mouseCapture'])
    .factory('dragging', ['mouseCapture', Factory]);
 
  function Factory (mouseCapture) {
    //
    // Threshold for dragging.
    // When the mouse moves by at least this amount dragging starts.
    //
    var threshold = 5;
 
    return {
 
      //
      // Called by users of the service to register a mousedown event and start dragging.
      // Acquires the 'mouse capture' until the mouseup event.
      //
      startDrag: function (evt, config) {
        var dragging = false;
        var x = evt.pageX;
        var y = evt.pageY;
 
        //
        // Handler for mousemove events while the mouse is 'captured'.
        //
        var mouseMove = function (evt) {
          if (!dragging) {
            if (Math.abs(evt.pageX - x) > threshold
              || Math.abs(evt.pageY - y) > threshold) {
              dragging = true;
 
              if (config.dragStarted) {
                config.dragStarted(x, y, evt);
              }
 
              if (config.dragging) {
                // First 'dragging' call to take into account that we have
                // already moved the mouse by a 'threshold' amount.
                config.dragging(evt.pageX, evt.pageY, evt);
              }
            }
          } else {
            if (config.dragging) {
              config.dragging(evt.pageX, evt.pageY, evt);
            }
 
            x = evt.pageX;
            y = evt.pageY;
          }
        };
 
        //
        // Handler for when mouse capture is released.
        //
        var released = function () {
          if (dragging) {
            if (config.dragEnded) {
              config.dragEnded();
            }
          } else {
            if (config.clicked) {
              config.clicked();
            }
          }
        };
 
        //
        // Handler for mouseup event while the mouse is 'captured'.
        // Mouseup releases the mouse capture.
        //
        var mouseUp = function (evt) {
          mouseCapture.release();
 
          evt.stopPropagation();
          evt.preventDefault();
        };
 
        //
        // Acquire the mouse capture and start handling mouse events.
        //
        mouseCapture.acquire(evt, {
          mouseMove: mouseMove,
          mouseUp: mouseUp,
          released: released
        });
 
        evt.stopPropagation();
        evt.preventDefault();
      }
    };
  }
})();