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.

182 lines
4.7 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 PointItem
  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 PointItem (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. PointItem.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. PointItem.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. PointItem.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. this.dirty = true;
  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.point.parentNode) {
  72. var foreground = this.parent.dom.foreground;
  73. if (!foreground) {
  74. throw new Error('Cannot redraw time axis: parent has no foreground container element');
  75. }
  76. foreground.appendChild(dom.point);
  77. }
  78. this.displayed = true;
  79. // Update DOM when item is marked dirty. An item is marked dirty when:
  80. // - the item is not yet rendered
  81. // - the item's data is changed
  82. // - the item is selected/deselected
  83. if (this.dirty) {
  84. this._updateContents(this.dom.content);
  85. this._updateTitle(this.dom.point);
  86. this._updateDataAttributes(this.dom.point);
  87. // update class
  88. var className = (this.data.className? ' ' + this.data.className : '') +
  89. (this.selected ? ' selected' : '');
  90. dom.point.className = 'item point' + className;
  91. dom.dot.className = 'item dot' + className;
  92. // recalculate size
  93. this.width = dom.point.offsetWidth;
  94. this.height = dom.point.offsetHeight;
  95. this.props.dot.width = dom.dot.offsetWidth;
  96. this.props.dot.height = dom.dot.offsetHeight;
  97. this.props.content.height = dom.content.offsetHeight;
  98. // resize contents
  99. dom.content.style.marginLeft = 2 * this.props.dot.width + 'px';
  100. //dom.content.style.marginRight = ... + 'px'; // TODO: margin right
  101. dom.dot.style.top = ((this.height - this.props.dot.height) / 2) + 'px';
  102. dom.dot.style.left = (this.props.dot.width / 2) + 'px';
  103. this.dirty = false;
  104. }
  105. this._repaintDeleteButton(dom.point);
  106. };
  107. /**
  108. * Show the item in the DOM (when not already visible). The items DOM will
  109. * be created when needed.
  110. */
  111. PointItem.prototype.show = function() {
  112. if (!this.displayed) {
  113. this.redraw();
  114. }
  115. };
  116. /**
  117. * Hide the item from the DOM (when visible)
  118. */
  119. PointItem.prototype.hide = function() {
  120. if (this.displayed) {
  121. if (this.dom.point.parentNode) {
  122. this.dom.point.parentNode.removeChild(this.dom.point);
  123. }
  124. this.top = null;
  125. this.left = null;
  126. this.displayed = false;
  127. }
  128. };
  129. /**
  130. * Reposition the item horizontally
  131. * @Override
  132. */
  133. PointItem.prototype.repositionX = function() {
  134. var start = this.conversion.toScreen(this.data.start);
  135. this.left = start - this.props.dot.width;
  136. // reposition point
  137. this.dom.point.style.left = this.left + 'px';
  138. };
  139. /**
  140. * Reposition the item vertically
  141. * @Override
  142. */
  143. PointItem.prototype.repositionY = function() {
  144. var orientation = this.options.orientation,
  145. point = this.dom.point;
  146. if (orientation == 'top') {
  147. point.style.top = this.top + 'px';
  148. }
  149. else {
  150. point.style.top = (this.parent.height - this.top - this.height) + 'px';
  151. }
  152. };
  153. module.exports = PointItem;