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.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
  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. *
  36. * @param {vis.Range} range with a timestamp for start and end
  37. * @returns {boolean} True if visible
  38. */
  39. RangeItem.prototype.isVisible = function(range) {
  40. // determine visibility
  41. return (this.data.start < range.end) && (this.data.end > range.start);
  42. };
  43. /**
  44. * Repaint the item
  45. */
  46. RangeItem.prototype.redraw = function() {
  47. var dom = this.dom;
  48. if (!dom) {
  49. // create DOM
  50. this.dom = {};
  51. dom = this.dom;
  52. // background box
  53. dom.box = document.createElement('div');
  54. // className is updated in redraw()
  55. // frame box (to prevent the item contents from overflowing)
  56. dom.frame = document.createElement('div');
  57. dom.frame.className = 'vis-item-overflow';
  58. dom.box.appendChild(dom.frame);
  59. // visible frame box (showing the frame that is always visible)
  60. dom.visibleFrame = document.createElement('div');
  61. dom.visibleFrame.className = 'vis-item-visible-frame';
  62. dom.box.appendChild(dom.visibleFrame);
  63. // contents box
  64. dom.content = document.createElement('div');
  65. dom.content.className = 'vis-item-content';
  66. dom.frame.appendChild(dom.content);
  67. // attach this item as attribute
  68. dom.box['timeline-item'] = this;
  69. this.dirty = true;
  70. }
  71. // append DOM to parent DOM
  72. if (!this.parent) {
  73. throw new Error('Cannot redraw item: no parent attached');
  74. }
  75. if (!dom.box.parentNode) {
  76. var foreground = this.parent.dom.foreground;
  77. if (!foreground) {
  78. throw new Error('Cannot redraw item: parent has no foreground container element');
  79. }
  80. foreground.appendChild(dom.box);
  81. }
  82. this.displayed = true;
  83. // Update DOM when item is marked dirty. An item is marked dirty when:
  84. // - the item is not yet rendered
  85. // - the item's data is changed
  86. // - the item is selected/deselected
  87. if (this.dirty) {
  88. this._updateContents(this.dom.content);
  89. this._updateDataAttributes(this.dom.box);
  90. this._updateStyle(this.dom.box);
  91. var editable = (this.editable.updateTime || this.editable.updateGroup);
  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.box.className = this.baseClassName + className;
  97. // determine from css whether this box has overflow
  98. this.overflow = window.getComputedStyle(dom.frame).overflow !== 'hidden';
  99. // recalculate size
  100. // turn off max-width to be able to calculate the real width
  101. // this causes an extra browser repaint/reflow, but so be it
  102. this.dom.content.style.maxWidth = 'none';
  103. this.props.content.width = this.dom.content.offsetWidth;
  104. this.height = this.dom.box.offsetHeight;
  105. this.dom.content.style.maxWidth = '';
  106. this.dirty = false;
  107. }
  108. this._repaintOnItemUpdateTimeTooltip(dom.box);
  109. this._repaintDeleteButton(dom.box);
  110. this._repaintDragCenter();
  111. this._repaintDragLeft();
  112. this._repaintDragRight();
  113. };
  114. /**
  115. * Show the item in the DOM (when not already visible). The items DOM will
  116. * be created when needed.
  117. */
  118. RangeItem.prototype.show = function() {
  119. if (!this.displayed) {
  120. this.redraw();
  121. }
  122. };
  123. /**
  124. * Hide the item from the DOM (when visible)
  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;