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.

786 lines
24 KiB

11 years ago
11 years ago
11 years ago
8 years ago
8 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. var moment = require('../module/moment');
  2. var util = require('../util');
  3. var DataSet = require('../DataSet');
  4. var DataView = require('../DataView');
  5. var Range = require('./Range');
  6. var Core = require('./Core');
  7. var TimeAxis = require('./component/TimeAxis');
  8. var CurrentTime = require('./component/CurrentTime');
  9. var CustomTime = require('./component/CustomTime');
  10. var ItemSet = require('./component/ItemSet');
  11. var printStyle = require('../shared/Validator').printStyle;
  12. var allOptions = require('./optionsTimeline').allOptions;
  13. var configureOptions = require('./optionsTimeline').configureOptions;
  14. var Configurator = require('../shared/Configurator').default;
  15. var Validator = require('../shared/Validator').default;
  16. /**
  17. * Create a timeline visualization
  18. * @param {HTMLElement} container
  19. * @param {vis.DataSet | vis.DataView | Array} [items]
  20. * @param {vis.DataSet | vis.DataView | Array} [groups]
  21. * @param {Object} [options] See Timeline.setOptions for the available options.
  22. * @constructor Timeline
  23. * @extends Core
  24. */
  25. function Timeline (container, items, groups, options) {
  26. this.initTime = new Date();
  27. this.itemsDone = false;
  28. if (!(this instanceof Timeline)) {
  29. throw new SyntaxError('Constructor must be called with the new operator');
  30. }
  31. // if the third element is options, the forth is groups (optionally);
  32. if (!(Array.isArray(groups) || groups instanceof DataSet || groups instanceof DataView) && groups instanceof Object) {
  33. var forthArgument = options;
  34. options = groups;
  35. groups = forthArgument;
  36. }
  37. // TODO: REMOVE THIS in the next MAJOR release
  38. // see https://github.com/almende/vis/issues/2511
  39. if (options && options.throttleRedraw) {
  40. console.warn("Timeline option \"throttleRedraw\" is DEPRICATED and no longer supported. It will be removed in the next MAJOR release.");
  41. }
  42. var me = this;
  43. this.defaultOptions = {
  44. start: null,
  45. end: null,
  46. autoResize: true,
  47. orientation: {
  48. axis: 'bottom', // axis orientation: 'bottom', 'top', or 'both'
  49. item: 'bottom' // not relevant
  50. },
  51. moment: moment,
  52. width: null,
  53. height: null,
  54. maxHeight: null,
  55. minHeight: null,
  56. };
  57. this.options = util.deepExtend({}, this.defaultOptions);
  58. // Create the DOM, props, and emitter
  59. this._create(container);
  60. if (!options || (options && typeof options.rtl == "undefined")) {
  61. this.dom.root.style.visibility = 'hidden';
  62. var directionFromDom, domNode = this.dom.root;
  63. while (!directionFromDom && domNode) {
  64. directionFromDom = window.getComputedStyle(domNode, null).direction;
  65. domNode = domNode.parentElement;
  66. }
  67. this.options.rtl = (directionFromDom && (directionFromDom.toLowerCase() == "rtl"));
  68. } else {
  69. this.options.rtl = options.rtl;
  70. }
  71. this.options.rollingMode = options && options.rollingMode;
  72. this.options.onInitialDrawComplete = options && options.onInitialDrawComplete;
  73. this.options.onTimeout = options && options.onTimeout;
  74. this.options.loadingScreenTemplate = options && options.loadingScreenTemplate;
  75. // Prepare loading screen
  76. var loadingScreenFragment = document.createElement('div');
  77. if (this.options.loadingScreenTemplate) {
  78. var templateFunction = this.options.loadingScreenTemplate.bind(this);
  79. var loadingScreen = templateFunction(this.dom.loadingScreen);
  80. if ((loadingScreen instanceof Object) && !(loadingScreen instanceof Element)) {
  81. templateFunction(loadingScreenFragment)
  82. } else {
  83. if (loadingScreen instanceof Element) {
  84. loadingScreenFragment.innerHTML = '';
  85. loadingScreenFragment.appendChild(loadingScreen);
  86. }
  87. else if (loadingScreen != undefined) {
  88. loadingScreenFragment.innerHTML = loadingScreen;
  89. }
  90. }
  91. }
  92. this.dom.loadingScreen.appendChild(loadingScreenFragment);
  93. // all components listed here will be repainted automatically
  94. this.components = [];
  95. this.body = {
  96. dom: this.dom,
  97. domProps: this.props,
  98. emitter: {
  99. on: this.on.bind(this),
  100. off: this.off.bind(this),
  101. emit: this.emit.bind(this)
  102. },
  103. hiddenDates: [],
  104. util: {
  105. getScale: function () {
  106. return me.timeAxis.step.scale;
  107. },
  108. getStep: function () {
  109. return me.timeAxis.step.step;
  110. },
  111. toScreen: me._toScreen.bind(me),
  112. toGlobalScreen: me._toGlobalScreen.bind(me), // this refers to the root.width
  113. toTime: me._toTime.bind(me),
  114. toGlobalTime : me._toGlobalTime.bind(me)
  115. }
  116. };
  117. // range
  118. this.range = new Range(this.body, this.options);
  119. this.components.push(this.range);
  120. this.body.range = this.range;
  121. // time axis
  122. this.timeAxis = new TimeAxis(this.body, this.options);
  123. this.timeAxis2 = null; // used in case of orientation option 'both'
  124. this.components.push(this.timeAxis);
  125. // current time bar
  126. this.currentTime = new CurrentTime(this.body, this.options);
  127. this.components.push(this.currentTime);
  128. // item set
  129. this.itemSet = new ItemSet(this.body, this.options);
  130. this.components.push(this.itemSet);
  131. this.itemsData = null; // DataSet
  132. this.groupsData = null; // DataSet
  133. this.dom.root.onclick = function (event) {
  134. me.emit('click', me.getEventProperties(event))
  135. };
  136. this.dom.root.ondblclick = function (event) {
  137. me.emit('doubleClick', me.getEventProperties(event))
  138. };
  139. this.dom.root.oncontextmenu = function (event) {
  140. me.emit('contextmenu', me.getEventProperties(event))
  141. };
  142. this.dom.root.onmouseover = function (event) {
  143. me.emit('mouseOver', me.getEventProperties(event))
  144. };
  145. if(window.PointerEvent) {
  146. this.dom.root.onpointerdown = function (event) {
  147. me.emit('mouseDown', me.getEventProperties(event))
  148. };
  149. this.dom.root.onpointermove = function (event) {
  150. me.emit('mouseMove', me.getEventProperties(event))
  151. };
  152. this.dom.root.onpointerup = function (event) {
  153. me.emit('mouseUp', me.getEventProperties(event))
  154. };
  155. } else {
  156. this.dom.root.onmousemove = function (event) {
  157. me.emit('mouseMove', me.getEventProperties(event))
  158. };
  159. this.dom.root.onmousedown = function (event) {
  160. me.emit('mouseDown', me.getEventProperties(event))
  161. };
  162. this.dom.root.onmouseup = function (event) {
  163. me.emit('mouseUp', me.getEventProperties(event))
  164. };
  165. }
  166. //Single time autoscale/fit
  167. this.initialFitDone = false;
  168. this.on('changed', function (){
  169. if (me.itemsData == null) return;
  170. if (!me.initialFitDone || !me.options.rollingMode) {
  171. me.initialFitDone = true;
  172. if (me.options.start != undefined || me.options.end != undefined) {
  173. if (me.options.start == undefined || me.options.end == undefined) {
  174. var range = me.getItemRange();
  175. }
  176. var start = me.options.start != undefined ? me.options.start : range.min;
  177. var end = me.options.end != undefined ? me.options.end : range.max;
  178. me.setWindow(start, end, {animation: false});
  179. } else {
  180. me.fit({animation: false});
  181. }
  182. }
  183. if (!me.initialDrawDone && (me.initialRangeChangeDone || (!me.options.start && !me.options.end) || me.options.rollingMode)) {
  184. me.initialDrawDone = true;
  185. me.itemSet.initialDrawDone = true;
  186. me.dom.root.style.visibility = 'visible';
  187. me.dom.loadingScreen.parentNode.removeChild(me.dom.loadingScreen);
  188. if (me.options.onInitialDrawComplete) {
  189. setTimeout(() => {
  190. return me.options.onInitialDrawComplete();
  191. }, 0)
  192. }
  193. }
  194. });
  195. this.on('destroyTimeline', () => {
  196. me.destroy()
  197. });
  198. // apply options
  199. if (options) {
  200. this.setOptions(options);
  201. }
  202. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  203. if (groups) {
  204. this.setGroups(groups);
  205. }
  206. // create itemset
  207. if (items) {
  208. this.setItems(items);
  209. }
  210. // draw for the first time
  211. this._redraw();
  212. }
  213. // Extend the functionality from Core
  214. Timeline.prototype = new Core();
  215. /**
  216. * Load a configurator
  217. * @return {Object}
  218. * @private
  219. */
  220. Timeline.prototype._createConfigurator = function () {
  221. return new Configurator(this, this.dom.container, configureOptions);
  222. };
  223. /**
  224. * Force a redraw. The size of all items will be recalculated.
  225. * Can be useful to manually redraw when option autoResize=false and the window
  226. * has been resized, or when the items CSS has been changed.
  227. *
  228. * Note: this function will be overridden on construction with a trottled version
  229. */
  230. Timeline.prototype.redraw = function() {
  231. this.itemSet && this.itemSet.markDirty({refreshItems: true});
  232. this._redraw();
  233. };
  234. Timeline.prototype.setOptions = function (options) {
  235. // validate options
  236. let errorFound = Validator.validate(options, allOptions);
  237. if (errorFound === true) {
  238. console.log('%cErrors have been found in the supplied options object.', printStyle);
  239. }
  240. Core.prototype.setOptions.call(this, options);
  241. if ('type' in options) {
  242. if (options.type !== this.options.type) {
  243. this.options.type = options.type;
  244. // force recreation of all items
  245. var itemsData = this.itemsData;
  246. if (itemsData) {
  247. var selection = this.getSelection();
  248. this.setItems(null); // remove all
  249. this.setItems(itemsData); // add all
  250. this.setSelection(selection); // restore selection
  251. }
  252. }
  253. }
  254. };
  255. /**
  256. * Set items
  257. * @param {vis.DataSet | Array | null} items
  258. */
  259. Timeline.prototype.setItems = function(items) {
  260. this.itemsDone = false;
  261. // convert to type DataSet when needed
  262. var newDataSet;
  263. if (!items) {
  264. newDataSet = null;
  265. }
  266. else if (items instanceof DataSet || items instanceof DataView) {
  267. newDataSet = items;
  268. }
  269. else {
  270. // turn an array into a dataset
  271. newDataSet = new DataSet(items, {
  272. type: {
  273. start: 'Date',
  274. end: 'Date'
  275. }
  276. });
  277. }
  278. // set items
  279. this.itemsData = newDataSet;
  280. this.itemSet && this.itemSet.setItems(newDataSet);
  281. };
  282. /**
  283. * Set groups
  284. * @param {vis.DataSet | Array} groups
  285. */
  286. Timeline.prototype.setGroups = function(groups) {
  287. // convert to type DataSet when needed
  288. var newDataSet;
  289. if (!groups) {
  290. newDataSet = null;
  291. }
  292. else {
  293. var filter = function(group) {
  294. return group.visible !== false;
  295. }
  296. if (groups instanceof DataSet || groups instanceof DataView) {
  297. newDataSet = new DataView(groups,{filter: filter});
  298. }
  299. else {
  300. // turn an array into a dataset
  301. newDataSet = new DataSet(groups.filter(filter));
  302. }
  303. }
  304. this.groupsData = newDataSet;
  305. this.itemSet.setGroups(newDataSet);
  306. };
  307. /**
  308. * Set both items and groups in one go
  309. * @param {{items: (Array | vis.DataSet), groups: (Array | vis.DataSet)}} data
  310. */
  311. Timeline.prototype.setData = function (data) {
  312. if (data && data.groups) {
  313. this.setGroups(data.groups);
  314. }
  315. if (data && data.items) {
  316. this.setItems(data.items);
  317. }
  318. };
  319. /**
  320. * Set selected items by their id. Replaces the current selection
  321. * Unknown id's are silently ignored.
  322. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  323. * selected. If ids is an empty array, all items will be
  324. * unselected.
  325. * @param {Object} [options] Available options:
  326. * `focus: boolean`
  327. * If true, focus will be set to the selected item(s)
  328. * `animation: boolean | {duration: number, easingFunction: string}`
  329. * If true (default), the range is animated
  330. * smoothly to the new window. An object can be
  331. * provided to specify duration and easing function.
  332. * Default duration is 500 ms, and default easing
  333. * function is 'easeInOutQuad'.
  334. * Only applicable when option focus is true.
  335. */
  336. Timeline.prototype.setSelection = function(ids, options) {
  337. this.itemSet && this.itemSet.setSelection(ids);
  338. if (options && options.focus) {
  339. this.focus(ids, options);
  340. }
  341. };
  342. /**
  343. * Get the selected items by their id
  344. * @return {Array} ids The ids of the selected items
  345. */
  346. Timeline.prototype.getSelection = function() {
  347. return this.itemSet && this.itemSet.getSelection() || [];
  348. };
  349. /**
  350. * Adjust the visible window such that the selected item (or multiple items)
  351. * are centered on screen.
  352. * @param {string | String[]} id An item id or array with item ids
  353. * @param {Object} [options] Available options:
  354. * `animation: boolean | {duration: number, easingFunction: string}`
  355. * If true (default), the range is animated
  356. * smoothly to the new window. An object can be
  357. * provided to specify duration and easing function.
  358. * Default duration is 500 ms, and default easing
  359. * function is 'easeInOutQuad'.
  360. */
  361. Timeline.prototype.focus = function(id, options) {
  362. if (!this.itemsData || id == undefined) return;
  363. var ids = Array.isArray(id) ? id : [id];
  364. // get the specified item(s)
  365. var itemsData = this.itemsData.getDataSet().get(ids, {
  366. type: {
  367. start: 'Date',
  368. end: 'Date'
  369. }
  370. });
  371. // calculate minimum start and maximum end of specified items
  372. var start = null;
  373. var end = null;
  374. itemsData.forEach(function (itemData) {
  375. var s = itemData.start.valueOf();
  376. var e = 'end' in itemData ? itemData.end.valueOf() : itemData.start.valueOf();
  377. if (start === null || s < start) {
  378. start = s;
  379. }
  380. if (end === null || e > end) {
  381. end = e;
  382. }
  383. });
  384. if (start !== null && end !== null) {
  385. var me = this;
  386. // Use the first item for the vertical focus
  387. var item = this.itemSet.items[ids[0]];
  388. var startPos = this._getScrollTop() * -1;
  389. var initialVerticalScroll = null;
  390. // Setup a handler for each frame of the vertical scroll
  391. var verticalAnimationFrame = function(ease, willDraw, done) {
  392. var verticalScroll = getItemVerticalScroll(me, item);
  393. if(!initialVerticalScroll) {
  394. initialVerticalScroll = verticalScroll;
  395. }
  396. if(initialVerticalScroll.itemTop == verticalScroll.itemTop && !initialVerticalScroll.shouldScroll) {
  397. return; // We don't need to scroll, so do nothing
  398. }
  399. else if(initialVerticalScroll.itemTop != verticalScroll.itemTop && verticalScroll.shouldScroll) {
  400. // The redraw shifted elements, so reset the animation to correct
  401. initialVerticalScroll = verticalScroll;
  402. startPos = me._getScrollTop() * -1;
  403. }
  404. var from = startPos;
  405. var to = initialVerticalScroll.scrollOffset;
  406. var scrollTop = done ? to : (from + (to - from) * ease);
  407. me._setScrollTop(-scrollTop);
  408. if(!willDraw) {
  409. me._redraw();
  410. }
  411. };
  412. // Enforces the final vertical scroll position
  413. var setFinalVerticalPosition = function() {
  414. var finalVerticalScroll = getItemVerticalScroll(me, item);
  415. if (finalVerticalScroll.shouldScroll && finalVerticalScroll.itemTop != initialVerticalScroll.itemTop) {
  416. me._setScrollTop(-finalVerticalScroll.scrollOffset);
  417. me._redraw();
  418. }
  419. };
  420. // Perform one last check at the end to make sure the final vertical
  421. // position is correct
  422. var finalVerticalCallback = function() {
  423. // Double check we ended at the proper scroll position
  424. setFinalVerticalPosition();
  425. // Let the redraw settle and finalize the position.
  426. setTimeout(setFinalVerticalPosition, 100);
  427. };
  428. // calculate the new middle and interval for the window
  429. var middle = (start + end) / 2;
  430. var interval = Math.max(this.range.end - this.range.start, (end - start) * 1.1);
  431. var animation = options && options.animation !== undefined ? options.animation : true;
  432. if (!animation) {
  433. // We aren't animating so set a default so that the final callback forces the vertical location
  434. initialVerticalScroll = { shouldScroll: false, scrollOffset: -1, itemTop: -1 };
  435. }
  436. this.range.setRange(middle - interval / 2, middle + interval / 2, { animation: animation }, finalVerticalCallback, verticalAnimationFrame);
  437. }
  438. };
  439. /**
  440. * Set Timeline window such that it fits all items
  441. * @param {Object} [options] Available options:
  442. * `animation: boolean | {duration: number, easingFunction: string}`
  443. * If true (default), the range is animated
  444. * smoothly to the new window. An object can be
  445. * provided to specify duration and easing function.
  446. * Default duration is 500 ms, and default easing
  447. * function is 'easeInOutQuad'.
  448. * @param {function} [callback]
  449. */
  450. Timeline.prototype.fit = function (options, callback) {
  451. var animation = (options && options.animation !== undefined) ? options.animation : true;
  452. var range;
  453. var dataset = this.itemsData && this.itemsData.getDataSet();
  454. if (dataset.length === 1 && dataset.get()[0].end === undefined) {
  455. // a single item -> don't fit, just show a range around the item from -4 to +3 days
  456. range = this.getDataRange();
  457. this.moveTo(range.min.valueOf(), {animation}, callback);
  458. }
  459. else {
  460. // exactly fit the items (plus a small margin)
  461. range = this.getItemRange();
  462. this.range.setRange(range.min, range.max, { animation: animation }, callback);
  463. }
  464. };
  465. /**
  466. *
  467. * @param {vis.Item} item
  468. * @returns {number}
  469. */
  470. function getStart(item) {
  471. return util.convert(item.data.start, 'Date').valueOf()
  472. }
  473. /**
  474. *
  475. * @param {vis.Item} item
  476. * @returns {number}
  477. */
  478. function getEnd(item) {
  479. var end = item.data.end != undefined ? item.data.end : item.data.start;
  480. return util.convert(end, 'Date').valueOf();
  481. }
  482. /**
  483. * @param {vis.Timeline} timeline
  484. * @param {vis.Item} item
  485. * @return {{shouldScroll: bool, scrollOffset: number, itemTop: number}}
  486. */
  487. function getItemVerticalScroll(timeline, item) {
  488. var leftHeight = timeline.props.leftContainer.height;
  489. var contentHeight = timeline.props.left.height;
  490. var group = item.parent;
  491. var offset = group.top;
  492. var shouldScroll = true;
  493. var orientation = timeline.timeAxis.options.orientation.axis;
  494. var itemTop = function () {
  495. if (orientation == "bottom") {
  496. return group.height - item.top - item.height;
  497. }
  498. else {
  499. return item.top;
  500. }
  501. };
  502. var currentScrollHeight = timeline._getScrollTop() * -1;
  503. var targetOffset = offset + itemTop();
  504. var height = item.height;
  505. if (targetOffset < currentScrollHeight) {
  506. if (offset + leftHeight <= offset + itemTop() + height) {
  507. offset += itemTop() - timeline.itemSet.options.margin.item.vertical;
  508. }
  509. }
  510. else if (targetOffset + height > currentScrollHeight + leftHeight) {
  511. offset += itemTop() + height - leftHeight + timeline.itemSet.options.margin.item.vertical;
  512. }
  513. else {
  514. shouldScroll = false;
  515. }
  516. offset = Math.min(offset, contentHeight - leftHeight);
  517. return { shouldScroll: shouldScroll, scrollOffset: offset, itemTop: targetOffset };
  518. }
  519. /**
  520. * Determine the range of the items, taking into account their actual width
  521. * and a margin of 10 pixels on both sides.
  522. *
  523. * @returns {{min: Date, max: Date}}
  524. */
  525. Timeline.prototype.getItemRange = function () {
  526. // get a rough approximation for the range based on the items start and end dates
  527. var range = this.getDataRange();
  528. var min = range.min !== null ? range.min.valueOf() : null;
  529. var max = range.max !== null ? range.max.valueOf() : null;
  530. var minItem = null;
  531. var maxItem = null;
  532. if (min != null && max != null) {
  533. var interval = (max - min); // ms
  534. if (interval <= 0) {
  535. interval = 10;
  536. }
  537. var factor = interval / this.props.center.width;
  538. var redrawQueue = {};
  539. var redrawQueueLength = 0;
  540. // collect redraw functions
  541. util.forEach(this.itemSet.items, function (item, key) {
  542. if (item.groupShowing) {
  543. var returnQueue = true;
  544. redrawQueue[key] = item.redraw(returnQueue);
  545. redrawQueueLength = redrawQueue[key].length;
  546. }
  547. })
  548. var needRedraw = redrawQueueLength > 0;
  549. if (needRedraw) {
  550. // redraw all regular items
  551. for (var i = 0; i < redrawQueueLength; i++) {
  552. util.forEach(redrawQueue, function (fns) {
  553. fns[i]();
  554. });
  555. }
  556. }
  557. // calculate the date of the left side and right side of the items given
  558. util.forEach(this.itemSet.items, function (item) {
  559. var start = getStart(item);
  560. var end = getEnd(item);
  561. var startSide;
  562. var endSide;
  563. if (this.options.rtl) {
  564. startSide = start - (item.getWidthRight() + 10) * factor;
  565. endSide = end + (item.getWidthLeft() + 10) * factor;
  566. } else {
  567. startSide = start - (item.getWidthLeft() + 10) * factor;
  568. endSide = end + (item.getWidthRight() + 10) * factor;
  569. }
  570. if (startSide < min) {
  571. min = startSide;
  572. minItem = item;
  573. }
  574. if (endSide > max) {
  575. max = endSide;
  576. maxItem = item;
  577. }
  578. }.bind(this));
  579. if (minItem && maxItem) {
  580. var lhs = minItem.getWidthLeft() + 10;
  581. var rhs = maxItem.getWidthRight() + 10;
  582. var delta = this.props.center.width - lhs - rhs; // px
  583. if (delta > 0) {
  584. if (this.options.rtl) {
  585. min = getStart(minItem) - rhs * interval / delta; // ms
  586. max = getEnd(maxItem) + lhs * interval / delta; // ms
  587. } else {
  588. min = getStart(minItem) - lhs * interval / delta; // ms
  589. max = getEnd(maxItem) + rhs * interval / delta; // ms
  590. }
  591. }
  592. }
  593. }
  594. return {
  595. min: min != null ? new Date(min) : null,
  596. max: max != null ? new Date(max) : null
  597. }
  598. };
  599. /**
  600. * Calculate the data range of the items start and end dates
  601. * @returns {{min: Date, max: Date}}
  602. */
  603. Timeline.prototype.getDataRange = function() {
  604. var min = null;
  605. var max = null;
  606. var dataset = this.itemsData && this.itemsData.getDataSet();
  607. if (dataset) {
  608. dataset.forEach(function (item) {
  609. var start = util.convert(item.start, 'Date').valueOf();
  610. var end = util.convert(item.end != undefined ? item.end : item.start, 'Date').valueOf();
  611. if (min === null || start < min) {
  612. min = start;
  613. }
  614. if (max === null || end > max) {
  615. max = end;
  616. }
  617. });
  618. }
  619. return {
  620. min: min != null ? new Date(min) : null,
  621. max: max != null ? new Date(max) : null
  622. }
  623. };
  624. /**
  625. * Generate Timeline related information from an event
  626. * @param {Event} event
  627. * @return {Object} An object with related information, like on which area
  628. * The event happened, whether clicked on an item, etc.
  629. */
  630. Timeline.prototype.getEventProperties = function (event) {
  631. var clientX = event.center ? event.center.x : event.clientX;
  632. var clientY = event.center ? event.center.y : event.clientY;
  633. var x;
  634. if (this.options.rtl) {
  635. x = util.getAbsoluteRight(this.dom.centerContainer) - clientX;
  636. } else {
  637. x = clientX - util.getAbsoluteLeft(this.dom.centerContainer);
  638. }
  639. var y = clientY - util.getAbsoluteTop(this.dom.centerContainer);
  640. var item = this.itemSet.itemFromTarget(event);
  641. var group = this.itemSet.groupFromTarget(event);
  642. var customTime = CustomTime.customTimeFromTarget(event);
  643. var snap = this.itemSet.options.snap || null;
  644. var scale = this.body.util.getScale();
  645. var step = this.body.util.getStep();
  646. var time = this._toTime(x);
  647. var snappedTime = snap ? snap(time, scale, step) : time;
  648. var element = util.getTarget(event);
  649. var what = null;
  650. if (item != null) {what = 'item';}
  651. else if (customTime != null) {what = 'custom-time';}
  652. else if (util.hasParent(element, this.timeAxis.dom.foreground)) {what = 'axis';}
  653. else if (this.timeAxis2 && util.hasParent(element, this.timeAxis2.dom.foreground)) {what = 'axis';}
  654. else if (util.hasParent(element, this.itemSet.dom.labelSet)) {what = 'group-label';}
  655. else if (util.hasParent(element, this.currentTime.bar)) {what = 'current-time';}
  656. else if (util.hasParent(element, this.dom.center)) {what = 'background';}
  657. return {
  658. event: event,
  659. item: item ? item.id : null,
  660. group: group ? group.groupId : null,
  661. what: what,
  662. pageX: event.srcEvent ? event.srcEvent.pageX : event.pageX,
  663. pageY: event.srcEvent ? event.srcEvent.pageY : event.pageY,
  664. x: x,
  665. y: y,
  666. time: time,
  667. snappedTime: snappedTime
  668. }
  669. };
  670. /**
  671. * Toggle Timeline rolling mode
  672. */
  673. Timeline.prototype.toggleRollingMode = function () {
  674. if (this.range.rolling) {
  675. this.range.stopRolling();
  676. } else {
  677. if (this.options.rollingMode == undefined) {
  678. this.setOptions(this.options)
  679. }
  680. this.range.startRolling();
  681. }
  682. }
  683. module.exports = Timeline;