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.

224 lines
6.1 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 = 'vis-item-content';
  58. dom.box.appendChild(dom.content);
  59. // line to axis
  60. dom.line = document.createElement('DIV');
  61. dom.line.className = 'vis-line';
  62. // dot on axis
  63. dom.dot = document.createElement('DIV');
  64. dom.dot.className = 'vis-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 ? ' vis-selected' : '');
  101. dom.box.className = 'vis-item vis-box' + className;
  102. dom.line.className = 'vis-item vis-line' + className;
  103. dom.dot.className = 'vis-item vis-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.top = null;
  133. this.left = null;
  134. this.displayed = false;
  135. }
  136. };
  137. /**
  138. * Reposition the item horizontally
  139. * @Override
  140. */
  141. BoxItem.prototype.repositionX = function() {
  142. var start = this.conversion.toScreen(this.data.start);
  143. var align = this.options.align;
  144. var left;
  145. var box = this.dom.box;
  146. var line = this.dom.line;
  147. var dot = this.dom.dot;
  148. // calculate left position of the box
  149. if (align == 'right') {
  150. this.left = start - this.width;
  151. }
  152. else if (align == 'left') {
  153. this.left = start;
  154. }
  155. else {
  156. // default or 'center'
  157. this.left = start - this.width / 2;
  158. }
  159. // reposition box
  160. box.style.left = this.left + 'px';
  161. // reposition line
  162. line.style.left = (start - this.props.line.width / 2) + 'px';
  163. // reposition dot
  164. dot.style.left = (start - this.props.dot.width / 2) + 'px';
  165. };
  166. /**
  167. * Reposition the item vertically
  168. * @Override
  169. */
  170. BoxItem.prototype.repositionY = function() {
  171. var orientation = this.options.orientation;
  172. var box = this.dom.box;
  173. var line = this.dom.line;
  174. var dot = this.dom.dot;
  175. if (orientation == 'top') {
  176. box.style.top = (this.top || 0) + 'px';
  177. line.style.top = '0';
  178. line.style.height = (this.parent.top + this.top + 1) + 'px';
  179. line.style.bottom = '';
  180. }
  181. else { // orientation 'bottom'
  182. var itemSetHeight = this.parent.itemSet.props.height; // TODO: this is nasty
  183. var lineHeight = itemSetHeight - this.parent.top - this.parent.height + this.top;
  184. box.style.top = (this.parent.height - this.top - this.height || 0) + 'px';
  185. line.style.top = (itemSetHeight - lineHeight) + 'px';
  186. line.style.bottom = '0';
  187. }
  188. dot.style.top = (-this.props.dot.height / 2) + 'px';
  189. };
  190. module.exports = BoxItem;