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