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.

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