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.

203 lines
6.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(margin) {
  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;
  123. // special positioning for subgroups
  124. if (this.data.subgroup !== undefined) {
  125. var subgroup = this.data.subgroup;
  126. var subgroups = this.parent.subgroups;
  127. // if the orientation is top, we need to take the difference in height into account.
  128. if (onTop == true) {
  129. // the first subgroup will have to account for the distance from the top to the first item.
  130. height = this.parent.subgroups[subgroup].height + margin.item.vertical;
  131. height += subgroup == 0 ? margin.axis - 0.5*margin.item.vertical : 0;
  132. var newTop = this.parent.top;
  133. for (var subgroupIdx in subgroups) {
  134. if (subgroups.hasOwnProperty(subgroupIdx)) {
  135. if (subgroups[subgroupIdx].visible == true && subgroupIdx < subgroup) {
  136. newTop += subgroups[subgroupIdx].height + margin.item.vertical;
  137. }
  138. }
  139. }
  140. // the others will have to be offset downwards with this same distance.
  141. newTop += subgroup != 0 ? margin.axis - 0.5 * margin.item.vertical : 0;
  142. this.dom.box.style.top = newTop + 'px';
  143. this.dom.box.style.bottom = '';
  144. }
  145. // and when the orientation is bottom:
  146. else {
  147. var newTop = this.parent.top;
  148. for (var subgroupIdx in subgroups) {
  149. if (subgroups.hasOwnProperty(subgroupIdx)) {
  150. if (subgroups[subgroupIdx].visible == true && subgroupIdx > subgroup) {
  151. newTop += subgroups[subgroupIdx].height + margin.item.vertical;
  152. }
  153. }
  154. }
  155. height = this.parent.subgroups[subgroup].height + margin.item.vertical;
  156. this.dom.box.style.top = newTop + 'px';
  157. this.dom.box.style.bottom = '';
  158. }
  159. }
  160. // and in the case of no subgroups:
  161. else {
  162. // we want backgrounds with groups to only show in groups.
  163. if (this.data.group !== undefined) {
  164. height = this.parent.height;
  165. // same alignment for items when orientation is top or bottom
  166. this.dom.box.style.top = this.parent.top + 'px';
  167. this.dom.box.style.bottom = '';
  168. }
  169. else {
  170. // if the item is not in a group:
  171. height = Math.max(this.parent.height, this.parent.itemSet.body.domProps.centerContainer.height);
  172. this.dom.box.style.top = onTop ? '0' : '';
  173. this.dom.box.style.bottom = onTop ? '' : '0';
  174. }
  175. }
  176. this.dom.box.style.height = height + 'px';
  177. };
  178. module.exports = BackgroundItem;