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.

200 lines
5.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
  1. var Item = require('./Item');
  2. /**
  3. * @constructor ItemPoint
  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 ItemPoint (data, conversion, options) {
  13. this.props = {
  14. dot: {
  15. top: 0,
  16. width: 0,
  17. height: 0
  18. },
  19. content: {
  20. height: 0,
  21. marginLeft: 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. ItemPoint.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. ItemPoint.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. ItemPoint.prototype.redraw = function() {
  48. var dom = this.dom;
  49. if (!dom) {
  50. // create DOM
  51. this.dom = {};
  52. dom = this.dom;
  53. // background box
  54. dom.point = document.createElement('div');
  55. // className is updated in redraw()
  56. // contents box, right from the dot
  57. dom.content = document.createElement('div');
  58. dom.content.className = 'content';
  59. dom.point.appendChild(dom.content);
  60. // dot at start
  61. dom.dot = document.createElement('div');
  62. dom.point.appendChild(dom.dot);
  63. // attach this item as attribute
  64. dom.point['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.point.parentNode) {
  71. var foreground = this.parent.dom.foreground;
  72. if (!foreground) {
  73. throw new Error('Cannot redraw time axis: parent has no foreground container element');
  74. }
  75. foreground.appendChild(dom.point);
  76. }
  77. this.displayed = true;
  78. // update contents
  79. if (this.data.content != this.content) {
  80. this.content = this.data.content;
  81. if (this.content instanceof Element) {
  82. dom.content.innerHTML = '';
  83. dom.content.appendChild(this.content);
  84. }
  85. else if (this.data.content != undefined) {
  86. dom.content.innerHTML = this.content;
  87. }
  88. else {
  89. throw new Error('Property "content" missing in item ' + this.data.id);
  90. }
  91. this.dirty = true;
  92. }
  93. // update title
  94. if (this.data.title != this.title) {
  95. dom.point.title = this.data.title;
  96. this.title = this.data.title;
  97. }
  98. // update class
  99. var className = (this.data.className? ' ' + this.data.className : '') +
  100. (this.selected ? ' selected' : '');
  101. if (this.className != className) {
  102. this.className = className;
  103. dom.point.className = 'item point' + className;
  104. dom.dot.className = 'item dot' + className;
  105. this.dirty = true;
  106. }
  107. // recalculate size
  108. if (this.dirty) {
  109. this.width = dom.point.offsetWidth;
  110. this.height = dom.point.offsetHeight;
  111. this.props.dot.width = dom.dot.offsetWidth;
  112. this.props.dot.height = dom.dot.offsetHeight;
  113. this.props.content.height = dom.content.offsetHeight;
  114. // resize contents
  115. dom.content.style.marginLeft = 2 * this.props.dot.width + 'px';
  116. //dom.content.style.marginRight = ... + 'px'; // TODO: margin right
  117. dom.dot.style.top = ((this.height - this.props.dot.height) / 2) + 'px';
  118. dom.dot.style.left = (this.props.dot.width / 2) + 'px';
  119. this.dirty = false;
  120. }
  121. this._repaintDeleteButton(dom.point);
  122. };
  123. /**
  124. * Show the item in the DOM (when not already visible). The items DOM will
  125. * be created when needed.
  126. */
  127. ItemPoint.prototype.show = function() {
  128. if (!this.displayed) {
  129. this.redraw();
  130. }
  131. };
  132. /**
  133. * Hide the item from the DOM (when visible)
  134. */
  135. ItemPoint.prototype.hide = function() {
  136. if (this.displayed) {
  137. if (this.dom.point.parentNode) {
  138. this.dom.point.parentNode.removeChild(this.dom.point);
  139. }
  140. this.top = null;
  141. this.left = null;
  142. this.displayed = false;
  143. }
  144. };
  145. /**
  146. * Reposition the item horizontally
  147. * @Override
  148. */
  149. ItemPoint.prototype.repositionX = function() {
  150. var start = this.conversion.toScreen(this.data.start);
  151. this.left = start - this.props.dot.width;
  152. // reposition point
  153. this.dom.point.style.left = this.left + 'px';
  154. };
  155. /**
  156. * Reposition the item vertically
  157. * @Override
  158. */
  159. ItemPoint.prototype.repositionY = function() {
  160. var orientation = this.options.orientation,
  161. point = this.dom.point;
  162. if (orientation == 'top') {
  163. point.style.top = this.top + 'px';
  164. }
  165. else {
  166. point.style.top = (this.parent.height - this.top - this.height) + 'px';
  167. }
  168. };
  169. module.exports = ItemPoint;