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.

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