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.

89 lines
2.5 KiB

  1. var mousetrap = require('mousetrap');
  2. var Hammer = require('../module/hammer');
  3. var util = require('../util');
  4. /**
  5. * Turn an element into an activatable element.
  6. * When not active, the element has a transparent overlay. When the overlay is
  7. * clicked, the mode is changed to active.
  8. * When active, the element is displayed with a blue border around it, and
  9. * the interactive contents of the element can be used. When clicked outside
  10. * the element, the elements mode is changed to inactive.
  11. * @param {Element} container
  12. * @constructor
  13. */
  14. function Activator(container) {
  15. this.active = false;
  16. this.dom = {
  17. container: container
  18. };
  19. this.dom.overlay = document.createElement('div');
  20. this.dom.overlay.className = 'overlay';
  21. this.dom.container.appendChild(this.dom.overlay);
  22. this.hammer = Hammer(this.dom.overlay, {prevent_default: false});
  23. this.hammer.on('tap', this._onTapOverlay.bind(this));
  24. // attach a tap event to the window, in order to deactivate when clicking outside the timeline
  25. this.windowHammer = Hammer(window, {prevent_default: false});
  26. this.windowHammer.on('tap', this.deactivate.bind(this));
  27. // mousetrap listener only bounded when active)
  28. this.escListener = this.deactivate.bind(this);
  29. }
  30. // The currently active activator
  31. Activator.current = null;
  32. /**
  33. * Destroy the activator. Cleans up all created DOM and event listeners
  34. */
  35. Activator.prototype.destroy = function () {
  36. this.deactivate();
  37. // remove dom
  38. this.dom.overlay.parentNode.removeChild(this.dom.overlay);
  39. // cleanup hammer instances
  40. this.hammer = null;
  41. this.windowHammer = null;
  42. };
  43. /**
  44. * Activate the element
  45. * Overlay is hidden, element is decorated with a blue shadow border
  46. */
  47. Activator.prototype.activate = function () {
  48. // we allow only one active activator at a time
  49. if (Activator.current) {
  50. Activator.current.deactivate();
  51. }
  52. Activator.current = this;
  53. this.active = true;
  54. this.dom.overlay.style.display = 'none';
  55. util.addClassName(this.dom.container, 'vis-active');
  56. mousetrap.bind('esc', this.escListener);
  57. };
  58. /**
  59. * Deactivate the element
  60. * Overlay is displayed on top of the element
  61. */
  62. Activator.prototype.deactivate = function () {
  63. this.active = false;
  64. this.dom.overlay.style.display = '';
  65. util.removeClassName(this.dom.container, 'vis-active');
  66. mousetrap.unbind('esc', this.escListener);
  67. };
  68. Activator.prototype._onTapOverlay = function (event) {
  69. // activate the container
  70. this.activate();
  71. event.stopPropagation();
  72. };
  73. module.exports = Activator;