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.

870 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. this.activator = new Activator(this.dom.root);
  183. }
  184. else {
  185. if (this.activator) {
  186. this.activator.destroy();
  187. delete this.activator;
  188. }
  189. }
  190. }
  191. // enable/disable autoResize
  192. this._initAutoResize();
  193. }
  194. // propagate options to all components
  195. this.components.forEach(function (component) {
  196. component.setOptions(options);
  197. });
  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(function (component) {
  242. component.destroy();
  243. });
  244. this.body = null;
  245. };
  246. /**
  247. * Set a custom time bar
  248. * @param {Date} time
  249. */
  250. Core.prototype.setCustomTime = function (time) {
  251. if (!this.customTime) {
  252. throw new Error('Cannot get custom time: Custom time bar is not enabled');
  253. }
  254. this.customTime.setCustomTime(time);
  255. };
  256. /**
  257. * Retrieve the current custom time.
  258. * @return {Date} customTime
  259. */
  260. Core.prototype.getCustomTime = function() {
  261. if (!this.customTime) {
  262. throw new Error('Cannot get custom time: Custom time bar is not enabled');
  263. }
  264. return this.customTime.getCustomTime();
  265. };
  266. /**
  267. * Get the id's of the currently visible items.
  268. * @returns {Array} The ids of the visible items
  269. */
  270. Core.prototype.getVisibleItems = function() {
  271. return this.itemSet && this.itemSet.getVisibleItems() || [];
  272. };
  273. /**
  274. * Clear the Core. By Default, items, groups and options are cleared.
  275. * Example usage:
  276. *
  277. * timeline.clear(); // clear items, groups, and options
  278. * timeline.clear({options: true}); // clear options only
  279. *
  280. * @param {Object} [what] Optionally specify what to clear. By default:
  281. * {items: true, groups: true, options: true}
  282. */
  283. Core.prototype.clear = function(what) {
  284. // clear items
  285. if (!what || what.items) {
  286. this.setItems(null);
  287. }
  288. // clear groups
  289. if (!what || what.groups) {
  290. this.setGroups(null);
  291. }
  292. // clear options of timeline and of each of the components
  293. if (!what || what.options) {
  294. this.components.forEach(function (component) {
  295. component.setOptions(component.defaultOptions);
  296. });
  297. this.setOptions(this.defaultOptions); // this will also do a redraw
  298. }
  299. };
  300. /**
  301. * Set Core window such that it fits all items
  302. * @param {Object} [options] Available options:
  303. * `animate: boolean | number`
  304. * If true (default), the range is animated
  305. * smoothly to the new window.
  306. * If a number, the number is taken as duration
  307. * for the animation. Default duration is 500 ms.
  308. */
  309. Core.prototype.fit = function(options) {
  310. var range = this._getDataRange();
  311. // skip range set if there is no start and end date
  312. if (range.start === null && range.end === null) {
  313. return;
  314. }
  315. var animate = (options && options.animate !== undefined) ? options.animate : true;
  316. this.range.setRange(range.start, range.end, animate);
  317. };
  318. /**
  319. * Calculate the data range of the items and applies a 5% window around it.
  320. * @returns {{start: Date | null, end: Date | null}}
  321. * @protected
  322. */
  323. Core.prototype._getDataRange = function() {
  324. // apply the data range as range
  325. var dataRange = this.getItemRange();
  326. // add 5% space on both sides
  327. var start = dataRange.min;
  328. var end = dataRange.max;
  329. if (start != null && end != null) {
  330. var interval = (end.valueOf() - start.valueOf());
  331. if (interval <= 0) {
  332. // prevent an empty interval
  333. interval = 24 * 60 * 60 * 1000; // 1 day
  334. }
  335. start = new Date(start.valueOf() - interval * 0.05);
  336. end = new Date(end.valueOf() + interval * 0.05);
  337. }
  338. return {
  339. start: start,
  340. end: end
  341. }
  342. };
  343. /**
  344. * Set the visible window. Both parameters are optional, you can change only
  345. * start or only end. Syntax:
  346. *
  347. * TimeLine.setWindow(start, end)
  348. * TimeLine.setWindow(range)
  349. *
  350. * Where start and end can be a Date, number, or string, and range is an
  351. * object with properties start and end.
  352. *
  353. * @param {Date | Number | String | Object} [start] Start date of visible window
  354. * @param {Date | Number | String} [end] End date of visible window
  355. * @param {Object} [options] Available options:
  356. * `animate: boolean | number`
  357. * If true (default), the range is animated
  358. * smoothly to the new window.
  359. * If a number, the number is taken as duration
  360. * for the animation. Default duration is 500 ms.
  361. */
  362. Core.prototype.setWindow = function(start, end, options) {
  363. var animate = (options && options.animate !== undefined) ? options.animate : true;
  364. if (arguments.length == 1) {
  365. var range = arguments[0];
  366. this.range.setRange(range.start, range.end, animate);
  367. }
  368. else {
  369. this.range.setRange(start, end, animate);
  370. }
  371. };
  372. /**
  373. * Move the window such that given time is centered on screen.
  374. * @param {Date | Number | String} time
  375. * @param {Object} [options] Available options:
  376. * `animate: boolean | number`
  377. * If true (default), the range is animated
  378. * smoothly to the new window.
  379. * If a number, the number is taken as duration
  380. * for the animation. Default duration is 500 ms.
  381. */
  382. Core.prototype.moveTo = function(time, options) {
  383. var interval = this.range.end - this.range.start;
  384. var t = util.convert(time, 'Date').valueOf();
  385. var start = t - interval / 2;
  386. var end = t + interval / 2;
  387. var animate = (options && options.animate !== undefined) ? options.animate : true;
  388. this.range.setRange(start, end, animate);
  389. };
  390. /**
  391. * Get the visible window
  392. * @return {{start: Date, end: Date}} Visible range
  393. */
  394. Core.prototype.getWindow = function() {
  395. var range = this.range.getRange();
  396. return {
  397. start: new Date(range.start),
  398. end: new Date(range.end)
  399. };
  400. };
  401. /**
  402. * Force a redraw of the Core. Can be useful to manually redraw when
  403. * option autoResize=false
  404. */
  405. Core.prototype.redraw = function() {
  406. var resized = false;
  407. var options = this.options;
  408. var props = this.props;
  409. var dom = this.dom;
  410. if (!dom) return; // when destroyed
  411. DateUtil.updateHiddenDates(this.body, this.options.hiddenDates);
  412. // update class names
  413. if (options.orientation == 'top') {
  414. util.addClassName(dom.root, 'top');
  415. util.removeClassName(dom.root, 'bottom');
  416. }
  417. else {
  418. util.removeClassName(dom.root, 'top');
  419. util.addClassName(dom.root, 'bottom');
  420. }
  421. // update root width and height options
  422. dom.root.style.maxHeight = util.option.asSize(options.maxHeight, '');
  423. dom.root.style.minHeight = util.option.asSize(options.minHeight, '');
  424. dom.root.style.width = util.option.asSize(options.width, '');
  425. // calculate border widths
  426. props.border.left = (dom.centerContainer.offsetWidth - dom.centerContainer.clientWidth) / 2;
  427. props.border.right = props.border.left;
  428. props.border.top = (dom.centerContainer.offsetHeight - dom.centerContainer.clientHeight) / 2;
  429. props.border.bottom = props.border.top;
  430. var borderRootHeight= dom.root.offsetHeight - dom.root.clientHeight;
  431. var borderRootWidth = dom.root.offsetWidth - dom.root.clientWidth;
  432. // workaround for a bug in IE: the clientWidth of an element with
  433. // a height:0px and overflow:hidden is not calculated and always has value 0
  434. if (dom.centerContainer.clientHeight === 0) {
  435. props.border.left = props.border.top;
  436. props.border.right = props.border.left;
  437. }
  438. if (dom.root.clientHeight === 0) {
  439. borderRootWidth = borderRootHeight;
  440. }
  441. // calculate the heights. If any of the side panels is empty, we set the height to
  442. // minus the border width, such that the border will be invisible
  443. props.center.height = dom.center.offsetHeight;
  444. props.left.height = dom.left.offsetHeight;
  445. props.right.height = dom.right.offsetHeight;
  446. props.top.height = dom.top.clientHeight || -props.border.top;
  447. props.bottom.height = dom.bottom.clientHeight || -props.border.bottom;
  448. // TODO: compensate borders when any of the panels is empty.
  449. // apply auto height
  450. // TODO: only calculate autoHeight when needed (else we cause an extra reflow/repaint of the DOM)
  451. var contentHeight = Math.max(props.left.height, props.center.height, props.right.height);
  452. var autoHeight = props.top.height + contentHeight + props.bottom.height +
  453. borderRootHeight + props.border.top + props.border.bottom;
  454. dom.root.style.height = util.option.asSize(options.height, autoHeight + 'px');
  455. // calculate heights of the content panels
  456. props.root.height = dom.root.offsetHeight;
  457. props.background.height = props.root.height - borderRootHeight;
  458. var containerHeight = props.root.height - props.top.height - props.bottom.height -
  459. borderRootHeight;
  460. props.centerContainer.height = containerHeight;
  461. props.leftContainer.height = containerHeight;
  462. props.rightContainer.height = props.leftContainer.height;
  463. // calculate the widths of the panels
  464. props.root.width = dom.root.offsetWidth;
  465. props.background.width = props.root.width - borderRootWidth;
  466. props.left.width = dom.leftContainer.clientWidth || -props.border.left;
  467. props.leftContainer.width = props.left.width;
  468. props.right.width = dom.rightContainer.clientWidth || -props.border.right;
  469. props.rightContainer.width = props.right.width;
  470. var centerWidth = props.root.width - props.left.width - props.right.width - borderRootWidth;
  471. props.center.width = centerWidth;
  472. props.centerContainer.width = centerWidth;
  473. props.top.width = centerWidth;
  474. props.bottom.width = centerWidth;
  475. // resize the panels
  476. dom.background.style.height = props.background.height + 'px';
  477. dom.backgroundVertical.style.height = props.background.height + 'px';
  478. dom.backgroundHorizontal.style.height = props.centerContainer.height + 'px';
  479. dom.centerContainer.style.height = props.centerContainer.height + 'px';
  480. dom.leftContainer.style.height = props.leftContainer.height + 'px';
  481. dom.rightContainer.style.height = props.rightContainer.height + 'px';
  482. dom.background.style.width = props.background.width + 'px';
  483. dom.backgroundVertical.style.width = props.centerContainer.width + 'px';
  484. dom.backgroundHorizontal.style.width = props.background.width + 'px';
  485. dom.centerContainer.style.width = props.center.width + 'px';
  486. dom.top.style.width = props.top.width + 'px';
  487. dom.bottom.style.width = props.bottom.width + 'px';
  488. // reposition the panels
  489. dom.background.style.left = '0';
  490. dom.background.style.top = '0';
  491. dom.backgroundVertical.style.left = (props.left.width + props.border.left) + 'px';
  492. dom.backgroundVertical.style.top = '0';
  493. dom.backgroundHorizontal.style.left = '0';
  494. dom.backgroundHorizontal.style.top = props.top.height + 'px';
  495. dom.centerContainer.style.left = props.left.width + 'px';
  496. dom.centerContainer.style.top = props.top.height + 'px';
  497. dom.leftContainer.style.left = '0';
  498. dom.leftContainer.style.top = props.top.height + 'px';
  499. dom.rightContainer.style.left = (props.left.width + props.center.width) + 'px';
  500. dom.rightContainer.style.top = props.top.height + 'px';
  501. dom.top.style.left = props.left.width + 'px';
  502. dom.top.style.top = '0';
  503. dom.bottom.style.left = props.left.width + 'px';
  504. dom.bottom.style.top = (props.top.height + props.centerContainer.height) + 'px';
  505. // update the scrollTop, feasible range for the offset can be changed
  506. // when the height of the Core or of the contents of the center changed
  507. this._updateScrollTop();
  508. // reposition the scrollable contents
  509. var offset = this.props.scrollTop;
  510. if (options.orientation == 'bottom') {
  511. offset += Math.max(this.props.centerContainer.height - this.props.center.height -
  512. this.props.border.top - this.props.border.bottom, 0);
  513. }
  514. dom.center.style.left = '0';
  515. dom.center.style.top = offset + 'px';
  516. dom.left.style.left = '0';
  517. dom.left.style.top = offset + 'px';
  518. dom.right.style.left = '0';
  519. dom.right.style.top = offset + 'px';
  520. // show shadows when vertical scrolling is available
  521. var visibilityTop = this.props.scrollTop == 0 ? 'hidden' : '';
  522. var visibilityBottom = this.props.scrollTop == this.props.scrollTopMin ? 'hidden' : '';
  523. dom.shadowTop.style.visibility = visibilityTop;
  524. dom.shadowBottom.style.visibility = visibilityBottom;
  525. dom.shadowTopLeft.style.visibility = visibilityTop;
  526. dom.shadowBottomLeft.style.visibility = visibilityBottom;
  527. dom.shadowTopRight.style.visibility = visibilityTop;
  528. dom.shadowBottomRight.style.visibility = visibilityBottom;
  529. // redraw all components
  530. this.components.forEach(function (component) {
  531. resized = component.redraw() || resized;
  532. });
  533. if (resized) {
  534. // keep repainting until all sizes are settled
  535. var MAX_REDRAWS = 3; // maximum number of consecutive redraws
  536. if (this.redrawCount < MAX_REDRAWS) {
  537. this.redrawCount++;
  538. this.redraw();
  539. }
  540. else {
  541. console.log('WARNING: infinite loop in redraw?')
  542. }
  543. this.redrawCount = 0;
  544. }
  545. this.emit("finishedRedraw");
  546. };
  547. // TODO: deprecated since version 1.1.0, remove some day
  548. Core.prototype.repaint = function () {
  549. throw new Error('Function repaint is deprecated. Use redraw instead.');
  550. };
  551. /**
  552. * Set a current time. This can be used for example to ensure that a client's
  553. * time is synchronized with a shared server time.
  554. * Only applicable when option `showCurrentTime` is true.
  555. * @param {Date | String | Number} time A Date, unix timestamp, or
  556. * ISO date string.
  557. */
  558. Core.prototype.setCurrentTime = function(time) {
  559. if (!this.currentTime) {
  560. throw new Error('Option showCurrentTime must be true');
  561. }
  562. this.currentTime.setCurrentTime(time);
  563. };
  564. /**
  565. * Get the current time.
  566. * Only applicable when option `showCurrentTime` is true.
  567. * @return {Date} Returns the current time.
  568. */
  569. Core.prototype.getCurrentTime = function() {
  570. if (!this.currentTime) {
  571. throw new Error('Option showCurrentTime must be true');
  572. }
  573. return this.currentTime.getCurrentTime();
  574. };
  575. /**
  576. * Convert a position on screen (pixels) to a datetime
  577. * @param {int} x Position on the screen in pixels
  578. * @return {Date} time The datetime the corresponds with given position x
  579. * @private
  580. */
  581. // TODO: move this function to Range
  582. Core.prototype._toTime = function(x) {
  583. return DateUtil.toTime(this, x, this.props.center.width);
  584. };
  585. /**
  586. * Convert a position on the global screen (pixels) to a datetime
  587. * @param {int} x Position on the screen in pixels
  588. * @return {Date} time The datetime the corresponds with given position x
  589. * @private
  590. */
  591. // TODO: move this function to Range
  592. Core.prototype._toGlobalTime = function(x) {
  593. return DateUtil.toTime(this, x, this.props.root.width);
  594. //var conversion = this.range.conversion(this.props.root.width);
  595. //return new Date(x / conversion.scale + conversion.offset);
  596. };
  597. /**
  598. * Convert a datetime (Date object) into a position on the screen
  599. * @param {Date} time A date
  600. * @return {int} x The position on the screen in pixels which corresponds
  601. * with the given date.
  602. * @private
  603. */
  604. // TODO: move this function to Range
  605. Core.prototype._toScreen = function(time) {
  606. return DateUtil.toScreen(this, time, this.props.center.width);
  607. };
  608. /**
  609. * Convert a datetime (Date object) into a position on the root
  610. * This is used to get the pixel density estimate for the screen, not the center panel
  611. * @param {Date} time A date
  612. * @return {int} x The position on root in pixels which corresponds
  613. * with the given date.
  614. * @private
  615. */
  616. // TODO: move this function to Range
  617. Core.prototype._toGlobalScreen = function(time) {
  618. return DateUtil.toScreen(this, time, this.props.root.width);
  619. //var conversion = this.range.conversion(this.props.root.width);
  620. //return (time.valueOf() - conversion.offset) * conversion.scale;
  621. };
  622. /**
  623. * Initialize watching when option autoResize is true
  624. * @private
  625. */
  626. Core.prototype._initAutoResize = function () {
  627. if (this.options.autoResize == true) {
  628. this._startAutoResize();
  629. }
  630. else {
  631. this._stopAutoResize();
  632. }
  633. };
  634. /**
  635. * Watch for changes in the size of the container. On resize, the Panel will
  636. * automatically redraw itself.
  637. * @private
  638. */
  639. Core.prototype._startAutoResize = function () {
  640. var me = this;
  641. this._stopAutoResize();
  642. this._onResize = function() {
  643. if (me.options.autoResize != true) {
  644. // stop watching when the option autoResize is changed to false
  645. me._stopAutoResize();
  646. return;
  647. }
  648. if (me.dom.root) {
  649. // check whether the frame is resized
  650. // Note: we compare offsetWidth here, not clientWidth. For some reason,
  651. // IE does not restore the clientWidth from 0 to the actual width after
  652. // changing the timeline's container display style from none to visible
  653. if ((me.dom.root.offsetWidth != me.props.lastWidth) ||
  654. (me.dom.root.offsetHeight != me.props.lastHeight)) {
  655. me.props.lastWidth = me.dom.root.offsetWidth;
  656. me.props.lastHeight = me.dom.root.offsetHeight;
  657. me.emit('change');
  658. }
  659. }
  660. };
  661. // add event listener to window resize
  662. util.addEventListener(window, 'resize', this._onResize);
  663. this.watchTimer = setInterval(this._onResize, 1000);
  664. };
  665. /**
  666. * Stop watching for a resize of the frame.
  667. * @private
  668. */
  669. Core.prototype._stopAutoResize = function () {
  670. if (this.watchTimer) {
  671. clearInterval(this.watchTimer);
  672. this.watchTimer = undefined;
  673. }
  674. // remove event listener on window.resize
  675. util.removeEventListener(window, 'resize', this._onResize);
  676. this._onResize = null;
  677. };
  678. /**
  679. * Start moving the timeline vertically
  680. * @param {Event} event
  681. * @private
  682. */
  683. Core.prototype._onTouch = function (event) {
  684. this.touch.allowDragging = true;
  685. };
  686. /**
  687. * Start moving the timeline vertically
  688. * @param {Event} event
  689. * @private
  690. */
  691. Core.prototype._onPinch = function (event) {
  692. this.touch.allowDragging = false;
  693. };
  694. /**
  695. * Start moving the timeline vertically
  696. * @param {Event} event
  697. * @private
  698. */
  699. Core.prototype._onDragStart = function (event) {
  700. this.touch.initialScrollTop = this.props.scrollTop;
  701. };
  702. /**
  703. * Move the timeline vertically
  704. * @param {Event} event
  705. * @private
  706. */
  707. Core.prototype._onDrag = function (event) {
  708. // refuse to drag when we where pinching to prevent the timeline make a jump
  709. // when releasing the fingers in opposite order from the touch screen
  710. if (!this.touch.allowDragging) return;
  711. var delta = event.gesture.deltaY;
  712. var oldScrollTop = this._getScrollTop();
  713. var newScrollTop = this._setScrollTop(this.touch.initialScrollTop + delta);
  714. if (newScrollTop != oldScrollTop) {
  715. this.redraw(); // TODO: this causes two redraws when dragging, the other is triggered by rangechange already
  716. this.emit("verticalDrag");
  717. }
  718. };
  719. /**
  720. * Apply a scrollTop
  721. * @param {Number} scrollTop
  722. * @returns {Number} scrollTop Returns the applied scrollTop
  723. * @private
  724. */
  725. Core.prototype._setScrollTop = function (scrollTop) {
  726. this.props.scrollTop = scrollTop;
  727. this._updateScrollTop();
  728. return this.props.scrollTop;
  729. };
  730. /**
  731. * Update the current scrollTop when the height of the containers has been changed
  732. * @returns {Number} scrollTop Returns the applied scrollTop
  733. * @private
  734. */
  735. Core.prototype._updateScrollTop = function () {
  736. // recalculate the scrollTopMin
  737. var scrollTopMin = Math.min(this.props.centerContainer.height - this.props.center.height, 0); // is negative or zero
  738. if (scrollTopMin != this.props.scrollTopMin) {
  739. // in case of bottom orientation, change the scrollTop such that the contents
  740. // do not move relative to the time axis at the bottom
  741. if (this.options.orientation == 'bottom') {
  742. this.props.scrollTop += (scrollTopMin - this.props.scrollTopMin);
  743. }
  744. this.props.scrollTopMin = scrollTopMin;
  745. }
  746. // limit the scrollTop to the feasible scroll range
  747. if (this.props.scrollTop > 0) this.props.scrollTop = 0;
  748. if (this.props.scrollTop < scrollTopMin) this.props.scrollTop = scrollTopMin;
  749. return this.props.scrollTop;
  750. };
  751. /**
  752. * Get the current scrollTop
  753. * @returns {number} scrollTop
  754. * @private
  755. */
  756. Core.prototype._getScrollTop = function () {
  757. return this.props.scrollTop;
  758. };
  759. module.exports = Core;