vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.0 KiB

  1. var keycharm = require('keycharm');
  2. var Emitter = require('emitter-component');
  3. var Hammer = require('../module/hammer');
  4. var util = require('../util');
  5. /**
  6. * Turn an element into an clickToUse element.
  7. * When not active, the element has a transparent overlay. When the overlay is
  8. * clicked, the mode is changed to active.
  9. * When active, the element is displayed with a blue border around it, and
  10. * the interactive contents of the element can be used. When clicked outside
  11. * the element, the elements mode is changed to inactive.
  12. * @param {Element} container
  13. * @constructor
  14. */
  15. function Activator(container) {
  16. this.active = false;
  17. this.dom = {
  18. container: container
  19. };
  20. this.dom.overlay = document.createElement('div');
  21. this.dom.overlay.className = 'overlay';
  22. this.dom.container.appendChild(this.dom.overlay);
  23. this.hammer = Hammer(this.dom.overlay, {prevent_default: false});
  24. this.hammer.on('tap', this._onTapOverlay.bind(this));
  25. // block all touch events (except tap)
  26. var me = this;
  27. var events = [
  28. 'touch', 'pinch',
  29. 'doubletap', 'hold',
  30. 'dragstart', 'drag', 'dragend',
  31. 'mousewheel', 'DOMMouseScroll' // DOMMouseScroll is needed for Firefox
  32. ];
  33. events.forEach(function (event) {
  34. me.hammer.on(event, function (event) {
  35. event.stopPropagation();
  36. });
  37. });
  38. // attach a tap event to the window, in order to deactivate when clicking outside the timeline
  39. this.windowHammer = Hammer(window, {prevent_default: false});
  40. this.windowHammer.on('tap', function (event) {
  41. // deactivate when clicked outside the container
  42. if (!_hasParent(event.target, container)) {
  43. me.deactivate();
  44. }
  45. });
  46. if (this.keycharm !== undefined) {
  47. this.keycharm.destroy();
  48. }
  49. this.keycharm = keycharm();
  50. // keycharm listener only bounded when active)
  51. this.escListener = this.deactivate.bind(this);
  52. }
  53. // turn into an event emitter
  54. Emitter(Activator.prototype);
  55. // The currently active activator
  56. Activator.current = null;
  57. /**
  58. * Destroy the activator. Cleans up all created DOM and event listeners
  59. */
  60. Activator.prototype.destroy = function () {
  61. this.deactivate();
  62. // remove dom
  63. this.dom.overlay.parentNode.removeChild(this.dom.overlay);
  64. // cleanup hammer instances
  65. this.hammer = null;
  66. this.windowHammer = null;
  67. // FIXME: cleaning up hammer instances doesn't work (Timeline not removed from memory)
  68. };
  69. /**
  70. * Activate the element
  71. * Overlay is hidden, element is decorated with a blue shadow border
  72. */
  73. Activator.prototype.activate = function () {
  74. // we allow only one active activator at a time
  75. if (Activator.current) {
  76. Activator.current.deactivate();
  77. }
  78. Activator.current = this;
  79. this.active = true;
  80. this.dom.overlay.style.display = 'none';
  81. util.addClassName(this.dom.container, 'vis-active');
  82. this.emit('change');
  83. this.emit('activate');
  84. // ugly hack: bind ESC after emitting the events, as the Network rebinds all
  85. // keyboard events on a 'change' event
  86. this.keycharm.bind('esc', this.escListener);
  87. };
  88. /**
  89. * Deactivate the element
  90. * Overlay is displayed on top of the element
  91. */
  92. Activator.prototype.deactivate = function () {
  93. this.active = false;
  94. this.dom.overlay.style.display = '';
  95. util.removeClassName(this.dom.container, 'vis-active');
  96. this.keycharm.unbind('esc', this.escListener);
  97. this.emit('change');
  98. this.emit('deactivate');
  99. };
  100. /**
  101. * Handle a tap event: activate the container
  102. * @param event
  103. * @private
  104. */
  105. Activator.prototype._onTapOverlay = function (event) {
  106. // activate the container
  107. this.activate();
  108. event.stopPropagation();
  109. };
  110. /**
  111. * Test whether the element has the requested parent element somewhere in
  112. * its chain of parent nodes.
  113. * @param {HTMLElement} element
  114. * @param {HTMLElement} parent
  115. * @returns {boolean} Returns true when the parent is found somewhere in the
  116. * chain of parent nodes.
  117. * @private
  118. */
  119. function _hasParent(element, parent) {
  120. while (element) {
  121. if (element === parent) {
  122. return true
  123. }
  124. element = element.parentNode;
  125. }
  126. return false;
  127. }
  128. module.exports = Activator;