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.

748 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.isActive()) {
  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', 'clickToUse'];
  163. util.selectiveExtend(fields, this.options, options);
  164. if ('clickToUse' in options) {
  165. if (options.clickToUse) {
  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. * Returns true when the Timeline is active.
  191. * @returns {boolean}
  192. */
  193. Core.prototype.isActive = function () {
  194. return !this.activator || this.activator.active;
  195. };
  196. /**
  197. * Destroy the Core, clean up all DOM elements and event listeners.
  198. */
  199. Core.prototype.destroy = function () {
  200. // unbind datasets
  201. this.clear();
  202. // remove all event listeners
  203. this.off();
  204. // stop checking for changed size
  205. this._stopAutoResize();
  206. // remove from DOM
  207. if (this.dom.root.parentNode) {
  208. this.dom.root.parentNode.removeChild(this.dom.root);
  209. }
  210. this.dom = null;
  211. // remove Activator
  212. if (this.activator) {
  213. this.activator.destroy();
  214. delete this.activator;
  215. }
  216. // cleanup hammer touch events
  217. for (var event in this.listeners) {
  218. if (this.listeners.hasOwnProperty(event)) {
  219. delete this.listeners[event];
  220. }
  221. }
  222. this.listeners = null;
  223. this.hammer = null;
  224. // give all components the opportunity to cleanup
  225. this.components.forEach(function (component) {
  226. component.destroy();
  227. });
  228. this.body = null;
  229. };
  230. /**
  231. * Set a custom time bar
  232. * @param {Date} time
  233. */
  234. Core.prototype.setCustomTime = function (time) {
  235. if (!this.customTime) {
  236. throw new Error('Cannot get custom time: Custom time bar is not enabled');
  237. }
  238. this.customTime.setCustomTime(time);
  239. };
  240. /**
  241. * Retrieve the current custom time.
  242. * @return {Date} customTime
  243. */
  244. Core.prototype.getCustomTime = function() {
  245. if (!this.customTime) {
  246. throw new Error('Cannot get custom time: Custom time bar is not enabled');
  247. }
  248. return this.customTime.getCustomTime();
  249. };
  250. /**
  251. * Get the id's of the currently visible items.
  252. * @returns {Array} The ids of the visible items
  253. */
  254. Core.prototype.getVisibleItems = function() {
  255. return this.itemSet && this.itemSet.getVisibleItems() || [];
  256. };
  257. /**
  258. * Clear the Core. By Default, items, groups and options are cleared.
  259. * Example usage:
  260. *
  261. * timeline.clear(); // clear items, groups, and options
  262. * timeline.clear({options: true}); // clear options only
  263. *
  264. * @param {Object} [what] Optionally specify what to clear. By default:
  265. * {items: true, groups: true, options: true}
  266. */
  267. Core.prototype.clear = function(what) {
  268. // clear items
  269. if (!what || what.items) {
  270. this.setItems(null);
  271. }
  272. // clear groups
  273. if (!what || what.groups) {
  274. this.setGroups(null);
  275. }
  276. // clear options of timeline and of each of the components
  277. if (!what || what.options) {
  278. this.components.forEach(function (component) {
  279. component.setOptions(component.defaultOptions);
  280. });
  281. this.setOptions(this.defaultOptions); // this will also do a redraw
  282. }
  283. };
  284. /**
  285. * Set Core window such that it fits all items
  286. */
  287. Core.prototype.fit = function() {
  288. // apply the data range as range
  289. var dataRange = this.getItemRange();
  290. // add 5% space on both sides
  291. var start = dataRange.min;
  292. var end = dataRange.max;
  293. if (start != null && end != null) {
  294. var interval = (end.valueOf() - start.valueOf());
  295. if (interval <= 0) {
  296. // prevent an empty interval
  297. interval = 24 * 60 * 60 * 1000; // 1 day
  298. }
  299. start = new Date(start.valueOf() - interval * 0.05);
  300. end = new Date(end.valueOf() + interval * 0.05);
  301. }
  302. // skip range set if there is no start and end date
  303. if (start === null && end === null) {
  304. return;
  305. }
  306. this.range.setRange(start, end);
  307. };
  308. /**
  309. * Set the visible window. Both parameters are optional, you can change only
  310. * start or only end. Syntax:
  311. *
  312. * TimeLine.setWindow(start, end)
  313. * TimeLine.setWindow(range)
  314. *
  315. * Where start and end can be a Date, number, or string, and range is an
  316. * object with properties start and end.
  317. *
  318. * @param {Date | Number | String | Object} [start] Start date of visible window
  319. * @param {Date | Number | String} [end] End date of visible window
  320. */
  321. Core.prototype.setWindow = function(start, end) {
  322. if (arguments.length == 1) {
  323. var range = arguments[0];
  324. this.range.setRange(range.start, range.end);
  325. }
  326. else {
  327. this.range.setRange(start, end);
  328. }
  329. };
  330. /**
  331. * Get the visible window
  332. * @return {{start: Date, end: Date}} Visible range
  333. */
  334. Core.prototype.getWindow = function() {
  335. var range = this.range.getRange();
  336. return {
  337. start: new Date(range.start),
  338. end: new Date(range.end)
  339. };
  340. };
  341. /**
  342. * Force a redraw of the Core. Can be useful to manually redraw when
  343. * option autoResize=false
  344. */
  345. Core.prototype.redraw = function() {
  346. var resized = false,
  347. options = this.options,
  348. props = this.props,
  349. dom = this.dom;
  350. if (!dom) return; // when destroyed
  351. // update class names
  352. if (options.orientation == 'top') {
  353. util.addClassName(dom.root, 'top');
  354. util.removeClassName(dom.root, 'bottom');
  355. }
  356. else {
  357. util.removeClassName(dom.root, 'top');
  358. util.addClassName(dom.root, 'bottom');
  359. }
  360. // update root width and height options
  361. dom.root.style.maxHeight = util.option.asSize(options.maxHeight, '');
  362. dom.root.style.minHeight = util.option.asSize(options.minHeight, '');
  363. dom.root.style.width = util.option.asSize(options.width, '');
  364. // calculate border widths
  365. props.border.left = (dom.centerContainer.offsetWidth - dom.centerContainer.clientWidth) / 2;
  366. props.border.right = props.border.left;
  367. props.border.top = (dom.centerContainer.offsetHeight - dom.centerContainer.clientHeight) / 2;
  368. props.border.bottom = props.border.top;
  369. var borderRootHeight= dom.root.offsetHeight - dom.root.clientHeight;
  370. var borderRootWidth = dom.root.offsetWidth - dom.root.clientWidth;
  371. // calculate the heights. If any of the side panels is empty, we set the height to
  372. // minus the border width, such that the border will be invisible
  373. props.center.height = dom.center.offsetHeight;
  374. props.left.height = dom.left.offsetHeight;
  375. props.right.height = dom.right.offsetHeight;
  376. props.top.height = dom.top.clientHeight || -props.border.top;
  377. props.bottom.height = dom.bottom.clientHeight || -props.border.bottom;
  378. // TODO: compensate borders when any of the panels is empty.
  379. // apply auto height
  380. // TODO: only calculate autoHeight when needed (else we cause an extra reflow/repaint of the DOM)
  381. var contentHeight = Math.max(props.left.height, props.center.height, props.right.height);
  382. var autoHeight = props.top.height + contentHeight + props.bottom.height +
  383. borderRootHeight + props.border.top + props.border.bottom;
  384. dom.root.style.height = util.option.asSize(options.height, autoHeight + 'px');
  385. // calculate heights of the content panels
  386. props.root.height = dom.root.offsetHeight;
  387. props.background.height = props.root.height - borderRootHeight;
  388. var containerHeight = props.root.height - props.top.height - props.bottom.height -
  389. borderRootHeight;
  390. props.centerContainer.height = containerHeight;
  391. props.leftContainer.height = containerHeight;
  392. props.rightContainer.height = props.leftContainer.height;
  393. // calculate the widths of the panels
  394. props.root.width = dom.root.offsetWidth;
  395. props.background.width = props.root.width - borderRootWidth;
  396. props.left.width = dom.leftContainer.clientWidth || -props.border.left;
  397. props.leftContainer.width = props.left.width;
  398. props.right.width = dom.rightContainer.clientWidth || -props.border.right;
  399. props.rightContainer.width = props.right.width;
  400. var centerWidth = props.root.width - props.left.width - props.right.width - borderRootWidth;
  401. props.center.width = centerWidth;
  402. props.centerContainer.width = centerWidth;
  403. props.top.width = centerWidth;
  404. props.bottom.width = centerWidth;
  405. // resize the panels
  406. dom.background.style.height = props.background.height + 'px';
  407. dom.backgroundVertical.style.height = props.background.height + 'px';
  408. dom.backgroundHorizontal.style.height = props.centerContainer.height + 'px';
  409. dom.centerContainer.style.height = props.centerContainer.height + 'px';
  410. dom.leftContainer.style.height = props.leftContainer.height + 'px';
  411. dom.rightContainer.style.height = props.rightContainer.height + 'px';
  412. dom.background.style.width = props.background.width + 'px';
  413. dom.backgroundVertical.style.width = props.centerContainer.width + 'px';
  414. dom.backgroundHorizontal.style.width = props.background.width + 'px';
  415. dom.centerContainer.style.width = props.center.width + 'px';
  416. dom.top.style.width = props.top.width + 'px';
  417. dom.bottom.style.width = props.bottom.width + 'px';
  418. // reposition the panels
  419. dom.background.style.left = '0';
  420. dom.background.style.top = '0';
  421. dom.backgroundVertical.style.left = props.left.width + 'px';
  422. dom.backgroundVertical.style.top = '0';
  423. dom.backgroundHorizontal.style.left = '0';
  424. dom.backgroundHorizontal.style.top = props.top.height + 'px';
  425. dom.centerContainer.style.left = props.left.width + 'px';
  426. dom.centerContainer.style.top = props.top.height + 'px';
  427. dom.leftContainer.style.left = '0';
  428. dom.leftContainer.style.top = props.top.height + 'px';
  429. dom.rightContainer.style.left = (props.left.width + props.center.width) + 'px';
  430. dom.rightContainer.style.top = props.top.height + 'px';
  431. dom.top.style.left = props.left.width + 'px';
  432. dom.top.style.top = '0';
  433. dom.bottom.style.left = props.left.width + 'px';
  434. dom.bottom.style.top = (props.top.height + props.centerContainer.height) + 'px';
  435. // update the scrollTop, feasible range for the offset can be changed
  436. // when the height of the Core or of the contents of the center changed
  437. this._updateScrollTop();
  438. // reposition the scrollable contents
  439. var offset = this.props.scrollTop;
  440. if (options.orientation == 'bottom') {
  441. offset += Math.max(this.props.centerContainer.height - this.props.center.height -
  442. this.props.border.top - this.props.border.bottom, 0);
  443. }
  444. dom.center.style.left = '0';
  445. dom.center.style.top = offset + 'px';
  446. dom.left.style.left = '0';
  447. dom.left.style.top = offset + 'px';
  448. dom.right.style.left = '0';
  449. dom.right.style.top = offset + 'px';
  450. // show shadows when vertical scrolling is available
  451. var visibilityTop = this.props.scrollTop == 0 ? 'hidden' : '';
  452. var visibilityBottom = this.props.scrollTop == this.props.scrollTopMin ? 'hidden' : '';
  453. dom.shadowTop.style.visibility = visibilityTop;
  454. dom.shadowBottom.style.visibility = visibilityBottom;
  455. dom.shadowTopLeft.style.visibility = visibilityTop;
  456. dom.shadowBottomLeft.style.visibility = visibilityBottom;
  457. dom.shadowTopRight.style.visibility = visibilityTop;
  458. dom.shadowBottomRight.style.visibility = visibilityBottom;
  459. // redraw all components
  460. this.components.forEach(function (component) {
  461. resized = component.redraw() || resized;
  462. });
  463. if (resized) {
  464. // keep repainting until all sizes are settled
  465. this.redraw();
  466. }
  467. };
  468. // TODO: deprecated since version 1.1.0, remove some day
  469. Core.prototype.repaint = function () {
  470. throw new Error('Function repaint is deprecated. Use redraw instead.');
  471. };
  472. /**
  473. * Convert a position on screen (pixels) to a datetime
  474. * @param {int} x Position on the screen in pixels
  475. * @return {Date} time The datetime the corresponds with given position x
  476. * @private
  477. */
  478. // TODO: move this function to Range
  479. Core.prototype._toTime = function(x) {
  480. var conversion = this.range.conversion(this.props.center.width);
  481. return new Date(x / conversion.scale + conversion.offset);
  482. };
  483. /**
  484. * Convert a position on the global screen (pixels) to a datetime
  485. * @param {int} x Position on the screen in pixels
  486. * @return {Date} time The datetime the corresponds with given position x
  487. * @private
  488. */
  489. // TODO: move this function to Range
  490. Core.prototype._toGlobalTime = function(x) {
  491. var conversion = this.range.conversion(this.props.root.width);
  492. return new Date(x / conversion.scale + conversion.offset);
  493. };
  494. /**
  495. * Convert a datetime (Date object) into a position on the screen
  496. * @param {Date} time A date
  497. * @return {int} x The position on the screen in pixels which corresponds
  498. * with the given date.
  499. * @private
  500. */
  501. // TODO: move this function to Range
  502. Core.prototype._toScreen = function(time) {
  503. var conversion = this.range.conversion(this.props.center.width);
  504. return (time.valueOf() - conversion.offset) * conversion.scale;
  505. };
  506. /**
  507. * Convert a datetime (Date object) into a position on the root
  508. * This is used to get the pixel density estimate for the screen, not the center panel
  509. * @param {Date} time A date
  510. * @return {int} x The position on root in pixels which corresponds
  511. * with the given date.
  512. * @private
  513. */
  514. // TODO: move this function to Range
  515. Core.prototype._toGlobalScreen = function(time) {
  516. var conversion = this.range.conversion(this.props.root.width);
  517. return (time.valueOf() - conversion.offset) * conversion.scale;
  518. };
  519. /**
  520. * Initialize watching when option autoResize is true
  521. * @private
  522. */
  523. Core.prototype._initAutoResize = function () {
  524. if (this.options.autoResize == true) {
  525. this._startAutoResize();
  526. }
  527. else {
  528. this._stopAutoResize();
  529. }
  530. };
  531. /**
  532. * Watch for changes in the size of the container. On resize, the Panel will
  533. * automatically redraw itself.
  534. * @private
  535. */
  536. Core.prototype._startAutoResize = function () {
  537. var me = this;
  538. this._stopAutoResize();
  539. this._onResize = function() {
  540. if (me.options.autoResize != true) {
  541. // stop watching when the option autoResize is changed to false
  542. me._stopAutoResize();
  543. return;
  544. }
  545. if (me.dom.root) {
  546. // check whether the frame is resized
  547. if ((me.dom.root.clientWidth != me.props.lastWidth) ||
  548. (me.dom.root.clientHeight != me.props.lastHeight)) {
  549. me.props.lastWidth = me.dom.root.clientWidth;
  550. me.props.lastHeight = me.dom.root.clientHeight;
  551. me.emit('change');
  552. }
  553. }
  554. };
  555. // add event listener to window resize
  556. util.addEventListener(window, 'resize', this._onResize);
  557. this.watchTimer = setInterval(this._onResize, 1000);
  558. };
  559. /**
  560. * Stop watching for a resize of the frame.
  561. * @private
  562. */
  563. Core.prototype._stopAutoResize = function () {
  564. if (this.watchTimer) {
  565. clearInterval(this.watchTimer);
  566. this.watchTimer = undefined;
  567. }
  568. // remove event listener on window.resize
  569. util.removeEventListener(window, 'resize', this._onResize);
  570. this._onResize = null;
  571. };
  572. /**
  573. * Start moving the timeline vertically
  574. * @param {Event} event
  575. * @private
  576. */
  577. Core.prototype._onTouch = function (event) {
  578. this.touch.allowDragging = true;
  579. };
  580. /**
  581. * Start moving the timeline vertically
  582. * @param {Event} event
  583. * @private
  584. */
  585. Core.prototype._onPinch = function (event) {
  586. this.touch.allowDragging = false;
  587. };
  588. /**
  589. * Start moving the timeline vertically
  590. * @param {Event} event
  591. * @private
  592. */
  593. Core.prototype._onDragStart = function (event) {
  594. this.touch.initialScrollTop = this.props.scrollTop;
  595. };
  596. /**
  597. * Move the timeline vertically
  598. * @param {Event} event
  599. * @private
  600. */
  601. Core.prototype._onDrag = function (event) {
  602. // refuse to drag when we where pinching to prevent the timeline make a jump
  603. // when releasing the fingers in opposite order from the touch screen
  604. if (!this.touch.allowDragging) return;
  605. var delta = event.gesture.deltaY;
  606. var oldScrollTop = this._getScrollTop();
  607. var newScrollTop = this._setScrollTop(this.touch.initialScrollTop + delta);
  608. if (newScrollTop != oldScrollTop) {
  609. this.redraw(); // TODO: this causes two redraws when dragging, the other is triggered by rangechange already
  610. }
  611. };
  612. /**
  613. * Apply a scrollTop
  614. * @param {Number} scrollTop
  615. * @returns {Number} scrollTop Returns the applied scrollTop
  616. * @private
  617. */
  618. Core.prototype._setScrollTop = function (scrollTop) {
  619. this.props.scrollTop = scrollTop;
  620. this._updateScrollTop();
  621. return this.props.scrollTop;
  622. };
  623. /**
  624. * Update the current scrollTop when the height of the containers has been changed
  625. * @returns {Number} scrollTop Returns the applied scrollTop
  626. * @private
  627. */
  628. Core.prototype._updateScrollTop = function () {
  629. // recalculate the scrollTopMin
  630. var scrollTopMin = Math.min(this.props.centerContainer.height - this.props.center.height, 0); // is negative or zero
  631. if (scrollTopMin != this.props.scrollTopMin) {
  632. // in case of bottom orientation, change the scrollTop such that the contents
  633. // do not move relative to the time axis at the bottom
  634. if (this.options.orientation == 'bottom') {
  635. this.props.scrollTop += (scrollTopMin - this.props.scrollTopMin);
  636. }
  637. this.props.scrollTopMin = scrollTopMin;
  638. }
  639. // limit the scrollTop to the feasible scroll range
  640. if (this.props.scrollTop > 0) this.props.scrollTop = 0;
  641. if (this.props.scrollTop < scrollTopMin) this.props.scrollTop = scrollTopMin;
  642. return this.props.scrollTop;
  643. };
  644. /**
  645. * Get the current scrollTop
  646. * @returns {number} scrollTop
  647. * @private
  648. */
  649. Core.prototype._getScrollTop = function () {
  650. return this.props.scrollTop;
  651. };
  652. module.exports = Core;