[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/rsrc/js/core/ -> behavior-gesture.js (source)

   1  /**
   2   * @provides javelin-behavior-phabricator-gesture
   3   * @requires javelin-behavior
   4   *           javelin-behavior-device
   5   *           javelin-stratcom
   6   *           javelin-vector
   7   *           javelin-dom
   8   *           javelin-magical-init
   9   * @javelin
  10   */
  11  
  12  /**
  13   * Basic gesture recognition. Unstable. Only supports swipes.
  14   */
  15  JX.behavior('phabricator-gesture', function() {
  16  
  17    var target = null;
  18    var swiping = false;
  19    var p0;
  20    var p1;
  21  
  22    JX.Stratcom.listen(
  23      ['touchstart', 'touchcancel', 'mousedown'],
  24      'touchable',
  25      function(e) {
  26        if (JX.Device.getDevice() == 'desktop') {
  27          return;
  28        }
  29        if (JX.Stratcom.pass()) {
  30          return;
  31        }
  32        if (target) {
  33          try {
  34            JX.DOM.invoke(target, 'gesture.swipe.cancel', get_swipe_data());
  35          } finally {
  36            stop_swipe();
  37          }
  38          return;
  39        }
  40  
  41        target = e.getNode('touchable');
  42        p0 = JX.Vector.getPos(e);
  43        p1 = JX.Vector.getPos(e);
  44    });
  45  
  46    JX.enableDispatch(document.body, 'mousemove');
  47    JX.Stratcom.listen(
  48      ['touchmove', 'mousemove'],
  49      null,
  50      function(e) {
  51        if (!target) {
  52          return;
  53        }
  54        p1 = JX.Vector.getPos(e);
  55        if (!swiping) {
  56  
  57          // Here, we haven't started the swipe yet. We're waiting for you to
  58          // move your finger far enough to make it clear that you intend to
  59          // swipe or drag, not just tap.
  60  
  61          var dx = (p1.x - p0.x);
  62          var dy = (p1.y - p0.y);
  63          var swipe_radius = 20;
  64          if ((dx * dx) + (dy * dy) >= (swipe_radius * swipe_radius)) {
  65  
  66            // You've moved your finger far enough away from the origin that
  67            // we're sure you mean to swipe, scroll, or drag, not just tap.
  68            // Decide if you're trying to scroll or swipe. If your finger's
  69            // motion has been primarily vertical, assume this is a scroll. If
  70            // your finger's motion has been primarily horizontal, assume this
  71            // is a swipe.
  72            if (dy * dy >= dx * dx) {
  73              stop_swipe();
  74              return;
  75            }
  76            swiping = true;
  77            JX.DOM.invoke(target, 'gesture.swipe.start', get_swipe_data());
  78          }
  79        }
  80        if (swiping) {
  81          if (!e.getNode('touchable')) {
  82            p1 = JX.$V(p0);
  83          }
  84          JX.DOM.invoke(target, 'gesture.swipe.move', get_swipe_data());
  85          e.prevent();
  86        }
  87      });
  88  
  89    JX.Stratcom.listen(
  90      ['touchend', 'mouseup'],
  91      null,
  92      function() {
  93        if (!target) {
  94          return;
  95        }
  96  
  97        try {
  98          if (swiping) {
  99            JX.DOM.invoke(target, 'gesture.swipe.end', get_swipe_data());
 100          }
 101        } finally {
 102          stop_swipe();
 103        }
 104      });
 105  
 106    function get_swipe_data() {
 107      var direction = (p1.x > p0.x) ? 'right' : 'left';
 108      var length = Math.abs(p1.x - p0.x);
 109  
 110      return {
 111        p0: p0,
 112        p1: p1,
 113        direction: direction,
 114        length: length
 115      };
 116    }
 117  
 118    function stop_swipe() {
 119      target = null;
 120      swiping = false;
 121    }
 122  
 123  });


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1