Skip to content

Commit

Permalink
gesture recognizer: Add a function to remove a registered gesture rec…
Browse files Browse the repository at this point in the history
…ognizer in gestures (ampproject#12271)
  • Loading branch information
cathyxz authored and gzgogo committed Jan 26, 2018
1 parent 4e5de7b commit 98fc907
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {Observable} from './observable';
import {Pass} from './pass';
import {dev} from './log';
import {toWin} from './types';
import {findIndex} from './utils/array';


const PROP_ = '__AMP_Gestures';

Expand Down Expand Up @@ -173,6 +175,35 @@ export class Gestures {
return overserver.add(handler);
}

/**
* Unsubscribes all handlers from the given gesture recognizer. Returns
* true if anything was done. Returns false if there were no handlers
* registered on the given gesture recognizer in first place.
*
* @param {function(new:GestureRecognizer<DATA>, !Gestures)} recognizerConstr
* @return {boolean}
*/
removeGesture(recognizerConstr) {
const type = new recognizerConstr(this).getType();
const overserver = this.overservers_[type];
if (overserver) {
overserver.removeAll();
const index = findIndex(this.recognizers_, e => e.getType() == type);
if (index < 0) {
return false;
}
// Remove the recognizer as well as all associated tracking state
this.recognizers_.splice(index, 1);
this.ready_.splice(index, 1);
this.pending_.splice(index, 1);
this.tracking_.splice(index, 1);
delete this.overservers_[type];
return true;
} else {
return false;
}
}

/**
* Subscribes to pointer down events, such as "touchstart" or "mousedown".
* @param {!Function} handler
Expand Down
10 changes: 10 additions & 0 deletions test/functional/test-gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,16 @@ describe('Gestures', () => {
expect(event.stopPropagation).to.have.not.been.called;
});

it('should gesture recognizer on removeGesture', () => {
expect(gestures.recognizers_.length).to.equal(1);
expect(gestures.removeGesture(TestRecognizer)).to.equal(true);
expect(gestures.removeGesture(Test2Recognizer)).to.equal(false);
expect(gestures.recognizers_.length).to.equal(0);
expect(gestures.ready_.length).to.equal(0);
expect(gestures.tracking_.length).to.equal(0);
expect(gestures.pending_.length).to.equal(0);
});

it('should remove listeners and shared cache instance on cleanup', () => {
const eventNames = ['touchstart', 'touchend', 'touchmove', 'touchcancel'];
const prop = '__AMP_Gestures';
Expand Down

0 comments on commit 98fc907

Please sign in to comment.