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.

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