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.

217 lines
5.7 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. marginRight: 0
  23. }
  24. };
  25. this.options = options;
  26. // validate data
  27. if (data) {
  28. if (data.start == undefined) {
  29. throw new Error('Property "start" missing in item ' + data);
  30. }
  31. }
  32. Item.call(this, data, conversion, options);
  33. }
  34. PointItem.prototype = new Item (null, null, null);
  35. /**
  36. * Check whether this item is visible inside given range
  37. * @param {{start: number, end: number}} range with a timestamp for start and end
  38. * @returns {boolean} True if visible
  39. */
  40. PointItem.prototype.isVisible = function(range) {
  41. // determine visibility
  42. var widthInMs = this.width * range.getMillisecondsPerPixel();
  43. return (this.data.start.getTime() + widthInMs > range.start ) && (this.data.start < range.end);
  44. };
  45. /**
  46. * Repaint the item
  47. */
  48. PointItem.prototype.redraw = function() {
  49. var dom = this.dom;
  50. if (!dom) {
  51. // create DOM
  52. this.dom = {};
  53. dom = this.dom;
  54. // background box
  55. dom.point = document.createElement('div');
  56. // className is updated in redraw()
  57. // contents box, right from the dot
  58. dom.content = document.createElement('div');
  59. dom.content.className = 'vis-item-content';
  60. dom.point.appendChild(dom.content);
  61. // dot at start
  62. dom.dot = document.createElement('div');
  63. dom.point.appendChild(dom.dot);
  64. // attach this item as attribute
  65. dom.point['timeline-item'] = this;
  66. this.dirty = true;
  67. }
  68. // append DOM to parent DOM
  69. if (!this.parent) {
  70. throw new Error('Cannot redraw item: no parent attached');
  71. }
  72. if (!dom.point.parentNode) {
  73. var foreground = this.parent.dom.foreground;
  74. if (!foreground) {
  75. throw new Error('Cannot redraw item: parent has no foreground container element');
  76. }
  77. foreground.appendChild(dom.point);
  78. }
  79. this.displayed = true;
  80. // Update DOM when item is marked dirty. An item is marked dirty when:
  81. // - the item is not yet rendered
  82. // - the item's data is changed
  83. // - the item is selected/deselected
  84. if (this.dirty) {
  85. this._updateContents(this.dom.content);
  86. this._updateDataAttributes(this.dom.point);
  87. this._updateStyle(this.dom.point);
  88. var editable = (this.editable.updateTime || this.editable.updateGroup);
  89. // update class
  90. var className = (this.data.className ? ' ' + this.data.className : '') +
  91. (this.selected ? ' vis-selected' : '') +
  92. (editable ? ' vis-editable' : ' vis-readonly');
  93. dom.point.className = 'vis-item vis-point' + className;
  94. dom.dot.className = 'vis-item vis-dot' + className;
  95. // recalculate size of dot and contents
  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. if (this.options.rtl) {
  101. dom.content.style.marginRight = 2 * this.props.dot.width + 'px';
  102. } else {
  103. dom.content.style.marginLeft = 2 * this.props.dot.width + 'px';
  104. }
  105. //dom.content.style.marginRight = ... + 'px'; // TODO: margin right
  106. // recalculate size
  107. this.width = dom.point.offsetWidth;
  108. this.height = dom.point.offsetHeight;
  109. // reposition the dot
  110. dom.dot.style.top = ((this.height - this.props.dot.height) / 2) + 'px';
  111. if (this.options.rtl) {
  112. dom.dot.style.right = (this.props.dot.width / 2) + 'px';
  113. } else {
  114. dom.dot.style.left = (this.props.dot.width / 2) + 'px';
  115. }
  116. this.dirty = false;
  117. }
  118. this._repaintOnItemUpdateTimeTooltip(dom.point);
  119. this._repaintDragCenter();
  120. this._repaintDeleteButton(dom.point);
  121. };
  122. /**
  123. * Show the item in the DOM (when not already visible). The items DOM will
  124. * be created when needed.
  125. */
  126. PointItem.prototype.show = function() {
  127. if (!this.displayed) {
  128. this.redraw();
  129. }
  130. };
  131. /**
  132. * Hide the item from the DOM (when visible)
  133. */
  134. PointItem.prototype.hide = function() {
  135. if (this.displayed) {
  136. if (this.dom.point.parentNode) {
  137. this.dom.point.parentNode.removeChild(this.dom.point);
  138. }
  139. this.displayed = false;
  140. }
  141. };
  142. /**
  143. * Reposition the item horizontally
  144. * @Override
  145. */
  146. PointItem.prototype.repositionX = function() {
  147. var start = this.conversion.toScreen(this.data.start);
  148. if (this.options.rtl) {
  149. this.right = start - this.props.dot.width;
  150. // reposition point
  151. this.dom.point.style.right = this.right + 'px';
  152. } else {
  153. this.left = start - this.props.dot.width;
  154. // reposition point
  155. this.dom.point.style.left = this.left + 'px';
  156. }
  157. };
  158. /**
  159. * Reposition the item vertically
  160. * @Override
  161. */
  162. PointItem.prototype.repositionY = function() {
  163. var orientation = this.options.orientation.item;
  164. var point = this.dom.point;
  165. if (orientation == 'top') {
  166. point.style.top = this.top + 'px';
  167. }
  168. else {
  169. point.style.top = (this.parent.height - this.top - this.height) + 'px';
  170. }
  171. };
  172. /**
  173. * Return the width of the item left from its start date
  174. * @return {number}
  175. */
  176. PointItem.prototype.getWidthLeft = function () {
  177. return this.props.dot.width;
  178. };
  179. /**
  180. * Return the width of the item right from its start date
  181. * @return {number}
  182. */
  183. PointItem.prototype.getWidthRight = function () {
  184. return this.props.dot.width;
  185. };
  186. module.exports = PointItem;