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.

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