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.

394 lines
11 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
  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. RangeItem.prototype._createDomElement = function() {
  44. if (!this.dom) {
  45. // create DOM
  46. this.dom = {};
  47. // background box
  48. this.dom.box = document.createElement('div');
  49. // className is updated in redraw()
  50. // frame box (to prevent the item contents from overflowing)
  51. this.dom.frame = document.createElement('div');
  52. this.dom.frame.className = 'vis-item-overflow';
  53. this.dom.box.appendChild(this.dom.frame);
  54. // visible frame box (showing the frame that is always visible)
  55. this.dom.visibleFrame = document.createElement('div');
  56. this.dom.visibleFrame.className = 'vis-item-visible-frame';
  57. this.dom.box.appendChild(this.dom.visibleFrame);
  58. // contents box
  59. this.dom.content = document.createElement('div');
  60. this.dom.content.className = 'vis-item-content';
  61. this.dom.frame.appendChild(this.dom.content);
  62. // attach this item as attribute
  63. this.dom.box['timeline-item'] = this;
  64. this.dirty = true;
  65. }
  66. }
  67. RangeItem.prototype._appendDomElement = function() {
  68. if (!this.parent) {
  69. throw new Error('Cannot redraw item: no parent attached');
  70. }
  71. if (!this.dom.box.parentNode) {
  72. var foreground = this.parent.dom.foreground;
  73. if (!foreground) {
  74. throw new Error('Cannot redraw item: parent has no foreground container element');
  75. }
  76. foreground.appendChild(this.dom.box);
  77. }
  78. this.displayed = true;
  79. }
  80. RangeItem.prototype._updateDirtyDomComponents = function() {
  81. // update dirty DOM. 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._updateDataAttributes(this.dom.box);
  88. this._updateStyle(this.dom.box);
  89. var editable = (this.editable.updateTime || this.editable.updateGroup);
  90. // update class
  91. var className = (this.data.className ? (' ' + this.data.className) : '') +
  92. (this.selected ? ' vis-selected' : '') +
  93. (editable ? ' vis-editable' : ' vis-readonly');
  94. this.dom.box.className = this.baseClassName + className;
  95. // turn off max-width to be able to calculate the real width
  96. // this causes an extra browser repaint/reflow, but so be it
  97. this.dom.content.style.maxWidth = 'none';
  98. }
  99. }
  100. RangeItem.prototype._getDomComponentsSizes = function() {
  101. // determine from css whether this box has overflow
  102. this.overflow = window.getComputedStyle(this.dom.frame).overflow !== 'hidden';
  103. return {
  104. content: {
  105. width: this.dom.content.offsetWidth,
  106. },
  107. box: {
  108. height: this.dom.box.offsetHeight
  109. }
  110. }
  111. }
  112. RangeItem.prototype._updateDomComponentsSizes = function(sizes) {
  113. this.props.content.width = sizes.content.width;
  114. this.height = sizes.box.height;
  115. this.dom.content.style.maxWidth = '';
  116. this.dirty = false;
  117. }
  118. RangeItem.prototype._repaintDomAdditionals = function() {
  119. this._repaintOnItemUpdateTimeTooltip(this.dom.box);
  120. this._repaintDeleteButton(this.dom.box);
  121. this._repaintDragCenter();
  122. this._repaintDragLeft();
  123. this._repaintDragRight();
  124. }
  125. /**
  126. * Repaint the item
  127. * @param {boolean} [returnQueue=false] return the queue
  128. * @return {boolean} the redraw queue if returnQueue=true
  129. */
  130. RangeItem.prototype.redraw = function(returnQueue) {
  131. var sizes;
  132. var queue = [
  133. // create item DOM
  134. this._createDomElement.bind(this),
  135. // append DOM to parent DOM
  136. this._appendDomElement.bind(this),
  137. // update dirty DOM
  138. this._updateDirtyDomComponents.bind(this),
  139. (function() {
  140. if (this.dirty) {
  141. sizes = this._getDomComponentsSizes.bind(this)();
  142. }
  143. }).bind(this),
  144. (function() {
  145. if (this.dirty) {
  146. this._updateDomComponentsSizes.bind(this)(sizes);
  147. }
  148. }).bind(this),
  149. // repaint DOM additionals
  150. this._repaintDomAdditionals.bind(this)
  151. ];
  152. if (returnQueue) {
  153. return queue;
  154. } else {
  155. var result;
  156. queue.forEach(function (fn) {
  157. result = fn();
  158. });
  159. return result;
  160. }
  161. };
  162. /**
  163. * Show the item in the DOM (when not already visible). The items DOM will
  164. * be created when needed.
  165. * @param {boolean} [returnQueue=false] whether to return a queue of functions to execute instead of just executing them
  166. * @return {boolean} the redraw queue if returnQueue=true
  167. */
  168. RangeItem.prototype.show = function(returnQueue) {
  169. if (!this.displayed) {
  170. return this.redraw(returnQueue);
  171. }
  172. };
  173. /**
  174. * Hide the item from the DOM (when visible)
  175. */
  176. RangeItem.prototype.hide = function() {
  177. if (this.displayed) {
  178. var box = this.dom.box;
  179. if (box.parentNode) {
  180. box.parentNode.removeChild(box);
  181. }
  182. this.displayed = false;
  183. }
  184. };
  185. /**
  186. * Reposition the item horizontally
  187. * @param {boolean} [limitSize=true] If true (default), the width of the range
  188. * item will be limited, as the browser cannot
  189. * display very wide divs. This means though
  190. * that the applied left and width may
  191. * not correspond to the ranges start and end
  192. * @Override
  193. */
  194. RangeItem.prototype.repositionX = function(limitSize) {
  195. var parentWidth = this.parent.width;
  196. var start = this.conversion.toScreen(this.data.start);
  197. var end = this.conversion.toScreen(this.data.end);
  198. var align = this.data.align === undefined ? this.options.align : this.data.align;
  199. var contentStartPosition;
  200. var contentWidth;
  201. // limit the width of the range, as browsers cannot draw very wide divs
  202. // unless limitSize: false is explicitly set in item data
  203. if (this.data.limitSize !== false && (limitSize === undefined || limitSize === true)) {
  204. if (start < -parentWidth) {
  205. start = -parentWidth;
  206. }
  207. if (end > 2 * parentWidth) {
  208. end = 2 * parentWidth;
  209. }
  210. }
  211. // add 0.5 to compensate floating-point values rounding
  212. var boxWidth = Math.max(end - start + 0.5, 1);
  213. if (this.overflow) {
  214. if (this.options.rtl) {
  215. this.right = start;
  216. } else {
  217. this.left = start;
  218. }
  219. this.width = boxWidth + this.props.content.width;
  220. contentWidth = this.props.content.width;
  221. // Note: The calculation of width is an optimistic calculation, giving
  222. // a width which will not change when moving the Timeline
  223. // So no re-stacking needed, which is nicer for the eye;
  224. }
  225. else {
  226. if (this.options.rtl) {
  227. this.right = start;
  228. } else {
  229. this.left = start;
  230. }
  231. this.width = boxWidth;
  232. contentWidth = Math.min(end - start, this.props.content.width);
  233. }
  234. if (this.options.rtl) {
  235. this.dom.box.style.right = this.right + 'px';
  236. } else {
  237. this.dom.box.style.left = this.left + 'px';
  238. }
  239. this.dom.box.style.width = boxWidth + 'px';
  240. switch (align) {
  241. case 'left':
  242. if (this.options.rtl) {
  243. this.dom.content.style.right = '0';
  244. } else {
  245. this.dom.content.style.left = '0';
  246. }
  247. break;
  248. case 'right':
  249. if (this.options.rtl) {
  250. this.dom.content.style.right = Math.max((boxWidth - contentWidth), 0) + 'px';
  251. } else {
  252. this.dom.content.style.left = Math.max((boxWidth - contentWidth), 0) + 'px';
  253. }
  254. break;
  255. case 'center':
  256. if (this.options.rtl) {
  257. this.dom.content.style.right = Math.max((boxWidth - contentWidth) / 2, 0) + 'px';
  258. } else {
  259. this.dom.content.style.left = Math.max((boxWidth - contentWidth) / 2, 0) + 'px';
  260. }
  261. break;
  262. default: // 'auto'
  263. // when range exceeds left of the window, position the contents at the left of the visible area
  264. if (this.overflow) {
  265. if (end > 0) {
  266. contentStartPosition = Math.max(-start, 0);
  267. }
  268. else {
  269. contentStartPosition = -contentWidth; // ensure it's not visible anymore
  270. }
  271. }
  272. else {
  273. if (start < 0) {
  274. contentStartPosition = -start;
  275. }
  276. else {
  277. contentStartPosition = 0;
  278. }
  279. }
  280. if (this.options.rtl) {
  281. this.dom.content.style.right = contentStartPosition + 'px';
  282. } else {
  283. this.dom.content.style.left = contentStartPosition + 'px';
  284. this.dom.content.style.width = 'calc(100% - ' + contentStartPosition + 'px)';
  285. }
  286. }
  287. };
  288. /**
  289. * Reposition the item vertically
  290. * @Override
  291. */
  292. RangeItem.prototype.repositionY = function() {
  293. var orientation = this.options.orientation.item;
  294. var box = this.dom.box;
  295. if (orientation == 'top') {
  296. box.style.top = this.top + 'px';
  297. }
  298. else {
  299. box.style.top = (this.parent.height - this.top - this.height) + 'px';
  300. }
  301. };
  302. /**
  303. * Repaint a drag area on the left side of the range when the range is selected
  304. * @protected
  305. */
  306. RangeItem.prototype._repaintDragLeft = function () {
  307. if ((this.selected || this.options.itemsAlwaysDraggable.range) && this.options.editable.updateTime && !this.dom.dragLeft) {
  308. // create and show drag area
  309. var dragLeft = document.createElement('div');
  310. dragLeft.className = 'vis-drag-left';
  311. dragLeft.dragLeftItem = this;
  312. this.dom.box.appendChild(dragLeft);
  313. this.dom.dragLeft = dragLeft;
  314. }
  315. else if (!this.selected && !this.options.itemsAlwaysDraggable.range && this.dom.dragLeft) {
  316. // delete drag area
  317. if (this.dom.dragLeft.parentNode) {
  318. this.dom.dragLeft.parentNode.removeChild(this.dom.dragLeft);
  319. }
  320. this.dom.dragLeft = null;
  321. }
  322. };
  323. /**
  324. * Repaint a drag area on the right side of the range when the range is selected
  325. * @protected
  326. */
  327. RangeItem.prototype._repaintDragRight = function () {
  328. if ((this.selected || this.options.itemsAlwaysDraggable.range) && this.options.editable.updateTime && !this.dom.dragRight) {
  329. // create and show drag area
  330. var dragRight = document.createElement('div');
  331. dragRight.className = 'vis-drag-right';
  332. dragRight.dragRightItem = this;
  333. this.dom.box.appendChild(dragRight);
  334. this.dom.dragRight = dragRight;
  335. }
  336. else if (!this.selected && !this.options.itemsAlwaysDraggable.range && this.dom.dragRight) {
  337. // delete drag area
  338. if (this.dom.dragRight.parentNode) {
  339. this.dom.dragRight.parentNode.removeChild(this.dom.dragRight);
  340. }
  341. this.dom.dragRight = null;
  342. }
  343. };
  344. module.exports = RangeItem;