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.

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