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.

151 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. 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. // update class
  87. var className = (this.data.className ? (' ' + this.data.className) : '') +
  88. (this.selected ? ' selected' : '');
  89. dom.box.className = this.baseClassName + className;
  90. // determine from css whether this box has overflow
  91. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  92. // recalculate size
  93. this.props.content.width = this.dom.content.offsetWidth;
  94. this.height = 0; // set height zero, so this item will be ignored when stacking items
  95. this.dirty = false;
  96. }
  97. };
  98. /**
  99. * Show the item in the DOM (when not already visible). The items DOM will
  100. * be created when needed.
  101. */
  102. BackgroundItem.prototype.show = RangeItem.prototype.show;
  103. /**
  104. * Hide the item from the DOM (when visible)
  105. * @return {Boolean} changed
  106. */
  107. BackgroundItem.prototype.hide = RangeItem.prototype.hide;
  108. /**
  109. * Reposition the item horizontally
  110. * @Override
  111. */
  112. BackgroundItem.prototype.repositionX = RangeItem.prototype.repositionX;
  113. /**
  114. * Reposition the item vertically
  115. * @Override
  116. */
  117. BackgroundItem.prototype.repositionY = function() {
  118. var onTop = this.options.orientation === 'top';
  119. this.dom.content.style.top = onTop ? '' : '0';
  120. this.dom.content.style.bottom = onTop ? '0' : '';
  121. var height = Math.max(this.parent.height,
  122. this.parent.itemSet.body.domProps.centerContainer.height);
  123. this.dom.box.style.top = onTop ? '0' : '';
  124. this.dom.box.style.bottom = onTop ? '' : '0';
  125. this.dom.box.style.height = height + 'px';
  126. };
  127. module.exports = BackgroundItem;