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.

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