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.

938 lines
31 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 DataSet = require('../DataSet');
  6. var DataView = require('../DataView');
  7. var Range = require('./Range');
  8. var ItemSet = require('./component/ItemSet');
  9. var Activator = require('../shared/Activator');
  10. var DateUtil = require('./DateUtil');
  11. var CustomTime = require('./component/CustomTime');
  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. var me = this;
  86. this.on('change', function (properties) {
  87. if (properties && properties.queue == true) {
  88. // redraw once on next tick
  89. if (!me._redrawTimer) {
  90. me._redrawTimer = setTimeout(function () {
  91. me._redrawTimer = null;
  92. me._redraw();
  93. }, 0)
  94. }
  95. }
  96. else {
  97. // redraw immediately
  98. me._redraw();
  99. }
  100. });
  101. // create event listeners for all interesting events, these events will be
  102. // emitted via emitter
  103. this.hammer = new Hammer(this.dom.root, {touchAction: 'pan-y'});
  104. this.hammer.get('pinch').set({enable: true});
  105. this.listeners = {};
  106. var events = [
  107. 'tap', 'doubletap', 'press',
  108. 'pinch',
  109. 'pan', 'panstart', 'panmove', 'panend'
  110. // TODO: cleanup
  111. //'touch', 'pinch',
  112. //'tap', 'doubletap', 'hold',
  113. //'dragstart', 'drag', 'dragend',
  114. //'mousewheel', 'DOMMouseScroll' // DOMMouseScroll is needed for Firefox
  115. ];
  116. events.forEach(function (type) {
  117. var listener = function (event) {
  118. if (me.isActive()) {
  119. me.emit(type, event);
  120. }
  121. };
  122. me.hammer.on(type, listener);
  123. me.listeners[type] = listener;
  124. });
  125. // emulate a touch event (emitted before the start of a pan, pinch, tap, or press)
  126. hammerUtil.onTouch(this.hammer, function (event) {
  127. me.emit('touch', event);
  128. }.bind(this));
  129. function onMouseWheel(event) {
  130. if (me.isActive()) {
  131. me.emit('mousewheel', event);
  132. }
  133. }
  134. this.dom.root.addEventListener('mousewheel', onMouseWheel);
  135. this.dom.root.addEventListener('DOMMouseScroll', onMouseWheel);
  136. // size properties of each of the panels
  137. this.props = {
  138. root: {},
  139. background: {},
  140. centerContainer: {},
  141. leftContainer: {},
  142. rightContainer: {},
  143. center: {},
  144. left: {},
  145. right: {},
  146. top: {},
  147. bottom: {},
  148. border: {},
  149. scrollTop: 0,
  150. scrollTopMin: 0
  151. };
  152. this.redrawCount = 0;
  153. // attach the root panel to the provided container
  154. if (!container) throw new Error('No container provided');
  155. container.appendChild(this.dom.root);
  156. };
  157. /**
  158. * Set options. Options will be passed to all components loaded in the Timeline.
  159. * @param {Object} [options]
  160. * {String} orientation
  161. * Vertical orientation for the Timeline,
  162. * can be 'bottom' (default) or 'top'.
  163. * {String | Number} width
  164. * Width for the timeline, a number in pixels or
  165. * a css string like '1000px' or '75%'. '100%' by default.
  166. * {String | Number} height
  167. * Fixed height for the Timeline, a number in pixels or
  168. * a css string like '400px' or '75%'. If undefined,
  169. * The Timeline will automatically size such that
  170. * its contents fit.
  171. * {String | Number} minHeight
  172. * Minimum height for the Timeline, a number in pixels or
  173. * a css string like '400px' or '75%'.
  174. * {String | Number} maxHeight
  175. * Maximum height for the Timeline, a number in pixels or
  176. * a css string like '400px' or '75%'.
  177. * {Number | Date | String} start
  178. * Start date for the visible window
  179. * {Number | Date | String} end
  180. * End date for the visible window
  181. */
  182. Core.prototype.setOptions = function (options) {
  183. if (options) {
  184. // copy the known options
  185. var fields = ['width', 'height', 'minHeight', 'maxHeight', 'autoResize', 'start', 'end', 'orientation', 'clickToUse', 'dataAttributes', 'hiddenDates'];
  186. util.selectiveExtend(fields, this.options, options);
  187. if ('hiddenDates' in this.options) {
  188. DateUtil.convertHiddenOptions(this.body, this.options.hiddenDates);
  189. }
  190. if ('clickToUse' in options) {
  191. if (options.clickToUse) {
  192. if (!this.activator) {
  193. this.activator = new Activator(this.dom.root);
  194. }
  195. }
  196. else {
  197. if (this.activator) {
  198. this.activator.destroy();
  199. delete this.activator;
  200. }
  201. }
  202. }
  203. // enable/disable autoResize
  204. this._initAutoResize();
  205. }
  206. // propagate options to all components
  207. this.components.forEach(component => component.setOptions(options));
  208. // TODO: remove deprecation error one day (deprecated since version 0.8.0)
  209. if (options && options.order) {
  210. throw new Error('Option order is deprecated. There is no replacement for this feature.');
  211. }
  212. // redraw everything
  213. this._redraw();
  214. };
  215. /**
  216. * Returns true when the Timeline is active.
  217. * @returns {boolean}
  218. */
  219. Core.prototype.isActive = function () {
  220. return !this.activator || this.activator.active;
  221. };
  222. /**
  223. * Destroy the Core, clean up all DOM elements and event listeners.
  224. */
  225. Core.prototype.destroy = function () {
  226. // unbind datasets
  227. this.clear();
  228. // remove all event listeners
  229. this.off();
  230. // stop checking for changed size
  231. this._stopAutoResize();
  232. // remove from DOM
  233. if (this.dom.root.parentNode) {
  234. this.dom.root.parentNode.removeChild(this.dom.root);
  235. }
  236. this.dom = null;
  237. // remove Activator
  238. if (this.activator) {
  239. this.activator.destroy();
  240. delete this.activator;
  241. }
  242. // cleanup hammer touch events
  243. for (var event in this.listeners) {
  244. if (this.listeners.hasOwnProperty(event)) {
  245. delete this.listeners[event];
  246. }
  247. }
  248. this.listeners = null;
  249. this.hammer = null;
  250. // give all components the opportunity to cleanup
  251. this.components.forEach(component => component.destroy());
  252. this.body = null;
  253. };
  254. /**
  255. * Set a custom time bar
  256. * @param {Date} time
  257. * @param {int} id
  258. */
  259. Core.prototype.setCustomTime = function (time, id) {
  260. if (!this.customTime) {
  261. throw new Error('Cannot get custom time: Custom time bar is not enabled');
  262. }
  263. var barId = id || 0;
  264. this.components.forEach(function (element, index, components) {
  265. if (element instanceof CustomTime && element.options.id === barId) {
  266. element.setCustomTime(time);
  267. }
  268. });
  269. };
  270. /**
  271. * Retrieve the current custom time.
  272. * @return {Date} customTime
  273. * @param {int} id
  274. */
  275. Core.prototype.getCustomTime = function(id) {
  276. if (!this.customTime) {
  277. throw new Error('Cannot get custom time: Custom time bar is not enabled');
  278. }
  279. var barId = id || 0,
  280. customTime = this.customTime.getCustomTime();
  281. this.components.forEach(function (element, index, components) {
  282. if (element instanceof CustomTime && element.options.id === barId) {
  283. customTime = element.getCustomTime();
  284. }
  285. });
  286. return customTime;
  287. };
  288. /**
  289. * Add custom vertical bar
  290. * @param {Date | String | Number} time A Date, unix timestamp, or
  291. * ISO date string. Time point where the new bar should be placed
  292. * @param {Number | String} ID of the new bar
  293. * @return {Number | String} ID of the new bar
  294. */
  295. Core.prototype.addCustomTime = function (time, id) {
  296. if (!this.currentTime) {
  297. throw new Error('Option showCurrentTime must be true');
  298. }
  299. if (time === undefined) {
  300. throw new Error('Time parameter for the custom bar must be provided');
  301. }
  302. var ts = util.convert(time, 'Date').valueOf(),
  303. numIds, customTime, customBarId;
  304. // All bar IDs are kept in 1 array, mixed types
  305. // Bar with ID 0 is the default bar.
  306. if (!this.customBarIds || this.customBarIds.constructor !== Array) {
  307. this.customBarIds = [0];
  308. }
  309. // If the ID is not provided, generate one, otherwise just use it
  310. if (id === undefined) {
  311. numIds = this.customBarIds.filter(function (element) {
  312. return util.isNumber(element);
  313. });
  314. customBarId = numIds.length > 0 ? Math.max.apply(null, numIds) + 1 : 1;
  315. } else {
  316. // Check for duplicates
  317. this.customBarIds.forEach(function (element) {
  318. if (element === id) {
  319. throw new Error('Custom time ID already exists');
  320. }
  321. });
  322. customBarId = id;
  323. }
  324. this.customBarIds.push(customBarId);
  325. customTime = new CustomTime(this.body, {
  326. showCustomTime : true,
  327. time : ts,
  328. id : customBarId
  329. });
  330. this.components.push(customTime);
  331. this.redraw();
  332. return customBarId;
  333. };
  334. /**
  335. * Remove previously added custom bar
  336. * @param {int} id ID of the custom bar to be removed
  337. * @return {boolean} True if the bar exists and is removed, false otherwise
  338. */
  339. Core.prototype.removeCustomTime = function (id) {
  340. var me = this;
  341. this.components.forEach(function (bar, index, components) {
  342. if (bar instanceof CustomTime && bar.options.id === id) {
  343. // Only the lines added by the user will be removed
  344. if (bar.options.id !== 0) {
  345. me.customBarIds.splice(me.customBarIds.indexOf(id), 1);
  346. components.splice(index, 1);
  347. bar.destroy();
  348. }
  349. }
  350. });
  351. };
  352. /**
  353. * Get the id's of the currently visible items.
  354. * @returns {Array} The ids of the visible items
  355. */
  356. Core.prototype.getVisibleItems = function() {
  357. return this.itemSet && this.itemSet.getVisibleItems() || [];
  358. };
  359. /**
  360. * Clear the Core. By Default, items, groups and options are cleared.
  361. * Example usage:
  362. *
  363. * timeline.clear(); // clear items, groups, and options
  364. * timeline.clear({options: true}); // clear options only
  365. *
  366. * @param {Object} [what] Optionally specify what to clear. By default:
  367. * {items: true, groups: true, options: true}
  368. */
  369. Core.prototype.clear = function(what) {
  370. // clear items
  371. if (!what || what.items) {
  372. this.setItems(null);
  373. }
  374. // clear groups
  375. if (!what || what.groups) {
  376. this.setGroups(null);
  377. }
  378. // clear options of timeline and of each of the components
  379. if (!what || what.options) {
  380. this.components.forEach(component => component.setOptions(component.defaultOptions));
  381. this.setOptions(this.defaultOptions); // this will also do a redraw
  382. }
  383. };
  384. /**
  385. * Set Core window such that it fits all items
  386. * @param {Object} [options] Available options:
  387. * `animate: boolean | number`
  388. * If true (default), the range is animated
  389. * smoothly to the new window.
  390. * If a number, the number is taken as duration
  391. * for the animation. Default duration is 500 ms.
  392. */
  393. Core.prototype.fit = function(options) {
  394. var range = this._getDataRange();
  395. // skip range set if there is no start and end date
  396. if (range.start === null && range.end === null) {
  397. return;
  398. }
  399. var animate = (options && options.animate !== undefined) ? options.animate : true;
  400. this.range.setRange(range.start, range.end, animate);
  401. };
  402. /**
  403. * Calculate the data range of the items and applies a 5% window around it.
  404. * @returns {{start: Date | null, end: Date | null}}
  405. * @protected
  406. */
  407. Core.prototype._getDataRange = function() {
  408. // apply the data range as range
  409. var dataRange = this.getItemRange();
  410. // add 5% space on both sides
  411. var start = dataRange.min;
  412. var end = dataRange.max;
  413. if (start != null && end != null) {
  414. var interval = (end.valueOf() - start.valueOf());
  415. if (interval <= 0) {
  416. // prevent an empty interval
  417. interval = 24 * 60 * 60 * 1000; // 1 day
  418. }
  419. start = new Date(start.valueOf() - interval * 0.05);
  420. end = new Date(end.valueOf() + interval * 0.05);
  421. }
  422. return {
  423. start: start,
  424. end: end
  425. }
  426. };
  427. /**
  428. * Set the visible window. Both parameters are optional, you can change only
  429. * start or only end. Syntax:
  430. *
  431. * TimeLine.setWindow(start, end)
  432. * TimeLine.setWindow(start, end, options)
  433. * TimeLine.setWindow(range)
  434. *
  435. * Where start and end can be a Date, number, or string, and range is an
  436. * object with properties start and end.
  437. *
  438. * @param {Date | Number | String | Object} [start] Start date of visible window
  439. * @param {Date | Number | String} [end] End date of visible window
  440. * @param {Object} [options] Available options:
  441. * `animate: boolean | number`
  442. * If true (default), the range is animated
  443. * smoothly to the new window.
  444. * If a number, the number is taken as duration
  445. * for the animation. Default duration is 500 ms.
  446. */
  447. Core.prototype.setWindow = function(start, end, options) {
  448. var animate;
  449. if (arguments.length == 1) {
  450. var range = arguments[0];
  451. animate = (range.animate !== undefined) ? range.animate : true;
  452. this.range.setRange(range.start, range.end, animate);
  453. }
  454. else {
  455. animate = (options && options.animate !== undefined) ? options.animate : true;
  456. this.range.setRange(start, end, animate);
  457. }
  458. };
  459. /**
  460. * Move the window such that given time is centered on screen.
  461. * @param {Date | Number | String} time
  462. * @param {Object} [options] Available options:
  463. * `animate: boolean | number`
  464. * If true (default), the range is animated
  465. * smoothly to the new window.
  466. * If a number, the number is taken as duration
  467. * for the animation. Default duration is 500 ms.
  468. */
  469. Core.prototype.moveTo = function(time, options) {
  470. var interval = this.range.end - this.range.start;
  471. var t = util.convert(time, 'Date').valueOf();
  472. var start = t - interval / 2;
  473. var end = t + interval / 2;
  474. var animate = (options && options.animate !== undefined) ? options.animate : true;
  475. this.range.setRange(start, end, animate);
  476. };
  477. /**
  478. * Get the visible window
  479. * @return {{start: Date, end: Date}} Visible range
  480. */
  481. Core.prototype.getWindow = function() {
  482. var range = this.range.getRange();
  483. return {
  484. start: new Date(range.start),
  485. end: new Date(range.end)
  486. };
  487. };
  488. /**
  489. * Force a redraw. Can be overridden by implementations of Core
  490. */
  491. Core.prototype.redraw = function() {
  492. this._redraw();
  493. };
  494. /**
  495. * Redraw for internal use. Redraws all components. See also the public
  496. * method redraw.
  497. * @protected
  498. */
  499. Core.prototype._redraw = function() {
  500. var resized = false;
  501. var options = this.options;
  502. var props = this.props;
  503. var dom = this.dom;
  504. if (!dom) return; // when destroyed
  505. DateUtil.updateHiddenDates(this.body, this.options.hiddenDates);
  506. // update class names
  507. if (options.orientation == 'top') {
  508. util.addClassName(dom.root, 'top');
  509. util.removeClassName(dom.root, 'bottom');
  510. }
  511. else {
  512. util.removeClassName(dom.root, 'top');
  513. util.addClassName(dom.root, 'bottom');
  514. }
  515. // update root width and height options
  516. dom.root.style.maxHeight = util.option.asSize(options.maxHeight, '');
  517. dom.root.style.minHeight = util.option.asSize(options.minHeight, '');
  518. dom.root.style.width = util.option.asSize(options.width, '');
  519. // calculate border widths
  520. props.border.left = (dom.centerContainer.offsetWidth - dom.centerContainer.clientWidth) / 2;
  521. props.border.right = props.border.left;
  522. props.border.top = (dom.centerContainer.offsetHeight - dom.centerContainer.clientHeight) / 2;
  523. props.border.bottom = props.border.top;
  524. var borderRootHeight= dom.root.offsetHeight - dom.root.clientHeight;
  525. var borderRootWidth = dom.root.offsetWidth - dom.root.clientWidth;
  526. // workaround for a bug in IE: the clientWidth of an element with
  527. // a height:0px and overflow:hidden is not calculated and always has value 0
  528. if (dom.centerContainer.clientHeight === 0) {
  529. props.border.left = props.border.top;
  530. props.border.right = props.border.left;
  531. }
  532. if (dom.root.clientHeight === 0) {
  533. borderRootWidth = borderRootHeight;
  534. }
  535. // calculate the heights. If any of the side panels is empty, we set the height to
  536. // minus the border width, such that the border will be invisible
  537. props.center.height = dom.center.offsetHeight;
  538. props.left.height = dom.left.offsetHeight;
  539. props.right.height = dom.right.offsetHeight;
  540. props.top.height = dom.top.clientHeight || -props.border.top;
  541. props.bottom.height = dom.bottom.clientHeight || -props.border.bottom;
  542. // TODO: compensate borders when any of the panels is empty.
  543. // apply auto height
  544. // TODO: only calculate autoHeight when needed (else we cause an extra reflow/repaint of the DOM)
  545. var contentHeight = Math.max(props.left.height, props.center.height, props.right.height);
  546. var autoHeight = props.top.height + contentHeight + props.bottom.height +
  547. borderRootHeight + props.border.top + props.border.bottom;
  548. dom.root.style.height = util.option.asSize(options.height, autoHeight + 'px');
  549. // calculate heights of the content panels
  550. props.root.height = dom.root.offsetHeight;
  551. props.background.height = props.root.height - borderRootHeight;
  552. var containerHeight = props.root.height - props.top.height - props.bottom.height -
  553. borderRootHeight;
  554. props.centerContainer.height = containerHeight;
  555. props.leftContainer.height = containerHeight;
  556. props.rightContainer.height = props.leftContainer.height;
  557. // calculate the widths of the panels
  558. props.root.width = dom.root.offsetWidth;
  559. props.background.width = props.root.width - borderRootWidth;
  560. props.left.width = dom.leftContainer.clientWidth || -props.border.left;
  561. props.leftContainer.width = props.left.width;
  562. props.right.width = dom.rightContainer.clientWidth || -props.border.right;
  563. props.rightContainer.width = props.right.width;
  564. var centerWidth = props.root.width - props.left.width - props.right.width - borderRootWidth;
  565. props.center.width = centerWidth;
  566. props.centerContainer.width = centerWidth;
  567. props.top.width = centerWidth;
  568. props.bottom.width = centerWidth;
  569. // resize the panels
  570. dom.background.style.height = props.background.height + 'px';
  571. dom.backgroundVertical.style.height = props.background.height + 'px';
  572. dom.backgroundHorizontal.style.height = props.centerContainer.height + 'px';
  573. dom.centerContainer.style.height = props.centerContainer.height + 'px';
  574. dom.leftContainer.style.height = props.leftContainer.height + 'px';
  575. dom.rightContainer.style.height = props.rightContainer.height + 'px';
  576. dom.background.style.width = props.background.width + 'px';
  577. dom.backgroundVertical.style.width = props.centerContainer.width + 'px';
  578. dom.backgroundHorizontal.style.width = props.background.width + 'px';
  579. dom.centerContainer.style.width = props.center.width + 'px';
  580. dom.top.style.width = props.top.width + 'px';
  581. dom.bottom.style.width = props.bottom.width + 'px';
  582. // reposition the panels
  583. dom.background.style.left = '0';
  584. dom.background.style.top = '0';
  585. dom.backgroundVertical.style.left = (props.left.width + props.border.left) + 'px';
  586. dom.backgroundVertical.style.top = '0';
  587. dom.backgroundHorizontal.style.left = '0';
  588. dom.backgroundHorizontal.style.top = props.top.height + 'px';
  589. dom.centerContainer.style.left = props.left.width + 'px';
  590. dom.centerContainer.style.top = props.top.height + 'px';
  591. dom.leftContainer.style.left = '0';
  592. dom.leftContainer.style.top = props.top.height + 'px';
  593. dom.rightContainer.style.left = (props.left.width + props.center.width) + 'px';
  594. dom.rightContainer.style.top = props.top.height + 'px';
  595. dom.top.style.left = props.left.width + 'px';
  596. dom.top.style.top = '0';
  597. dom.bottom.style.left = props.left.width + 'px';
  598. dom.bottom.style.top = (props.top.height + props.centerContainer.height) + 'px';
  599. // update the scrollTop, feasible range for the offset can be changed
  600. // when the height of the Core or of the contents of the center changed
  601. this._updateScrollTop();
  602. // reposition the scrollable contents
  603. var offset = this.props.scrollTop;
  604. if (options.orientation == 'bottom') {
  605. offset += Math.max(this.props.centerContainer.height - this.props.center.height -
  606. this.props.border.top - this.props.border.bottom, 0);
  607. }
  608. dom.center.style.left = '0';
  609. dom.center.style.top = offset + 'px';
  610. dom.left.style.left = '0';
  611. dom.left.style.top = offset + 'px';
  612. dom.right.style.left = '0';
  613. dom.right.style.top = offset + 'px';
  614. // show shadows when vertical scrolling is available
  615. var visibilityTop = this.props.scrollTop == 0 ? 'hidden' : '';
  616. var visibilityBottom = this.props.scrollTop == this.props.scrollTopMin ? 'hidden' : '';
  617. dom.shadowTop.style.visibility = visibilityTop;
  618. dom.shadowBottom.style.visibility = visibilityBottom;
  619. dom.shadowTopLeft.style.visibility = visibilityTop;
  620. dom.shadowBottomLeft.style.visibility = visibilityBottom;
  621. dom.shadowTopRight.style.visibility = visibilityTop;
  622. dom.shadowBottomRight.style.visibility = visibilityBottom;
  623. // redraw all components
  624. this.components.forEach(function (component) {
  625. resized = component.redraw() || resized;
  626. });
  627. if (resized) {
  628. // keep repainting until all sizes are settled
  629. var MAX_REDRAWS = 3; // maximum number of consecutive redraws
  630. if (this.redrawCount < MAX_REDRAWS) {
  631. this.redrawCount++;
  632. this._redraw();
  633. }
  634. else {
  635. console.log('WARNING: infinite loop in redraw?');
  636. }
  637. this.redrawCount = 0;
  638. }
  639. this.emit("finishedRedraw");
  640. };
  641. // TODO: deprecated since version 1.1.0, remove some day
  642. Core.prototype.repaint = function () {
  643. throw new Error('Function repaint is deprecated. Use redraw instead.');
  644. };
  645. /**
  646. * Set a current time. This can be used for example to ensure that a client's
  647. * time is synchronized with a shared server time.
  648. * Only applicable when option `showCurrentTime` is true.
  649. * @param {Date | String | Number} time A Date, unix timestamp, or
  650. * ISO date string.
  651. */
  652. Core.prototype.setCurrentTime = function(time) {
  653. if (!this.currentTime) {
  654. throw new Error('Option showCurrentTime must be true');
  655. }
  656. this.currentTime.setCurrentTime(time);
  657. };
  658. /**
  659. * Get the current time.
  660. * Only applicable when option `showCurrentTime` is true.
  661. * @return {Date} Returns the current time.
  662. */
  663. Core.prototype.getCurrentTime = function() {
  664. if (!this.currentTime) {
  665. throw new Error('Option showCurrentTime must be true');
  666. }
  667. return this.currentTime.getCurrentTime();
  668. };
  669. /**
  670. * Convert a position on screen (pixels) to a datetime
  671. * @param {int} x Position on the screen in pixels
  672. * @return {Date} time The datetime the corresponds with given position x
  673. * @private
  674. */
  675. // TODO: move this function to Range
  676. Core.prototype._toTime = function(x) {
  677. return DateUtil.toTime(this, x, this.props.center.width);
  678. };
  679. /**
  680. * Convert a position on the global screen (pixels) to a datetime
  681. * @param {int} x Position on the screen in pixels
  682. * @return {Date} time The datetime the corresponds with given position x
  683. * @private
  684. */
  685. // TODO: move this function to Range
  686. Core.prototype._toGlobalTime = function(x) {
  687. return DateUtil.toTime(this, x, this.props.root.width);
  688. //var conversion = this.range.conversion(this.props.root.width);
  689. //return new Date(x / conversion.scale + conversion.offset);
  690. };
  691. /**
  692. * Convert a datetime (Date object) into a position on the screen
  693. * @param {Date} time A date
  694. * @return {int} x The position on the screen in pixels which corresponds
  695. * with the given date.
  696. * @private
  697. */
  698. // TODO: move this function to Range
  699. Core.prototype._toScreen = function(time) {
  700. return DateUtil.toScreen(this, time, this.props.center.width);
  701. };
  702. /**
  703. * Convert a datetime (Date object) into a position on the root
  704. * This is used to get the pixel density estimate for the screen, not the center panel
  705. * @param {Date} time A date
  706. * @return {int} x The position on root in pixels which corresponds
  707. * with the given date.
  708. * @private
  709. */
  710. // TODO: move this function to Range
  711. Core.prototype._toGlobalScreen = function(time) {
  712. return DateUtil.toScreen(this, time, this.props.root.width);
  713. //var conversion = this.range.conversion(this.props.root.width);
  714. //return (time.valueOf() - conversion.offset) * conversion.scale;
  715. };
  716. /**
  717. * Initialize watching when option autoResize is true
  718. * @private
  719. */
  720. Core.prototype._initAutoResize = function () {
  721. if (this.options.autoResize == true) {
  722. this._startAutoResize();
  723. }
  724. else {
  725. this._stopAutoResize();
  726. }
  727. };
  728. /**
  729. * Watch for changes in the size of the container. On resize, the Panel will
  730. * automatically redraw itself.
  731. * @private
  732. */
  733. Core.prototype._startAutoResize = function () {
  734. var me = this;
  735. this._stopAutoResize();
  736. this._onResize = function() {
  737. if (me.options.autoResize != true) {
  738. // stop watching when the option autoResize is changed to false
  739. me._stopAutoResize();
  740. return;
  741. }
  742. if (me.dom.root) {
  743. // check whether the frame is resized
  744. // Note: we compare offsetWidth here, not clientWidth. For some reason,
  745. // IE does not restore the clientWidth from 0 to the actual width after
  746. // changing the timeline's container display style from none to visible
  747. if ((me.dom.root.offsetWidth != me.props.lastWidth) ||
  748. (me.dom.root.offsetHeight != me.props.lastHeight)) {
  749. me.props.lastWidth = me.dom.root.offsetWidth;
  750. me.props.lastHeight = me.dom.root.offsetHeight;
  751. me.emit('change');
  752. }
  753. }
  754. };
  755. // add event listener to window resize
  756. util.addEventListener(window, 'resize', this._onResize);
  757. this.watchTimer = setInterval(this._onResize, 1000);
  758. };
  759. /**
  760. * Stop watching for a resize of the frame.
  761. * @private
  762. */
  763. Core.prototype._stopAutoResize = function () {
  764. if (this.watchTimer) {
  765. clearInterval(this.watchTimer);
  766. this.watchTimer = undefined;
  767. }
  768. // remove event listener on window.resize
  769. util.removeEventListener(window, 'resize', this._onResize);
  770. this._onResize = null;
  771. };
  772. /**
  773. * Apply a scrollTop
  774. * @param {Number} scrollTop
  775. * @returns {Number} scrollTop Returns the applied scrollTop
  776. * @private
  777. */
  778. Core.prototype._setScrollTop = function (scrollTop) {
  779. this.props.scrollTop = scrollTop;
  780. this._updateScrollTop();
  781. return this.props.scrollTop;
  782. };
  783. /**
  784. * Update the current scrollTop when the height of the containers has been changed
  785. * @returns {Number} scrollTop Returns the applied scrollTop
  786. * @private
  787. */
  788. Core.prototype._updateScrollTop = function () {
  789. // recalculate the scrollTopMin
  790. var scrollTopMin = Math.min(this.props.centerContainer.height - this.props.center.height, 0); // is negative or zero
  791. if (scrollTopMin != this.props.scrollTopMin) {
  792. // in case of bottom orientation, change the scrollTop such that the contents
  793. // do not move relative to the time axis at the bottom
  794. if (this.options.orientation == 'bottom') {
  795. this.props.scrollTop += (scrollTopMin - this.props.scrollTopMin);
  796. }
  797. this.props.scrollTopMin = scrollTopMin;
  798. }
  799. // limit the scrollTop to the feasible scroll range
  800. if (this.props.scrollTop > 0) this.props.scrollTop = 0;
  801. if (this.props.scrollTop < scrollTopMin) this.props.scrollTop = scrollTopMin;
  802. return this.props.scrollTop;
  803. };
  804. /**
  805. * Get the current scrollTop
  806. * @returns {number} scrollTop
  807. * @private
  808. */
  809. Core.prototype._getScrollTop = function () {
  810. return this.props.scrollTop;
  811. };
  812. module.exports = Core;