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.

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