      1 pockets.gestures = {};
      2
      3 pockets.gestures.gestureStart = "gesturestart";
      4 pockets.gestures.gestureChange = "gesturechange";
      5 pockets.gestures.gestureEnd = "gestureend";
      6
      7 /**
      8  * pockets.gestures.GestureHandler provides support for expands, pinches and two-finger taps
      9  */
     10 pockets.gestures.GestureHandler = Class.create({
     11   EXPAND_CUTOFF: 1.5,
     12   PINCH_CUTOFF: 0.75,
     13   DEFAULT_EXPAND_STEP :0.5,
     14   DEFAULT_PINCH_STEP :0.25,
     15
     16   initialize:function(eventManager) {
     17     this._gestureStartHandler = this._handleGestureStart.bind(this);
     18     this._gestureChangeHandler = this._handleGestureChange.bind(this);
     19     this._gestureEndHandler = this._handleGestureEnd.bind(this);
     20     var doc = Mojo.Controller.getAppController().getActiveStageController().window.document;
     21     eventManager.register(doc, pockets.gestures.gestureStart, this._gestureStartHandler);
     22     eventManager.register(doc, pockets.gestures.gestureChange, this._gestureChangeHandler);
     23     eventManager.register(doc, pockets.gestures.gestureEnd, this._gestureEndHandler);
     24
     25     this.expandStep = this.DEFAULT_EXPAND_STEP;
     26     this.pinchStep = this.DEFAULT_PINCH_STEP;
     27
     28     this._reset();
     29   },
     30   _reset:function() {
     31     this.inGesture = false;
     32     this.lastScale = 0;
     33   },
     34   _handleGestureStart:function(event) {
     35     this.inGesture = true;
     36     this.lastScale = event.scale;
     37   },
     38   _handleGestureChange:function(event) {
     39     if (!this._handleExpanding(event.scale)) {
     40       if (this._handleExpand(event.scale)) {
     41         this._reset();
     42       }
     43     }
     44     else {
     45       this.lastScale = event.scale;
     46     }
     47     if (!this._handlePinching(event.scale)) {
     48       if (this._handlePinch(event.scale)) {
     49         this._reset();
     50       }
     51     }
     52     else {
     53       this.lastScale = event.scale;
     54     }
     55   },
     56   _handleGestureEnd:function(event) {
     57     if (!this._handleExpanding(event.scale)) {
     58       this._handleExpand(event.scale);
     59     }
     60     if (!this._handlePinching(event.scale)) {
     61       this._handlePinch(event.scale);
     62     }
     63     this._reset();
     64   },
     65   _handleExpand:function (scale) {
     66     return this._testScaleAndPossiblyDispatch(scale, this.expandHandler, this.EXPAND_CUTOFF, true);
     67   },
     68   _handleExpanding:function (scale) {
     69     return this._testScaleAndPossiblyDispatch(scale, this.expandingHandler, (this.lastScale + this.expandStep), true);
     70   },
     71   _handlePinch:function (scale) {
     72     return this._testScaleAndPossiblyDispatch(scale, this.pinchHandler, this.PINCH_CUTOFF, false);
     73   },
     74   _handlePinching:function (scale) {
     75     return this._testScaleAndPossiblyDispatch(scale, this.pinchingHandler, (this.lastScale - this.pinchStep), false);
     76   },
     77   _testScaleAndPossiblyDispatch: function(scale, handler, expression, isExpandTest) {
     78     if (this.inGesture && handler) {
     79       if ((isExpandTest && scale >= expression) || (!isExpandTest && scale <= expression)) {
     80         handler();
     81         return true;
     82       }
     83       return false;
     84     }
     85   }
     86 });