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.

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