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.

152 lines
4.5 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. Item.call(this, data, conversion, options);
  32. this.ignoreStacking = true; // this is not used when stacking
  33. this.emptyContent = false;
  34. }
  35. BackgroundItem.prototype = new Item (null, null, null);
  36. BackgroundItem.prototype.baseClassName = 'item background';
  37. /**
  38. * Check whether this item is visible inside given range
  39. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  40. * @returns {boolean} True if visible
  41. */
  42. BackgroundItem.prototype.isVisible = function(range) {
  43. // determine visibility
  44. return (this.data.start < range.end) && (this.data.end > range.start);
  45. };
  46. /**
  47. * Repaint the item
  48. */
  49. BackgroundItem.prototype.redraw = function() {
  50. var dom = this.dom;
  51. if (!dom) {
  52. // create DOM
  53. this.dom = {};
  54. dom = this.dom;
  55. // background box
  56. dom.box = document.createElement('div');
  57. // className is updated in redraw()
  58. // contents box
  59. dom.content = document.createElement('div');
  60. dom.content.className = 'content';
  61. dom.box.appendChild(dom.content);
  62. // attach this item as attribute
  63. dom.box['timeline-item'] = this;
  64. this.dirty = true;
  65. }
  66. // append DOM to parent DOM
  67. if (!this.parent) {
  68. throw new Error('Cannot redraw item: no parent attached');
  69. }
  70. if (!dom.box.parentNode) {
  71. var background = this.parent.dom.background;
  72. if (!background) {
  73. throw new Error('Cannot redraw item: parent has no background container element');
  74. }
  75. background.appendChild(dom.box);
  76. }
  77. this.displayed = true;
  78. // Update DOM when item is marked dirty. An item is marked dirty when:
  79. // - the item is not yet rendered
  80. // - the item's data is changed
  81. // - the item is selected/deselected
  82. if (this.dirty) {
  83. this._updateContents(this.dom.content);
  84. this._updateTitle(this.dom.content);
  85. this._updateDataAttributes(this.dom.content);
  86. this._updateStyle(this.dom.box);
  87. // update class
  88. var className = (this.data.className ? (' ' + this.data.className) : '') +
  89. (this.selected ? ' selected' : '');
  90. dom.box.className = this.baseClassName + className;
  91. // determine from css whether this box has overflow
  92. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  93. // recalculate size
  94. this.props.content.width = this.dom.content.offsetWidth;
  95. this.height = 0; // set height zero, so this item will be ignored when stacking items
  96. this.dirty = false;
  97. }
  98. };
  99. /**
  100. * Show the item in the DOM (when not already visible). The items DOM will
  101. * be created when needed.
  102. */
  103. BackgroundItem.prototype.show = RangeItem.prototype.show;
  104. /**
  105. * Hide the item from the DOM (when visible)
  106. * @return {Boolean} changed
  107. */
  108. BackgroundItem.prototype.hide = RangeItem.prototype.hide;
  109. /**
  110. * Reposition the item horizontally
  111. * @Override
  112. */
  113. BackgroundItem.prototype.repositionX = RangeItem.prototype.repositionX;
  114. /**
  115. * Reposition the item vertically
  116. * @Override
  117. */
  118. BackgroundItem.prototype.repositionY = function() {
  119. var onTop = this.options.orientation === 'top';
  120. this.dom.content.style.top = onTop ? '' : '0';
  121. this.dom.content.style.bottom = onTop ? '0' : '';
  122. var height = Math.max(this.parent.height,
  123. this.parent.itemSet.body.domProps.centerContainer.height);
  124. this.dom.box.style.top = onTop ? '0' : '';
  125. this.dom.box.style.bottom = onTop ? '' : '0';
  126. this.dom.box.style.height = height + 'px';
  127. };
  128. module.exports = BackgroundItem;