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.

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