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.

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