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.

150 lines
3.9 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 = 'vis-overlay';
  22. this.dom.container.appendChild(this.dom.overlay);
  23. this.hammer = Hammer(this.dom.overlay);
  24. this.hammer.on('tap', this._onTapOverlay.bind(this));
  25. // block all touch events (except tap)
  26. var me = this;
  27. var events = [
  28. 'tap', 'doubletap', 'press',
  29. 'pinch',
  30. 'pan', 'panstart', 'panmove', 'panend'
  31. ];
  32. events.forEach(function (event) {
  33. me.hammer.on(event, function (event) {
  34. event.stopPropagation();
  35. });
  36. });
  37. // attach a tap event to the window, in order to deactivate when clicking outside the timeline
  38. this.bodyHammer = Hammer(document && document.body, {prevent_default: false});
  39. this.bodyHammer.on('tap', function (event) {
  40. // deactivate when clicked outside the container
  41. if (!_hasParent(event.target, container)) {
  42. me.deactivate();
  43. }
  44. });
  45. if (this.keycharm !== undefined) {
  46. this.keycharm.destroy();
  47. }
  48. this.keycharm = keycharm();
  49. // keycharm listener only bounded when active)
  50. this.escListener = this.deactivate.bind(this);
  51. }
  52. // turn into an event emitter
  53. Emitter(Activator.prototype);
  54. // The currently active activator
  55. Activator.current = null;
  56. /**
  57. * Destroy the activator. Cleans up all created DOM and event listeners
  58. */
  59. Activator.prototype.destroy = function () {
  60. this.deactivate();
  61. // remove dom
  62. this.dom.overlay.parentNode.removeChild(this.dom.overlay);
  63. // cleanup hammer instances
  64. this.hammer = null;
  65. this.bodyHammer = null;
  66. // FIXME: cleaning up hammer instances doesn't work (Timeline not removed from memory)
  67. };
  68. /**
  69. * Activate the element
  70. * Overlay is hidden, element is decorated with a blue shadow border
  71. */
  72. Activator.prototype.activate = function () {
  73. // we allow only one active activator at a time
  74. if (Activator.current) {
  75. Activator.current.deactivate();
  76. }
  77. Activator.current = this;
  78. this.active = true;
  79. this.dom.overlay.style.display = 'none';
  80. util.addClassName(this.dom.container, 'vis-active');
  81. this.emit('change');
  82. this.emit('activate');
  83. // ugly hack: bind ESC after emitting the events, as the Network rebinds all
  84. // keyboard events on a 'change' event
  85. this.keycharm.bind('esc', this.escListener);
  86. };
  87. /**
  88. * Deactivate the element
  89. * Overlay is displayed on top of the element
  90. */
  91. Activator.prototype.deactivate = function () {
  92. this.active = false;
  93. this.dom.overlay.style.display = '';
  94. util.removeClassName(this.dom.container, 'vis-active');
  95. this.keycharm.unbind('esc', this.escListener);
  96. this.emit('change');
  97. this.emit('deactivate');
  98. };
  99. /**
  100. * Handle a tap event: activate the container
  101. * @param event
  102. * @private
  103. */
  104. Activator.prototype._onTapOverlay = function (event) {
  105. // activate the container
  106. this.activate();
  107. event.stopPropagation();
  108. };
  109. /**
  110. * Test whether the element has the requested parent element somewhere in
  111. * its chain of parent nodes.
  112. * @param {HTMLElement} element
  113. * @param {HTMLElement} parent
  114. * @returns {boolean} Returns true when the parent is found somewhere in the
  115. * chain of parent nodes.
  116. * @private
  117. */
  118. function _hasParent(element, parent) {
  119. while (element) {
  120. if (element === parent) {
  121. return true
  122. }
  123. element = element.parentNode;
  124. }
  125. return false;
  126. }
  127. module.exports = Activator;