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.

335 lines
9.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
  1. var Item = require('./Item');
  2. /**
  3. * @constructor RangeItem
  4. * @extends Item
  5. * @param {Object} data Object containing parameters start, end
  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 options
  11. */
  12. function RangeItem (data, conversion, options) {
  13. this.props = {
  14. content: {
  15. width: 0
  16. }
  17. };
  18. this.overflow = false; // if contents can overflow (css styling), this flag is set to true
  19. this.options = options;
  20. // validate data
  21. if (data) {
  22. if (data.start == undefined) {
  23. throw new Error('Property "start" missing in item ' + data.id);
  24. }
  25. if (data.end == undefined) {
  26. throw new Error('Property "end" missing in item ' + data.id);
  27. }
  28. }
  29. Item.call(this, data, conversion, options);
  30. }
  31. RangeItem.prototype = new Item (null, null, null);
  32. RangeItem.prototype.baseClassName = 'vis-item vis-range';
  33. /**
  34. * Check whether this item is visible inside given range
  35. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  36. * @returns {boolean} True if visible
  37. */
  38. RangeItem.prototype.isVisible = function(range) {
  39. // determine visibility
  40. return (this.data.start < range.end) && (this.data.end > range.start);
  41. };
  42. /**
  43. * Repaint the item
  44. */
  45. RangeItem.prototype.redraw = function() {
  46. var dom = this.dom;
  47. if (!dom) {
  48. // create DOM
  49. this.dom = {};
  50. dom = this.dom;
  51. // background box
  52. dom.box = document.createElement('div');
  53. // className is updated in redraw()
  54. // frame box (to prevent the item contents from overflowing)
  55. dom.frame = document.createElement('div');
  56. dom.frame.className = 'vis-item-overflow';
  57. dom.box.appendChild(dom.frame);
  58. // visible frame box (showing the frame that is always visible)
  59. dom.visibleFrame = document.createElement('div');
  60. dom.visibleFrame.className = 'vis-item-visible-frame';
  61. dom.box.appendChild(dom.visibleFrame);
  62. // contents box
  63. dom.content = document.createElement('div');
  64. dom.content.className = 'vis-item-content';
  65. dom.frame.appendChild(dom.content);
  66. // attach this item as attribute
  67. dom.box['timeline-item'] = this;
  68. this.dirty = true;
  69. }
  70. // append DOM to parent DOM
  71. if (!this.parent) {
  72. throw new Error('Cannot redraw item: no parent attached');
  73. }
  74. if (!dom.box.parentNode) {
  75. var foreground = this.parent.dom.foreground;
  76. if (!foreground) {
  77. throw new Error('Cannot redraw item: parent has no foreground container element');
  78. }
  79. foreground.appendChild(dom.box);
  80. }
  81. this.displayed = true;
  82. // Update DOM when item is marked dirty. An item is marked dirty when:
  83. // - the item is not yet rendered
  84. // - the item's data is changed
  85. // - the item is selected/deselected
  86. if (this.dirty) {
  87. this._updateContents(this.dom.content);
  88. this._updateDataAttributes(this.dom.box);
  89. this._updateStyle(this.dom.box);
  90. var editable = (this.editable.updateTime || this.editable.updateGroup);
  91. // update class
  92. var className = (this.data.className ? (' ' + this.data.className) : '') +
  93. (this.selected ? ' vis-selected' : '') +
  94. (editable ? ' vis-editable' : ' vis-readonly');
  95. dom.box.className = this.baseClassName + className;
  96. // determine from css whether this box has overflow
  97. this.overflow = window.getComputedStyle(dom.frame).overflow !== 'hidden';
  98. // recalculate size
  99. // turn off max-width to be able to calculate the real width
  100. // this causes an extra browser repaint/reflow, but so be it
  101. this.dom.content.style.maxWidth = 'none';
  102. this.props.content.width = this.dom.content.offsetWidth;
  103. this.height = this.dom.box.offsetHeight;
  104. this.dom.content.style.maxWidth = '';
  105. this.dirty = false;
  106. }
  107. this._repaintOnItemUpdateTimeTooltip(dom.box);
  108. this._repaintDeleteButton(dom.box);
  109. this._repaintDragCenter();
  110. this._repaintDragLeft();
  111. this._repaintDragRight();
  112. };
  113. /**
  114. * Show the item in the DOM (when not already visible). The items DOM will
  115. * be created when needed.
  116. */
  117. RangeItem.prototype.show = function() {
  118. if (!this.displayed) {
  119. this.redraw();
  120. }
  121. };
  122. /**
  123. * Hide the item from the DOM (when visible)
  124. * @return {Boolean} changed
  125. */
  126. RangeItem.prototype.hide = function() {
  127. if (this.displayed) {
  128. var box = this.dom.box;
  129. if (box.parentNode) {
  130. box.parentNode.removeChild(box);
  131. }
  132. this.displayed = false;
  133. }
  134. };
  135. /**
  136. * Reposition the item horizontally
  137. * @param {boolean} [limitSize=true] If true (default), the width of the range
  138. * item will be limited, as the browser cannot
  139. * display very wide divs. This means though
  140. * that the applied left and width may
  141. * not correspond to the ranges start and end
  142. * @Override
  143. */
  144. RangeItem.prototype.repositionX = function(limitSize) {
  145. var parentWidth = this.parent.width;
  146. var start = this.conversion.toScreen(this.data.start);
  147. var end = this.conversion.toScreen(this.data.end);
  148. var align = this.data.align === undefined ? this.options.align : this.data.align;
  149. var contentStartPosition;
  150. var contentWidth;
  151. // limit the width of the range, as browsers cannot draw very wide divs
  152. if (limitSize === undefined || limitSize === true) {
  153. if (start < -parentWidth) {
  154. start = -parentWidth;
  155. }
  156. if (end > 2 * parentWidth) {
  157. end = 2 * parentWidth;
  158. }
  159. }
  160. // add 0.5 to compensate floating-point values rounding
  161. var boxWidth = Math.max(end - start + 0.5, 1);
  162. if (this.overflow) {
  163. if (this.options.rtl) {
  164. this.right = start;
  165. } else {
  166. this.left = start;
  167. }
  168. this.width = boxWidth + this.props.content.width;
  169. contentWidth = this.props.content.width;
  170. // Note: The calculation of width is an optimistic calculation, giving
  171. // a width which will not change when moving the Timeline
  172. // So no re-stacking needed, which is nicer for the eye;
  173. }
  174. else {
  175. if (this.options.rtl) {
  176. this.right = start;
  177. } else {
  178. this.left = start;
  179. }
  180. this.width = boxWidth;
  181. contentWidth = Math.min(end - start, this.props.content.width);
  182. }
  183. if (this.options.rtl) {
  184. this.dom.box.style.right = this.right + 'px';
  185. } else {
  186. this.dom.box.style.left = this.left + 'px';
  187. }
  188. this.dom.box.style.width = boxWidth + 'px';
  189. switch (align) {
  190. case 'left':
  191. if (this.options.rtl) {
  192. this.dom.content.style.right = '0';
  193. } else {
  194. this.dom.content.style.left = '0';
  195. }
  196. break;
  197. case 'right':
  198. if (this.options.rtl) {
  199. this.dom.content.style.right = Math.max((boxWidth - contentWidth), 0) + 'px';
  200. } else {
  201. this.dom.content.style.left = Math.max((boxWidth - contentWidth), 0) + 'px';
  202. }
  203. break;
  204. case 'center':
  205. if (this.options.rtl) {
  206. this.dom.content.style.right = Math.max((boxWidth - contentWidth) / 2, 0) + 'px';
  207. } else {
  208. this.dom.content.style.left = Math.max((boxWidth - contentWidth) / 2, 0) + 'px';
  209. }
  210. break;
  211. default: // 'auto'
  212. // when range exceeds left of the window, position the contents at the left of the visible area
  213. if (this.overflow) {
  214. if (end > 0) {
  215. contentStartPosition = Math.max(-start, 0);
  216. }
  217. else {
  218. contentStartPosition = -contentWidth; // ensure it's not visible anymore
  219. }
  220. }
  221. else {
  222. if (start < 0) {
  223. contentStartPosition = -start;
  224. }
  225. else {
  226. contentStartPosition = 0;
  227. }
  228. }
  229. if (this.options.rtl) {
  230. this.dom.content.style.right = contentStartPosition + 'px';
  231. } else {
  232. this.dom.content.style.left = contentStartPosition + 'px';
  233. this.dom.content.style.width = 'calc(100% - ' + contentStartPosition + 'px)';
  234. }
  235. }
  236. };
  237. /**
  238. * Reposition the item vertically
  239. * @Override
  240. */
  241. RangeItem.prototype.repositionY = function() {
  242. var orientation = this.options.orientation.item;
  243. var box = this.dom.box;
  244. if (orientation == 'top') {
  245. box.style.top = this.top + 'px';
  246. }
  247. else {
  248. box.style.top = (this.parent.height - this.top - this.height) + 'px';
  249. }
  250. };
  251. /**
  252. * Repaint a drag area on the left side of the range when the range is selected
  253. * @protected
  254. */
  255. RangeItem.prototype._repaintDragLeft = function () {
  256. if ((this.selected || this.options.itemsAlwaysDraggable.range) && this.options.editable.updateTime && !this.dom.dragLeft) {
  257. // create and show drag area
  258. var dragLeft = document.createElement('div');
  259. dragLeft.className = 'vis-drag-left';
  260. dragLeft.dragLeftItem = this;
  261. this.dom.box.appendChild(dragLeft);
  262. this.dom.dragLeft = dragLeft;
  263. }
  264. else if (!this.selected && !this.options.itemsAlwaysDraggable.range && this.dom.dragLeft) {
  265. // delete drag area
  266. if (this.dom.dragLeft.parentNode) {
  267. this.dom.dragLeft.parentNode.removeChild(this.dom.dragLeft);
  268. }
  269. this.dom.dragLeft = null;
  270. }
  271. };
  272. /**
  273. * Repaint a drag area on the right side of the range when the range is selected
  274. * @protected
  275. */
  276. RangeItem.prototype._repaintDragRight = function () {
  277. if ((this.selected || this.options.itemsAlwaysDraggable.range) && this.options.editable.updateTime && !this.dom.dragRight) {
  278. // create and show drag area
  279. var dragRight = document.createElement('div');
  280. dragRight.className = 'vis-drag-right';
  281. dragRight.dragRightItem = this;
  282. this.dom.box.appendChild(dragRight);
  283. this.dom.dragRight = dragRight;
  284. }
  285. else if (!this.selected && !this.options.itemsAlwaysDraggable.range && this.dom.dragRight) {
  286. // delete drag area
  287. if (this.dom.dragRight.parentNode) {
  288. this.dom.dragRight.parentNode.removeChild(this.dom.dragRight);
  289. }
  290. this.dom.dragRight = null;
  291. }
  292. };
  293. module.exports = RangeItem;