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.

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