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.

236 lines
7.1 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. * @param {vis.Range} 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. BackgroundItem.prototype._createDomElement = function() {
  46. if (!this.dom) {
  47. // create DOM
  48. this.dom = {};
  49. // background box
  50. this.dom.box = document.createElement('div');
  51. // className is updated in redraw()
  52. // frame box (to prevent the item contents from overflowing
  53. this.dom.frame = document.createElement('div');
  54. this.dom.frame.className = 'vis-item-overflow';
  55. this.dom.box.appendChild(this.dom.frame);
  56. // contents box
  57. this.dom.content = document.createElement('div');
  58. this.dom.content.className = 'vis-item-content';
  59. this.dom.frame.appendChild(this.dom.content);
  60. // Note: we do NOT attach this item as attribute to the DOM,
  61. // such that background items cannot be selected
  62. //this.dom.box['timeline-item'] = this;
  63. this.dirty = true;
  64. }
  65. }
  66. BackgroundItem.prototype._appendDomElement = function() {
  67. if (!this.parent) {
  68. throw new Error('Cannot redraw item: no parent attached');
  69. }
  70. if (!this.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(this.dom.box);
  76. }
  77. this.displayed = true;
  78. }
  79. BackgroundItem.prototype._updateDirtyDomComponents = function() {
  80. // update dirty DOM. An item is marked dirty when:
  81. // - the item is not yet rendered
  82. // - the item's data is changed
  83. // - the item is selected/deselected
  84. if (this.dirty) {
  85. this._updateContents(this.dom.content);
  86. this._updateDataAttributes(this.dom.content);
  87. this._updateStyle(this.dom.box);
  88. // update class
  89. var className = (this.data.className ? (' ' + this.data.className) : '') +
  90. (this.selected ? ' vis-selected' : '');
  91. this.dom.box.className = this.baseClassName + className;
  92. }
  93. }
  94. BackgroundItem.prototype._getDomComponentsSizes = function() {
  95. // determine from css whether this box has overflow
  96. this.overflow = window.getComputedStyle(this.dom.content).overflow !== 'hidden';
  97. return {
  98. content: {
  99. width: this.dom.content.offsetWidth
  100. }
  101. }
  102. }
  103. BackgroundItem.prototype._updateDomComponentsSizes = function(sizes) {
  104. // recalculate size
  105. this.props.content.width = sizes.content.width;
  106. this.height = 0; // set height zero, so this item will be ignored when stacking items
  107. this.dirty = false;
  108. }
  109. BackgroundItem.prototype._repaintDomAdditionals = function() {
  110. }
  111. /**
  112. * Repaint the item
  113. * @param {boolean} [returnQueue=false] return the queue
  114. * @return {boolean} the redraw result or the redraw queue if returnQueue=true
  115. */
  116. BackgroundItem.prototype.redraw = function(returnQueue) {
  117. var sizes
  118. var queue = [
  119. // create item DOM
  120. this._createDomElement.bind(this),
  121. // append DOM to parent DOM
  122. this._appendDomElement.bind(this),
  123. this._updateDirtyDomComponents.bind(this),
  124. (function() {
  125. if (this.dirty) {
  126. sizes = this._getDomComponentsSizes.bind(this)();
  127. }
  128. }).bind(this),
  129. (function() {
  130. if (this.dirty) {
  131. this._updateDomComponentsSizes.bind(this)(sizes);
  132. }
  133. }).bind(this),
  134. // repaint DOM additionals
  135. this._repaintDomAdditionals.bind(this)
  136. ];
  137. if (returnQueue) {
  138. return queue;
  139. } else {
  140. var result;
  141. queue.forEach(function (fn) {
  142. result = fn();
  143. });
  144. return result;
  145. }
  146. };
  147. /**
  148. * Show the item in the DOM (when not already visible). The items DOM will
  149. * be created when needed.
  150. */
  151. BackgroundItem.prototype.show = RangeItem.prototype.show;
  152. /**
  153. * Hide the item from the DOM (when visible)
  154. * @return {Boolean} changed
  155. */
  156. BackgroundItem.prototype.hide = RangeItem.prototype.hide;
  157. /**
  158. * Reposition the item horizontally
  159. * @Override
  160. */
  161. BackgroundItem.prototype.repositionX = RangeItem.prototype.repositionX;
  162. /**
  163. * Reposition the item vertically
  164. * @Override
  165. */
  166. BackgroundItem.prototype.repositionY = function(margin) { // eslint-disable-line no-unused-vars
  167. var height;
  168. var orientation = this.options.orientation.item;
  169. // special positioning for subgroups
  170. if (this.data.subgroup !== undefined) {
  171. // TODO: instead of calculating the top position of the subgroups here for every BackgroundItem, calculate the top of the subgroup once in Itemset
  172. var itemSubgroup = this.data.subgroup;
  173. this.dom.box.style.height = this.parent.subgroups[itemSubgroup].height + 'px';
  174. if (orientation == 'top') {
  175. this.dom.box.style.top = this.parent.top + this.parent.subgroups[itemSubgroup].top + 'px';
  176. } else {
  177. this.dom.box.style.top = (this.parent.top + this.parent.height - this.parent.subgroups[itemSubgroup].top - this.parent.subgroups[itemSubgroup].height) + 'px';
  178. }
  179. this.dom.box.style.bottom = '';
  180. }
  181. // and in the case of no subgroups:
  182. else {
  183. // we want backgrounds with groups to only show in groups.
  184. if (this.parent instanceof BackgroundGroup) {
  185. // if the item is not in a group:
  186. height = Math.max(this.parent.height,
  187. this.parent.itemSet.body.domProps.center.height,
  188. this.parent.itemSet.body.domProps.centerContainer.height);
  189. this.dom.box.style.bottom = orientation == 'bottom' ? '0' : '';
  190. this.dom.box.style.top = orientation == 'top' ? '0' : '';
  191. }
  192. else {
  193. height = this.parent.height;
  194. // same alignment for items when orientation is top or bottom
  195. this.dom.box.style.top = this.parent.top + 'px';
  196. this.dom.box.style.bottom = '';
  197. }
  198. }
  199. this.dom.box.style.height = height + 'px';
  200. };
  201. module.exports = BackgroundItem;