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.

1373 lines
48 KiB

  1. var Emitter = require('emitter-component');
  2. var Hammer = require('../module/hammer');
  3. var hammerUtil = require('../hammerUtil');
  4. var util = require('../util');
  5. var TimeAxis = require('./component/TimeAxis');
  6. var Activator = require('../shared/Activator');
  7. var DateUtil = require('./DateUtil');
  8. var CustomTime = require('./component/CustomTime');
  9. /**
  10. * Create a timeline visualization
  11. * @constructor Core
  12. */
  13. function Core () {}
  14. // turn Core into an event emitter
  15. Emitter(Core.prototype);
  16. /**
  17. * Create the main DOM for the Core: a root panel containing left, right,
  18. * top, bottom, content, and background panel.
  19. * @param {Element} container The container element where the Core will
  20. * be attached.
  21. * @protected
  22. */
  23. Core.prototype._create = function (container) {
  24. this.dom = {};
  25. this.dom.container = container;
  26. this.dom.container.style.position = 'relative';
  27. this.dom.root = document.createElement('div');
  28. this.dom.background = document.createElement('div');
  29. this.dom.backgroundVertical = document.createElement('div');
  30. this.dom.backgroundHorizontal = document.createElement('div');
  31. this.dom.centerContainer = document.createElement('div');
  32. this.dom.leftContainer = document.createElement('div');
  33. this.dom.rightContainer = document.createElement('div');
  34. this.dom.center = document.createElement('div');
  35. this.dom.left = document.createElement('div');
  36. this.dom.right = document.createElement('div');
  37. this.dom.top = document.createElement('div');
  38. this.dom.bottom = document.createElement('div');
  39. this.dom.shadowTop = document.createElement('div');
  40. this.dom.shadowBottom = document.createElement('div');
  41. this.dom.shadowTopLeft = document.createElement('div');
  42. this.dom.shadowBottomLeft = document.createElement('div');
  43. this.dom.shadowTopRight = document.createElement('div');
  44. this.dom.shadowBottomRight = document.createElement('div');
  45. this.dom.rollingModeBtn = document.createElement('div');
  46. this.dom.loadingScreen = document.createElement('div');
  47. this.dom.root.className = 'vis-timeline';
  48. this.dom.background.className = 'vis-panel vis-background';
  49. this.dom.backgroundVertical.className = 'vis-panel vis-background vis-vertical';
  50. this.dom.backgroundHorizontal.className = 'vis-panel vis-background vis-horizontal';
  51. this.dom.centerContainer.className = 'vis-panel vis-center';
  52. this.dom.leftContainer.className = 'vis-panel vis-left';
  53. this.dom.rightContainer.className = 'vis-panel vis-right';
  54. this.dom.top.className = 'vis-panel vis-top';
  55. this.dom.bottom.className = 'vis-panel vis-bottom';
  56. this.dom.left.className = 'vis-content';
  57. this.dom.center.className = 'vis-content';
  58. this.dom.right.className = 'vis-content';
  59. this.dom.shadowTop.className = 'vis-shadow vis-top';
  60. this.dom.shadowBottom.className = 'vis-shadow vis-bottom';
  61. this.dom.shadowTopLeft.className = 'vis-shadow vis-top';
  62. this.dom.shadowBottomLeft.className = 'vis-shadow vis-bottom';
  63. this.dom.shadowTopRight.className = 'vis-shadow vis-top';
  64. this.dom.shadowBottomRight.className = 'vis-shadow vis-bottom';
  65. this.dom.rollingModeBtn.className = 'vis-rolling-mode-btn';
  66. this.dom.loadingScreen.className = 'vis-loading-screen';
  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.root.appendChild(this.dom.rollingModeBtn);
  76. this.dom.centerContainer.appendChild(this.dom.center);
  77. this.dom.leftContainer.appendChild(this.dom.left);
  78. this.dom.rightContainer.appendChild(this.dom.right);
  79. this.dom.centerContainer.appendChild(this.dom.shadowTop);
  80. this.dom.centerContainer.appendChild(this.dom.shadowBottom);
  81. this.dom.leftContainer.appendChild(this.dom.shadowTopLeft);
  82. this.dom.leftContainer.appendChild(this.dom.shadowBottomLeft);
  83. this.dom.rightContainer.appendChild(this.dom.shadowTopRight);
  84. this.dom.rightContainer.appendChild(this.dom.shadowBottomRight);
  85. // size properties of each of the panels
  86. this.props = {
  87. root: {},
  88. background: {},
  89. centerContainer: {},
  90. leftContainer: {},
  91. rightContainer: {},
  92. center: {},
  93. left: {},
  94. right: {},
  95. top: {},
  96. bottom: {},
  97. border: {},
  98. scrollTop: 0,
  99. scrollTopMin: 0
  100. };
  101. this.on('rangechange', function () {
  102. if (this.initialDrawDone === true) {
  103. this._redraw();
  104. }
  105. }.bind(this));
  106. this.on('rangechanged', function () {
  107. if (!this.initialRangeChangeDone) {
  108. this.initialRangeChangeDone = true;
  109. }
  110. }.bind(this));
  111. this.on('touch', this._onTouch.bind(this));
  112. this.on('panmove', this._onDrag.bind(this));
  113. var me = this;
  114. this._origRedraw = this._redraw.bind(this);
  115. this._redraw = util.throttle(this._origRedraw);
  116. this.on('_change', function (properties) {
  117. if (me.itemSet && me.itemSet.initialItemSetDrawn && properties && properties.queue == true) {
  118. me._redraw()
  119. } else {
  120. me._origRedraw();
  121. }
  122. });
  123. // create event listeners for all interesting events, these events will be
  124. // emitted via emitter
  125. this.hammer = new Hammer(this.dom.root);
  126. var pinchRecognizer = this.hammer.get('pinch').set({enable: true});
  127. pinchRecognizer && hammerUtil.disablePreventDefaultVertically(pinchRecognizer);
  128. this.hammer.get('pan').set({threshold:5, direction: Hammer.DIRECTION_HORIZONTAL});
  129. this.listeners = {};
  130. var events = [
  131. 'tap', 'doubletap', 'press',
  132. 'pinch',
  133. 'pan', 'panstart', 'panmove', 'panend'
  134. // TODO: cleanup
  135. //'touch', 'pinch',
  136. //'tap', 'doubletap', 'hold',
  137. //'dragstart', 'drag', 'dragend',
  138. //'mousewheel', 'DOMMouseScroll' // DOMMouseScroll is needed for Firefox
  139. ];
  140. events.forEach(function (type) {
  141. var listener = function (event) {
  142. if (me.isActive()) {
  143. me.emit(type, event);
  144. }
  145. };
  146. me.hammer.on(type, listener);
  147. me.listeners[type] = listener;
  148. });
  149. // emulate a touch event (emitted before the start of a pan, pinch, tap, or press)
  150. hammerUtil.onTouch(this.hammer, function (event) {
  151. me.emit('touch', event);
  152. }.bind(this));
  153. // emulate a release event (emitted after a pan, pinch, tap, or press)
  154. hammerUtil.onRelease(this.hammer, function (event) {
  155. me.emit('release', event);
  156. }.bind(this));
  157. /**
  158. *
  159. * @param {WheelEvent} event
  160. */
  161. function onMouseWheel(event) {
  162. // Reasonable default wheel deltas
  163. const LINE_HEIGHT = 40;
  164. const PAGE_HEIGHT = 800;
  165. if (this.isActive()) {
  166. this.emit('mousewheel', event);
  167. }
  168. // deltaX and deltaY normalization from jquery.mousewheel.js
  169. var deltaX = 0;
  170. var deltaY = 0;
  171. // Old school scrollwheel delta
  172. if ( 'detail' in event ) { deltaY = event.detail * -1; }
  173. if ( 'wheelDelta' in event ) { deltaY = event.wheelDelta; }
  174. if ( 'wheelDeltaY' in event ) { deltaY = event.wheelDeltaY; }
  175. if ( 'wheelDeltaX' in event ) { deltaX = event.wheelDeltaX * -1; }
  176. // Firefox < 17 horizontal scrolling related to DOMMouseScroll event
  177. if ( 'axis' in event && event.axis === event.HORIZONTAL_AXIS ) {
  178. deltaX = deltaY * -1;
  179. deltaY = 0;
  180. }
  181. // New school wheel delta (wheel event)
  182. if ( 'deltaY' in event ) {
  183. deltaY = event.deltaY * -1;
  184. }
  185. if ( 'deltaX' in event ) {
  186. deltaX = event.deltaX;
  187. }
  188. // Normalize deltas
  189. if (event.deltaMode) {
  190. if (event.deltaMode === 1) { // delta in LINE units
  191. deltaX *= LINE_HEIGHT;
  192. deltaY *= LINE_HEIGHT;
  193. } else { // delta in PAGE units
  194. deltaX *= LINE_HEIGHT;
  195. deltaY *= PAGE_HEIGHT;
  196. }
  197. }
  198. // Prevent scrolling when zooming (no zoom key, or pressing zoom key)
  199. if (!this.options.zoomKey || event[this.options.zoomKey]) return;
  200. // Don't preventDefault if you can't scroll
  201. if (!this.options.verticalScroll && !this.options.horizontalScroll) return;
  202. // Prevent default actions caused by mouse wheel
  203. // (else the page and timeline both scroll)
  204. event.preventDefault();
  205. if (this.options.verticalScroll && Math.abs(deltaY) >= Math.abs(deltaX)) {
  206. var current = this.props.scrollTop;
  207. var adjusted = current + deltaY;
  208. if (this.isActive()) {
  209. this._setScrollTop(adjusted);
  210. this._redraw();
  211. this.emit('scroll', event);
  212. }
  213. } else if (this.options.horizontalScroll) {
  214. var delta = Math.abs(deltaX) >= Math.abs(deltaY) ? deltaX : deltaY;
  215. // calculate a single scroll jump relative to the range scale
  216. var diff = (delta / 120) * (this.range.end - this.range.start) / 20;
  217. // calculate new start and end
  218. var newStart = this.range.start + diff;
  219. var newEnd = this.range.end + diff;
  220. var options = {
  221. animation: false,
  222. byUser: true,
  223. event: event
  224. };
  225. this.range.setRange(newStart, newEnd, options);
  226. }
  227. }
  228. // Add modern wheel event listener
  229. if (this.dom.centerContainer.addEventListener) {
  230. const wheel = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support "wheel"
  231. document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel"
  232. "DOMMouseScroll"; // Older Firefox versions like "DOMMouseScroll"
  233. this.dom.centerContainer.addEventListener(wheel, onMouseWheel.bind(this), false);
  234. } else {
  235. // IE 6/7/8
  236. this.dom.centerContainer.attachEvent("onmousewheel", onMouseWheel.bind(this));
  237. }
  238. /**
  239. *
  240. * @param {scroll} event
  241. */
  242. function onMouseScrollSide(event) {
  243. if (!me.options.verticalScroll) return;
  244. event.preventDefault();
  245. if (me.isActive()) {
  246. var adjusted = -event.target.scrollTop;
  247. me._setScrollTop(adjusted);
  248. me._redraw();
  249. me.emit('scrollSide', event);
  250. }
  251. }
  252. this.dom.left.parentNode.addEventListener('scroll', onMouseScrollSide.bind(this));
  253. this.dom.right.parentNode.addEventListener('scroll', onMouseScrollSide.bind(this));
  254. var itemAddedToTimeline = false;
  255. /**
  256. *
  257. * @param {dragover} event
  258. * @returns {boolean}
  259. */
  260. function handleDragOver(event) {
  261. if (event.preventDefault) {
  262. event.preventDefault(); // Necessary. Allows us to drop.
  263. }
  264. // make sure your target is a vis element
  265. if (!event.target.className.indexOf("vis") > -1) return;
  266. // make sure only one item is added every time you're over the timeline
  267. if (itemAddedToTimeline) return;
  268. event.dataTransfer.dropEffect = 'move';
  269. itemAddedToTimeline = true;
  270. return false;
  271. }
  272. /**
  273. *
  274. * @param {drop} event
  275. * @returns {boolean}
  276. */
  277. function handleDrop(event) {
  278. // prevent redirect to blank page - Firefox
  279. if(event.preventDefault) { event.preventDefault(); }
  280. if(event.stopPropagation) { event.stopPropagation(); }
  281. // return when dropping non-vis items
  282. try {
  283. var itemData = JSON.parse(event.dataTransfer.getData("text"))
  284. if (!itemData || !itemData.content) return
  285. } catch (err) {
  286. return false;
  287. }
  288. itemAddedToTimeline = false;
  289. event.center = {
  290. x: event.clientX,
  291. y: event.clientY
  292. };
  293. if (itemData.target !== 'item') {
  294. me.itemSet._onAddItem(event);
  295. } else {
  296. me.itemSet._onDropObjectOnItem(event);
  297. }
  298. me.emit('drop', me.getEventProperties(event))
  299. return false;
  300. }
  301. this.dom.center.addEventListener('dragover', handleDragOver.bind(this), false);
  302. this.dom.center.addEventListener('drop', handleDrop.bind(this), false);
  303. this.customTimes = [];
  304. // store state information needed for touch events
  305. this.touch = {};
  306. this.redrawCount = 0;
  307. this.initialDrawDone = false;
  308. this.initialRangeChangeDone = false;
  309. // attach the root panel to the provided container
  310. if (!container) throw new Error('No container provided');
  311. container.appendChild(this.dom.root);
  312. container.appendChild(this.dom.loadingScreen);
  313. };
  314. /**
  315. * Set options. Options will be passed to all components loaded in the Timeline.
  316. * @param {Object} [options]
  317. * {String} orientation
  318. * Vertical orientation for the Timeline,
  319. * can be 'bottom' (default) or 'top'.
  320. * {string | number} width
  321. * Width for the timeline, a number in pixels or
  322. * a css string like '1000px' or '75%'. '100%' by default.
  323. * {string | number} height
  324. * Fixed height for the Timeline, a number in pixels or
  325. * a css string like '400px' or '75%'. If undefined,
  326. * The Timeline will automatically size such that
  327. * its contents fit.
  328. * {string | number} minHeight
  329. * Minimum height for the Timeline, a number in pixels or
  330. * a css string like '400px' or '75%'.
  331. * {string | number} maxHeight
  332. * Maximum height for the Timeline, a number in pixels or
  333. * a css string like '400px' or '75%'.
  334. * {number | Date | string} start
  335. * Start date for the visible window
  336. * {number | Date | string} end
  337. * End date for the visible window
  338. */
  339. Core.prototype.setOptions = function (options) {
  340. if (options) {
  341. // copy the known options
  342. var fields = [
  343. 'width', 'height', 'minHeight', 'maxHeight', 'autoResize',
  344. 'start', 'end', 'clickToUse', 'dataAttributes', 'hiddenDates',
  345. 'locale', 'locales', 'moment', 'rtl', 'zoomKey', 'horizontalScroll', 'verticalScroll'
  346. ];
  347. util.selectiveExtend(fields, this.options, options);
  348. this.dom.rollingModeBtn.style.visibility = 'hidden';
  349. if (this.options.rtl) {
  350. this.dom.container.style.direction = "rtl";
  351. this.dom.backgroundVertical.className = 'vis-panel vis-background vis-vertical-rtl';
  352. }
  353. if (this.options.verticalScroll) {
  354. if (this.options.rtl) {
  355. this.dom.rightContainer.className = 'vis-panel vis-right vis-vertical-scroll';
  356. } else {
  357. this.dom.leftContainer.className = 'vis-panel vis-left vis-vertical-scroll';
  358. }
  359. }
  360. if (typeof this.options.orientation !== 'object') {
  361. this.options.orientation = {item:undefined,axis:undefined};
  362. }
  363. if ('orientation' in options) {
  364. if (typeof options.orientation === 'string') {
  365. this.options.orientation = {
  366. item: options.orientation,
  367. axis: options.orientation
  368. };
  369. }
  370. else if (typeof options.orientation === 'object') {
  371. if ('item' in options.orientation) {
  372. this.options.orientation.item = options.orientation.item;
  373. }
  374. if ('axis' in options.orientation) {
  375. this.options.orientation.axis = options.orientation.axis;
  376. }
  377. }
  378. }
  379. if (this.options.orientation.axis === 'both') {
  380. if (!this.timeAxis2) {
  381. var timeAxis2 = this.timeAxis2 = new TimeAxis(this.body);
  382. timeAxis2.setOptions = function (options) {
  383. var _options = options ? util.extend({}, options) : {};
  384. _options.orientation = 'top'; // override the orientation option, always top
  385. TimeAxis.prototype.setOptions.call(timeAxis2, _options);
  386. };
  387. this.components.push(timeAxis2);
  388. }
  389. }
  390. else {
  391. if (this.timeAxis2) {
  392. var index = this.components.indexOf(this.timeAxis2);
  393. if (index !== -1) {
  394. this.components.splice(index, 1);
  395. }
  396. this.timeAxis2.destroy();
  397. this.timeAxis2 = null;
  398. }
  399. }
  400. // if the graph2d's drawPoints is a function delegate the callback to the onRender property
  401. if (typeof options.drawPoints == 'function') {
  402. options.drawPoints = {
  403. onRender: options.drawPoints
  404. };
  405. }
  406. if ('hiddenDates' in this.options) {
  407. DateUtil.convertHiddenOptions(this.options.moment, this.body, this.options.hiddenDates);
  408. }
  409. if ('clickToUse' in options) {
  410. if (options.clickToUse) {
  411. if (!this.activator) {
  412. this.activator = new Activator(this.dom.root);
  413. }
  414. }
  415. else {
  416. if (this.activator) {
  417. this.activator.destroy();
  418. delete this.activator;
  419. }
  420. }
  421. }
  422. if ('showCustomTime' in options) {
  423. throw new Error('Option `showCustomTime` is deprecated. Create a custom time bar via timeline.addCustomTime(time [, id])');
  424. }
  425. // enable/disable autoResize
  426. this._initAutoResize();
  427. }
  428. // propagate options to all components
  429. this.components.forEach(component => component.setOptions(options));
  430. // enable/disable configure
  431. if ('configure' in options) {
  432. if (!this.configurator) {
  433. this.configurator = this._createConfigurator();
  434. }
  435. this.configurator.setOptions(options.configure);
  436. // collect the settings of all components, and pass them to the configuration system
  437. var appliedOptions = util.deepExtend({}, this.options);
  438. this.components.forEach(function (component) {
  439. util.deepExtend(appliedOptions, component.options);
  440. });
  441. this.configurator.setModuleOptions({global: appliedOptions});
  442. }
  443. this._redraw();
  444. };
  445. /**
  446. * Returns true when the Timeline is active.
  447. * @returns {boolean}
  448. */
  449. Core.prototype.isActive = function () {
  450. return !this.activator || this.activator.active;
  451. };
  452. /**
  453. * Destroy the Core, clean up all DOM elements and event listeners.
  454. */
  455. Core.prototype.destroy = function () {
  456. // unbind datasets
  457. this.setItems(null);
  458. this.setGroups(null);
  459. // remove all event listeners
  460. this.off();
  461. // stop checking for changed size
  462. this._stopAutoResize();
  463. // remove from DOM
  464. if (this.dom.root.parentNode) {
  465. this.dom.root.parentNode.removeChild(this.dom.root);
  466. }
  467. this.dom = null;
  468. // remove Activator
  469. if (this.activator) {
  470. this.activator.destroy();
  471. delete this.activator;
  472. }
  473. // cleanup hammer touch events
  474. for (var event in this.listeners) {
  475. if (this.listeners.hasOwnProperty(event)) {
  476. delete this.listeners[event];
  477. }
  478. }
  479. this.listeners = null;
  480. this.hammer && this.hammer.destroy();
  481. this.hammer = null;
  482. // give all components the opportunity to cleanup
  483. this.components.forEach(component => component.destroy());
  484. this.body = null;
  485. };
  486. /**
  487. * Set a custom time bar
  488. * @param {Date} time
  489. * @param {number} [id=undefined] Optional id of the custom time bar to be adjusted.
  490. */
  491. Core.prototype.setCustomTime = function (time, id) {
  492. var customTimes = this.customTimes.filter(function (component) {
  493. return id === component.options.id;
  494. });
  495. if (customTimes.length === 0) {
  496. throw new Error('No custom time bar found with id ' + JSON.stringify(id))
  497. }
  498. if (customTimes.length > 0) {
  499. customTimes[0].setCustomTime(time);
  500. }
  501. };
  502. /**
  503. * Retrieve the current custom time.
  504. * @param {number} [id=undefined] Id of the custom time bar.
  505. * @return {Date | undefined} customTime
  506. */
  507. Core.prototype.getCustomTime = function(id) {
  508. var customTimes = this.customTimes.filter(function (component) {
  509. return component.options.id === id;
  510. });
  511. if (customTimes.length === 0) {
  512. throw new Error('No custom time bar found with id ' + JSON.stringify(id))
  513. }
  514. return customTimes[0].getCustomTime();
  515. };
  516. /**
  517. * Set a custom title for the custom time bar.
  518. * @param {string} [title] Custom title
  519. * @param {number} [id=undefined] Id of the custom time bar.
  520. * @returns {*}
  521. */
  522. Core.prototype.setCustomTimeTitle = function(title, id) {
  523. var customTimes = this.customTimes.filter(function (component) {
  524. return component.options.id === id;
  525. });
  526. if (customTimes.length === 0) {
  527. throw new Error('No custom time bar found with id ' + JSON.stringify(id))
  528. }
  529. if (customTimes.length > 0) {
  530. return customTimes[0].setCustomTitle(title);
  531. }
  532. };
  533. /**
  534. * Retrieve meta information from an event.
  535. * Should be overridden by classes extending Core
  536. * @param {Event} event
  537. * @return {Object} An object with related information.
  538. */
  539. Core.prototype.getEventProperties = function (event) {
  540. return { event: event };
  541. };
  542. /**
  543. * Add custom vertical bar
  544. * @param {Date | string | number} [time] A Date, unix timestamp, or
  545. * ISO date string. Time point where
  546. * the new bar should be placed.
  547. * If not provided, `new Date()` will
  548. * be used.
  549. * @param {number | string} [id=undefined] Id of the new bar. Optional
  550. * @param {object} [options={}] Control options for the new bar. Supported
  551. * optoins are:
  552. * editable {true, false} determines whether the
  553. * bar can be dragged by the user. Default is true.
  554. * @return {number | string} Returns the id of the new bar
  555. */
  556. Core.prototype.addCustomTime = function (time, id, options) {
  557. var timestamp = time !== undefined
  558. ? util.convert(time, 'Date').valueOf()
  559. : new Date();
  560. var exists = this.customTimes.some(function (customTime) {
  561. return customTime.options.id === id;
  562. });
  563. if (exists) {
  564. throw new Error('A custom time with id ' + JSON.stringify(id) + ' already exists');
  565. }
  566. var customTime = new CustomTime(this.body, util.extend({}, this.options, options, {
  567. time : timestamp,
  568. id : id
  569. }));
  570. this.customTimes.push(customTime);
  571. this.components.push(customTime);
  572. this._redraw();
  573. return id;
  574. };
  575. /**
  576. * Remove previously added custom bar
  577. * @param {int} id ID of the custom bar to be removed
  578. * [at]returns {boolean} True if the bar exists and is removed, false otherwise
  579. */
  580. Core.prototype.removeCustomTime = function (id) {
  581. var customTimes = this.customTimes.filter(function (bar) {
  582. return (bar.options.id === id);
  583. });
  584. if (customTimes.length === 0) {
  585. throw new Error('No custom time bar found with id ' + JSON.stringify(id))
  586. }
  587. customTimes.forEach(function (customTime) {
  588. this.customTimes.splice(this.customTimes.indexOf(customTime), 1);
  589. this.components.splice(this.components.indexOf(customTime), 1);
  590. customTime.destroy();
  591. }.bind(this))
  592. };
  593. /**
  594. * Get the id's of the currently visible items.
  595. * @returns {Array} The ids of the visible items
  596. */
  597. Core.prototype.getVisibleItems = function() {
  598. return this.itemSet && this.itemSet.getVisibleItems() || [];
  599. };
  600. /**
  601. * Get the id's of the currently visible groups.
  602. * @returns {Array} The ids of the visible groups
  603. */
  604. Core.prototype.getVisibleGroups = function() {
  605. return this.itemSet && this.itemSet.getVisibleGroups() || [];
  606. };
  607. /**
  608. * Set Core window such that it fits all items
  609. * @param {Object} [options] Available options:
  610. * `animation: boolean | {duration: number, easingFunction: string}`
  611. * If true (default), the range is animated
  612. * smoothly to the new window. An object can be
  613. * provided to specify duration and easing function.
  614. * Default duration is 500 ms, and default easing
  615. * function is 'easeInOutQuad'.
  616. * @param {function} [callback] a callback funtion to be executed at the end of this function
  617. */
  618. Core.prototype.fit = function(options, callback) {
  619. var range = this.getDataRange();
  620. // skip range set if there is no min and max date
  621. if (range.min === null && range.max === null) {
  622. return;
  623. }
  624. // apply a margin of 1% left and right of the data
  625. var interval = range.max - range.min;
  626. var min = new Date(range.min.valueOf() - interval * 0.01);
  627. var max = new Date(range.max.valueOf() + interval * 0.01);
  628. var animation = (options && options.animation !== undefined) ? options.animation : true;
  629. this.range.setRange(min, max, { animation: animation }, callback);
  630. };
  631. /**
  632. * Calculate the data range of the items start and end dates
  633. * [at]returns {{min: [Date], max: [Date]}}
  634. * @protected
  635. */
  636. Core.prototype.getDataRange = function() {
  637. // must be implemented by Timeline and Graph2d
  638. throw new Error('Cannot invoke abstract method getDataRange');
  639. };
  640. /**
  641. * Set the visible window. Both parameters are optional, you can change only
  642. * start or only end. Syntax:
  643. *
  644. * TimeLine.setWindow(start, end)
  645. * TimeLine.setWindow(start, end, options)
  646. * TimeLine.setWindow(range)
  647. *
  648. * Where start and end can be a Date, number, or string, and range is an
  649. * object with properties start and end.
  650. *
  651. * @param {Date | number | string | Object} [start] Start date of visible window
  652. * @param {Date | number | string} [end] End date of visible window
  653. * @param {Object} [options] Available options:
  654. * `animation: boolean | {duration: number, easingFunction: string}`
  655. * If true (default), the range is animated
  656. * smoothly to the new window. An object can be
  657. * provided to specify duration and easing function.
  658. * Default duration is 500 ms, and default easing
  659. * function is 'easeInOutQuad'.
  660. * @param {function} [callback] a callback funtion to be executed at the end of this function
  661. */
  662. Core.prototype.setWindow = function(start, end, options, callback) {
  663. if (typeof arguments[2] == "function") {
  664. callback = arguments[2];
  665. options = {};
  666. }
  667. var animation;
  668. var range;
  669. if (arguments.length == 1) {
  670. range = arguments[0];
  671. animation = (range.animation !== undefined) ? range.animation : true;
  672. this.range.setRange(range.start, range.end, { animation: animation });
  673. }
  674. else if (arguments.length == 2 && typeof arguments[1] == "function") {
  675. range = arguments[0];
  676. callback = arguments[1];
  677. animation = (range.animation !== undefined) ? range.animation : true;
  678. this.range.setRange(range.start, range.end, { animation: animation }, callback);
  679. }
  680. else {
  681. animation = (options && options.animation !== undefined) ? options.animation : true;
  682. this.range.setRange(start, end, { animation: animation }, callback);
  683. }
  684. };
  685. /**
  686. * Move the window such that given time is centered on screen.
  687. * @param {Date | number | string} time
  688. * @param {Object} [options] Available options:
  689. * `animation: boolean | {duration: number, easingFunction: string}`
  690. * If true (default), the range is animated
  691. * smoothly to the new window. An object can be
  692. * provided to specify duration and easing function.
  693. * Default duration is 500 ms, and default easing
  694. * function is 'easeInOutQuad'.
  695. * @param {function} [callback] a callback funtion to be executed at the end of this function
  696. */
  697. Core.prototype.moveTo = function(time, options, callback) {
  698. if (typeof arguments[1] == "function") {
  699. callback = arguments[1];
  700. options = {};
  701. }
  702. var interval = this.range.end - this.range.start;
  703. var t = util.convert(time, 'Date').valueOf();
  704. var start = t - interval / 2;
  705. var end = t + interval / 2;
  706. var animation = (options && options.animation !== undefined) ? options.animation : true;
  707. this.range.setRange(start, end, { animation: animation }, callback);
  708. };
  709. /**
  710. * Get the visible window
  711. * @return {{start: Date, end: Date}} Visible range
  712. */
  713. Core.prototype.getWindow = function() {
  714. var range = this.range.getRange();
  715. return {
  716. start: new Date(range.start),
  717. end: new Date(range.end)
  718. };
  719. };
  720. /**
  721. * Zoom in the window such that given time is centered on screen.
  722. * @param {number} percentage - must be between [0..1]
  723. * @param {Object} [options] Available options:
  724. * `animation: boolean | {duration: number, easingFunction: string}`
  725. * If true (default), the range is animated
  726. * smoothly to the new window. An object can be
  727. * provided to specify duration and easing function.
  728. * Default duration is 500 ms, and default easing
  729. * function is 'easeInOutQuad'.
  730. * @param {function} [callback] a callback funtion to be executed at the end of this function
  731. */
  732. Core.prototype.zoomIn = function(percentage, options, callback) {
  733. if (!percentage || percentage < 0 || percentage > 1) return;
  734. if (typeof arguments[1] == "function") {
  735. callback = arguments[1];
  736. options = {};
  737. }
  738. var range = this.getWindow();
  739. var start = range.start.valueOf();
  740. var end = range.end.valueOf();
  741. var interval = end - start;
  742. var newInterval = interval / (1 + percentage);
  743. var distance = (interval - newInterval) / 2;
  744. var newStart = start + distance;
  745. var newEnd = end - distance;
  746. this.setWindow(newStart, newEnd, options, callback);
  747. };
  748. /**
  749. * Zoom out the window such that given time is centered on screen.
  750. * @param {number} percentage - must be between [0..1]
  751. * @param {Object} [options] Available options:
  752. * `animation: boolean | {duration: number, easingFunction: string}`
  753. * If true (default), the range is animated
  754. * smoothly to the new window. An object can be
  755. * provided to specify duration and easing function.
  756. * Default duration is 500 ms, and default easing
  757. * function is 'easeInOutQuad'.
  758. * @param {function} [callback] a callback funtion to be executed at the end of this function
  759. */
  760. Core.prototype.zoomOut = function(percentage, options, callback) {
  761. if (!percentage || percentage < 0 || percentage > 1) return
  762. if (typeof arguments[1] == "function") {
  763. callback = arguments[1];
  764. options = {};
  765. }
  766. var range = this.getWindow();
  767. var start = range.start.valueOf();
  768. var end = range.end.valueOf();
  769. var interval = end - start;
  770. var newStart = start - interval * percentage / 2;
  771. var newEnd = end + interval * percentage / 2;
  772. this.setWindow(newStart, newEnd, options, callback);
  773. };
  774. /**
  775. * Force a redraw. Can be overridden by implementations of Core
  776. *
  777. * Note: this function will be overridden on construction with a trottled version
  778. */
  779. Core.prototype.redraw = function() {
  780. this._redraw();
  781. };
  782. /**
  783. * Redraw for internal use. Redraws all components. See also the public
  784. * method redraw.
  785. * @protected
  786. */
  787. Core.prototype._redraw = function() {
  788. this.redrawCount++;
  789. var resized = false;
  790. var options = this.options;
  791. var props = this.props;
  792. var dom = this.dom;
  793. if (!dom || !dom.container || dom.root.offsetWidth == 0) return; // when destroyed, or invisible
  794. DateUtil.updateHiddenDates(this.options.moment, this.body, this.options.hiddenDates);
  795. // update class names
  796. if (options.orientation == 'top') {
  797. util.addClassName(dom.root, 'vis-top');
  798. util.removeClassName(dom.root, 'vis-bottom');
  799. }
  800. else {
  801. util.removeClassName(dom.root, 'vis-top');
  802. util.addClassName(dom.root, 'vis-bottom');
  803. }
  804. // update root width and height options
  805. dom.root.style.maxHeight = util.option.asSize(options.maxHeight, '');
  806. dom.root.style.minHeight = util.option.asSize(options.minHeight, '');
  807. dom.root.style.width = util.option.asSize(options.width, '');
  808. // calculate border widths
  809. props.border.left = (dom.centerContainer.offsetWidth - dom.centerContainer.clientWidth) / 2;
  810. props.border.right = props.border.left;
  811. props.border.top = (dom.centerContainer.offsetHeight - dom.centerContainer.clientHeight) / 2;
  812. props.border.bottom = props.border.top;
  813. props.borderRootHeight= dom.root.offsetHeight - dom.root.clientHeight;
  814. props.borderRootWidth = dom.root.offsetWidth - dom.root.clientWidth;
  815. // workaround for a bug in IE: the clientWidth of an element with
  816. // a height:0px and overflow:hidden is not calculated and always has value 0
  817. if (dom.centerContainer.clientHeight === 0) {
  818. props.border.left = props.border.top;
  819. props.border.right = props.border.left;
  820. }
  821. if (dom.root.clientHeight === 0) {
  822. props.borderRootWidth = props.borderRootHeight;
  823. }
  824. // calculate the heights. If any of the side panels is empty, we set the height to
  825. // minus the border width, such that the border will be invisible
  826. props.center.height = dom.center.offsetHeight;
  827. props.left.height = dom.left.offsetHeight;
  828. props.right.height = dom.right.offsetHeight;
  829. props.top.height = dom.top.clientHeight || -props.border.top;
  830. props.bottom.height = dom.bottom.clientHeight || -props.border.bottom;
  831. // TODO: compensate borders when any of the panels is empty.
  832. // apply auto height
  833. // TODO: only calculate autoHeight when needed (else we cause an extra reflow/repaint of the DOM)
  834. var contentHeight = Math.max(props.left.height, props.center.height, props.right.height);
  835. var autoHeight = props.top.height + contentHeight + props.bottom.height +
  836. props.borderRootHeight + props.border.top + props.border.bottom;
  837. dom.root.style.height = util.option.asSize(options.height, autoHeight + 'px');
  838. // calculate heights of the content panels
  839. props.root.height = dom.root.offsetHeight;
  840. props.background.height = props.root.height - props.borderRootHeight;
  841. var containerHeight = props.root.height - props.top.height - props.bottom.height -
  842. props.borderRootHeight;
  843. props.centerContainer.height = containerHeight;
  844. props.leftContainer.height = containerHeight;
  845. props.rightContainer.height = props.leftContainer.height;
  846. // calculate the widths of the panels
  847. props.root.width = dom.root.offsetWidth;
  848. props.background.width = props.root.width - props.borderRootWidth;
  849. if (!this.initialDrawDone) {
  850. props.scrollbarWidth = util.getScrollBarWidth();
  851. }
  852. if (options.verticalScroll) {
  853. if (options.rtl) {
  854. props.left.width = dom.leftContainer.clientWidth || -props.border.left;
  855. props.right.width = dom.rightContainer.clientWidth + props.scrollbarWidth || -props.border.right;
  856. } else {
  857. props.left.width = dom.leftContainer.clientWidth + props.scrollbarWidth || -props.border.left;
  858. props.right.width = dom.rightContainer.clientWidth || -props.border.right;
  859. }
  860. } else {
  861. props.left.width = dom.leftContainer.clientWidth || -props.border.left;
  862. props.right.width = dom.rightContainer.clientWidth || -props.border.right;
  863. }
  864. this._setDOM();
  865. // update the scrollTop, feasible range for the offset can be changed
  866. // when the height of the Core or of the contents of the center changed
  867. var offset = this._updateScrollTop();
  868. // reposition the scrollable contents
  869. if (options.orientation.item != 'top') {
  870. offset += Math.max(props.centerContainer.height - props.center.height -
  871. props.border.top - props.border.bottom, 0);
  872. }
  873. dom.center.style.top = offset + 'px';
  874. // show shadows when vertical scrolling is available
  875. var visibilityTop = props.scrollTop == 0 ? 'hidden' : '';
  876. var visibilityBottom = props.scrollTop == props.scrollTopMin ? 'hidden' : '';
  877. dom.shadowTop.style.visibility = visibilityTop;
  878. dom.shadowBottom.style.visibility = visibilityBottom;
  879. dom.shadowTopLeft.style.visibility = visibilityTop;
  880. dom.shadowBottomLeft.style.visibility = visibilityBottom;
  881. dom.shadowTopRight.style.visibility = visibilityTop;
  882. dom.shadowBottomRight.style.visibility = visibilityBottom;
  883. if (options.verticalScroll) {
  884. dom.rightContainer.className = 'vis-panel vis-right vis-vertical-scroll';
  885. dom.leftContainer.className = 'vis-panel vis-left vis-vertical-scroll';
  886. dom.shadowTopRight.style.visibility = "hidden";
  887. dom.shadowBottomRight.style.visibility = "hidden";
  888. dom.shadowTopLeft.style.visibility = "hidden";
  889. dom.shadowBottomLeft.style.visibility = "hidden";
  890. dom.left.style.top = '0px';
  891. dom.right.style.top = '0px';
  892. }
  893. if (!options.verticalScroll || props.center.height < props.centerContainer.height) {
  894. dom.left.style.top = offset + 'px';
  895. dom.right.style.top = offset + 'px';
  896. dom.rightContainer.className = dom.rightContainer.className.replace(new RegExp('(?:^|\\s)'+ 'vis-vertical-scroll' + '(?:\\s|$)'), ' ');
  897. dom.leftContainer.className = dom.leftContainer.className.replace(new RegExp('(?:^|\\s)'+ 'vis-vertical-scroll' + '(?:\\s|$)'), ' ');
  898. props.left.width = dom.leftContainer.clientWidth || -props.border.left;
  899. props.right.width = dom.rightContainer.clientWidth || -props.border.right;
  900. this._setDOM();
  901. }
  902. // enable/disable vertical panning
  903. var contentsOverflow = props.center.height > props.centerContainer.height;
  904. this.hammer.get('pan').set({
  905. direction: contentsOverflow ? Hammer.DIRECTION_ALL : Hammer.DIRECTION_HORIZONTAL
  906. });
  907. // redraw all components
  908. this.components.forEach(function (component) {
  909. resized = component.redraw() || resized;
  910. });
  911. var MAX_REDRAW = 5;
  912. if (resized) {
  913. if (this.redrawCount < MAX_REDRAW) {
  914. this.body.emitter.emit('_change');
  915. return;
  916. }
  917. else {
  918. console.log('WARNING: infinite loop in redraw?');
  919. }
  920. } else {
  921. this.redrawCount = 0;
  922. }
  923. //Emit public 'changed' event for UI updates, see issue #1592
  924. this.body.emitter.emit("changed");
  925. };
  926. Core.prototype._setDOM = function () {
  927. var props = this.props;
  928. var dom = this.dom;
  929. props.leftContainer.width = props.left.width;
  930. props.rightContainer.width = props.right.width;
  931. var centerWidth = props.root.width - props.left.width - props.right.width - props.borderRootWidth;
  932. props.center.width = centerWidth;
  933. props.centerContainer.width = centerWidth;
  934. props.top.width = centerWidth;
  935. props.bottom.width = centerWidth;
  936. // resize the panels
  937. dom.background.style.height = props.background.height + 'px';
  938. dom.backgroundVertical.style.height = props.background.height + 'px';
  939. dom.backgroundHorizontal.style.height = props.centerContainer.height + 'px';
  940. dom.centerContainer.style.height = props.centerContainer.height + 'px';
  941. dom.leftContainer.style.height = props.leftContainer.height + 'px';
  942. dom.rightContainer.style.height = props.rightContainer.height + 'px';
  943. dom.background.style.width = props.background.width + 'px';
  944. dom.backgroundVertical.style.width = props.centerContainer.width + 'px';
  945. dom.backgroundHorizontal.style.width = props.background.width + 'px';
  946. dom.centerContainer.style.width = props.center.width + 'px';
  947. dom.top.style.width = props.top.width + 'px';
  948. dom.bottom.style.width = props.bottom.width + 'px';
  949. // reposition the panels
  950. dom.background.style.left = '0';
  951. dom.background.style.top = '0';
  952. dom.backgroundVertical.style.left = (props.left.width + props.border.left) + 'px';
  953. dom.backgroundVertical.style.top = '0';
  954. dom.backgroundHorizontal.style.left = '0';
  955. dom.backgroundHorizontal.style.top = props.top.height + 'px';
  956. dom.centerContainer.style.left = props.left.width + 'px';
  957. dom.centerContainer.style.top = props.top.height + 'px';
  958. dom.leftContainer.style.left = '0';
  959. dom.leftContainer.style.top = props.top.height + 'px';
  960. dom.rightContainer.style.left = (props.left.width + props.center.width) + 'px';
  961. dom.rightContainer.style.top = props.top.height + 'px';
  962. dom.top.style.left = props.left.width + 'px';
  963. dom.top.style.top = '0';
  964. dom.bottom.style.left = props.left.width + 'px';
  965. dom.bottom.style.top = (props.top.height + props.centerContainer.height) + 'px';
  966. dom.center.style.left = '0';
  967. dom.left.style.left = '0';
  968. dom.right.style.left = '0';
  969. };
  970. // TODO: deprecated since version 1.1.0, remove some day
  971. Core.prototype.repaint = function () {
  972. throw new Error('Function repaint is deprecated. Use redraw instead.');
  973. };
  974. /**
  975. * Set a current time. This can be used for example to ensure that a client's
  976. * time is synchronized with a shared server time.
  977. * Only applicable when option `showCurrentTime` is true.
  978. * @param {Date | string | number} time A Date, unix timestamp, or
  979. * ISO date string.
  980. */
  981. Core.prototype.setCurrentTime = function(time) {
  982. if (!this.currentTime) {
  983. throw new Error('Option showCurrentTime must be true');
  984. }
  985. this.currentTime.setCurrentTime(time);
  986. };
  987. /**
  988. * Get the current time.
  989. * Only applicable when option `showCurrentTime` is true.
  990. * @return {Date} Returns the current time.
  991. */
  992. Core.prototype.getCurrentTime = function() {
  993. if (!this.currentTime) {
  994. throw new Error('Option showCurrentTime must be true');
  995. }
  996. return this.currentTime.getCurrentTime();
  997. };
  998. /**
  999. * Convert a position on screen (pixels) to a datetime
  1000. * @param {int} x Position on the screen in pixels
  1001. * @return {Date} time The datetime the corresponds with given position x
  1002. * @protected
  1003. */
  1004. // TODO: move this function to Range
  1005. Core.prototype._toTime = function(x) {
  1006. return DateUtil.toTime(this, x, this.props.center.width);
  1007. };
  1008. /**
  1009. * Convert a position on the global screen (pixels) to a datetime
  1010. * @param {int} x Position on the screen in pixels
  1011. * @return {Date} time The datetime the corresponds with given position x
  1012. * @protected
  1013. */
  1014. // TODO: move this function to Range
  1015. Core.prototype._toGlobalTime = function(x) {
  1016. return DateUtil.toTime(this, x, this.props.root.width);
  1017. //var conversion = this.range.conversion(this.props.root.width);
  1018. //return new Date(x / conversion.scale + conversion.offset);
  1019. };
  1020. /**
  1021. * Convert a datetime (Date object) into a position on the screen
  1022. * @param {Date} time A date
  1023. * @return {int} x The position on the screen in pixels which corresponds
  1024. * with the given date.
  1025. * @protected
  1026. */
  1027. // TODO: move this function to Range
  1028. Core.prototype._toScreen = function(time) {
  1029. return DateUtil.toScreen(this, time, this.props.center.width);
  1030. };
  1031. /**
  1032. * Convert a datetime (Date object) into a position on the root
  1033. * This is used to get the pixel density estimate for the screen, not the center panel
  1034. * @param {Date} time A date
  1035. * @return {int} x The position on root in pixels which corresponds
  1036. * with the given date.
  1037. * @protected
  1038. */
  1039. // TODO: move this function to Range
  1040. Core.prototype._toGlobalScreen = function(time) {
  1041. return DateUtil.toScreen(this, time, this.props.root.width);
  1042. //var conversion = this.range.conversion(this.props.root.width);
  1043. //return (time.valueOf() - conversion.offset) * conversion.scale;
  1044. };
  1045. /**
  1046. * Initialize watching when option autoResize is true
  1047. * @private
  1048. */
  1049. Core.prototype._initAutoResize = function () {
  1050. if (this.options.autoResize == true) {
  1051. this._startAutoResize();
  1052. }
  1053. else {
  1054. this._stopAutoResize();
  1055. }
  1056. };
  1057. /**
  1058. * Watch for changes in the size of the container. On resize, the Panel will
  1059. * automatically redraw itself.
  1060. * @private
  1061. */
  1062. Core.prototype._startAutoResize = function () {
  1063. var me = this;
  1064. this._stopAutoResize();
  1065. this._onResize = function() {
  1066. if (me.options.autoResize != true) {
  1067. // stop watching when the option autoResize is changed to false
  1068. me._stopAutoResize();
  1069. return;
  1070. }
  1071. if (me.dom.root) {
  1072. // check whether the frame is resized
  1073. // Note: we compare offsetWidth here, not clientWidth. For some reason,
  1074. // IE does not restore the clientWidth from 0 to the actual width after
  1075. // changing the timeline's container display style from none to visible
  1076. if ((me.dom.root.offsetWidth != me.props.lastWidth) ||
  1077. (me.dom.root.offsetHeight != me.props.lastHeight)) {
  1078. me.props.lastWidth = me.dom.root.offsetWidth;
  1079. me.props.lastHeight = me.dom.root.offsetHeight;
  1080. me.props.scrollbarWidth = util.getScrollBarWidth();
  1081. me.body.emitter.emit('_change');
  1082. }
  1083. }
  1084. };
  1085. // add event listener to window resize
  1086. util.addEventListener(window, 'resize', this._onResize);
  1087. //Prevent initial unnecessary redraw
  1088. if (me.dom.root) {
  1089. me.props.lastWidth = me.dom.root.offsetWidth;
  1090. me.props.lastHeight = me.dom.root.offsetHeight;
  1091. }
  1092. this.watchTimer = setInterval(this._onResize, 1000);
  1093. };
  1094. /**
  1095. * Stop watching for a resize of the frame.
  1096. * @private
  1097. */
  1098. Core.prototype._stopAutoResize = function () {
  1099. if (this.watchTimer) {
  1100. clearInterval(this.watchTimer);
  1101. this.watchTimer = undefined;
  1102. }
  1103. // remove event listener on window.resize
  1104. if (this._onResize) {
  1105. util.removeEventListener(window, 'resize', this._onResize);
  1106. this._onResize = null;
  1107. }
  1108. };
  1109. /**
  1110. * Start moving the timeline vertically
  1111. * @param {Event} event
  1112. * @private
  1113. */
  1114. Core.prototype._onTouch = function (event) { // eslint-disable-line no-unused-vars
  1115. this.touch.allowDragging = true;
  1116. this.touch.initialScrollTop = this.props.scrollTop;
  1117. };
  1118. /**
  1119. * Start moving the timeline vertically
  1120. * @param {Event} event
  1121. * @private
  1122. */
  1123. Core.prototype._onPinch = function (event) { // eslint-disable-line no-unused-vars
  1124. this.touch.allowDragging = false;
  1125. };
  1126. /**
  1127. * Move the timeline vertically
  1128. * @param {Event} event
  1129. * @private
  1130. */
  1131. Core.prototype._onDrag = function (event) {
  1132. if (!event) return
  1133. // refuse to drag when we where pinching to prevent the timeline make a jump
  1134. // when releasing the fingers in opposite order from the touch screen
  1135. if (!this.touch.allowDragging) return;
  1136. var delta = event.deltaY;
  1137. var oldScrollTop = this._getScrollTop();
  1138. var newScrollTop = this._setScrollTop(this.touch.initialScrollTop + delta);
  1139. if (this.options.verticalScroll) {
  1140. this.dom.left.parentNode.scrollTop = -this.props.scrollTop;
  1141. this.dom.right.parentNode.scrollTop = -this.props.scrollTop;
  1142. }
  1143. if (newScrollTop != oldScrollTop) {
  1144. this.emit("verticalDrag");
  1145. }
  1146. };
  1147. /**
  1148. * Apply a scrollTop
  1149. * @param {number} scrollTop
  1150. * @returns {number} scrollTop Returns the applied scrollTop
  1151. * @private
  1152. */
  1153. Core.prototype._setScrollTop = function (scrollTop) {
  1154. this.props.scrollTop = scrollTop;
  1155. this._updateScrollTop();
  1156. return this.props.scrollTop;
  1157. };
  1158. /**
  1159. * Update the current scrollTop when the height of the containers has been changed
  1160. * @returns {number} scrollTop Returns the applied scrollTop
  1161. * @private
  1162. */
  1163. Core.prototype._updateScrollTop = function () {
  1164. // recalculate the scrollTopMin
  1165. var scrollTopMin = Math.min(this.props.centerContainer.height - this.props.center.height, 0); // is negative or zero
  1166. if (scrollTopMin != this.props.scrollTopMin) {
  1167. // in case of bottom orientation, change the scrollTop such that the contents
  1168. // do not move relative to the time axis at the bottom
  1169. if (this.options.orientation.item != 'top') {
  1170. this.props.scrollTop += (scrollTopMin - this.props.scrollTopMin);
  1171. }
  1172. this.props.scrollTopMin = scrollTopMin;
  1173. }
  1174. // limit the scrollTop to the feasible scroll range
  1175. if (this.props.scrollTop > 0) this.props.scrollTop = 0;
  1176. if (this.props.scrollTop < scrollTopMin) this.props.scrollTop = scrollTopMin;
  1177. if (this.options.verticalScroll) {
  1178. this.dom.left.parentNode.scrollTop = -this.props.scrollTop;
  1179. this.dom.right.parentNode.scrollTop = -this.props.scrollTop;
  1180. }
  1181. return this.props.scrollTop;
  1182. };
  1183. /**
  1184. * Get the current scrollTop
  1185. * @returns {number} scrollTop
  1186. * @private
  1187. */
  1188. Core.prototype._getScrollTop = function () {
  1189. return this.props.scrollTop;
  1190. };
  1191. /**
  1192. * Load a configurator
  1193. * [at]returns {Object}
  1194. * @private
  1195. */
  1196. Core.prototype._createConfigurator = function () {
  1197. throw new Error('Cannot invoke abstract method _createConfigurator');
  1198. };
  1199. module.exports = Core;