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.

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