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.

1622 lines
45 KiB

10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var Hammer = require('../../module/hammer');
  2. var util = require('../../util');
  3. var DataSet = require('../../DataSet');
  4. var DataView = require('../../DataView');
  5. var TimeStep = require('../TimeStep');
  6. var Component = require('./Component');
  7. var Group = require('./Group');
  8. var BackgroundGroup = require('./BackgroundGroup');
  9. var BoxItem = require('./item/BoxItem');
  10. var PointItem = require('./item/PointItem');
  11. var RangeItem = require('./item/RangeItem');
  12. var BackgroundItem = require('./item/BackgroundItem');
  13. var UNGROUPED = '__ungrouped__'; // reserved group id for ungrouped items
  14. var BACKGROUND = '__background__'; // reserved group id for background items without group
  15. /**
  16. * An ItemSet holds a set of items and ranges which can be displayed in a
  17. * range. The width is determined by the parent of the ItemSet, and the height
  18. * is determined by the size of the items.
  19. * @param {{dom: Object, domProps: Object, emitter: Emitter, range: Range}} body
  20. * @param {Object} [options] See ItemSet.setOptions for the available options.
  21. * @constructor ItemSet
  22. * @extends Component
  23. */
  24. function ItemSet(body, options) {
  25. this.body = body;
  26. this.defaultOptions = {
  27. type: null, // 'box', 'point', 'range', 'background'
  28. orientation: 'bottom', // item orientation: 'top' or 'bottom'
  29. align: 'auto', // alignment of box items
  30. stack: true,
  31. groupOrder: null,
  32. selectable: true,
  33. editable: {
  34. updateTime: false,
  35. updateGroup: false,
  36. add: false,
  37. remove: false
  38. },
  39. snap: TimeStep.snap,
  40. onAdd: function (item, callback) {
  41. callback(item);
  42. },
  43. onUpdate: function (item, callback) {
  44. callback(item);
  45. },
  46. onMove: function (item, callback) {
  47. callback(item);
  48. },
  49. onRemove: function (item, callback) {
  50. callback(item);
  51. },
  52. onMoving: function (item, callback) {
  53. callback(item);
  54. },
  55. margin: {
  56. item: {
  57. horizontal: 10,
  58. vertical: 10
  59. },
  60. axis: 20
  61. },
  62. padding: 5
  63. };
  64. // options is shared by this ItemSet and all its items
  65. this.options = util.extend({}, this.defaultOptions);
  66. // options for getting items from the DataSet with the correct type
  67. this.itemOptions = {
  68. type: {start: 'Date', end: 'Date'}
  69. };
  70. this.conversion = {
  71. toScreen: body.util.toScreen,
  72. toTime: body.util.toTime
  73. };
  74. this.dom = {};
  75. this.props = {};
  76. this.hammer = null;
  77. var me = this;
  78. this.itemsData = null; // DataSet
  79. this.groupsData = null; // DataSet
  80. // listeners for the DataSet of the items
  81. this.itemListeners = {
  82. 'add': function (event, params, senderId) {
  83. me._onAdd(params.items);
  84. },
  85. 'update': function (event, params, senderId) {
  86. me._onUpdate(params.items);
  87. },
  88. 'remove': function (event, params, senderId) {
  89. me._onRemove(params.items);
  90. }
  91. };
  92. // listeners for the DataSet of the groups
  93. this.groupListeners = {
  94. 'add': function (event, params, senderId) {
  95. me._onAddGroups(params.items);
  96. },
  97. 'update': function (event, params, senderId) {
  98. me._onUpdateGroups(params.items);
  99. },
  100. 'remove': function (event, params, senderId) {
  101. me._onRemoveGroups(params.items);
  102. }
  103. };
  104. this.items = {}; // object with an Item for every data item
  105. this.groups = {}; // Group object for every group
  106. this.groupIds = [];
  107. this.selection = []; // list with the ids of all selected nodes
  108. this.stackDirty = true; // if true, all items will be restacked on next redraw
  109. this.touchParams = {}; // stores properties while dragging
  110. // create the HTML DOM
  111. this._create();
  112. this.setOptions(options);
  113. }
  114. ItemSet.prototype = new Component();
  115. // available item types will be registered here
  116. ItemSet.types = {
  117. background: BackgroundItem,
  118. box: BoxItem,
  119. range: RangeItem,
  120. point: PointItem
  121. };
  122. /**
  123. * Create the HTML DOM for the ItemSet
  124. */
  125. ItemSet.prototype._create = function(){
  126. var frame = document.createElement('div');
  127. frame.className = 'itemset';
  128. frame['timeline-itemset'] = this;
  129. this.dom.frame = frame;
  130. // create background panel
  131. var background = document.createElement('div');
  132. background.className = 'background';
  133. frame.appendChild(background);
  134. this.dom.background = background;
  135. // create foreground panel
  136. var foreground = document.createElement('div');
  137. foreground.className = 'foreground';
  138. frame.appendChild(foreground);
  139. this.dom.foreground = foreground;
  140. // create axis panel
  141. var axis = document.createElement('div');
  142. axis.className = 'axis';
  143. this.dom.axis = axis;
  144. // create labelset
  145. var labelSet = document.createElement('div');
  146. labelSet.className = 'labelset';
  147. this.dom.labelSet = labelSet;
  148. // create ungrouped Group
  149. this._updateUngrouped();
  150. // create background Group
  151. var backgroundGroup = new BackgroundGroup(BACKGROUND, null, this);
  152. backgroundGroup.show();
  153. this.groups[BACKGROUND] = backgroundGroup;
  154. // attach event listeners
  155. // Note: we bind to the centerContainer for the case where the height
  156. // of the center container is larger than of the ItemSet, so we
  157. // can click in the empty area to create a new item or deselect an item.
  158. this.hammer = Hammer(this.body.dom.centerContainer, {
  159. preventDefault: true
  160. });
  161. // drag items when selected
  162. this.hammer.on('touch', this._onTouch.bind(this));
  163. this.hammer.on('dragstart', this._onDragStart.bind(this));
  164. this.hammer.on('drag', this._onDrag.bind(this));
  165. this.hammer.on('dragend', this._onDragEnd.bind(this));
  166. // single select (or unselect) when tapping an item
  167. this.hammer.on('tap', this._onSelectItem.bind(this));
  168. // multi select when holding mouse/touch, or on ctrl+click
  169. this.hammer.on('hold', this._onMultiSelectItem.bind(this));
  170. // add item on doubletap
  171. this.hammer.on('doubletap', this._onAddItem.bind(this));
  172. // attach to the DOM
  173. this.show();
  174. };
  175. /**
  176. * Set options for the ItemSet. Existing options will be extended/overwritten.
  177. * @param {Object} [options] The following options are available:
  178. * {String} type
  179. * Default type for the items. Choose from 'box'
  180. * (default), 'point', 'range', or 'background'.
  181. * The default style can be overwritten by
  182. * individual items.
  183. * {String} align
  184. * Alignment for the items, only applicable for
  185. * BoxItem. Choose 'center' (default), 'left', or
  186. * 'right'.
  187. * {String} orientation
  188. * Orientation of the item set. Choose 'top' or
  189. * 'bottom' (default).
  190. * {Function} groupOrder
  191. * A sorting function for ordering groups
  192. * {Boolean} stack
  193. * If true (deafult), items will be stacked on
  194. * top of each other.
  195. * {Number} margin.axis
  196. * Margin between the axis and the items in pixels.
  197. * Default is 20.
  198. * {Number} margin.item.horizontal
  199. * Horizontal margin between items in pixels.
  200. * Default is 10.
  201. * {Number} margin.item.vertical
  202. * Vertical Margin between items in pixels.
  203. * Default is 10.
  204. * {Number} margin.item
  205. * Margin between items in pixels in both horizontal
  206. * and vertical direction. Default is 10.
  207. * {Number} margin
  208. * Set margin for both axis and items in pixels.
  209. * {Number} padding
  210. * Padding of the contents of an item in pixels.
  211. * Must correspond with the items css. Default is 5.
  212. * {Boolean} selectable
  213. * If true (default), items can be selected.
  214. * {Boolean} editable
  215. * Set all editable options to true or false
  216. * {Boolean} editable.updateTime
  217. * Allow dragging an item to an other moment in time
  218. * {Boolean} editable.updateGroup
  219. * Allow dragging an item to an other group
  220. * {Boolean} editable.add
  221. * Allow creating new items on double tap
  222. * {Boolean} editable.remove
  223. * Allow removing items by clicking the delete button
  224. * top right of a selected item.
  225. * {Function(item: Item, callback: Function)} onAdd
  226. * Callback function triggered when an item is about to be added:
  227. * when the user double taps an empty space in the Timeline.
  228. * {Function(item: Item, callback: Function)} onUpdate
  229. * Callback function fired when an item is about to be updated.
  230. * This function typically has to show a dialog where the user
  231. * change the item. If not implemented, nothing happens.
  232. * {Function(item: Item, callback: Function)} onMove
  233. * Fired when an item has been moved. If not implemented,
  234. * the move action will be accepted.
  235. * {Function(item: Item, callback: Function)} onRemove
  236. * Fired when an item is about to be deleted.
  237. * If not implemented, the item will be always removed.
  238. */
  239. ItemSet.prototype.setOptions = function(options) {
  240. if (options) {
  241. // copy all options that we know
  242. var fields = ['type', 'align', 'order', 'padding', 'stack', 'selectable', 'groupOrder', 'dataAttributes', 'template','hide', 'snap'];
  243. util.selectiveExtend(fields, this.options, options);
  244. if ('orientation' in options) {
  245. if (typeof options.orientation === 'string') {
  246. this.options.orientation = options.orientation;
  247. }
  248. else if (typeof options.orientation === 'object' && 'item' in options.orientation) {
  249. this.options.orientation = options.orientation.item;
  250. }
  251. }
  252. if ('margin' in options) {
  253. if (typeof options.margin === 'number') {
  254. this.options.margin.axis = options.margin;
  255. this.options.margin.item.horizontal = options.margin;
  256. this.options.margin.item.vertical = options.margin;
  257. }
  258. else if (typeof options.margin === 'object') {
  259. util.selectiveExtend(['axis'], this.options.margin, options.margin);
  260. if ('item' in options.margin) {
  261. if (typeof options.margin.item === 'number') {
  262. this.options.margin.item.horizontal = options.margin.item;
  263. this.options.margin.item.vertical = options.margin.item;
  264. }
  265. else if (typeof options.margin.item === 'object') {
  266. util.selectiveExtend(['horizontal', 'vertical'], this.options.margin.item, options.margin.item);
  267. }
  268. }
  269. }
  270. }
  271. if ('editable' in options) {
  272. if (typeof options.editable === 'boolean') {
  273. this.options.editable.updateTime = options.editable;
  274. this.options.editable.updateGroup = options.editable;
  275. this.options.editable.add = options.editable;
  276. this.options.editable.remove = options.editable;
  277. }
  278. else if (typeof options.editable === 'object') {
  279. util.selectiveExtend(['updateTime', 'updateGroup', 'add', 'remove'], this.options.editable, options.editable);
  280. }
  281. }
  282. // callback functions
  283. var addCallback = (function (name) {
  284. var fn = options[name];
  285. if (fn) {
  286. if (!(fn instanceof Function)) {
  287. throw new Error('option ' + name + ' must be a function ' + name + '(item, callback)');
  288. }
  289. this.options[name] = fn;
  290. }
  291. }).bind(this);
  292. ['onAdd', 'onUpdate', 'onRemove', 'onMove', 'onMoving'].forEach(addCallback);
  293. // force the itemSet to refresh: options like orientation and margins may be changed
  294. this.markDirty();
  295. }
  296. };
  297. /**
  298. * Mark the ItemSet dirty so it will refresh everything with next redraw.
  299. * Optionally, all items can be marked as dirty and be refreshed.
  300. * @param {{refreshItems: boolean}} [options]
  301. */
  302. ItemSet.prototype.markDirty = function(options) {
  303. this.groupIds = [];
  304. this.stackDirty = true;
  305. if (options && options.refreshItems) {
  306. util.forEach(this.items, function (item) {
  307. item.dirty = true;
  308. if (item.displayed) item.redraw();
  309. });
  310. }
  311. };
  312. /**
  313. * Destroy the ItemSet
  314. */
  315. ItemSet.prototype.destroy = function() {
  316. this.hide();
  317. this.setItems(null);
  318. this.setGroups(null);
  319. this.hammer = null;
  320. this.body = null;
  321. this.conversion = null;
  322. };
  323. /**
  324. * Hide the component from the DOM
  325. */
  326. ItemSet.prototype.hide = function() {
  327. // remove the frame containing the items
  328. if (this.dom.frame.parentNode) {
  329. this.dom.frame.parentNode.removeChild(this.dom.frame);
  330. }
  331. // remove the axis with dots
  332. if (this.dom.axis.parentNode) {
  333. this.dom.axis.parentNode.removeChild(this.dom.axis);
  334. }
  335. // remove the labelset containing all group labels
  336. if (this.dom.labelSet.parentNode) {
  337. this.dom.labelSet.parentNode.removeChild(this.dom.labelSet);
  338. }
  339. };
  340. /**
  341. * Show the component in the DOM (when not already visible).
  342. * @return {Boolean} changed
  343. */
  344. ItemSet.prototype.show = function() {
  345. // show frame containing the items
  346. if (!this.dom.frame.parentNode) {
  347. this.body.dom.center.appendChild(this.dom.frame);
  348. }
  349. // show axis with dots
  350. if (!this.dom.axis.parentNode) {
  351. this.body.dom.backgroundVertical.appendChild(this.dom.axis);
  352. }
  353. // show labelset containing labels
  354. if (!this.dom.labelSet.parentNode) {
  355. this.body.dom.left.appendChild(this.dom.labelSet);
  356. }
  357. };
  358. /**
  359. * Set selected items by their id. Replaces the current selection
  360. * Unknown id's are silently ignored.
  361. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  362. * selected, or a single item id. If ids is undefined
  363. * or an empty array, all items will be unselected.
  364. */
  365. ItemSet.prototype.setSelection = function(ids) {
  366. var i, ii, id, item;
  367. if (ids == undefined) ids = [];
  368. if (!Array.isArray(ids)) ids = [ids];
  369. // unselect currently selected items
  370. for (i = 0, ii = this.selection.length; i < ii; i++) {
  371. id = this.selection[i];
  372. item = this.items[id];
  373. if (item) item.unselect();
  374. }
  375. // select items
  376. this.selection = [];
  377. for (i = 0, ii = ids.length; i < ii; i++) {
  378. id = ids[i];
  379. item = this.items[id];
  380. if (item) {
  381. this.selection.push(id);
  382. item.select();
  383. }
  384. }
  385. };
  386. /**
  387. * Get the selected items by their id
  388. * @return {Array} ids The ids of the selected items
  389. */
  390. ItemSet.prototype.getSelection = function() {
  391. return this.selection.concat([]);
  392. };
  393. /**
  394. * Get the id's of the currently visible items.
  395. * @returns {Array} The ids of the visible items
  396. */
  397. ItemSet.prototype.getVisibleItems = function() {
  398. var range = this.body.range.getRange();
  399. var left = this.body.util.toScreen(range.start);
  400. var right = this.body.util.toScreen(range.end);
  401. var ids = [];
  402. for (var groupId in this.groups) {
  403. if (this.groups.hasOwnProperty(groupId)) {
  404. var group = this.groups[groupId];
  405. var rawVisibleItems = group.visibleItems;
  406. // filter the "raw" set with visibleItems into a set which is really
  407. // visible by pixels
  408. for (var i = 0; i < rawVisibleItems.length; i++) {
  409. var item = rawVisibleItems[i];
  410. // TODO: also check whether visible vertically
  411. if ((item.left < right) && (item.left + item.width > left)) {
  412. ids.push(item.id);
  413. }
  414. }
  415. }
  416. }
  417. return ids;
  418. };
  419. /**
  420. * Deselect a selected item
  421. * @param {String | Number} id
  422. * @private
  423. */
  424. ItemSet.prototype._deselect = function(id) {
  425. var selection = this.selection;
  426. for (var i = 0, ii = selection.length; i < ii; i++) {
  427. if (selection[i] == id) { // non-strict comparison!
  428. selection.splice(i, 1);
  429. break;
  430. }
  431. }
  432. };
  433. /**
  434. * Repaint the component
  435. * @return {boolean} Returns true if the component is resized
  436. */
  437. ItemSet.prototype.redraw = function() {
  438. var margin = this.options.margin,
  439. range = this.body.range,
  440. asSize = util.option.asSize,
  441. options = this.options,
  442. orientation = options.orientation,
  443. resized = false,
  444. frame = this.dom.frame,
  445. editable = options.editable.updateTime || options.editable.updateGroup;
  446. // recalculate absolute position (before redrawing groups)
  447. this.props.top = this.body.domProps.top.height + this.body.domProps.border.top;
  448. this.props.left = this.body.domProps.left.width + this.body.domProps.border.left;
  449. // update class name
  450. frame.className = 'itemset' + (editable ? ' editable' : '');
  451. // reorder the groups (if needed)
  452. resized = this._orderGroups() || resized;
  453. // check whether zoomed (in that case we need to re-stack everything)
  454. // TODO: would be nicer to get this as a trigger from Range
  455. var visibleInterval = range.end - range.start;
  456. var zoomed = (visibleInterval != this.lastVisibleInterval) || (this.props.width != this.props.lastWidth);
  457. if (zoomed) this.stackDirty = true;
  458. this.lastVisibleInterval = visibleInterval;
  459. this.props.lastWidth = this.props.width;
  460. var restack = this.stackDirty;
  461. var firstGroup = this._firstGroup();
  462. var firstMargin = {
  463. item: margin.item,
  464. axis: margin.axis
  465. };
  466. var nonFirstMargin = {
  467. item: margin.item,
  468. axis: margin.item.vertical / 2
  469. };
  470. var height = 0;
  471. var minHeight = margin.axis + margin.item.vertical;
  472. // redraw the background group
  473. this.groups[BACKGROUND].redraw(range, nonFirstMargin, restack);
  474. // redraw all regular groups
  475. util.forEach(this.groups, function (group) {
  476. var groupMargin = (group == firstGroup) ? firstMargin : nonFirstMargin;
  477. var groupResized = group.redraw(range, groupMargin, restack);
  478. resized = groupResized || resized;
  479. height += group.height;
  480. });
  481. height = Math.max(height, minHeight);
  482. this.stackDirty = false;
  483. // update frame height
  484. frame.style.height = asSize(height);
  485. // calculate actual size
  486. this.props.width = frame.offsetWidth;
  487. this.props.height = height;
  488. // reposition axis
  489. this.dom.axis.style.top = asSize((orientation == 'top') ?
  490. (this.body.domProps.top.height + this.body.domProps.border.top) :
  491. (this.body.domProps.top.height + this.body.domProps.centerContainer.height));
  492. this.dom.axis.style.left = '0';
  493. // check if this component is resized
  494. resized = this._isResized() || resized;
  495. return resized;
  496. };
  497. /**
  498. * Get the first group, aligned with the axis
  499. * @return {Group | null} firstGroup
  500. * @private
  501. */
  502. ItemSet.prototype._firstGroup = function() {
  503. var firstGroupIndex = (this.options.orientation == 'top') ? 0 : (this.groupIds.length - 1);
  504. var firstGroupId = this.groupIds[firstGroupIndex];
  505. var firstGroup = this.groups[firstGroupId] || this.groups[UNGROUPED];
  506. return firstGroup || null;
  507. };
  508. /**
  509. * Create or delete the group holding all ungrouped items. This group is used when
  510. * there are no groups specified.
  511. * @protected
  512. */
  513. ItemSet.prototype._updateUngrouped = function() {
  514. var ungrouped = this.groups[UNGROUPED];
  515. var background = this.groups[BACKGROUND];
  516. var item, itemId;
  517. if (this.groupsData) {
  518. // remove the group holding all ungrouped items
  519. if (ungrouped) {
  520. ungrouped.hide();
  521. delete this.groups[UNGROUPED];
  522. for (itemId in this.items) {
  523. if (this.items.hasOwnProperty(itemId)) {
  524. item = this.items[itemId];
  525. item.parent && item.parent.remove(item);
  526. var groupId = this._getGroupId(item.data);
  527. var group = this.groups[groupId];
  528. group && group.add(item) || item.hide();
  529. }
  530. }
  531. }
  532. }
  533. else {
  534. // create a group holding all (unfiltered) items
  535. if (!ungrouped) {
  536. var id = null;
  537. var data = null;
  538. ungrouped = new Group(id, data, this);
  539. this.groups[UNGROUPED] = ungrouped;
  540. for (itemId in this.items) {
  541. if (this.items.hasOwnProperty(itemId)) {
  542. item = this.items[itemId];
  543. ungrouped.add(item);
  544. }
  545. }
  546. ungrouped.show();
  547. }
  548. }
  549. };
  550. /**
  551. * Get the element for the labelset
  552. * @return {HTMLElement} labelSet
  553. */
  554. ItemSet.prototype.getLabelSet = function() {
  555. return this.dom.labelSet;
  556. };
  557. /**
  558. * Set items
  559. * @param {vis.DataSet | null} items
  560. */
  561. ItemSet.prototype.setItems = function(items) {
  562. var me = this,
  563. ids,
  564. oldItemsData = this.itemsData;
  565. // replace the dataset
  566. if (!items) {
  567. this.itemsData = null;
  568. }
  569. else if (items instanceof DataSet || items instanceof DataView) {
  570. this.itemsData = items;
  571. }
  572. else {
  573. throw new TypeError('Data must be an instance of DataSet or DataView');
  574. }
  575. if (oldItemsData) {
  576. // unsubscribe from old dataset
  577. util.forEach(this.itemListeners, function (callback, event) {
  578. oldItemsData.off(event, callback);
  579. });
  580. // remove all drawn items
  581. ids = oldItemsData.getIds();
  582. this._onRemove(ids);
  583. }
  584. if (this.itemsData) {
  585. // subscribe to new dataset
  586. var id = this.id;
  587. util.forEach(this.itemListeners, function (callback, event) {
  588. me.itemsData.on(event, callback, id);
  589. });
  590. // add all new items
  591. ids = this.itemsData.getIds();
  592. this._onAdd(ids);
  593. // update the group holding all ungrouped items
  594. this._updateUngrouped();
  595. }
  596. };
  597. /**
  598. * Get the current items
  599. * @returns {vis.DataSet | null}
  600. */
  601. ItemSet.prototype.getItems = function() {
  602. return this.itemsData;
  603. };
  604. /**
  605. * Set groups
  606. * @param {vis.DataSet} groups
  607. */
  608. ItemSet.prototype.setGroups = function(groups) {
  609. var me = this,
  610. ids;
  611. // unsubscribe from current dataset
  612. if (this.groupsData) {
  613. util.forEach(this.groupListeners, function (callback, event) {
  614. me.groupsData.unsubscribe(event, callback);
  615. });
  616. // remove all drawn groups
  617. ids = this.groupsData.getIds();
  618. this.groupsData = null;
  619. this._onRemoveGroups(ids); // note: this will cause a redraw
  620. }
  621. // replace the dataset
  622. if (!groups) {
  623. this.groupsData = null;
  624. }
  625. else if (groups instanceof DataSet || groups instanceof DataView) {
  626. this.groupsData = groups;
  627. }
  628. else {
  629. throw new TypeError('Data must be an instance of DataSet or DataView');
  630. }
  631. if (this.groupsData) {
  632. // subscribe to new dataset
  633. var id = this.id;
  634. util.forEach(this.groupListeners, function (callback, event) {
  635. me.groupsData.on(event, callback, id);
  636. });
  637. // draw all ms
  638. ids = this.groupsData.getIds();
  639. this._onAddGroups(ids);
  640. }
  641. // update the group holding all ungrouped items
  642. this._updateUngrouped();
  643. // update the order of all items in each group
  644. this._order();
  645. this.body.emitter.emit('change', {queue: true});
  646. };
  647. /**
  648. * Get the current groups
  649. * @returns {vis.DataSet | null} groups
  650. */
  651. ItemSet.prototype.getGroups = function() {
  652. return this.groupsData;
  653. };
  654. /**
  655. * Remove an item by its id
  656. * @param {String | Number} id
  657. */
  658. ItemSet.prototype.removeItem = function(id) {
  659. var item = this.itemsData.get(id),
  660. dataset = this.itemsData.getDataSet();
  661. if (item) {
  662. // confirm deletion
  663. this.options.onRemove(item, function (item) {
  664. if (item) {
  665. // remove by id here, it is possible that an item has no id defined
  666. // itself, so better not delete by the item itself
  667. dataset.remove(id);
  668. }
  669. });
  670. }
  671. };
  672. /**
  673. * Get the time of an item based on it's data and options.type
  674. * @param {Object} itemData
  675. * @returns {string} Returns the type
  676. * @private
  677. */
  678. ItemSet.prototype._getType = function (itemData) {
  679. return itemData.type || this.options.type || (itemData.end ? 'range' : 'box');
  680. };
  681. /**
  682. * Get the group id for an item
  683. * @param {Object} itemData
  684. * @returns {string} Returns the groupId
  685. * @private
  686. */
  687. ItemSet.prototype._getGroupId = function (itemData) {
  688. var type = this._getType(itemData);
  689. if (type == 'background' && itemData.group == undefined) {
  690. return BACKGROUND;
  691. }
  692. else {
  693. return this.groupsData ? itemData.group : UNGROUPED;
  694. }
  695. };
  696. /**
  697. * Handle updated items
  698. * @param {Number[]} ids
  699. * @protected
  700. */
  701. ItemSet.prototype._onUpdate = function(ids) {
  702. var me = this;
  703. ids.forEach(function (id) {
  704. var itemData = me.itemsData.get(id, me.itemOptions);
  705. var item = me.items[id];
  706. var type = me._getType(itemData);
  707. var constructor = ItemSet.types[type];
  708. if (item) {
  709. // update item
  710. if (!constructor || !(item instanceof constructor)) {
  711. // item type has changed, delete the item and recreate it
  712. me._removeItem(item);
  713. item = null;
  714. }
  715. else {
  716. me._updateItem(item, itemData);
  717. }
  718. }
  719. if (!item) {
  720. // create item
  721. if (constructor) {
  722. item = new constructor(itemData, me.conversion, me.options);
  723. item.id = id; // TODO: not so nice setting id afterwards
  724. me._addItem(item);
  725. }
  726. else if (type == 'rangeoverflow') {
  727. // TODO: deprecated since version 2.1.0 (or 3.0.0?). cleanup some day
  728. throw new TypeError('Item type "rangeoverflow" is deprecated. Use css styling instead: ' +
  729. '.vis.timeline .item.range .content {overflow: visible;}');
  730. }
  731. else {
  732. throw new TypeError('Unknown item type "' + type + '"');
  733. }
  734. }
  735. });
  736. this._order();
  737. this.stackDirty = true; // force re-stacking of all items next redraw
  738. this.body.emitter.emit('change', {queue: true});
  739. };
  740. /**
  741. * Handle added items
  742. * @param {Number[]} ids
  743. * @protected
  744. */
  745. ItemSet.prototype._onAdd = ItemSet.prototype._onUpdate;
  746. /**
  747. * Handle removed items
  748. * @param {Number[]} ids
  749. * @protected
  750. */
  751. ItemSet.prototype._onRemove = function(ids) {
  752. var count = 0;
  753. var me = this;
  754. ids.forEach(function (id) {
  755. var item = me.items[id];
  756. if (item) {
  757. count++;
  758. me._removeItem(item);
  759. }
  760. });
  761. if (count) {
  762. // update order
  763. this._order();
  764. this.stackDirty = true; // force re-stacking of all items next redraw
  765. this.body.emitter.emit('change', {queue: true});
  766. }
  767. };
  768. /**
  769. * Update the order of item in all groups
  770. * @private
  771. */
  772. ItemSet.prototype._order = function() {
  773. // reorder the items in all groups
  774. // TODO: optimization: only reorder groups affected by the changed items
  775. util.forEach(this.groups, function (group) {
  776. group.order();
  777. });
  778. };
  779. /**
  780. * Handle updated groups
  781. * @param {Number[]} ids
  782. * @private
  783. */
  784. ItemSet.prototype._onUpdateGroups = function(ids) {
  785. this._onAddGroups(ids);
  786. };
  787. /**
  788. * Handle changed groups (added or updated)
  789. * @param {Number[]} ids
  790. * @private
  791. */
  792. ItemSet.prototype._onAddGroups = function(ids) {
  793. var me = this;
  794. ids.forEach(function (id) {
  795. var groupData = me.groupsData.get(id);
  796. var group = me.groups[id];
  797. if (!group) {
  798. // check for reserved ids
  799. if (id == UNGROUPED || id == BACKGROUND) {
  800. throw new Error('Illegal group id. ' + id + ' is a reserved id.');
  801. }
  802. var groupOptions = Object.create(me.options);
  803. util.extend(groupOptions, {
  804. height: null
  805. });
  806. group = new Group(id, groupData, me);
  807. me.groups[id] = group;
  808. // add items with this groupId to the new group
  809. for (var itemId in me.items) {
  810. if (me.items.hasOwnProperty(itemId)) {
  811. var item = me.items[itemId];
  812. if (item.data.group == id) {
  813. group.add(item);
  814. }
  815. }
  816. }
  817. group.order();
  818. group.show();
  819. }
  820. else {
  821. // update group
  822. group.setData(groupData);
  823. }
  824. });
  825. this.body.emitter.emit('change', {queue: true});
  826. };
  827. /**
  828. * Handle removed groups
  829. * @param {Number[]} ids
  830. * @private
  831. */
  832. ItemSet.prototype._onRemoveGroups = function(ids) {
  833. var groups = this.groups;
  834. ids.forEach(function (id) {
  835. var group = groups[id];
  836. if (group) {
  837. group.hide();
  838. delete groups[id];
  839. }
  840. });
  841. this.markDirty();
  842. this.body.emitter.emit('change', {queue: true});
  843. };
  844. /**
  845. * Reorder the groups if needed
  846. * @return {boolean} changed
  847. * @private
  848. */
  849. ItemSet.prototype._orderGroups = function () {
  850. if (this.groupsData) {
  851. // reorder the groups
  852. var groupIds = this.groupsData.getIds({
  853. order: this.options.groupOrder
  854. });
  855. var changed = !util.equalArray(groupIds, this.groupIds);
  856. if (changed) {
  857. // hide all groups, removes them from the DOM
  858. var groups = this.groups;
  859. groupIds.forEach(function (groupId) {
  860. groups[groupId].hide();
  861. });
  862. // show the groups again, attach them to the DOM in correct order
  863. groupIds.forEach(function (groupId) {
  864. groups[groupId].show();
  865. });
  866. this.groupIds = groupIds;
  867. }
  868. return changed;
  869. }
  870. else {
  871. return false;
  872. }
  873. };
  874. /**
  875. * Add a new item
  876. * @param {Item} item
  877. * @private
  878. */
  879. ItemSet.prototype._addItem = function(item) {
  880. this.items[item.id] = item;
  881. // add to group
  882. var groupId = this._getGroupId(item.data);
  883. var group = this.groups[groupId];
  884. if (group) group.add(item);
  885. };
  886. /**
  887. * Update an existing item
  888. * @param {Item} item
  889. * @param {Object} itemData
  890. * @private
  891. */
  892. ItemSet.prototype._updateItem = function(item, itemData) {
  893. var oldGroupId = item.data.group;
  894. var oldSubGroupId = item.data.subgroup;
  895. // update the items data (will redraw the item when displayed)
  896. item.setData(itemData);
  897. // update group
  898. if (oldGroupId != item.data.group || oldSubGroupId != item.data.subgroup) {
  899. var oldGroup = this.groups[oldGroupId];
  900. if (oldGroup) oldGroup.remove(item);
  901. var groupId = this._getGroupId(item.data);
  902. var group = this.groups[groupId];
  903. if (group) group.add(item);
  904. }
  905. };
  906. /**
  907. * Delete an item from the ItemSet: remove it from the DOM, from the map
  908. * with items, and from the map with visible items, and from the selection
  909. * @param {Item} item
  910. * @private
  911. */
  912. ItemSet.prototype._removeItem = function(item) {
  913. // remove from DOM
  914. item.hide();
  915. // remove from items
  916. delete this.items[item.id];
  917. // remove from selection
  918. var index = this.selection.indexOf(item.id);
  919. if (index != -1) this.selection.splice(index, 1);
  920. // remove from group
  921. item.parent && item.parent.remove(item);
  922. };
  923. /**
  924. * Create an array containing all items being a range (having an end date)
  925. * @param array
  926. * @returns {Array}
  927. * @private
  928. */
  929. ItemSet.prototype._constructByEndArray = function(array) {
  930. var endArray = [];
  931. for (var i = 0; i < array.length; i++) {
  932. if (array[i] instanceof RangeItem) {
  933. endArray.push(array[i]);
  934. }
  935. }
  936. return endArray;
  937. };
  938. /**
  939. * Register the clicked item on touch, before dragStart is initiated.
  940. *
  941. * dragStart is initiated from a mousemove event, which can have left the item
  942. * already resulting in an item == null
  943. *
  944. * @param {Event} event
  945. * @private
  946. */
  947. ItemSet.prototype._onTouch = function (event) {
  948. // store the touched item, used in _onDragStart
  949. this.touchParams.item = this.itemFromTarget(event);
  950. };
  951. /**
  952. * Start dragging the selected events
  953. * @param {Event} event
  954. * @private
  955. */
  956. ItemSet.prototype._onDragStart = function (event) {
  957. if (!this.options.editable.updateTime && !this.options.editable.updateGroup) {
  958. return;
  959. }
  960. var item = this.touchParams.item || null;
  961. var me = this;
  962. var props;
  963. if (item && item.selected) {
  964. var dragLeftItem = event.target.dragLeftItem;
  965. var dragRightItem = event.target.dragRightItem;
  966. if (dragLeftItem) {
  967. props = {
  968. item: dragLeftItem,
  969. initialX: event.gesture.center.pageX,
  970. dragLeft: true,
  971. data: util.extend({}, item.data) // clone the items data
  972. };
  973. this.touchParams.itemProps = [props];
  974. }
  975. else if (dragRightItem) {
  976. props = {
  977. item: dragRightItem,
  978. initialX: event.gesture.center.pageX,
  979. dragRight: true,
  980. data: util.extend({}, item.data) // clone the items data
  981. };
  982. this.touchParams.itemProps = [props];
  983. }
  984. else {
  985. this.touchParams.itemProps = this.getSelection().map(function (id) {
  986. var item = me.items[id];
  987. var props = {
  988. item: item,
  989. initialX: event.gesture.center.pageX,
  990. data: util.extend({}, item.data) // clone the items data
  991. };
  992. return props;
  993. });
  994. }
  995. event.stopPropagation();
  996. }
  997. else if (this.options.editable.add && event.gesture.srcEvent.ctrlKey) {
  998. // create a new range item when dragging with ctrl key down
  999. this._onDragStartAddItem(event);
  1000. }
  1001. };
  1002. /**
  1003. * Start creating a new range item by dragging.
  1004. * @param {Event} event
  1005. * @private
  1006. */
  1007. ItemSet.prototype._onDragStartAddItem = function (event) {
  1008. var snap = this.options.snap || null;
  1009. var xAbs = util.getAbsoluteLeft(this.dom.frame);
  1010. var x = event.gesture.center.pageX - xAbs - 10; // minus 10 to compensate for the drag starting as soon as you've moved 10px
  1011. var time = this.body.util.toTime(x);
  1012. var scale = this.body.util.getScale();
  1013. var step = this.body.util.getStep();
  1014. var start = snap ? snap(time, scale, step) : start;
  1015. var end = start;
  1016. var itemData = {
  1017. type: 'range',
  1018. start: start,
  1019. end: end,
  1020. content: 'new item'
  1021. };
  1022. var id = util.randomUUID();
  1023. itemData[this.itemsData._fieldId] = id;
  1024. var group = this.groupFromTarget(event);
  1025. if (group) {
  1026. itemData.group = group.groupId;
  1027. }
  1028. var newItem = new RangeItem(itemData, this.conversion, this.options);
  1029. newItem.id = id; // TODO: not so nice setting id afterwards
  1030. newItem.data = itemData;
  1031. this._addItem(newItem);
  1032. var props = {
  1033. item: newItem,
  1034. dragRight: true,
  1035. initialX: event.gesture.center.pageX,
  1036. data: util.extend({}, itemData)
  1037. };
  1038. this.touchParams.itemProps = [props];
  1039. event.stopPropagation();
  1040. };
  1041. /**
  1042. * Drag selected items
  1043. * @param {Event} event
  1044. * @private
  1045. */
  1046. ItemSet.prototype._onDrag = function (event) {
  1047. event.preventDefault();
  1048. if (this.touchParams.itemProps) {
  1049. event.stopPropagation();
  1050. var me = this;
  1051. var snap = this.options.snap || null;
  1052. var xOffset = this.body.dom.root.offsetLeft + this.body.domProps.left.width;
  1053. var scale = this.body.util.getScale();
  1054. var step = this.body.util.getStep();
  1055. // move
  1056. this.touchParams.itemProps.forEach(function (props) {
  1057. var current = me.body.util.toTime(event.gesture.center.pageX - xOffset);
  1058. var initial = me.body.util.toTime(props.initialX - xOffset);
  1059. var offset = current - initial;
  1060. var itemData = util.extend({}, props.item.data); // clone the data
  1061. if (me.options.editable.updateTime) {
  1062. if (props.dragLeft) {
  1063. // drag left side of a range item
  1064. if (itemData.start != undefined) {
  1065. var initialStart = util.convert(props.data.start, 'Date');
  1066. var start = new Date(initialStart.valueOf() + offset);
  1067. itemData.start = snap ? snap(start, scale, step) : start;
  1068. }
  1069. }
  1070. else if (props.dragRight) {
  1071. // drag right side of a range item
  1072. if (itemData.end != undefined) {
  1073. var initialEnd = util.convert(props.data.end, 'Date');
  1074. var end = new Date(initialEnd.valueOf() + offset);
  1075. itemData.end = snap ? snap(end, scale, step) : end;
  1076. }
  1077. }
  1078. else {
  1079. // drag both start and end
  1080. if (itemData.start != undefined) {
  1081. var initialStart = util.convert(props.data.start, 'Date').valueOf();
  1082. var start = new Date(initialStart + offset);
  1083. if (itemData.end != undefined) {
  1084. var initialEnd = util.convert(props.data.end, 'Date');
  1085. var duration = initialEnd.valueOf() - initialStart.valueOf();
  1086. itemData.start = snap ? snap(start, scale, step) : start;
  1087. itemData.end = new Date(itemData.start.valueOf() + duration);
  1088. }
  1089. else {
  1090. itemData.start = snap ? snap(start, scale, step) : start;
  1091. }
  1092. }
  1093. }
  1094. }
  1095. if (me.options.editable.updateGroup && (!props.dragLeft && !props.dragRight)) {
  1096. if (itemData.group != undefined) {
  1097. // drag from one group to another
  1098. var group = me.groupFromTarget(event);
  1099. if (group) {
  1100. itemData.group = group.groupId;
  1101. }
  1102. }
  1103. }
  1104. // confirm moving the item
  1105. me.options.onMoving(itemData, function (itemData) {
  1106. if (itemData) {
  1107. props.item.setData(itemData);
  1108. }
  1109. });
  1110. });
  1111. this.stackDirty = true; // force re-stacking of all items next redraw
  1112. this.body.emitter.emit('change');
  1113. }
  1114. };
  1115. /**
  1116. * Move an item to another group
  1117. * @param {Item} item
  1118. * @param {String | Number} groupId
  1119. * @private
  1120. */
  1121. ItemSet.prototype._moveToGroup = function(item, groupId) {
  1122. var group = this.groups[groupId];
  1123. if (group && group.groupId != item.data.group) {
  1124. var oldGroup = item.parent;
  1125. oldGroup.remove(item);
  1126. oldGroup.order();
  1127. group.add(item);
  1128. group.order();
  1129. item.data.group = group.groupId;
  1130. }
  1131. };
  1132. /**
  1133. * End of dragging selected items
  1134. * @param {Event} event
  1135. * @private
  1136. */
  1137. ItemSet.prototype._onDragEnd = function (event) {
  1138. event.preventDefault();
  1139. if (this.touchParams.itemProps) {
  1140. event.stopPropagation();
  1141. // prepare a change set for the changed items
  1142. var changes = [];
  1143. var me = this;
  1144. var dataset = this.itemsData.getDataSet();
  1145. var itemProps = this.touchParams.itemProps ;
  1146. this.touchParams.itemProps = null;
  1147. itemProps.forEach(function (props) {
  1148. var id = props.item.id;
  1149. var exists = me.itemsData.get(id, me.itemOptions) != null;
  1150. if (!exists) {
  1151. // add a new item
  1152. me.options.onAdd(props.item.data, function (itemData) {
  1153. me._removeItem(props.item); // remove temporary item
  1154. if (itemData) {
  1155. me.itemsData.getDataSet().add(itemData);
  1156. }
  1157. // force re-stacking of all items next redraw
  1158. me.stackDirty = true;
  1159. me.body.emitter.emit('change');
  1160. });
  1161. }
  1162. else {
  1163. // update existing item
  1164. var itemData = util.extend({}, props.item.data); // clone the data
  1165. me.options.onMove(itemData, function (itemData) {
  1166. if (itemData) {
  1167. // apply changes
  1168. itemData[dataset._fieldId] = id; // ensure the item contains its id (can be undefined)
  1169. changes.push(itemData);
  1170. }
  1171. else {
  1172. // restore original values
  1173. props.item.setData(props.data);
  1174. me.stackDirty = true; // force re-stacking of all items next redraw
  1175. me.body.emitter.emit('change');
  1176. }
  1177. });
  1178. }
  1179. });
  1180. // apply the changes to the data (if there are changes)
  1181. if (changes.length) {
  1182. dataset.update(changes);
  1183. }
  1184. }
  1185. };
  1186. /**
  1187. * Handle selecting/deselecting an item when tapping it
  1188. * @param {Event} event
  1189. * @private
  1190. */
  1191. ItemSet.prototype._onSelectItem = function (event) {
  1192. if (!this.options.selectable) return;
  1193. var ctrlKey = event.gesture.srcEvent && event.gesture.srcEvent.ctrlKey;
  1194. var shiftKey = event.gesture.srcEvent && event.gesture.srcEvent.shiftKey;
  1195. if (ctrlKey || shiftKey) {
  1196. this._onMultiSelectItem(event);
  1197. return;
  1198. }
  1199. var oldSelection = this.getSelection();
  1200. var item = this.itemFromTarget(event);
  1201. var selection = item ? [item.id] : [];
  1202. this.setSelection(selection);
  1203. var newSelection = this.getSelection();
  1204. // emit a select event,
  1205. // except when old selection is empty and new selection is still empty
  1206. if (newSelection.length > 0 || oldSelection.length > 0) {
  1207. this.body.emitter.emit('select', {
  1208. items: newSelection
  1209. });
  1210. }
  1211. };
  1212. /**
  1213. * Handle creation and updates of an item on double tap
  1214. * @param event
  1215. * @private
  1216. */
  1217. ItemSet.prototype._onAddItem = function (event) {
  1218. if (!this.options.selectable) return;
  1219. if (!this.options.editable.add) return;
  1220. var me = this,
  1221. snap = this.options.snap || null,
  1222. item = this.itemFromTarget(event);
  1223. if (item) {
  1224. // update item
  1225. // execute async handler to update the item (or cancel it)
  1226. var itemData = me.itemsData.get(item.id); // get a clone of the data from the dataset
  1227. this.options.onUpdate(itemData, function (itemData) {
  1228. if (itemData) {
  1229. me.itemsData.getDataSet().update(itemData);
  1230. }
  1231. });
  1232. }
  1233. else {
  1234. // add item
  1235. var xAbs = util.getAbsoluteLeft(this.dom.frame);
  1236. var x = event.gesture.center.pageX - xAbs;
  1237. var start = this.body.util.toTime(x);
  1238. var scale = this.body.util.getScale();
  1239. var step = this.body.util.getStep();
  1240. var newItem = {
  1241. start: snap ? snap(start, scale, step) : start,
  1242. content: 'new item'
  1243. };
  1244. // when default type is a range, add a default end date to the new item
  1245. if (this.options.type === 'range') {
  1246. var end = this.body.util.toTime(x + this.props.width / 5);
  1247. newItem.end = snap ? snap(end, scale, step) : end;
  1248. }
  1249. newItem[this.itemsData._fieldId] = util.randomUUID();
  1250. var group = this.groupFromTarget(event);
  1251. if (group) {
  1252. newItem.group = group.groupId;
  1253. }
  1254. // execute async handler to customize (or cancel) adding an item
  1255. this.options.onAdd(newItem, function (item) {
  1256. if (item) {
  1257. me.itemsData.getDataSet().add(item);
  1258. // TODO: need to trigger a redraw?
  1259. }
  1260. });
  1261. }
  1262. };
  1263. /**
  1264. * Handle selecting/deselecting multiple items when holding an item
  1265. * @param {Event} event
  1266. * @private
  1267. */
  1268. ItemSet.prototype._onMultiSelectItem = function (event) {
  1269. if (!this.options.selectable) return;
  1270. var selection,
  1271. item = this.itemFromTarget(event);
  1272. if (item) {
  1273. // multi select items
  1274. selection = this.getSelection(); // current selection
  1275. var shiftKey = event.gesture.touches[0] && event.gesture.touches[0].shiftKey || false;
  1276. if (shiftKey) {
  1277. // select all items between the old selection and the tapped item
  1278. // determine the selection range
  1279. selection.push(item.id);
  1280. var range = ItemSet._getItemRange(this.itemsData.get(selection, this.itemOptions));
  1281. // select all items within the selection range
  1282. selection = [];
  1283. for (var id in this.items) {
  1284. if (this.items.hasOwnProperty(id)) {
  1285. var _item = this.items[id];
  1286. var start = _item.data.start;
  1287. var end = (_item.data.end !== undefined) ? _item.data.end : start;
  1288. if (start >= range.min &&
  1289. end <= range.max &&
  1290. !(_item instanceof BackgroundItem)) {
  1291. selection.push(_item.id); // do not use id but item.id, id itself is stringified
  1292. }
  1293. }
  1294. }
  1295. }
  1296. else {
  1297. // add/remove this item from the current selection
  1298. var index = selection.indexOf(item.id);
  1299. if (index == -1) {
  1300. // item is not yet selected -> select it
  1301. selection.push(item.id);
  1302. }
  1303. else {
  1304. // item is already selected -> deselect it
  1305. selection.splice(index, 1);
  1306. }
  1307. }
  1308. this.setSelection(selection);
  1309. this.body.emitter.emit('select', {
  1310. items: this.getSelection()
  1311. });
  1312. }
  1313. };
  1314. /**
  1315. * Calculate the time range of a list of items
  1316. * @param {Array.<Object>} itemsData
  1317. * @return {{min: Date, max: Date}} Returns the range of the provided items
  1318. * @private
  1319. */
  1320. ItemSet._getItemRange = function(itemsData) {
  1321. var max = null;
  1322. var min = null;
  1323. itemsData.forEach(function (data) {
  1324. if (min == null || data.start < min) {
  1325. min = data.start;
  1326. }
  1327. if (data.end != undefined) {
  1328. if (max == null || data.end > max) {
  1329. max = data.end;
  1330. }
  1331. }
  1332. else {
  1333. if (max == null || data.start > max) {
  1334. max = data.start;
  1335. }
  1336. }
  1337. });
  1338. return {
  1339. min: min,
  1340. max: max
  1341. }
  1342. };
  1343. /**
  1344. * Find an item from an event target:
  1345. * searches for the attribute 'timeline-item' in the event target's element tree
  1346. * @param {Event} event
  1347. * @return {Item | null} item
  1348. */
  1349. ItemSet.prototype.itemFromTarget = function(event) {
  1350. var target = event.target;
  1351. while (target) {
  1352. if (target.hasOwnProperty('timeline-item')) {
  1353. return target['timeline-item'];
  1354. }
  1355. target = target.parentNode;
  1356. }
  1357. return null;
  1358. };
  1359. /**
  1360. * Find the Group from an event target:
  1361. * searches for the attribute 'timeline-group' in the event target's element tree
  1362. * @param {Event} event
  1363. * @return {Group | null} group
  1364. */
  1365. ItemSet.prototype.groupFromTarget = function(event) {
  1366. var pageY = event.gesture ? event.gesture.center.pageY : event.pageY;
  1367. for (var i = 0; i < this.groupIds.length; i++) {
  1368. var groupId = this.groupIds[i];
  1369. var group = this.groups[groupId];
  1370. var foreground = group.dom.foreground;
  1371. var top = util.getAbsoluteTop(foreground);
  1372. if (pageY > top && pageY < top + foreground.offsetHeight) {
  1373. return group;
  1374. }
  1375. if (this.options.orientation === 'top') {
  1376. if (i === this.groupIds.length - 1 && pageY > top) {
  1377. return group;
  1378. }
  1379. }
  1380. else {
  1381. if (i === 0 && pageY < top + foreground.offset) {
  1382. return group;
  1383. }
  1384. }
  1385. }
  1386. return null;
  1387. };
  1388. /**
  1389. * Find the ItemSet from an event target:
  1390. * searches for the attribute 'timeline-itemset' in the event target's element tree
  1391. * @param {Event} event
  1392. * @return {ItemSet | null} item
  1393. */
  1394. ItemSet.itemSetFromTarget = function(event) {
  1395. var target = event.target;
  1396. while (target) {
  1397. if (target.hasOwnProperty('timeline-itemset')) {
  1398. return target['timeline-itemset'];
  1399. }
  1400. target = target.parentNode;
  1401. }
  1402. return null;
  1403. };
  1404. module.exports = ItemSet;