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.

740 lines
25 KiB

  1. var Emitter = require('emitter-component');
  2. var Hammer = require('../module/hammer');
  3. var util = require('../util');
  4. var DataSet = require('../DataSet');
  5. var DataView = require('../DataView');
  6. var Range = require('./Range');
  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 Activator = require('../shared/Activator');
  12. /**
  13. * Create a timeline visualization
  14. * @param {HTMLElement} container
  15. * @param {vis.DataSet | Array | google.visualization.DataTable} [items]
  16. * @param {Object} [options] See Core.setOptions for the available options.
  17. * @constructor
  18. */
  19. function Core () {}
  20. // turn Core into an event emitter
  21. Emitter(Core.prototype);
  22. /**
  23. * Create the main DOM for the Core: a root panel containing left, right,
  24. * top, bottom, content, and background panel.
  25. * @param {Element} container The container element where the Core will
  26. * be attached.
  27. * @private
  28. */
  29. Core.prototype._create = function (container) {
  30. this.dom = {};
  31. this.dom.root = document.createElement('div');
  32. this.dom.background = document.createElement('div');
  33. this.dom.backgroundVertical = document.createElement('div');
  34. this.dom.backgroundHorizontal = document.createElement('div');
  35. this.dom.centerContainer = document.createElement('div');
  36. this.dom.leftContainer = document.createElement('div');
  37. this.dom.rightContainer = document.createElement('div');
  38. this.dom.center = document.createElement('div');
  39. this.dom.left = document.createElement('div');
  40. this.dom.right = document.createElement('div');
  41. this.dom.top = document.createElement('div');
  42. this.dom.bottom = document.createElement('div');
  43. this.dom.shadowTop = document.createElement('div');
  44. this.dom.shadowBottom = document.createElement('div');
  45. this.dom.shadowTopLeft = document.createElement('div');
  46. this.dom.shadowBottomLeft = document.createElement('div');
  47. this.dom.shadowTopRight = document.createElement('div');
  48. this.dom.shadowBottomRight = document.createElement('div');
  49. this.dom.root.className = 'vis timeline root';
  50. this.dom.background.className = 'vispanel background';
  51. this.dom.backgroundVertical.className = 'vispanel background vertical';
  52. this.dom.backgroundHorizontal.className = 'vispanel background horizontal';
  53. this.dom.centerContainer.className = 'vispanel center';
  54. this.dom.leftContainer.className = 'vispanel left';
  55. this.dom.rightContainer.className = 'vispanel right';
  56. this.dom.top.className = 'vispanel top';
  57. this.dom.bottom.className = 'vispanel bottom';
  58. this.dom.left.className = 'content';
  59. this.dom.center.className = 'content';
  60. this.dom.right.className = 'content';
  61. this.dom.shadowTop.className = 'shadow top';
  62. this.dom.shadowBottom.className = 'shadow bottom';
  63. this.dom.shadowTopLeft.className = 'shadow top';
  64. this.dom.shadowBottomLeft.className = 'shadow bottom';
  65. this.dom.shadowTopRight.className = 'shadow top';
  66. this.dom.shadowBottomRight.className = 'shadow bottom';
  67. this.dom.root.appendChild(this.dom.background);
  68. this.dom.root.appendChild(this.dom.backgroundVertical);
  69. this.dom.root.appendChild(this.dom.backgroundHorizontal);
  70. this.dom.root.appendChild(this.dom.centerContainer);
  71. this.dom.root.appendChild(this.dom.leftContainer);
  72. this.dom.root.appendChild(this.dom.rightContainer);
  73. this.dom.root.appendChild(this.dom.top);
  74. this.dom.root.appendChild(this.dom.bottom);
  75. this.dom.centerContainer.appendChild(this.dom.center);
  76. this.dom.leftContainer.appendChild(this.dom.left);
  77. this.dom.rightContainer.appendChild(this.dom.right);
  78. this.dom.centerContainer.appendChild(this.dom.shadowTop);
  79. this.dom.centerContainer.appendChild(this.dom.shadowBottom);
  80. this.dom.leftContainer.appendChild(this.dom.shadowTopLeft);
  81. this.dom.leftContainer.appendChild(this.dom.shadowBottomLeft);
  82. this.dom.rightContainer.appendChild(this.dom.shadowTopRight);
  83. this.dom.rightContainer.appendChild(this.dom.shadowBottomRight);
  84. this.on('rangechange', this.redraw.bind(this));
  85. this.on('change', this.redraw.bind(this));
  86. this.on('touch', this._onTouch.bind(this));
  87. this.on('pinch', this._onPinch.bind(this));
  88. this.on('dragstart', this._onDragStart.bind(this));
  89. this.on('drag', this._onDrag.bind(this));
  90. // create event listeners for all interesting events, these events will be
  91. // emitted via emitter
  92. this.hammer = Hammer(this.dom.root, {
  93. prevent_default: true
  94. });
  95. this.listeners = {};
  96. var me = this;
  97. var events = [
  98. 'touch', 'pinch',
  99. 'tap', 'doubletap', 'hold',
  100. 'dragstart', 'drag', 'dragend',
  101. 'mousewheel', 'DOMMouseScroll' // DOMMouseScroll is needed for Firefox
  102. ];
  103. events.forEach(function (event) {
  104. var listener = function () {
  105. var args = [event].concat(Array.prototype.slice.call(arguments, 0));
  106. if (!me.activator || me.activator.active) {
  107. me.emit.apply(me, args);
  108. }
  109. };
  110. me.hammer.on(event, listener);
  111. me.listeners[event] = listener;
  112. });
  113. // size properties of each of the panels
  114. this.props = {
  115. root: {},
  116. background: {},
  117. centerContainer: {},
  118. leftContainer: {},
  119. rightContainer: {},
  120. center: {},
  121. left: {},
  122. right: {},
  123. top: {},
  124. bottom: {},
  125. border: {},
  126. scrollTop: 0,
  127. scrollTopMin: 0
  128. };
  129. this.touch = {}; // store state information needed for touch events
  130. // attach the root panel to the provided container
  131. if (!container) throw new Error('No container provided');
  132. container.appendChild(this.dom.root);
  133. };
  134. /**
  135. * Set options. Options will be passed to all components loaded in the Timeline.
  136. * @param {Object} [options]
  137. * {String} orientation
  138. * Vertical orientation for the Timeline,
  139. * can be 'bottom' (default) or 'top'.
  140. * {String | Number} width
  141. * Width for the timeline, a number in pixels or
  142. * a css string like '1000px' or '75%'. '100%' by default.
  143. * {String | Number} height
  144. * Fixed height for the Timeline, a number in pixels or
  145. * a css string like '400px' or '75%'. If undefined,
  146. * The Timeline will automatically size such that
  147. * its contents fit.
  148. * {String | Number} minHeight
  149. * Minimum height for the Timeline, a number in pixels or
  150. * a css string like '400px' or '75%'.
  151. * {String | Number} maxHeight
  152. * Maximum height for the Timeline, a number in pixels or
  153. * a css string like '400px' or '75%'.
  154. * {Number | Date | String} start
  155. * Start date for the visible window
  156. * {Number | Date | String} end
  157. * End date for the visible window
  158. */
  159. Core.prototype.setOptions = function (options) {
  160. if (options) {
  161. // copy the known options
  162. var fields = ['width', 'height', 'minHeight', 'maxHeight', 'autoResize', 'start', 'end', 'orientation', 'activatable'];
  163. util.selectiveExtend(fields, this.options, options);
  164. if ('activatable' in options) {
  165. if (options.activatable) {
  166. this.activator = new Activator(this.dom.root);
  167. }
  168. else {
  169. if (this.activator) {
  170. this.activator.destroy();
  171. delete this.activator;
  172. }
  173. }
  174. }
  175. // enable/disable autoResize
  176. this._initAutoResize();
  177. }
  178. // propagate options to all components
  179. this.components.forEach(function (component) {
  180. component.setOptions(options);
  181. });
  182. // TODO: remove deprecation error one day (deprecated since version 0.8.0)
  183. if (options && options.order) {
  184. throw new Error('Option order is deprecated. There is no replacement for this feature.');
  185. }
  186. // redraw everything
  187. this.redraw();
  188. };
  189. /**
  190. * Destroy the Core, clean up all DOM elements and event listeners.
  191. */
  192. Core.prototype.destroy = function () {
  193. // unbind datasets
  194. this.clear();
  195. // remove all event listeners
  196. this.off();
  197. // stop checking for changed size
  198. this._stopAutoResize();
  199. // remove from DOM
  200. if (this.dom.root.parentNode) {
  201. this.dom.root.parentNode.removeChild(this.dom.root);
  202. }
  203. this.dom = null;
  204. // remove Activator
  205. if (this.activator) {
  206. this.activator.destroy();
  207. delete this.activator;
  208. }
  209. // cleanup hammer touch events
  210. for (var event in this.listeners) {
  211. if (this.listeners.hasOwnProperty(event)) {
  212. delete this.listeners[event];
  213. }
  214. }
  215. this.listeners = null;
  216. this.hammer = null;
  217. // give all components the opportunity to cleanup
  218. this.components.forEach(function (component) {
  219. component.destroy();
  220. });
  221. this.body = null;
  222. };
  223. /**
  224. * Set a custom time bar
  225. * @param {Date} time
  226. */
  227. Core.prototype.setCustomTime = function (time) {
  228. if (!this.customTime) {
  229. throw new Error('Cannot get custom time: Custom time bar is not enabled');
  230. }
  231. this.customTime.setCustomTime(time);
  232. };
  233. /**
  234. * Retrieve the current custom time.
  235. * @return {Date} customTime
  236. */
  237. Core.prototype.getCustomTime = function() {
  238. if (!this.customTime) {
  239. throw new Error('Cannot get custom time: Custom time bar is not enabled');
  240. }
  241. return this.customTime.getCustomTime();
  242. };
  243. /**
  244. * Get the id's of the currently visible items.
  245. * @returns {Array} The ids of the visible items
  246. */
  247. Core.prototype.getVisibleItems = function() {
  248. return this.itemSet && this.itemSet.getVisibleItems() || [];
  249. };
  250. /**
  251. * Clear the Core. By Default, items, groups and options are cleared.
  252. * Example usage:
  253. *
  254. * timeline.clear(); // clear items, groups, and options
  255. * timeline.clear({options: true}); // clear options only
  256. *
  257. * @param {Object} [what] Optionally specify what to clear. By default:
  258. * {items: true, groups: true, options: true}
  259. */
  260. Core.prototype.clear = function(what) {
  261. // clear items
  262. if (!what || what.items) {
  263. this.setItems(null);
  264. }
  265. // clear groups
  266. if (!what || what.groups) {
  267. this.setGroups(null);
  268. }
  269. // clear options of timeline and of each of the components
  270. if (!what || what.options) {
  271. this.components.forEach(function (component) {
  272. component.setOptions(component.defaultOptions);
  273. });
  274. this.setOptions(this.defaultOptions); // this will also do a redraw
  275. }
  276. };
  277. /**
  278. * Set Core window such that it fits all items
  279. */
  280. Core.prototype.fit = function() {
  281. // apply the data range as range
  282. var dataRange = this.getItemRange();
  283. // add 5% space on both sides
  284. var start = dataRange.min;
  285. var end = dataRange.max;
  286. if (start != null && end != null) {
  287. var interval = (end.valueOf() - start.valueOf());
  288. if (interval <= 0) {
  289. // prevent an empty interval
  290. interval = 24 * 60 * 60 * 1000; // 1 day
  291. }
  292. start = new Date(start.valueOf() - interval * 0.05);
  293. end = new Date(end.valueOf() + interval * 0.05);
  294. }
  295. // skip range set if there is no start and end date
  296. if (start === null && end === null) {
  297. return;
  298. }
  299. this.range.setRange(start, end);
  300. };
  301. /**
  302. * Set the visible window. Both parameters are optional, you can change only
  303. * start or only end. Syntax:
  304. *
  305. * TimeLine.setWindow(start, end)
  306. * TimeLine.setWindow(range)
  307. *
  308. * Where start and end can be a Date, number, or string, and range is an
  309. * object with properties start and end.
  310. *
  311. * @param {Date | Number | String | Object} [start] Start date of visible window
  312. * @param {Date | Number | String} [end] End date of visible window
  313. */
  314. Core.prototype.setWindow = function(start, end) {
  315. if (arguments.length == 1) {
  316. var range = arguments[0];
  317. this.range.setRange(range.start, range.end);
  318. }
  319. else {
  320. this.range.setRange(start, end);
  321. }
  322. };
  323. /**
  324. * Get the visible window
  325. * @return {{start: Date, end: Date}} Visible range
  326. */
  327. Core.prototype.getWindow = function() {
  328. var range = this.range.getRange();
  329. return {
  330. start: new Date(range.start),
  331. end: new Date(range.end)
  332. };
  333. };
  334. /**
  335. * Force a redraw of the Core. Can be useful to manually redraw when
  336. * option autoResize=false
  337. */
  338. Core.prototype.redraw = function() {
  339. var resized = false,
  340. options = this.options,
  341. props = this.props,
  342. dom = this.dom;
  343. if (!dom) return; // when destroyed
  344. // update class names
  345. if (options.orientation == 'top') {
  346. util.addClassName(dom.root, 'top');
  347. util.removeClassName(dom.root, 'bottom');
  348. }
  349. else {
  350. util.removeClassName(dom.root, 'top');
  351. util.addClassName(dom.root, 'bottom');
  352. }
  353. // update root width and height options
  354. dom.root.style.maxHeight = util.option.asSize(options.maxHeight, '');
  355. dom.root.style.minHeight = util.option.asSize(options.minHeight, '');
  356. dom.root.style.width = util.option.asSize(options.width, '');
  357. // calculate border widths
  358. props.border.left = (dom.centerContainer.offsetWidth - dom.centerContainer.clientWidth) / 2;
  359. props.border.right = props.border.left;
  360. props.border.top = (dom.centerContainer.offsetHeight - dom.centerContainer.clientHeight) / 2;
  361. props.border.bottom = props.border.top;
  362. var borderRootHeight= dom.root.offsetHeight - dom.root.clientHeight;
  363. var borderRootWidth = dom.root.offsetWidth - dom.root.clientWidth;
  364. // calculate the heights. If any of the side panels is empty, we set the height to
  365. // minus the border width, such that the border will be invisible
  366. props.center.height = dom.center.offsetHeight;
  367. props.left.height = dom.left.offsetHeight;
  368. props.right.height = dom.right.offsetHeight;
  369. props.top.height = dom.top.clientHeight || -props.border.top;
  370. props.bottom.height = dom.bottom.clientHeight || -props.border.bottom;
  371. // TODO: compensate borders when any of the panels is empty.
  372. // apply auto height
  373. // TODO: only calculate autoHeight when needed (else we cause an extra reflow/repaint of the DOM)
  374. var contentHeight = Math.max(props.left.height, props.center.height, props.right.height);
  375. var autoHeight = props.top.height + contentHeight + props.bottom.height +
  376. borderRootHeight + props.border.top + props.border.bottom;
  377. dom.root.style.height = util.option.asSize(options.height, autoHeight + 'px');
  378. // calculate heights of the content panels
  379. props.root.height = dom.root.offsetHeight;
  380. props.background.height = props.root.height - borderRootHeight;
  381. var containerHeight = props.root.height - props.top.height - props.bottom.height -
  382. borderRootHeight;
  383. props.centerContainer.height = containerHeight;
  384. props.leftContainer.height = containerHeight;
  385. props.rightContainer.height = props.leftContainer.height;
  386. // calculate the widths of the panels
  387. props.root.width = dom.root.offsetWidth;
  388. props.background.width = props.root.width - borderRootWidth;
  389. props.left.width = dom.leftContainer.clientWidth || -props.border.left;
  390. props.leftContainer.width = props.left.width;
  391. props.right.width = dom.rightContainer.clientWidth || -props.border.right;
  392. props.rightContainer.width = props.right.width;
  393. var centerWidth = props.root.width - props.left.width - props.right.width - borderRootWidth;
  394. props.center.width = centerWidth;
  395. props.centerContainer.width = centerWidth;
  396. props.top.width = centerWidth;
  397. props.bottom.width = centerWidth;
  398. // resize the panels
  399. dom.background.style.height = props.background.height + 'px';
  400. dom.backgroundVertical.style.height = props.background.height + 'px';
  401. dom.backgroundHorizontal.style.height = props.centerContainer.height + 'px';
  402. dom.centerContainer.style.height = props.centerContainer.height + 'px';
  403. dom.leftContainer.style.height = props.leftContainer.height + 'px';
  404. dom.rightContainer.style.height = props.rightContainer.height + 'px';
  405. dom.background.style.width = props.background.width + 'px';
  406. dom.backgroundVertical.style.width = props.centerContainer.width + 'px';
  407. dom.backgroundHorizontal.style.width = props.background.width + 'px';
  408. dom.centerContainer.style.width = props.center.width + 'px';
  409. dom.top.style.width = props.top.width + 'px';
  410. dom.bottom.style.width = props.bottom.width + 'px';
  411. // reposition the panels
  412. dom.background.style.left = '0';
  413. dom.background.style.top = '0';
  414. dom.backgroundVertical.style.left = props.left.width + 'px';
  415. dom.backgroundVertical.style.top = '0';
  416. dom.backgroundHorizontal.style.left = '0';
  417. dom.backgroundHorizontal.style.top = props.top.height + 'px';
  418. dom.centerContainer.style.left = props.left.width + 'px';
  419. dom.centerContainer.style.top = props.top.height + 'px';
  420. dom.leftContainer.style.left = '0';
  421. dom.leftContainer.style.top = props.top.height + 'px';
  422. dom.rightContainer.style.left = (props.left.width + props.center.width) + 'px';
  423. dom.rightContainer.style.top = props.top.height + 'px';
  424. dom.top.style.left = props.left.width + 'px';
  425. dom.top.style.top = '0';
  426. dom.bottom.style.left = props.left.width + 'px';
  427. dom.bottom.style.top = (props.top.height + props.centerContainer.height) + 'px';
  428. // update the scrollTop, feasible range for the offset can be changed
  429. // when the height of the Core or of the contents of the center changed
  430. this._updateScrollTop();
  431. // reposition the scrollable contents
  432. var offset = this.props.scrollTop;
  433. if (options.orientation == 'bottom') {
  434. offset += Math.max(this.props.centerContainer.height - this.props.center.height -
  435. this.props.border.top - this.props.border.bottom, 0);
  436. }
  437. dom.center.style.left = '0';
  438. dom.center.style.top = offset + 'px';
  439. dom.left.style.left = '0';
  440. dom.left.style.top = offset + 'px';
  441. dom.right.style.left = '0';
  442. dom.right.style.top = offset + 'px';
  443. // show shadows when vertical scrolling is available
  444. var visibilityTop = this.props.scrollTop == 0 ? 'hidden' : '';
  445. var visibilityBottom = this.props.scrollTop == this.props.scrollTopMin ? 'hidden' : '';
  446. dom.shadowTop.style.visibility = visibilityTop;
  447. dom.shadowBottom.style.visibility = visibilityBottom;
  448. dom.shadowTopLeft.style.visibility = visibilityTop;
  449. dom.shadowBottomLeft.style.visibility = visibilityBottom;
  450. dom.shadowTopRight.style.visibility = visibilityTop;
  451. dom.shadowBottomRight.style.visibility = visibilityBottom;
  452. // redraw all components
  453. this.components.forEach(function (component) {
  454. resized = component.redraw() || resized;
  455. });
  456. if (resized) {
  457. // keep repainting until all sizes are settled
  458. this.redraw();
  459. }
  460. };
  461. // TODO: deprecated since version 1.1.0, remove some day
  462. Core.prototype.repaint = function () {
  463. throw new Error('Function repaint is deprecated. Use redraw instead.');
  464. };
  465. /**
  466. * Convert a position on screen (pixels) to a datetime
  467. * @param {int} x Position on the screen in pixels
  468. * @return {Date} time The datetime the corresponds with given position x
  469. * @private
  470. */
  471. // TODO: move this function to Range
  472. Core.prototype._toTime = function(x) {
  473. var conversion = this.range.conversion(this.props.center.width);
  474. return new Date(x / conversion.scale + conversion.offset);
  475. };
  476. /**
  477. * Convert a position on the global screen (pixels) to a datetime
  478. * @param {int} x Position on the screen in pixels
  479. * @return {Date} time The datetime the corresponds with given position x
  480. * @private
  481. */
  482. // TODO: move this function to Range
  483. Core.prototype._toGlobalTime = function(x) {
  484. var conversion = this.range.conversion(this.props.root.width);
  485. return new Date(x / conversion.scale + conversion.offset);
  486. };
  487. /**
  488. * Convert a datetime (Date object) into a position on the screen
  489. * @param {Date} time A date
  490. * @return {int} x The position on the screen in pixels which corresponds
  491. * with the given date.
  492. * @private
  493. */
  494. // TODO: move this function to Range
  495. Core.prototype._toScreen = function(time) {
  496. var conversion = this.range.conversion(this.props.center.width);
  497. return (time.valueOf() - conversion.offset) * conversion.scale;
  498. };
  499. /**
  500. * Convert a datetime (Date object) into a position on the root
  501. * This is used to get the pixel density estimate for the screen, not the center panel
  502. * @param {Date} time A date
  503. * @return {int} x The position on root in pixels which corresponds
  504. * with the given date.
  505. * @private
  506. */
  507. // TODO: move this function to Range
  508. Core.prototype._toGlobalScreen = function(time) {
  509. var conversion = this.range.conversion(this.props.root.width);
  510. return (time.valueOf() - conversion.offset) * conversion.scale;
  511. };
  512. /**
  513. * Initialize watching when option autoResize is true
  514. * @private
  515. */
  516. Core.prototype._initAutoResize = function () {
  517. if (this.options.autoResize == true) {
  518. this._startAutoResize();
  519. }
  520. else {
  521. this._stopAutoResize();
  522. }
  523. };
  524. /**
  525. * Watch for changes in the size of the container. On resize, the Panel will
  526. * automatically redraw itself.
  527. * @private
  528. */
  529. Core.prototype._startAutoResize = function () {
  530. var me = this;
  531. this._stopAutoResize();
  532. this._onResize = function() {
  533. if (me.options.autoResize != true) {
  534. // stop watching when the option autoResize is changed to false
  535. me._stopAutoResize();
  536. return;
  537. }
  538. if (me.dom.root) {
  539. // check whether the frame is resized
  540. if ((me.dom.root.clientWidth != me.props.lastWidth) ||
  541. (me.dom.root.clientHeight != me.props.lastHeight)) {
  542. me.props.lastWidth = me.dom.root.clientWidth;
  543. me.props.lastHeight = me.dom.root.clientHeight;
  544. me.emit('change');
  545. }
  546. }
  547. };
  548. // add event listener to window resize
  549. util.addEventListener(window, 'resize', this._onResize);
  550. this.watchTimer = setInterval(this._onResize, 1000);
  551. };
  552. /**
  553. * Stop watching for a resize of the frame.
  554. * @private
  555. */
  556. Core.prototype._stopAutoResize = function () {
  557. if (this.watchTimer) {
  558. clearInterval(this.watchTimer);
  559. this.watchTimer = undefined;
  560. }
  561. // remove event listener on window.resize
  562. util.removeEventListener(window, 'resize', this._onResize);
  563. this._onResize = null;
  564. };
  565. /**
  566. * Start moving the timeline vertically
  567. * @param {Event} event
  568. * @private
  569. */
  570. Core.prototype._onTouch = function (event) {
  571. this.touch.allowDragging = true;
  572. };
  573. /**
  574. * Start moving the timeline vertically
  575. * @param {Event} event
  576. * @private
  577. */
  578. Core.prototype._onPinch = function (event) {
  579. this.touch.allowDragging = false;
  580. };
  581. /**
  582. * Start moving the timeline vertically
  583. * @param {Event} event
  584. * @private
  585. */
  586. Core.prototype._onDragStart = function (event) {
  587. this.touch.initialScrollTop = this.props.scrollTop;
  588. };
  589. /**
  590. * Move the timeline vertically
  591. * @param {Event} event
  592. * @private
  593. */
  594. Core.prototype._onDrag = function (event) {
  595. // refuse to drag when we where pinching to prevent the timeline make a jump
  596. // when releasing the fingers in opposite order from the touch screen
  597. if (!this.touch.allowDragging) return;
  598. var delta = event.gesture.deltaY;
  599. var oldScrollTop = this._getScrollTop();
  600. var newScrollTop = this._setScrollTop(this.touch.initialScrollTop + delta);
  601. if (newScrollTop != oldScrollTop) {
  602. this.redraw(); // TODO: this causes two redraws when dragging, the other is triggered by rangechange already
  603. }
  604. };
  605. /**
  606. * Apply a scrollTop
  607. * @param {Number} scrollTop
  608. * @returns {Number} scrollTop Returns the applied scrollTop
  609. * @private
  610. */
  611. Core.prototype._setScrollTop = function (scrollTop) {
  612. this.props.scrollTop = scrollTop;
  613. this._updateScrollTop();
  614. return this.props.scrollTop;
  615. };
  616. /**
  617. * Update the current scrollTop when the height of the containers has been changed
  618. * @returns {Number} scrollTop Returns the applied scrollTop
  619. * @private
  620. */
  621. Core.prototype._updateScrollTop = function () {
  622. // recalculate the scrollTopMin
  623. var scrollTopMin = Math.min(this.props.centerContainer.height - this.props.center.height, 0); // is negative or zero
  624. if (scrollTopMin != this.props.scrollTopMin) {
  625. // in case of bottom orientation, change the scrollTop such that the contents
  626. // do not move relative to the time axis at the bottom
  627. if (this.options.orientation == 'bottom') {
  628. this.props.scrollTop += (scrollTopMin - this.props.scrollTopMin);
  629. }
  630. this.props.scrollTopMin = scrollTopMin;
  631. }
  632. // limit the scrollTop to the feasible scroll range
  633. if (this.props.scrollTop > 0) this.props.scrollTop = 0;
  634. if (this.props.scrollTop < scrollTopMin) this.props.scrollTop = scrollTopMin;
  635. return this.props.scrollTop;
  636. };
  637. /**
  638. * Get the current scrollTop
  639. * @returns {number} scrollTop
  640. * @private
  641. */
  642. Core.prototype._getScrollTop = function () {
  643. return this.props.scrollTop;
  644. };
  645. module.exports = Core;