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.

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