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
6.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. var Item = require('./Item');
  2. var util = require('../../../util');
  3. /**
  4. * @constructor BoxItem
  5. * @extends Item
  6. * @param {Object} data Object containing parameters start
  7. * content, className.
  8. * @param {{toScreen: function, toTime: function}} conversion
  9. * Conversion functions from time to screen and vice versa
  10. * @param {Object} [options] Configuration options
  11. * // TODO: describe available options
  12. */
  13. function BoxItem (data, conversion, options) {
  14. this.props = {
  15. dot: {
  16. width: 0,
  17. height: 0
  18. },
  19. line: {
  20. width: 0,
  21. height: 0
  22. }
  23. };
  24. // validate data
  25. if (data) {
  26. if (data.start == undefined) {
  27. throw new Error('Property "start" missing in item ' + data);
  28. }
  29. }
  30. Item.call(this, data, conversion, options);
  31. }
  32. BoxItem.prototype = new Item (null, null, null);
  33. /**
  34. * Check whether this item is visible inside given range
  35. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  36. * @returns {boolean} True if visible
  37. */
  38. BoxItem.prototype.isVisible = function(range) {
  39. // determine visibility
  40. // TODO: account for the real width of the item. Right now we just add 1/4 to the window
  41. var interval = (range.end - range.start) / 4;
  42. return (this.data.start > range.start - interval) && (this.data.start < range.end + interval);
  43. };
  44. /**
  45. * Repaint the item
  46. */
  47. BoxItem.prototype.redraw = function() {
  48. var dom = this.dom;
  49. if (!dom) {
  50. // create DOM
  51. this.dom = {};
  52. dom = this.dom;
  53. // create main box
  54. dom.box = document.createElement('DIV');
  55. // contents box (inside the background box). used for making margins
  56. dom.content = document.createElement('DIV');
  57. dom.content.className = 'content';
  58. dom.box.appendChild(dom.content);
  59. // line to axis
  60. dom.line = document.createElement('DIV');
  61. dom.line.className = 'line';
  62. // dot on axis
  63. dom.dot = document.createElement('DIV');
  64. dom.dot.className = 'dot';
  65. // attach this item as attribute
  66. dom.box['timeline-item'] = this;
  67. this.dirty = true;
  68. }
  69. // append DOM to parent DOM
  70. if (!this.parent) {
  71. throw new Error('Cannot redraw item: no parent attached');
  72. }
  73. if (!dom.box.parentNode) {
  74. var foreground = this.parent.dom.foreground;
  75. if (!foreground) throw new Error('Cannot redraw item: parent has no foreground container element');
  76. foreground.appendChild(dom.box);
  77. }
  78. if (!dom.line.parentNode) {
  79. var background = this.parent.dom.background;
  80. if (!background) throw new Error('Cannot redraw item: parent has no background container element');
  81. background.appendChild(dom.line);
  82. }
  83. if (!dom.dot.parentNode) {
  84. var axis = this.parent.dom.axis;
  85. if (!background) throw new Error('Cannot redraw item: parent has no axis container element');
  86. axis.appendChild(dom.dot);
  87. }
  88. this.displayed = true;
  89. // Update DOM when item is marked dirty. An item is marked dirty when:
  90. // - the item is not yet rendered
  91. // - the item's data is changed
  92. // - the item is selected/deselected
  93. if (this.dirty) {
  94. this._updateContents(this.dom.content);
  95. this._updateTitle(this.dom.box);
  96. this._updateDataAttributes(this.dom.box);
  97. this._updateStyle(this.dom.box);
  98. // update class
  99. var className = (this.data.className? ' ' + this.data.className : '') +
  100. (this.selected ? ' selected' : '');
  101. dom.box.className = 'item box' + className;
  102. dom.line.className = 'item line' + className;
  103. dom.dot.className = 'item dot' + className;
  104. // recalculate size
  105. this.props.dot.height = dom.dot.offsetHeight;
  106. this.props.dot.width = dom.dot.offsetWidth;
  107. this.props.line.width = dom.line.offsetWidth;
  108. this.width = dom.box.offsetWidth;
  109. this.height = dom.box.offsetHeight;
  110. this.dirty = false;
  111. }
  112. this._repaintDeleteButton(dom.box);
  113. };
  114. /**
  115. * Show the item in the DOM (when not already displayed). The items DOM will
  116. * be created when needed.
  117. */
  118. BoxItem.prototype.show = function() {
  119. if (!this.displayed) {
  120. this.redraw();
  121. }
  122. };
  123. /**
  124. * Hide the item from the DOM (when visible)
  125. */
  126. BoxItem.prototype.hide = function() {
  127. if (this.displayed) {
  128. var dom = this.dom;
  129. if (dom.box.parentNode) dom.box.parentNode.removeChild(dom.box);
  130. if (dom.line.parentNode) dom.line.parentNode.removeChild(dom.line);
  131. if (dom.dot.parentNode) dom.dot.parentNode.removeChild(dom.dot);
  132. this.displayed = false;
  133. }
  134. };
  135. /**
  136. * Reposition the item horizontally
  137. * @Override
  138. */
  139. BoxItem.prototype.repositionX = function() {
  140. var start = this.conversion.toScreen(this.data.start);
  141. var align = this.options.align;
  142. var left;
  143. // calculate left position of the box
  144. if (align == 'right') {
  145. this.left = start - this.width;
  146. }
  147. else if (align == 'left') {
  148. this.left = start;
  149. }
  150. else {
  151. // default or 'center'
  152. this.left = start - this.width / 2;
  153. }
  154. // reposition box
  155. this.dom.box.style.left = this.left + 'px';
  156. // reposition line
  157. this.dom.line.style.left = (start - this.props.line.width / 2) + 'px';
  158. // reposition dot
  159. this.dom.dot.style.left = (start - this.props.dot.width / 2) + 'px';
  160. };
  161. /**
  162. * Reposition the item vertically
  163. * @Override
  164. */
  165. BoxItem.prototype.repositionY = function() {
  166. var orientation = this.options.orientation;
  167. var box = this.dom.box;
  168. var line = this.dom.line;
  169. var dot = this.dom.dot;
  170. if (orientation == 'top') {
  171. box.style.top = (this.top || 0) + 'px';
  172. line.style.top = '0';
  173. line.style.height = (this.parent.top + this.top + 1) + 'px';
  174. line.style.bottom = '';
  175. }
  176. else { // orientation 'bottom'
  177. var itemSetHeight = this.parent.itemSet.props.height; // TODO: this is nasty
  178. var lineHeight = itemSetHeight - this.parent.top - this.parent.height + this.top;
  179. box.style.top = (this.parent.height - this.top - this.height || 0) + 'px';
  180. line.style.top = (itemSetHeight - lineHeight) + 'px';
  181. line.style.bottom = '0';
  182. }
  183. dot.style.top = (-this.props.dot.height / 2) + 'px';
  184. };
  185. module.exports = BoxItem;