EventHelper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2013, 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. /**
  8. * Helper class to provide various event functionality.
  9. *
  10. * This class provide an 'on' method to attach a handler. The return is object with an off() method that will disconnect the event handler.
  11. *
  12. * This class provide some gesture support like tap and hold
  13. *
  14. * The class will create an event wrapper that will be provide the same interface to get pointer location whether it is a touch or mouse event
  15. *
  16. */
  17. define(['jquery', 'hammerjs', 'jquery.hammer', '../../../lib/@waca/core-client/js/core-client/utils/EventHelper'], function ($, hammer) {
  18. var isTouch = 'ontouchstart' in window;
  19. var hammerGestures = {};
  20. for (var name in hammer.gestures) {
  21. if (hammer.gestures.hasOwnProperty(name)) {
  22. hammerGestures[hammer.gestures[name].name] = 1;
  23. }
  24. }
  25. var getTouches = function getTouches(e) {
  26. var touches;
  27. if (e.targetTouches) {
  28. touches = e.targetTouches;
  29. } else if (e.originalEvent && e.originalEvent.targetTouches) {
  30. touches = e.originalEvent.targetTouches;
  31. } else if (e.gesture) {
  32. touches = e.gesture.touches;
  33. } else {
  34. touches = null;
  35. }
  36. return touches;
  37. };
  38. return {
  39. // Used to track event handlers that don't get cleaned up.
  40. currentHandlerCount: 0,
  41. /**
  42. * Attach an event handler to a given node.
  43. * This is is a helper method that can be used if we want to attach hammer gestures as well as native dom events
  44. *
  45. *
  46. * @param node
  47. * @param eventName - name of the event e.g. 'mousedown', 'click', etc..
  48. * @param handler - function called when the event is triggered.
  49. * @param options - hammer options.
  50. * @returns - an object with 'off' method that can be used to remove the event handler.
  51. */
  52. on: function on(node, eventName, handler) {
  53. if (!isTouch && this.isTouch(eventName)) {
  54. // When we have a touch event and no touch support, we simply don't register the event
  55. return {
  56. off: function off() {}
  57. };
  58. }
  59. // Make sure to fix the event so that location info for touch and mouse are in the same location in the event
  60. var f = function (evt) {
  61. return handler(this.fixEvent(evt));
  62. }.bind(this);
  63. this._addListener(node, eventName, f);
  64. return {
  65. off: function () {
  66. this._removeListener(node, eventName, f);
  67. }.bind(this)
  68. };
  69. },
  70. fixEvent: function fixEvent(e) {
  71. var event = e;
  72. // get the information form the right place if we have a touch event (native, jquery or hammer)
  73. var touches = getTouches(e);
  74. event.isTouch = touches !== null ? true : false;
  75. var coords = touches && touches.length ? touches[0] : null;
  76. // touchend does not have coordinates
  77. if (coords && coords.clientX !== undefined) {
  78. event.clientX = coords.clientX;
  79. event.clientY = coords.clientY;
  80. event.pageX = coords.pageX;
  81. event.pageY = coords.pageY;
  82. }
  83. return event;
  84. },
  85. _addListener: function _addListener(node, eventName, handler) {
  86. if (handler) {
  87. this.currentHandlerCount++;
  88. $(node).on(eventName, handler);
  89. }
  90. },
  91. _removeListener: function _removeListener(node, eventName, handler) {
  92. if (handler) {
  93. this.currentHandlerCount--;
  94. $(node).off(eventName, handler);
  95. }
  96. },
  97. isTouch: function isTouch(name) {
  98. return name.match(/touch/) || hammerGestures[name];
  99. },
  100. setInlineEdit: function setInlineEdit(sId, fCallback, options) {
  101. var $node = $(sId);
  102. $node.inlineEditor(fCallback, options);
  103. return {
  104. 'off': function off() {
  105. $node.inlineEditor('remove');
  106. },
  107. 'blur': function blur() {
  108. $node.inlineEditor('blur');
  109. },
  110. 'isEditing': function isEditing() {
  111. return $node.inlineEditor('isEditing');
  112. }
  113. };
  114. }
  115. };
  116. });
  117. //# sourceMappingURL=EventHelper.js.map