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.

198 lines
5.2 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 of dot and contents
  94. this.props.dot.width = dom.dot.offsetWidth;
  95. this.props.dot.height = dom.dot.offsetHeight;
  96. this.props.content.height = dom.content.offsetHeight;
  97. // resize contents
  98. dom.content.style.marginLeft = 2 * this.props.dot.width + 'px';
  99. //dom.content.style.marginRight = ... + 'px'; // TODO: margin right
  100. dom.dot.style.top = ((this.height - this.props.dot.height) / 2) + 'px';
  101. dom.dot.style.left = (this.props.dot.width / 2) + 'px';
  102. // recalculate size
  103. this.width = dom.point.offsetWidth;
  104. this.height = dom.point.offsetHeight;
  105. this.dirty = false;
  106. }
  107. this._repaintDeleteButton(dom.point);
  108. };
  109. /**
  110. * Show the item in the DOM (when not already visible). The items DOM will
  111. * be created when needed.
  112. */
  113. PointItem.prototype.show = function() {
  114. if (!this.displayed) {
  115. this.redraw();
  116. }
  117. };
  118. /**
  119. * Hide the item from the DOM (when visible)
  120. */
  121. PointItem.prototype.hide = function() {
  122. if (this.displayed) {
  123. if (this.dom.point.parentNode) {
  124. this.dom.point.parentNode.removeChild(this.dom.point);
  125. }
  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.item;
  145. var 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. /**
  154. * Return the width of the item left from its start date
  155. * @return {number}
  156. */
  157. PointItem.prototype.getWidthLeft = function () {
  158. return this.props.dot.width;
  159. };
  160. /**
  161. * Return the width of the item right from its start date
  162. * @return {number}
  163. */
  164. PointItem.prototype.getWidthRight = function () {
  165. return this.width - this.props.dot.width;
  166. };
  167. module.exports = PointItem;