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.

139 lines
3.2 KiB

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