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.

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