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.

149 lines
4.4 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. dom.content.className = 'content';
  60. dom.box.appendChild(dom.content);
  61. // attach this item as attribute
  62. dom.box['timeline-item'] = this;
  63. this.dirty = true;
  64. }
  65. // append DOM to parent DOM
  66. if (!this.parent) {
  67. throw new Error('Cannot redraw item: no parent attached');
  68. }
  69. if (!dom.box.parentNode) {
  70. var background = this.parent.dom.background;
  71. if (!background) {
  72. throw new Error('Cannot redraw item: parent has no background container element');
  73. }
  74. background.appendChild(dom.box);
  75. }
  76. this.displayed = true;
  77. // Update DOM when item is marked dirty. An item is marked dirty when:
  78. // - the item is not yet rendered
  79. // - the item's data is changed
  80. // - the item is selected/deselected
  81. if (this.dirty) {
  82. this._updateContents(this.dom.content);
  83. this._updateTitle(this.dom.content);
  84. this._updateDataAttributes(this.dom.content);
  85. // update class
  86. var className = (this.data.className ? (' ' + this.data.className) : '') +
  87. (this.selected ? ' selected' : '');
  88. dom.box.className = this.baseClassName + className;
  89. // determine from css whether this box has overflow
  90. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  91. // recalculate size
  92. this.props.content.width = this.dom.content.offsetWidth;
  93. this.height = 0; // set height zero, so this item will be ignored when stacking items
  94. this.dirty = false;
  95. }
  96. };
  97. /**
  98. * Show the item in the DOM (when not already visible). The items DOM will
  99. * be created when needed.
  100. */
  101. BackgroundItem.prototype.show = RangeItem.prototype.show;
  102. /**
  103. * Hide the item from the DOM (when visible)
  104. * @return {Boolean} changed
  105. */
  106. BackgroundItem.prototype.hide = RangeItem.prototype.hide;
  107. /**
  108. * Reposition the item horizontally
  109. * @Override
  110. */
  111. BackgroundItem.prototype.repositionX = RangeItem.prototype.repositionX;
  112. /**
  113. * Reposition the item vertically
  114. * @Override
  115. */
  116. BackgroundItem.prototype.repositionY = function() {
  117. var onTop = this.options.orientation === 'top';
  118. this.dom.content.style.top = onTop ? '' : '0';
  119. this.dom.content.style.bottom = onTop ? '0' : '';
  120. var height = Math.max(this.parent.height,
  121. this.parent.itemSet.body.domProps.centerContainer.height);
  122. this.dom.box.style.top = onTop ? '0' : '';
  123. this.dom.box.style.bottom = onTop ? '' : '0';
  124. this.dom.box.style.height = height + 'px';
  125. };
  126. module.exports = BackgroundItem;