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.

143 lines
3.3 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. var Hammer = require('hammerjs');
  2. /**
  3. * @constructor Item
  4. * @param {Object} data Object containing (optional) parameters type,
  5. * start, end, content, group, className.
  6. * @param {{toScreen: function, toTime: function}} conversion
  7. * Conversion functions from time to screen and vice versa
  8. * @param {Object} options Configuration options
  9. * // TODO: describe available options
  10. */
  11. function Item (data, conversion, options) {
  12. this.id = null;
  13. this.parent = null;
  14. this.data = data;
  15. this.dom = null;
  16. this.conversion = conversion || {};
  17. this.options = options || {};
  18. this.selected = false;
  19. this.displayed = false;
  20. this.dirty = true;
  21. this.top = null;
  22. this.left = null;
  23. this.width = null;
  24. this.height = null;
  25. }
  26. /**
  27. * Select current item
  28. */
  29. Item.prototype.select = function() {
  30. this.selected = true;
  31. if (this.displayed) this.redraw();
  32. };
  33. /**
  34. * Unselect current item
  35. */
  36. Item.prototype.unselect = function() {
  37. this.selected = false;
  38. if (this.displayed) this.redraw();
  39. };
  40. /**
  41. * Set a parent for the item
  42. * @param {ItemSet | Group} parent
  43. */
  44. Item.prototype.setParent = function(parent) {
  45. if (this.displayed) {
  46. this.hide();
  47. this.parent = parent;
  48. if (this.parent) {
  49. this.show();
  50. }
  51. }
  52. else {
  53. this.parent = parent;
  54. }
  55. };
  56. /**
  57. * Check whether this item is visible inside given range
  58. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  59. * @returns {boolean} True if visible
  60. */
  61. Item.prototype.isVisible = function(range) {
  62. // Should be implemented by Item implementations
  63. return false;
  64. };
  65. /**
  66. * Show the Item in the DOM (when not already visible)
  67. * @return {Boolean} changed
  68. */
  69. Item.prototype.show = function() {
  70. return false;
  71. };
  72. /**
  73. * Hide the Item from the DOM (when visible)
  74. * @return {Boolean} changed
  75. */
  76. Item.prototype.hide = function() {
  77. return false;
  78. };
  79. /**
  80. * Repaint the item
  81. */
  82. Item.prototype.redraw = function() {
  83. // should be implemented by the item
  84. };
  85. /**
  86. * Reposition the Item horizontally
  87. */
  88. Item.prototype.repositionX = function() {
  89. // should be implemented by the item
  90. };
  91. /**
  92. * Reposition the Item vertically
  93. */
  94. Item.prototype.repositionY = function() {
  95. // should be implemented by the item
  96. };
  97. /**
  98. * Repaint a delete button on the top right of the item when the item is selected
  99. * @param {HTMLElement} anchor
  100. * @protected
  101. */
  102. Item.prototype._repaintDeleteButton = function (anchor) {
  103. if (this.selected && this.options.editable.remove && !this.dom.deleteButton) {
  104. // create and show button
  105. var me = this;
  106. var deleteButton = document.createElement('div');
  107. deleteButton.className = 'delete';
  108. deleteButton.title = 'Delete this item';
  109. Hammer(deleteButton, {
  110. preventDefault: true
  111. }).on('tap', function (event) {
  112. me.parent.removeFromDataSet(me);
  113. event.stopPropagation();
  114. });
  115. anchor.appendChild(deleteButton);
  116. this.dom.deleteButton = deleteButton;
  117. }
  118. else if (!this.selected && this.dom.deleteButton) {
  119. // remove button
  120. if (this.dom.deleteButton.parentNode) {
  121. this.dom.deleteButton.parentNode.removeChild(this.dom.deleteButton);
  122. }
  123. this.dom.deleteButton = null;
  124. }
  125. };
  126. module.exports = Item;