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.

846 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('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. //var startDate = new Date(this.options.hide.start).getTime();
  552. //var endDate = new Date(this.options.hide.end).getTime();
  553. //var duration = endDate - startDate;
  554. //if (!(startDate >= this.range.start && endDate < this.range.end)) {
  555. // duration = 0;
  556. //}
  557. //
  558. //var conversion = this.range.conversion(this.props.center.width, duration);
  559. //var time = new Date(x / conversion.scale + conversion.offset);
  560. //
  561. //if (time >= endDate && startDate >= this.range.start && endDate < this.range.end) {
  562. // time -= duration;
  563. //}
  564. //
  565. //
  566. //return time;
  567. };
  568. /**
  569. * Convert a position on the global screen (pixels) to a datetime
  570. * @param {int} x Position on the screen in pixels
  571. * @return {Date} time The datetime the corresponds with given position x
  572. * @private
  573. */
  574. // TODO: move this function to Range
  575. Core.prototype._toGlobalTime = function(x) {
  576. return DateUtil.toTime(this.body, this.range, x, this.props.root.width);
  577. //var conversion = this.range.conversion(this.props.root.width);
  578. //return new Date(x / conversion.scale + conversion.offset);
  579. };
  580. /**
  581. * Convert a datetime (Date object) into a position on the screen
  582. * @param {Date} time A date
  583. * @return {int} x The position on the screen in pixels which corresponds
  584. * with the given date.
  585. * @private
  586. */
  587. // TODO: move this function to Range
  588. Core.prototype._toScreen = function(time) {
  589. return DateUtil.toScreen(this, time, this.props.center.width);
  590. };
  591. /**
  592. * Convert a datetime (Date object) into a position on the root
  593. * This is used to get the pixel density estimate for the screen, not the center panel
  594. * @param {Date} time A date
  595. * @return {int} x The position on root in pixels which corresponds
  596. * with the given date.
  597. * @private
  598. */
  599. // TODO: move this function to Range
  600. Core.prototype._toGlobalScreen = function(time) {
  601. return DateUtil.toScreen(this, time, this.props.root.width);
  602. //var conversion = this.range.conversion(this.props.root.width);
  603. //return (time.valueOf() - conversion.offset) * conversion.scale;
  604. };
  605. /**
  606. * Initialize watching when option autoResize is true
  607. * @private
  608. */
  609. Core.prototype._initAutoResize = function () {
  610. if (this.options.autoResize == true) {
  611. this._startAutoResize();
  612. }
  613. else {
  614. this._stopAutoResize();
  615. }
  616. };
  617. /**
  618. * Watch for changes in the size of the container. On resize, the Panel will
  619. * automatically redraw itself.
  620. * @private
  621. */
  622. Core.prototype._startAutoResize = function () {
  623. var me = this;
  624. this._stopAutoResize();
  625. this._onResize = function() {
  626. if (me.options.autoResize != true) {
  627. // stop watching when the option autoResize is changed to false
  628. me._stopAutoResize();
  629. return;
  630. }
  631. if (me.dom.root) {
  632. // check whether the frame is resized
  633. // Note: we compare offsetWidth here, not clientWidth. For some reason,
  634. // IE does not restore the clientWidth from 0 to the actual width after
  635. // changing the timeline's container display style from none to visible
  636. if ((me.dom.root.offsetWidth != me.props.lastWidth) ||
  637. (me.dom.root.offsetHeight != me.props.lastHeight)) {
  638. me.props.lastWidth = me.dom.root.offsetWidth;
  639. me.props.lastHeight = me.dom.root.offsetHeight;
  640. me.emit('change');
  641. }
  642. }
  643. };
  644. // add event listener to window resize
  645. util.addEventListener(window, 'resize', this._onResize);
  646. this.watchTimer = setInterval(this._onResize, 1000);
  647. };
  648. /**
  649. * Stop watching for a resize of the frame.
  650. * @private
  651. */
  652. Core.prototype._stopAutoResize = function () {
  653. if (this.watchTimer) {
  654. clearInterval(this.watchTimer);
  655. this.watchTimer = undefined;
  656. }
  657. // remove event listener on window.resize
  658. util.removeEventListener(window, 'resize', this._onResize);
  659. this._onResize = null;
  660. };
  661. /**
  662. * Start moving the timeline vertically
  663. * @param {Event} event
  664. * @private
  665. */
  666. Core.prototype._onTouch = function (event) {
  667. this.touch.allowDragging = true;
  668. };
  669. /**
  670. * Start moving the timeline vertically
  671. * @param {Event} event
  672. * @private
  673. */
  674. Core.prototype._onPinch = function (event) {
  675. this.touch.allowDragging = false;
  676. };
  677. /**
  678. * Start moving the timeline vertically
  679. * @param {Event} event
  680. * @private
  681. */
  682. Core.prototype._onDragStart = function (event) {
  683. this.touch.initialScrollTop = this.props.scrollTop;
  684. };
  685. /**
  686. * Move the timeline vertically
  687. * @param {Event} event
  688. * @private
  689. */
  690. Core.prototype._onDrag = function (event) {
  691. // refuse to drag when we where pinching to prevent the timeline make a jump
  692. // when releasing the fingers in opposite order from the touch screen
  693. if (!this.touch.allowDragging) return;
  694. var delta = event.gesture.deltaY;
  695. var oldScrollTop = this._getScrollTop();
  696. var newScrollTop = this._setScrollTop(this.touch.initialScrollTop + delta);
  697. if (newScrollTop != oldScrollTop) {
  698. this.redraw(); // TODO: this causes two redraws when dragging, the other is triggered by rangechange already
  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;