super.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ///////////////////////
  2. /// SUPER CLASS ///
  3. ///////////////////////
  4. import * as u from './utils';
  5. // Constants
  6. var isTouch = !!('ontouchstart' in window);
  7. var isPointer = window.PointerEvent ? true : false;
  8. var isMSPointer = window.MSPointerEvent ? true : false;
  9. var events = {
  10. touch: {
  11. start: 'touchstart',
  12. move: 'touchmove',
  13. end: 'touchend, touchcancel'
  14. },
  15. mouse: {
  16. start: 'mousedown',
  17. move: 'mousemove',
  18. end: 'mouseup'
  19. },
  20. pointer: {
  21. start: 'pointerdown',
  22. move: 'pointermove',
  23. end: 'pointerup, pointercancel'
  24. },
  25. MSPointer: {
  26. start: 'MSPointerDown',
  27. move: 'MSPointerMove',
  28. end: 'MSPointerUp'
  29. }
  30. };
  31. var toBind;
  32. var secondBind = {};
  33. if (isPointer) {
  34. toBind = events.pointer;
  35. } else if (isMSPointer) {
  36. toBind = events.MSPointer;
  37. } else if (isTouch) {
  38. toBind = events.touch;
  39. secondBind = events.mouse;
  40. } else {
  41. toBind = events.mouse;
  42. }
  43. function Super () {}
  44. // Basic event system.
  45. Super.prototype.on = function (arg, cb) {
  46. var self = this;
  47. var types = arg.split(/[ ,]+/g);
  48. var type;
  49. self._handlers_ = self._handlers_ || {};
  50. for (var i = 0; i < types.length; i += 1) {
  51. type = types[i];
  52. self._handlers_[type] = self._handlers_[type] || [];
  53. self._handlers_[type].push(cb);
  54. }
  55. return self;
  56. };
  57. Super.prototype.off = function (type, cb) {
  58. var self = this;
  59. self._handlers_ = self._handlers_ || {};
  60. if (type === undefined) {
  61. self._handlers_ = {};
  62. } else if (cb === undefined) {
  63. self._handlers_[type] = null;
  64. } else if (self._handlers_[type] &&
  65. self._handlers_[type].indexOf(cb) >= 0) {
  66. self._handlers_[type].splice(self._handlers_[type].indexOf(cb), 1);
  67. }
  68. return self;
  69. };
  70. Super.prototype.trigger = function (arg, data) {
  71. var self = this;
  72. var types = arg.split(/[ ,]+/g);
  73. var type;
  74. self._handlers_ = self._handlers_ || {};
  75. for (var i = 0; i < types.length; i += 1) {
  76. type = types[i];
  77. if (self._handlers_[type] && self._handlers_[type].length) {
  78. self._handlers_[type].forEach(function (handler) {
  79. handler.call(self, {
  80. type: type,
  81. target: self
  82. }, data);
  83. });
  84. }
  85. }
  86. };
  87. // Configuration
  88. Super.prototype.config = function (options) {
  89. var self = this;
  90. self.options = self.defaults || {};
  91. if (options) {
  92. self.options = u.safeExtend(self.options, options);
  93. }
  94. };
  95. // Bind internal events.
  96. Super.prototype.bindEvt = function (el, type) {
  97. var self = this;
  98. self._domHandlers_ = self._domHandlers_ || {};
  99. self._domHandlers_[type] = function () {
  100. if (typeof self['on' + type] === 'function') {
  101. self['on' + type].apply(self, arguments);
  102. } else {
  103. // eslint-disable-next-line no-console
  104. console.warn('[WARNING] : Missing "on' + type + '" handler.');
  105. }
  106. };
  107. u.bindEvt(el, toBind[type], self._domHandlers_[type]);
  108. if (secondBind[type]) {
  109. // Support for both touch and mouse at the same time.
  110. u.bindEvt(el, secondBind[type], self._domHandlers_[type]);
  111. }
  112. return self;
  113. };
  114. // Unbind dom events.
  115. Super.prototype.unbindEvt = function (el, type) {
  116. var self = this;
  117. self._domHandlers_ = self._domHandlers_ || {};
  118. u.unbindEvt(el, toBind[type], self._domHandlers_[type]);
  119. if (secondBind[type]) {
  120. // Support for both touch and mouse at the same time.
  121. u.unbindEvt(el, secondBind[type], self._domHandlers_[type]);
  122. }
  123. delete self._domHandlers_[type];
  124. return this;
  125. };
  126. export default Super;