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.

548 lines
17 KiB

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