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.

220 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. // TODO: account for the real width of the item. Right now we just add 1/4 to the window
  43. var interval = (range.end - range.start) / 4;
  44. return (this.data.start > range.start - interval) && (this.data.start < range.end + interval);
  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(this.dom.point);
  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._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;