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.

202 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. this._attachDataAttributes();
  108. // recalculate size
  109. if (this.dirty) {
  110. this.width = dom.point.offsetWidth;
  111. this.height = dom.point.offsetHeight;
  112. this.props.dot.width = dom.dot.offsetWidth;
  113. this.props.dot.height = dom.dot.offsetHeight;
  114. this.props.content.height = dom.content.offsetHeight;
  115. // resize contents
  116. dom.content.style.marginLeft = 2 * this.props.dot.width + 'px';
  117. //dom.content.style.marginRight = ... + 'px'; // TODO: margin right
  118. dom.dot.style.top = ((this.height - this.props.dot.height) / 2) + 'px';
  119. dom.dot.style.left = (this.props.dot.width / 2) + 'px';
  120. this.dirty = false;
  121. }
  122. this._repaintDeleteButton(dom.point);
  123. };
  124. /**
  125. * Show the item in the DOM (when not already visible). The items DOM will
  126. * be created when needed.
  127. */
  128. ItemPoint.prototype.show = function() {
  129. if (!this.displayed) {
  130. this.redraw();
  131. }
  132. };
  133. /**
  134. * Hide the item from the DOM (when visible)
  135. */
  136. ItemPoint.prototype.hide = function() {
  137. if (this.displayed) {
  138. if (this.dom.point.parentNode) {
  139. this.dom.point.parentNode.removeChild(this.dom.point);
  140. }
  141. this.top = null;
  142. this.left = null;
  143. this.displayed = false;
  144. }
  145. };
  146. /**
  147. * Reposition the item horizontally
  148. * @Override
  149. */
  150. ItemPoint.prototype.repositionX = function() {
  151. var start = this.conversion.toScreen(this.data.start);
  152. this.left = start - this.props.dot.width;
  153. // reposition point
  154. this.dom.point.style.left = this.left + 'px';
  155. };
  156. /**
  157. * Reposition the item vertically
  158. * @Override
  159. */
  160. ItemPoint.prototype.repositionY = function() {
  161. var orientation = this.options.orientation,
  162. point = this.dom.point;
  163. if (orientation == 'top') {
  164. point.style.top = this.top + 'px';
  165. }
  166. else {
  167. point.style.top = (this.parent.height - this.top - this.height) + 'px';
  168. }
  169. };
  170. module.exports = ItemPoint;