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.

160 lines
4.7 KiB

  1. var Hammer = require('../../../module/hammer');
  2. var Item = require('./Item');
  3. var RangeItem = require('./RangeItem');
  4. /**
  5. * @constructor BackgroundItem
  6. * @extends Item
  7. * @param {Object} data Object containing parameters start, end
  8. * content, className.
  9. * @param {{toScreen: function, toTime: function}} conversion
  10. * Conversion functions from time to screen and vice versa
  11. * @param {Object} [options] Configuration options
  12. * // TODO: describe options
  13. */
  14. // TODO: implement support for the BackgroundItem just having a start, then being displayed as a sort of an annotation
  15. function BackgroundItem (data, conversion, options) {
  16. this.props = {
  17. content: {
  18. width: 0
  19. }
  20. };
  21. this.overflow = false; // if contents can overflow (css styling), this flag is set to true
  22. // validate data
  23. if (data) {
  24. if (data.start == undefined) {
  25. throw new Error('Property "start" missing in item ' + data.id);
  26. }
  27. if (data.end == undefined) {
  28. throw new Error('Property "end" missing in item ' + data.id);
  29. }
  30. }
  31. this.emptyContent = false;
  32. Item.call(this, data, conversion, options);
  33. }
  34. BackgroundItem.prototype = new Item (null, null, null);
  35. BackgroundItem.prototype.baseClassName = 'item background';
  36. /**
  37. * Check whether this item is visible inside given range
  38. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  39. * @returns {boolean} True if visible
  40. */
  41. BackgroundItem.prototype.isVisible = function(range) {
  42. // determine visibility
  43. return (this.data.start < range.end) && (this.data.end > range.start);
  44. };
  45. /**
  46. * Repaint the item
  47. */
  48. BackgroundItem.prototype.redraw = function() {
  49. var dom = this.dom;
  50. if (!dom) {
  51. // create DOM
  52. this.dom = {};
  53. dom = this.dom;
  54. // background box
  55. dom.box = document.createElement('div');
  56. // className is updated in redraw()
  57. // contents box
  58. dom.content = document.createElement('div');
  59. if (this.data.content === undefined) {
  60. dom.content.className = 'noContent';
  61. this.emptyContent = true;
  62. }
  63. else {
  64. dom.content.className = 'content';
  65. this.emptyContent = false;
  66. }
  67. dom.box.appendChild(dom.content);
  68. // attach this item as attribute
  69. dom.box['timeline-item'] = this;
  70. this.dirty = true;
  71. }
  72. // append DOM to parent DOM
  73. if (!this.parent) {
  74. throw new Error('Cannot redraw item: no parent attached');
  75. }
  76. if (!dom.box.parentNode) {
  77. var background = this.parent.dom.background;
  78. if (!background) {
  79. throw new Error('Cannot redraw time axis: parent has no background container element');
  80. }
  81. background.appendChild(dom.box);
  82. }
  83. this.displayed = true;
  84. // Update DOM when item is marked dirty. An item is marked dirty when:
  85. // - the item is not yet rendered
  86. // - the item's data is changed
  87. // - the item is selected/deselected
  88. if (this.dirty) {
  89. // only change content class when needed
  90. if (this.data.content === undefined && this.emptyContent == false) {
  91. dom.content.className = 'noContent';
  92. this.emptyContent = true;
  93. }
  94. else if (this.data.content !== undefined && this.emptyContent == true) {
  95. dom.content.className = 'content';
  96. this.emptyContent = false;
  97. }
  98. this._updateContents(this.dom.content);
  99. this._updateTitle(this.dom.content);
  100. this._updateDataAttributes(this.dom.content);
  101. // update class
  102. var className = (this.data.className ? (' ' + this.data.className) : '') +
  103. (this.selected ? ' selected' : '');
  104. dom.box.className = this.baseClassName + className;
  105. // determine from css whether this box has overflow
  106. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  107. // recalculate size
  108. this.props.content.width = this.dom.content.offsetWidth;
  109. this.height = 0; // set height zero, so this item will be ignored when stacking items
  110. this.dirty = false;
  111. }
  112. };
  113. /**
  114. * Show the item in the DOM (when not already visible). The items DOM will
  115. * be created when needed.
  116. */
  117. BackgroundItem.prototype.show = RangeItem.prototype.show;
  118. /**
  119. * Hide the item from the DOM (when visible)
  120. * @return {Boolean} changed
  121. */
  122. BackgroundItem.prototype.hide = RangeItem.prototype.hide;
  123. /**
  124. * Reposition the item horizontally
  125. * @Override
  126. */
  127. BackgroundItem.prototype.repositionX = RangeItem.prototype.repositionX;
  128. /**
  129. * Reposition the item vertically
  130. * @Override
  131. */
  132. BackgroundItem.prototype.repositionY = function() {
  133. var onTop = this.options.orientation === 'top';
  134. this.dom.content.style.top = onTop ? '' : '0';
  135. this.dom.content.style.bottom = onTop ? '0' : '';
  136. };
  137. module.exports = BackgroundItem;