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.

880 lines
29 KiB

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