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.

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