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.

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