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.

240 lines
6.4 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 ItemBox
  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 ItemBox (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. ItemBox.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. ItemBox.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. ItemBox.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. }
  67. // append DOM to parent DOM
  68. if (!this.parent) {
  69. throw new Error('Cannot redraw item: no parent attached');
  70. }
  71. if (!dom.box.parentNode) {
  72. var foreground = this.parent.dom.foreground;
  73. if (!foreground) throw new Error('Cannot redraw time axis: parent has no foreground container element');
  74. foreground.appendChild(dom.box);
  75. }
  76. if (!dom.line.parentNode) {
  77. var background = this.parent.dom.background;
  78. if (!background) throw new Error('Cannot redraw time axis: parent has no background container element');
  79. background.appendChild(dom.line);
  80. }
  81. if (!dom.dot.parentNode) {
  82. var axis = this.parent.dom.axis;
  83. if (!background) throw new Error('Cannot redraw time axis: parent has no axis container element');
  84. axis.appendChild(dom.dot);
  85. }
  86. this.displayed = true;
  87. // update contents
  88. if (this.data.content != this.content) {
  89. this.content = this.data.content;
  90. if (this.content instanceof Element) {
  91. dom.content.innerHTML = '';
  92. dom.content.appendChild(this.content);
  93. }
  94. else if (this.data.content != undefined) {
  95. dom.content.innerHTML = this.content;
  96. }
  97. else {
  98. throw new Error('Property "content" missing in item ' + this.data.id);
  99. }
  100. this.dirty = true;
  101. }
  102. // update title
  103. if (this.data.title != this.title) {
  104. dom.box.title = this.data.title;
  105. this.title = this.data.title;
  106. }
  107. // update class
  108. var className = (this.data.className? ' ' + this.data.className : '') +
  109. (this.selected ? ' selected' : '');
  110. if (this.className != className) {
  111. this.className = className;
  112. dom.box.className = 'item box' + className;
  113. dom.line.className = 'item line' + className;
  114. dom.dot.className = 'item dot' + className;
  115. this.dirty = true;
  116. }
  117. // recalculate size
  118. if (this.dirty) {
  119. this.props.dot.height = dom.dot.offsetHeight;
  120. this.props.dot.width = dom.dot.offsetWidth;
  121. this.props.line.width = dom.line.offsetWidth;
  122. this.width = dom.box.offsetWidth;
  123. this.height = dom.box.offsetHeight;
  124. this.dirty = false;
  125. }
  126. this._repaintDeleteButton(dom.box);
  127. };
  128. /**
  129. * Show the item in the DOM (when not already displayed). The items DOM will
  130. * be created when needed.
  131. */
  132. ItemBox.prototype.show = function() {
  133. if (!this.displayed) {
  134. this.redraw();
  135. }
  136. };
  137. /**
  138. * Hide the item from the DOM (when visible)
  139. */
  140. ItemBox.prototype.hide = function() {
  141. if (this.displayed) {
  142. var dom = this.dom;
  143. if (dom.box.parentNode) dom.box.parentNode.removeChild(dom.box);
  144. if (dom.line.parentNode) dom.line.parentNode.removeChild(dom.line);
  145. if (dom.dot.parentNode) dom.dot.parentNode.removeChild(dom.dot);
  146. this.top = null;
  147. this.left = null;
  148. this.displayed = false;
  149. }
  150. };
  151. /**
  152. * Reposition the item horizontally
  153. * @Override
  154. */
  155. ItemBox.prototype.repositionX = function() {
  156. var start = this.conversion.toScreen(this.data.start),
  157. align = this.options.align,
  158. left,
  159. box = this.dom.box,
  160. line = this.dom.line,
  161. dot = this.dom.dot;
  162. // calculate left position of the box
  163. if (align == 'right') {
  164. this.left = start - this.width;
  165. }
  166. else if (align == 'left') {
  167. this.left = start;
  168. }
  169. else {
  170. // default or 'center'
  171. this.left = start - this.width / 2;
  172. }
  173. // reposition box
  174. box.style.left = this.left + 'px';
  175. // reposition line
  176. line.style.left = (start - this.props.line.width / 2) + 'px';
  177. // reposition dot
  178. dot.style.left = (start - this.props.dot.width / 2) + 'px';
  179. };
  180. /**
  181. * Reposition the item vertically
  182. * @Override
  183. */
  184. ItemBox.prototype.repositionY = function() {
  185. var orientation = this.options.orientation,
  186. box = this.dom.box,
  187. line = this.dom.line,
  188. dot = this.dom.dot;
  189. if (orientation == 'top') {
  190. box.style.top = (this.top || 0) + 'px';
  191. line.style.top = '0';
  192. line.style.height = (this.parent.top + this.top + 1) + 'px';
  193. line.style.bottom = '';
  194. }
  195. else { // orientation 'bottom'
  196. var itemSetHeight = this.parent.itemSet.props.height; // TODO: this is nasty
  197. var lineHeight = itemSetHeight - this.parent.top - this.parent.height + this.top;
  198. box.style.top = (this.parent.height - this.top - this.height || 0) + 'px';
  199. line.style.top = (itemSetHeight - lineHeight) + 'px';
  200. line.style.bottom = '0';
  201. }
  202. dot.style.top = (-this.props.dot.height / 2) + 'px';
  203. };
  204. module.exports = ItemBox;