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.

218 lines
7.3 KiB

  1. var Hammer = require('../../../module/hammer');
  2. var Item = require('./Item');
  3. var BackgroundGroup = require('../BackgroundGroup');
  4. var RangeItem = require('./RangeItem');
  5. /**
  6. * @constructor BackgroundItem
  7. * @extends Item
  8. * @param {Object} data Object containing parameters start, end
  9. * content, className.
  10. * @param {{toScreen: function, toTime: function}} conversion
  11. * Conversion functions from time to screen and vice versa
  12. * @param {Object} [options] Configuration options
  13. * // TODO: describe options
  14. */
  15. // TODO: implement support for the BackgroundItem just having a start, then being displayed as a sort of an annotation
  16. function BackgroundItem (data, conversion, options) {
  17. this.props = {
  18. content: {
  19. width: 0
  20. }
  21. };
  22. this.overflow = false; // if contents can overflow (css styling), this flag is set to true
  23. // validate data
  24. if (data) {
  25. if (data.start == undefined) {
  26. throw new Error('Property "start" missing in item ' + data.id);
  27. }
  28. if (data.end == undefined) {
  29. throw new Error('Property "end" missing in item ' + data.id);
  30. }
  31. }
  32. Item.call(this, data, conversion, options);
  33. }
  34. BackgroundItem.prototype = new Item (null, null, null);
  35. BackgroundItem.prototype.baseClassName = 'vis-item vis-background';
  36. BackgroundItem.prototype.stack = false;
  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. // frame box (to prevent the item contents from overflowing
  59. dom.frame = document.createElement('div');
  60. dom.frame.className = 'vis-item-frame';
  61. dom.box.appendChild(dom.frame);
  62. // contents box
  63. dom.content = document.createElement('div');
  64. dom.content.className = 'vis-item-content';
  65. dom.frame.appendChild(dom.content);
  66. // Note: we do NOT attach this item as attribute to the DOM,
  67. // such that background items cannot be selected
  68. //dom.box['timeline-item'] = this;
  69. this.dirty = true;
  70. }
  71. // append DOM to parent DOM
  72. if (!this.parent) {
  73. throw new Error('Cannot redraw item: no parent attached');
  74. }
  75. if (!dom.box.parentNode) {
  76. var background = this.parent.dom.background;
  77. if (!background) {
  78. throw new Error('Cannot redraw item: parent has no background container element');
  79. }
  80. background.appendChild(dom.box);
  81. }
  82. this.displayed = true;
  83. // Update DOM when item is marked dirty. An item is marked dirty when:
  84. // - the item is not yet rendered
  85. // - the item's data is changed
  86. // - the item is selected/deselected
  87. if (this.dirty) {
  88. this._updateContents(this.dom.content);
  89. this._updateTitle(this.dom.content);
  90. this._updateDataAttributes(this.dom.content);
  91. this._updateStyle(this.dom.box);
  92. // update class
  93. var className = (this.data.className ? (' ' + this.data.className) : '') +
  94. (this.selected ? ' vis-selected' : '');
  95. dom.box.className = this.baseClassName + className;
  96. // determine from css whether this box has overflow
  97. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  98. // recalculate size
  99. this.props.content.width = this.dom.content.offsetWidth;
  100. this.height = 0; // set height zero, so this item will be ignored when stacking items
  101. this.dirty = false;
  102. }
  103. };
  104. /**
  105. * Show the item in the DOM (when not already visible). The items DOM will
  106. * be created when needed.
  107. */
  108. BackgroundItem.prototype.show = RangeItem.prototype.show;
  109. /**
  110. * Hide the item from the DOM (when visible)
  111. * @return {Boolean} changed
  112. */
  113. BackgroundItem.prototype.hide = RangeItem.prototype.hide;
  114. /**
  115. * Reposition the item horizontally
  116. * @Override
  117. */
  118. BackgroundItem.prototype.repositionX = RangeItem.prototype.repositionX;
  119. /**
  120. * Reposition the item vertically
  121. * @Override
  122. */
  123. BackgroundItem.prototype.repositionY = function(margin) {
  124. var onTop = this.options.orientation === 'top';
  125. this.dom.content.style.top = onTop ? '' : '0';
  126. this.dom.content.style.bottom = onTop ? '0' : '';
  127. var height;
  128. // special positioning for subgroups
  129. if (this.data.subgroup !== undefined) {
  130. // TODO: instead of calculating the top position of the subgroups here for every BackgroundItem, calculate the top of the subgroup once in Itemset
  131. var itemSubgroup = this.data.subgroup;
  132. var subgroups = this.parent.subgroups;
  133. var subgroupIndex = subgroups[itemSubgroup].index;
  134. // if the orientation is top, we need to take the difference in height into account.
  135. if (onTop == true) {
  136. // the first subgroup will have to account for the distance from the top to the first item.
  137. height = this.parent.subgroups[itemSubgroup].height + margin.item.vertical;
  138. height += subgroupIndex == 0 ? margin.axis - 0.5*margin.item.vertical : 0;
  139. var newTop = this.parent.top;
  140. for (var subgroup in subgroups) {
  141. if (subgroups.hasOwnProperty(subgroup)) {
  142. if (subgroups[subgroup].visible == true && subgroups[subgroup].index < subgroupIndex) {
  143. newTop += subgroups[subgroup].height + margin.item.vertical;
  144. }
  145. }
  146. }
  147. // the others will have to be offset downwards with this same distance.
  148. newTop += subgroupIndex != 0 ? margin.axis - 0.5 * margin.item.vertical : 0;
  149. this.dom.box.style.top = newTop + 'px';
  150. this.dom.box.style.bottom = '';
  151. }
  152. // and when the orientation is bottom:
  153. else {
  154. var newTop = this.parent.top;
  155. var totalHeight = 0;
  156. for (var subgroup in subgroups) {
  157. if (subgroups.hasOwnProperty(subgroup)) {
  158. if (subgroups[subgroup].visible == true) {
  159. var newHeight = subgroups[subgroup].height + margin.item.vertical;
  160. totalHeight += newHeight;
  161. if (subgroups[subgroup].index > subgroupIndex) {
  162. newTop += newHeight;
  163. }
  164. }
  165. }
  166. }
  167. height = this.parent.subgroups[itemSubgroup].height + margin.item.vertical;
  168. this.dom.box.style.top = (this.parent.height - totalHeight + newTop) + 'px';
  169. this.dom.box.style.bottom = '';
  170. }
  171. }
  172. // and in the case of no subgroups:
  173. else {
  174. // we want backgrounds with groups to only show in groups.
  175. if (this.parent instanceof BackgroundGroup) {
  176. // if the item is not in a group:
  177. height = Math.max(this.parent.height,
  178. this.parent.itemSet.body.domProps.center.height,
  179. this.parent.itemSet.body.domProps.centerContainer.height);
  180. this.dom.box.style.top = onTop ? '0' : '';
  181. this.dom.box.style.bottom = onTop ? '' : '0';
  182. }
  183. else {
  184. height = this.parent.height;
  185. // same alignment for items when orientation is top or bottom
  186. this.dom.box.style.top = this.parent.top + 'px';
  187. this.dom.box.style.bottom = '';
  188. }
  189. }
  190. this.dom.box.style.height = height + 'px';
  191. };
  192. module.exports = BackgroundItem;