123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. (function (factory) {
  2. if (typeof module === "object" && typeof module.exports === "object") {
  3. var v = factory(require, exports);
  4. if (v !== undefined) module.exports = v;
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. define(["require", "exports", "../util/util", "./hammer"], factory);
  8. }
  9. })(function (require, exports) {
  10. "use strict";
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var util_1 = require("../util/util");
  13. var hammer_1 = require("./hammer");
  14. /**
  15. * @hidden
  16. * A gesture recognizer class.
  17. *
  18. * TODO(mlynch): Re-enable the DOM event simulation that was causing issues (or verify hammer does this already, it might);
  19. */
  20. var Gesture = (function () {
  21. function Gesture(element, opts) {
  22. if (opts === void 0) { opts = {}; }
  23. this._callbacks = {};
  24. this.isListening = false;
  25. util_1.defaults(opts, {
  26. domEvents: true
  27. });
  28. this.element = element;
  29. // Map 'x' or 'y' string to hammerjs opts
  30. this.direction = opts.direction || 'x';
  31. opts.direction = this.direction === 'x' ?
  32. hammer_1.DIRECTION_HORIZONTAL :
  33. hammer_1.DIRECTION_VERTICAL;
  34. this._options = opts;
  35. }
  36. Gesture.prototype.options = function (opts) {
  37. Object.assign(this._options, opts);
  38. };
  39. Gesture.prototype.on = function (type, cb) {
  40. if (type === 'pinch' || type === 'rotate') {
  41. this._hammer.get(type).set({ enable: true });
  42. }
  43. this._hammer.on(type, cb);
  44. (this._callbacks[type] || (this._callbacks[type] = [])).push(cb);
  45. };
  46. Gesture.prototype.off = function (type, cb) {
  47. this._hammer.off(type, this._callbacks[type] ? cb : null);
  48. };
  49. Gesture.prototype.listen = function () {
  50. if (!this.isListening) {
  51. this._hammer = hammer_1.Hammer(this.element, this._options);
  52. }
  53. this.isListening = true;
  54. };
  55. Gesture.prototype.unlisten = function () {
  56. var eventType;
  57. var i;
  58. if (this._hammer && this.isListening) {
  59. for (eventType in this._callbacks) {
  60. for (i = 0; i < this._callbacks[eventType].length; i++) {
  61. this._hammer.off(eventType, this._callbacks[eventType]);
  62. }
  63. }
  64. this._hammer.destroy();
  65. }
  66. this._callbacks = {};
  67. this._hammer = null;
  68. this.isListening = false;
  69. };
  70. Gesture.prototype.destroy = function () {
  71. this.unlisten();
  72. this.element = this._options = null;
  73. };
  74. return Gesture;
  75. }());
  76. exports.Gesture = Gesture;
  77. });
  78. //# sourceMappingURL=gesture.js.map