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.

230 lines
6.2 KiB

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