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.

180 lines
4.8 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 = 'vis-item-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 item: 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. this._updateStyle(this.dom.point);
  88. // update class
  89. var className = (this.data.className ? ' ' + this.data.className : '') +
  90. (this.selected ? ' vis-selected' : '');
  91. dom.point.className = 'vis-item vis-point' + className;
  92. dom.dot.className = 'vis-item vis-dot' + className;
  93. // recalculate size
  94. this.width = dom.point.offsetWidth;
  95. this.height = dom.point.offsetHeight;
  96. this.props.dot.width = dom.dot.offsetWidth;
  97. this.props.dot.height = dom.dot.offsetHeight;
  98. this.props.content.height = dom.content.offsetHeight;
  99. // resize contents
  100. dom.content.style.marginLeft = 2 * this.props.dot.width + 'px';
  101. //dom.content.style.marginRight = ... + 'px'; // TODO: margin right
  102. dom.dot.style.top = ((this.height - this.props.dot.height) / 2) + 'px';
  103. dom.dot.style.left = (this.props.dot.width / 2) + 'px';
  104. this.dirty = false;
  105. }
  106. this._repaintDeleteButton(dom.point);
  107. };
  108. /**
  109. * Show the item in the DOM (when not already visible). The items DOM will
  110. * be created when needed.
  111. */
  112. PointItem.prototype.show = function() {
  113. if (!this.displayed) {
  114. this.redraw();
  115. }
  116. };
  117. /**
  118. * Hide the item from the DOM (when visible)
  119. */
  120. PointItem.prototype.hide = function() {
  121. if (this.displayed) {
  122. if (this.dom.point.parentNode) {
  123. this.dom.point.parentNode.removeChild(this.dom.point);
  124. }
  125. this.displayed = false;
  126. }
  127. };
  128. /**
  129. * Reposition the item horizontally
  130. * @Override
  131. */
  132. PointItem.prototype.repositionX = function() {
  133. var start = this.conversion.toScreen(this.data.start);
  134. this.left = start - this.props.dot.width;
  135. // reposition point
  136. this.dom.point.style.left = this.left + 'px';
  137. };
  138. /**
  139. * Reposition the item vertically
  140. * @Override
  141. */
  142. PointItem.prototype.repositionY = function() {
  143. var orientation = this.options.orientation,
  144. point = this.dom.point;
  145. if (orientation == 'top') {
  146. point.style.top = this.top + 'px';
  147. }
  148. else {
  149. point.style.top = (this.parent.height - this.top - this.height) + 'px';
  150. }
  151. };
  152. module.exports = PointItem;