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.

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