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.

182 lines
5.9 KiB

  1. var Item = require('./Item');
  2. var BackgroundGroup = require('../BackgroundGroup');
  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. * // TODO: implement support for the BackgroundItem just having a start, then being displayed as a sort of an annotation
  14. */
  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 = 'vis-item vis-background';
  35. BackgroundItem.prototype.stack = false;
  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. // frame box (to prevent the item contents from overflowing
  58. dom.frame = document.createElement('div');
  59. dom.frame.className = 'vis-item-overflow';
  60. dom.box.appendChild(dom.frame);
  61. // contents box
  62. dom.content = document.createElement('div');
  63. dom.content.className = 'vis-item-content';
  64. dom.frame.appendChild(dom.content);
  65. // Note: we do NOT attach this item as attribute to the DOM,
  66. // such that background items cannot be selected
  67. //dom.box['timeline-item'] = this;
  68. this.dirty = true;
  69. }
  70. // append DOM to parent DOM
  71. if (!this.parent) {
  72. throw new Error('Cannot redraw item: no parent attached');
  73. }
  74. if (!dom.box.parentNode) {
  75. var background = this.parent.dom.background;
  76. if (!background) {
  77. throw new Error('Cannot redraw item: parent has no background container element');
  78. }
  79. background.appendChild(dom.box);
  80. }
  81. this.displayed = true;
  82. // Update DOM when item is marked dirty. An item is marked dirty when:
  83. // - the item is not yet rendered
  84. // - the item's data is changed
  85. // - the item is selected/deselected
  86. if (this.dirty) {
  87. this._updateContents(this.dom.content);
  88. this._updateDataAttributes(this.dom.content);
  89. this._updateStyle(this.dom.box);
  90. // update class
  91. var className = (this.data.className ? (' ' + this.data.className) : '') +
  92. (this.selected ? ' vis-selected' : '');
  93. dom.box.className = this.baseClassName + className;
  94. // determine from css whether this box has overflow
  95. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  96. // recalculate size
  97. this.props.content.width = this.dom.content.offsetWidth;
  98. this.height = 0; // set height zero, so this item will be ignored when stacking items
  99. this.dirty = false;
  100. }
  101. };
  102. /**
  103. * Show the item in the DOM (when not already visible). The items DOM will
  104. * be created when needed.
  105. */
  106. BackgroundItem.prototype.show = RangeItem.prototype.show;
  107. /**
  108. * Hide the item from the DOM (when visible)
  109. * @return {Boolean} changed
  110. */
  111. BackgroundItem.prototype.hide = RangeItem.prototype.hide;
  112. /**
  113. * Reposition the item horizontally
  114. * @Override
  115. */
  116. BackgroundItem.prototype.repositionX = RangeItem.prototype.repositionX;
  117. /**
  118. * Reposition the item vertically
  119. * @Override
  120. */
  121. BackgroundItem.prototype.repositionY = function(margin) { // eslint-disable-line no-unused-vars
  122. var height;
  123. var orientation = this.options.orientation.item;
  124. // special positioning for subgroups
  125. if (this.data.subgroup !== undefined) {
  126. // TODO: instead of calculating the top position of the subgroups here for every BackgroundItem, calculate the top of the subgroup once in Itemset
  127. var itemSubgroup = this.data.subgroup;
  128. this.dom.box.style.height = this.parent.subgroups[itemSubgroup].height + 'px';
  129. if (orientation == 'top') {
  130. this.dom.box.style.top = this.parent.top + this.parent.subgroups[itemSubgroup].top + 'px';
  131. } else {
  132. this.dom.box.style.top = (this.parent.top + this.parent.height - this.parent.subgroups[itemSubgroup].top - this.parent.subgroups[itemSubgroup].height) + 'px';
  133. }
  134. this.dom.box.style.bottom = '';
  135. }
  136. // and in the case of no subgroups:
  137. else {
  138. // we want backgrounds with groups to only show in groups.
  139. if (this.parent instanceof BackgroundGroup) {
  140. // if the item is not in a group:
  141. height = Math.max(this.parent.height,
  142. this.parent.itemSet.body.domProps.center.height,
  143. this.parent.itemSet.body.domProps.centerContainer.height);
  144. this.dom.box.style.bottom = orientation == 'bottom' ? '0' : '';
  145. this.dom.box.style.top = orientation == 'top' ? '0' : '';
  146. }
  147. else {
  148. height = this.parent.height;
  149. // same alignment for items when orientation is top or bottom
  150. this.dom.box.style.top = this.parent.top + 'px';
  151. this.dom.box.style.bottom = '';
  152. }
  153. }
  154. this.dom.box.style.height = height + 'px';
  155. };
  156. module.exports = BackgroundItem;