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.

577 lines
18 KiB

12 years ago
12 years ago
12 years ago
8 years ago
8 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. var Emitter = require('emitter-component');
  2. var Hammer = require('../module/hammer');
  3. var moment = require('../module/moment');
  4. var util = require('../util');
  5. var DataSet = require('../DataSet');
  6. var DataView = require('../DataView');
  7. var Range = require('./Range');
  8. var Core = require('./Core');
  9. var TimeAxis = require('./component/TimeAxis');
  10. var CurrentTime = require('./component/CurrentTime');
  11. var CustomTime = require('./component/CustomTime');
  12. var ItemSet = require('./component/ItemSet');
  13. var printStyle = require('../shared/Validator').printStyle;
  14. var allOptions = require('./optionsTimeline').allOptions;
  15. var configureOptions = require('./optionsTimeline').configureOptions;
  16. import Configurator from '../shared/Configurator';
  17. import Validator from '../shared/Validator';
  18. /**
  19. * Create a timeline visualization
  20. * @param {HTMLElement} container
  21. * @param {vis.DataSet | vis.DataView | Array} [items]
  22. * @param {vis.DataSet | vis.DataView | Array} [groups]
  23. * @param {Object} [options] See Timeline.setOptions for the available options.
  24. * @constructor
  25. * @extends Core
  26. */
  27. function Timeline (container, items, groups, options) {
  28. if (!(this instanceof Timeline)) {
  29. throw new SyntaxError('Constructor must be called with the new operator');
  30. }
  31. // if the third element is options, the forth is groups (optionally);
  32. if (!(Array.isArray(groups) || groups instanceof DataSet || groups instanceof DataView) && groups instanceof Object) {
  33. var forthArgument = options;
  34. options = groups;
  35. groups = forthArgument;
  36. }
  37. // TODO: REMOVE THIS in the next MAJOR release
  38. // see https://github.com/almende/vis/issues/2511
  39. if (options && options.throttleRedraw) {
  40. console.warn("Timeline option \"throttleRedraw\" is DEPRICATED and no longer supported. It will be removed in the next MAJOR release.");
  41. }
  42. var me = this;
  43. this.defaultOptions = {
  44. start: null,
  45. end: null,
  46. autoResize: true,
  47. orientation: {
  48. axis: 'bottom', // axis orientation: 'bottom', 'top', or 'both'
  49. item: 'bottom' // not relevant
  50. },
  51. moment: moment,
  52. width: null,
  53. height: null,
  54. maxHeight: null,
  55. minHeight: null
  56. };
  57. this.options = util.deepExtend({}, this.defaultOptions);
  58. // Create the DOM, props, and emitter
  59. this._create(container);
  60. if (!options || (options && typeof options.rtl == "undefined")) {
  61. var directionFromDom, domNode = this.dom.root;
  62. while (!directionFromDom && domNode) {
  63. directionFromDom = window.getComputedStyle(domNode, null).direction;
  64. domNode = domNode.parentElement;
  65. }
  66. this.options.rtl = (directionFromDom && (directionFromDom.toLowerCase() == "rtl"));
  67. } else {
  68. this.options.rtl = options.rtl;
  69. }
  70. this.options.rollingMode = options && options.rollingMode;
  71. // all components listed here will be repainted automatically
  72. this.components = [];
  73. this.body = {
  74. dom: this.dom,
  75. domProps: this.props,
  76. emitter: {
  77. on: this.on.bind(this),
  78. off: this.off.bind(this),
  79. emit: this.emit.bind(this)
  80. },
  81. hiddenDates: [],
  82. util: {
  83. getScale: function () {
  84. return me.timeAxis.step.scale;
  85. },
  86. getStep: function () {
  87. return me.timeAxis.step.step;
  88. },
  89. toScreen: me._toScreen.bind(me),
  90. toGlobalScreen: me._toGlobalScreen.bind(me), // this refers to the root.width
  91. toTime: me._toTime.bind(me),
  92. toGlobalTime : me._toGlobalTime.bind(me)
  93. }
  94. };
  95. // range
  96. this.range = new Range(this.body, this.options);
  97. this.components.push(this.range);
  98. this.body.range = this.range;
  99. // time axis
  100. this.timeAxis = new TimeAxis(this.body, this.options);
  101. this.timeAxis2 = null; // used in case of orientation option 'both'
  102. this.components.push(this.timeAxis);
  103. // current time bar
  104. this.currentTime = new CurrentTime(this.body, this.options);
  105. this.components.push(this.currentTime);
  106. // item set
  107. this.itemSet = new ItemSet(this.body, this.options);
  108. this.components.push(this.itemSet);
  109. this.itemsData = null; // DataSet
  110. this.groupsData = null; // DataSet
  111. this.dom.root.onclick = function (event) {
  112. me.emit('click', me.getEventProperties(event))
  113. };
  114. this.dom.root.ondblclick = function (event) {
  115. me.emit('doubleClick', me.getEventProperties(event))
  116. };
  117. this.dom.root.oncontextmenu = function (event) {
  118. me.emit('contextmenu', me.getEventProperties(event))
  119. };
  120. this.dom.root.onmouseover = function (event) {
  121. me.emit('mouseOver', me.getEventProperties(event))
  122. };
  123. this.dom.root.onmousemove = function (event) {
  124. me.emit('mouseMove', me.getEventProperties(event))
  125. };
  126. //Single time autoscale/fit
  127. this.fitDone = false;
  128. this.on('changed', function (){
  129. if (this.itemsData == null || this.options.rollingMode) return;
  130. if (!me.fitDone) {
  131. me.fitDone = true;
  132. if (me.options.start != undefined || me.options.end != undefined) {
  133. if (me.options.start == undefined || me.options.end == undefined) {
  134. var range = me.getItemRange();
  135. }
  136. var start = me.options.start != undefined ? me.options.start : range.min;
  137. var end = me.options.end != undefined ? me.options.end : range.max;
  138. me.setWindow(start, end, {animation: false});
  139. }
  140. else {
  141. me.fit({animation: false});
  142. }
  143. }
  144. });
  145. // apply options
  146. if (options) {
  147. this.setOptions(options);
  148. }
  149. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  150. if (groups) {
  151. this.setGroups(groups);
  152. }
  153. // create itemset
  154. if (items) {
  155. this.setItems(items);
  156. }
  157. // draw for the first time
  158. this._redraw();
  159. }
  160. // Extend the functionality from Core
  161. Timeline.prototype = new Core();
  162. /**
  163. * Load a configurator
  164. * @return {Object}
  165. * @private
  166. */
  167. Timeline.prototype._createConfigurator = function () {
  168. return new Configurator(this, this.dom.container, configureOptions);
  169. };
  170. /**
  171. * Force a redraw. The size of all items will be recalculated.
  172. * Can be useful to manually redraw when option autoResize=false and the window
  173. * has been resized, or when the items CSS has been changed.
  174. *
  175. * Note: this function will be overridden on construction with a trottled version
  176. */
  177. Timeline.prototype.redraw = function() {
  178. this.itemSet && this.itemSet.markDirty({refreshItems: true});
  179. this._redraw();
  180. };
  181. Timeline.prototype.setOptions = function (options) {
  182. // validate options
  183. let errorFound = Validator.validate(options, allOptions);
  184. if (errorFound === true) {
  185. console.log('%cErrors have been found in the supplied options object.', printStyle);
  186. }
  187. Core.prototype.setOptions.call(this, options);
  188. if ('type' in options) {
  189. if (options.type !== this.options.type) {
  190. this.options.type = options.type;
  191. // force recreation of all items
  192. var itemsData = this.itemsData;
  193. if (itemsData) {
  194. var selection = this.getSelection();
  195. this.setItems(null); // remove all
  196. this.setItems(itemsData); // add all
  197. this.setSelection(selection); // restore selection
  198. }
  199. }
  200. }
  201. };
  202. /**
  203. * Set items
  204. * @param {vis.DataSet | Array | null} items
  205. */
  206. Timeline.prototype.setItems = function(items) {
  207. // convert to type DataSet when needed
  208. var newDataSet;
  209. if (!items) {
  210. newDataSet = null;
  211. }
  212. else if (items instanceof DataSet || items instanceof DataView) {
  213. newDataSet = items;
  214. }
  215. else {
  216. // turn an array into a dataset
  217. newDataSet = new DataSet(items, {
  218. type: {
  219. start: 'Date',
  220. end: 'Date'
  221. }
  222. });
  223. }
  224. // set items
  225. this.itemsData = newDataSet;
  226. this.itemSet && this.itemSet.setItems(newDataSet);
  227. };
  228. /**
  229. * Set groups
  230. * @param {vis.DataSet | Array} groups
  231. */
  232. Timeline.prototype.setGroups = function(groups) {
  233. // convert to type DataSet when needed
  234. var newDataSet;
  235. if (!groups) {
  236. newDataSet = null;
  237. }
  238. else {
  239. var filter = {
  240. filter : function(group){
  241. return group.visible !== false;
  242. }
  243. }
  244. if (groups instanceof DataSet || groups instanceof DataView) {
  245. newDataSet = new DataView(groups,filter);
  246. }
  247. else {
  248. // turn an array into a dataset
  249. newDataSet = new DataSet(groups.filter(filter));
  250. }
  251. }
  252. this.groupsData = newDataSet;
  253. this.itemSet.setGroups(newDataSet);
  254. };
  255. /**
  256. * Set both items and groups in one go
  257. * @param {{items: Array | vis.DataSet, groups: Array | vis.DataSet}} data
  258. */
  259. Timeline.prototype.setData = function (data) {
  260. if (data && data.groups) {
  261. this.setGroups(data.groups);
  262. }
  263. if (data && data.items) {
  264. this.setItems(data.items);
  265. }
  266. };
  267. /**
  268. * Set selected items by their id. Replaces the current selection
  269. * Unknown id's are silently ignored.
  270. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  271. * selected. If ids is an empty array, all items will be
  272. * unselected.
  273. * @param {Object} [options] Available options:
  274. * `focus: boolean`
  275. * If true, focus will be set to the selected item(s)
  276. * `animation: boolean | {duration: number, easingFunction: string}`
  277. * If true (default), the range is animated
  278. * smoothly to the new window. An object can be
  279. * provided to specify duration and easing function.
  280. * Default duration is 500 ms, and default easing
  281. * function is 'easeInOutQuad'.
  282. * Only applicable when option focus is true.
  283. */
  284. Timeline.prototype.setSelection = function(ids, options) {
  285. this.itemSet && this.itemSet.setSelection(ids);
  286. if (options && options.focus) {
  287. this.focus(ids, options);
  288. }
  289. };
  290. /**
  291. * Get the selected items by their id
  292. * @return {Array} ids The ids of the selected items
  293. */
  294. Timeline.prototype.getSelection = function() {
  295. return this.itemSet && this.itemSet.getSelection() || [];
  296. };
  297. /**
  298. * Adjust the visible window such that the selected item (or multiple items)
  299. * are centered on screen.
  300. * @param {String | String[]} id An item id or array with item ids
  301. * @param {Object} [options] Available options:
  302. * `animation: boolean | {duration: number, easingFunction: string}`
  303. * If true (default), the range is animated
  304. * smoothly to the new window. An object can be
  305. * provided to specify duration and easing function.
  306. * Default duration is 500 ms, and default easing
  307. * function is 'easeInOutQuad'.
  308. */
  309. Timeline.prototype.focus = function(id, options) {
  310. if (!this.itemsData || id == undefined) return;
  311. var ids = Array.isArray(id) ? id : [id];
  312. // get the specified item(s)
  313. var itemsData = this.itemsData.getDataSet().get(ids, {
  314. type: {
  315. start: 'Date',
  316. end: 'Date'
  317. }
  318. });
  319. // calculate minimum start and maximum end of specified items
  320. var start = null;
  321. var end = null;
  322. itemsData.forEach(function (itemData) {
  323. var s = itemData.start.valueOf();
  324. var e = 'end' in itemData ? itemData.end.valueOf() : itemData.start.valueOf();
  325. if (start === null || s < start) {
  326. start = s;
  327. }
  328. if (end === null || e > end) {
  329. end = e;
  330. }
  331. });
  332. if (start !== null && end !== null) {
  333. // calculate the new middle and interval for the window
  334. var middle = (start + end) / 2;
  335. var interval = Math.max((this.range.end - this.range.start), (end - start) * 1.1);
  336. var animation = (options && options.animation !== undefined) ? options.animation : true;
  337. this.range.setRange(middle - interval / 2, middle + interval / 2, animation);
  338. }
  339. };
  340. /**
  341. * Set Timeline window such that it fits all items
  342. * @param {Object} [options] Available options:
  343. * `animation: boolean | {duration: number, easingFunction: string}`
  344. * If true (default), the range is animated
  345. * smoothly to the new window. An object can be
  346. * provided to specify duration and easing function.
  347. * Default duration is 500 ms, and default easing
  348. * function is 'easeInOutQuad'.
  349. */
  350. Timeline.prototype.fit = function (options) {
  351. var animation = (options && options.animation !== undefined) ? options.animation : true;
  352. var range;
  353. var dataset = this.itemsData && this.itemsData.getDataSet();
  354. if (dataset.length === 1 && dataset.get()[0].end === undefined) {
  355. // a single item -> don't fit, just show a range around the item from -4 to +3 days
  356. range = this.getDataRange();
  357. this.moveTo(range.min.valueOf(), {animation});
  358. }
  359. else {
  360. // exactly fit the items (plus a small margin)
  361. range = this.getItemRange();
  362. this.range.setRange(range.min, range.max, animation);
  363. }
  364. };
  365. /**
  366. * Determine the range of the items, taking into account their actual width
  367. * and a margin of 10 pixels on both sides.
  368. * @return {{min: Date | null, max: Date | null}}
  369. */
  370. Timeline.prototype.getItemRange = function () {
  371. // get a rough approximation for the range based on the items start and end dates
  372. var range = this.getDataRange();
  373. var min = range.min !== null ? range.min.valueOf() : null;
  374. var max = range.max !== null ? range.max.valueOf() : null;
  375. var minItem = null;
  376. var maxItem = null;
  377. if (min != null && max != null) {
  378. var interval = (max - min); // ms
  379. if (interval <= 0) {
  380. interval = 10;
  381. }
  382. var factor = interval / this.props.center.width;
  383. function getStart(item) {
  384. return util.convert(item.data.start, 'Date').valueOf()
  385. }
  386. function getEnd(item) {
  387. var end = item.data.end != undefined ? item.data.end : item.data.start;
  388. return util.convert(end, 'Date').valueOf();
  389. }
  390. // calculate the date of the left side and right side of the items given
  391. util.forEach(this.itemSet.items, function (item) {
  392. if (item.groupShowing) {
  393. item.show();
  394. item.repositionX();
  395. }
  396. var start = getStart(item);
  397. var end = getEnd(item);
  398. if (this.options.rtl) {
  399. var startSide = start - (item.getWidthRight() + 10) * factor;
  400. var endSide = end + (item.getWidthLeft() + 10) * factor;
  401. } else {
  402. var startSide = start - (item.getWidthLeft() + 10) * factor;
  403. var endSide = end + (item.getWidthRight() + 10) * factor;
  404. }
  405. if (startSide < min) {
  406. min = startSide;
  407. minItem = item;
  408. }
  409. if (endSide > max) {
  410. max = endSide;
  411. maxItem = item;
  412. }
  413. }.bind(this));
  414. if (minItem && maxItem) {
  415. var lhs = minItem.getWidthLeft() + 10;
  416. var rhs = maxItem.getWidthRight() + 10;
  417. var delta = this.props.center.width - lhs - rhs; // px
  418. if (delta > 0) {
  419. if (this.options.rtl) {
  420. min = getStart(minItem) - rhs * interval / delta; // ms
  421. max = getEnd(maxItem) + lhs * interval / delta; // ms
  422. } else {
  423. min = getStart(minItem) - lhs * interval / delta; // ms
  424. max = getEnd(maxItem) + rhs * interval / delta; // ms
  425. }
  426. }
  427. }
  428. }
  429. return {
  430. min: min != null ? new Date(min) : null,
  431. max: max != null ? new Date(max) : null
  432. }
  433. };
  434. /**
  435. * Calculate the data range of the items start and end dates
  436. * @returns {{min: Date | null, max: Date | null}}
  437. */
  438. Timeline.prototype.getDataRange = function() {
  439. var min = null;
  440. var max = null;
  441. var dataset = this.itemsData && this.itemsData.getDataSet();
  442. if (dataset) {
  443. dataset.forEach(function (item) {
  444. var start = util.convert(item.start, 'Date').valueOf();
  445. var end = util.convert(item.end != undefined ? item.end : item.start, 'Date').valueOf();
  446. if (min === null || start < min) {
  447. min = start;
  448. }
  449. if (max === null || end > max) {
  450. max = end;
  451. }
  452. });
  453. }
  454. return {
  455. min: min != null ? new Date(min) : null,
  456. max: max != null ? new Date(max) : null
  457. }
  458. };
  459. /**
  460. * Generate Timeline related information from an event
  461. * @param {Event} event
  462. * @return {Object} An object with related information, like on which area
  463. * The event happened, whether clicked on an item, etc.
  464. */
  465. Timeline.prototype.getEventProperties = function (event) {
  466. var clientX = event.center ? event.center.x : event.clientX;
  467. var clientY = event.center ? event.center.y : event.clientY;
  468. if (this.options.rtl) {
  469. var x = util.getAbsoluteRight(this.dom.centerContainer) - clientX;
  470. } else {
  471. var x = clientX - util.getAbsoluteLeft(this.dom.centerContainer);
  472. }
  473. var y = clientY - util.getAbsoluteTop(this.dom.centerContainer);
  474. var item = this.itemSet.itemFromTarget(event);
  475. var group = this.itemSet.groupFromTarget(event);
  476. var customTime = CustomTime.customTimeFromTarget(event);
  477. var snap = this.itemSet.options.snap || null;
  478. var scale = this.body.util.getScale();
  479. var step = this.body.util.getStep();
  480. var time = this._toTime(x);
  481. var snappedTime = snap ? snap(time, scale, step) : time;
  482. var element = util.getTarget(event);
  483. var what = null;
  484. if (item != null) {what = 'item';}
  485. else if (customTime != null) {what = 'custom-time';}
  486. else if (util.hasParent(element, this.timeAxis.dom.foreground)) {what = 'axis';}
  487. else if (this.timeAxis2 && util.hasParent(element, this.timeAxis2.dom.foreground)) {what = 'axis';}
  488. else if (util.hasParent(element, this.itemSet.dom.labelSet)) {what = 'group-label';}
  489. else if (util.hasParent(element, this.currentTime.bar)) {what = 'current-time';}
  490. else if (util.hasParent(element, this.dom.center)) {what = 'background';}
  491. return {
  492. event: event,
  493. item: item ? item.id : null,
  494. group: group ? group.groupId : null,
  495. what: what,
  496. pageX: event.srcEvent ? event.srcEvent.pageX : event.pageX,
  497. pageY: event.srcEvent ? event.srcEvent.pageY : event.pageY,
  498. x: x,
  499. y: y,
  500. time: time,
  501. snappedTime: snappedTime
  502. }
  503. };
  504. module.exports = Timeline;