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.

1653 lines
46 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.clientX
  969. };
  970. if (me.options.editable.updateTime) {
  971. props.start = item.data.start.valueOf();
  972. }
  973. if (me.options.editable.updateGroup) {
  974. if ('group' in item.data) props.group = item.data.group;
  975. }
  976. this.touchParams.itemProps = [props];
  977. }
  978. else if (dragRightItem) {
  979. props = {
  980. item: dragRightItem,
  981. initialX: event.gesture.center.clientX
  982. };
  983. if (me.options.editable.updateTime) {
  984. props.end = item.data.end.valueOf();
  985. }
  986. if (me.options.editable.updateGroup) {
  987. if ('group' in item.data) props.group = item.data.group;
  988. }
  989. this.touchParams.itemProps = [props];
  990. }
  991. else {
  992. this.touchParams.itemProps = this.getSelection().map(function (id) {
  993. var item = me.items[id];
  994. var props = {
  995. item: item,
  996. initialX: event.gesture.center.clientX
  997. };
  998. if (me.options.editable.updateTime) {
  999. if ('start' in item.data) {
  1000. props.start = item.data.start.valueOf();
  1001. if ('end' in item.data) {
  1002. // we store a duration here in order not to change the width
  1003. // of the item when moving it.
  1004. props.duration = item.data.end.valueOf() - props.start;
  1005. }
  1006. }
  1007. }
  1008. if (me.options.editable.updateGroup) {
  1009. if ('group' in item.data) props.group = item.data.group;
  1010. }
  1011. return props;
  1012. });
  1013. }
  1014. event.stopPropagation();
  1015. }
  1016. else if (this.options.editable.add && event.gesture.srcEvent.ctrlKey) {
  1017. // create a new range item when dragging with ctrl key down
  1018. this._onDragStartAddItem(event);
  1019. }
  1020. };
  1021. /**
  1022. * Start creating a new range item by dragging.
  1023. * @param {Event} event
  1024. * @private
  1025. */
  1026. ItemSet.prototype._onDragStartAddItem = function (event) {
  1027. var snap = this.options.snap || null;
  1028. var xAbs = util.getAbsoluteLeft(this.dom.frame);
  1029. var x = event.gesture.center.pageX - xAbs - 10; // minus 10 to compensate for the drag starting as soon as you've moved 10px
  1030. var time = this.body.util.toTime(x);
  1031. var scale = this.body.util.getScale();
  1032. var step = this.body.util.getStep();
  1033. var start = snap ? snap(time, scale, step) : start;
  1034. var end = start;
  1035. var itemData = {
  1036. type: 'range',
  1037. start: start,
  1038. end: end,
  1039. content: 'new item'
  1040. };
  1041. var id = util.randomUUID();
  1042. itemData[this.itemsData._fieldId] = id;
  1043. var group = this.groupFromTarget(event);
  1044. if (group) {
  1045. itemData.group = group.groupId;
  1046. }
  1047. var newItem = new RangeItem(itemData, this.conversion, this.options);
  1048. newItem.id = id; // TODO: not so nice setting id afterwards
  1049. this._addItem(newItem);
  1050. var props = {
  1051. item: newItem,
  1052. end: end.valueOf(),
  1053. initialX: event.gesture.center.clientX
  1054. };
  1055. this.touchParams.itemProps = [props];
  1056. event.stopPropagation();
  1057. };
  1058. /**
  1059. * Drag selected items
  1060. * @param {Event} event
  1061. * @private
  1062. */
  1063. ItemSet.prototype._onDrag = function (event) {
  1064. event.preventDefault();
  1065. if (this.touchParams.itemProps) {
  1066. event.stopPropagation();
  1067. var me = this;
  1068. var snap = this.options.snap || null;
  1069. var xOffset = this.body.dom.root.offsetLeft + this.body.domProps.left.width;
  1070. var scale = this.body.util.getScale();
  1071. var step = this.body.util.getStep();
  1072. // move
  1073. this.touchParams.itemProps.forEach(function (props) {
  1074. var newProps = {};
  1075. var current = me.body.util.toTime(event.gesture.center.clientX - xOffset);
  1076. var initial = me.body.util.toTime(props.initialX - xOffset);
  1077. var offset = current - initial;
  1078. if ('start' in props) {
  1079. var start = new Date(props.start + offset);
  1080. newProps.start = snap ? snap(start, scale, step) : start;
  1081. }
  1082. if ('end' in props) {
  1083. var end = new Date(props.end + offset);
  1084. newProps.end = snap ? snap(end, scale, step) : end;
  1085. }
  1086. else if ('duration' in props) {
  1087. newProps.end = new Date(newProps.start.valueOf() + props.duration);
  1088. }
  1089. if ('group' in props) {
  1090. // drag from one group to another
  1091. var group = me.groupFromTarget(event);
  1092. newProps.group = group && group.groupId;
  1093. }
  1094. // confirm moving the item
  1095. var itemData = util.extend({}, props.item.data, newProps);
  1096. me.options.onMoving(itemData, function (itemData) {
  1097. if (itemData) {
  1098. me._updateItemProps(props.item, itemData);
  1099. }
  1100. });
  1101. });
  1102. this.stackDirty = true; // force re-stacking of all items next redraw
  1103. this.body.emitter.emit('change');
  1104. }
  1105. };
  1106. /**
  1107. * Update an items properties
  1108. * @param {Item} item
  1109. * @param {Object} props Can contain properties start, end, and group.
  1110. * @private
  1111. */
  1112. ItemSet.prototype._updateItemProps = function(item, props) {
  1113. // TODO: copy all properties from props to item? (also new ones)
  1114. if ('start' in props) {
  1115. item.data.start = props.start;
  1116. }
  1117. if ('end' in props) {
  1118. item.data.end = props.end;
  1119. }
  1120. else if ('duration' in props) {
  1121. item.data.end = new Date(props.start.valueOf() + props.duration);
  1122. }
  1123. if ('group' in props && item.data.group != props.group) {
  1124. this._moveToGroup(item, props.group)
  1125. }
  1126. };
  1127. /**
  1128. * Move an item to another group
  1129. * @param {Item} item
  1130. * @param {String | Number} groupId
  1131. * @private
  1132. */
  1133. ItemSet.prototype._moveToGroup = function(item, groupId) {
  1134. var group = this.groups[groupId];
  1135. if (group && group.groupId != item.data.group) {
  1136. var oldGroup = item.parent;
  1137. oldGroup.remove(item);
  1138. oldGroup.order();
  1139. group.add(item);
  1140. group.order();
  1141. item.data.group = group.groupId;
  1142. }
  1143. };
  1144. /**
  1145. * End of dragging selected items
  1146. * @param {Event} event
  1147. * @private
  1148. */
  1149. ItemSet.prototype._onDragEnd = function (event) {
  1150. event.preventDefault();
  1151. if (this.touchParams.itemProps) {
  1152. event.stopPropagation();
  1153. // prepare a change set for the changed items
  1154. var changes = [];
  1155. var me = this;
  1156. var dataset = this.itemsData.getDataSet();
  1157. var itemProps = this.touchParams.itemProps ;
  1158. this.touchParams.itemProps = null;
  1159. itemProps.forEach(function (props) {
  1160. var id = props.item.id;
  1161. var itemData = me.itemsData.get(id, me.itemOptions);
  1162. if (!itemData) {
  1163. // add a new item
  1164. me.options.onAdd(props.item.data, function (itemData) {
  1165. me._removeItem(props.item); // remove temporary item
  1166. if (itemData) {
  1167. me.itemsData.getDataSet().add(itemData);
  1168. }
  1169. // force re-stacking of all items next redraw
  1170. me.stackDirty = true;
  1171. me.body.emitter.emit('change');
  1172. });
  1173. }
  1174. else {
  1175. // update existing item
  1176. var changed = false;
  1177. if ('start' in props.item.data) {
  1178. changed = (props.start != props.item.data.start.valueOf());
  1179. itemData.start = util.convert(props.item.data.start,
  1180. dataset._options.type && dataset._options.type.start || 'Date');
  1181. }
  1182. if ('end' in props.item.data) {
  1183. changed = changed || (props.end != props.item.data.end.valueOf());
  1184. itemData.end = util.convert(props.item.data.end,
  1185. dataset._options.type && dataset._options.type.end || 'Date');
  1186. }
  1187. if ('group' in props.item.data) {
  1188. changed = changed || (props.group != props.item.data.group);
  1189. itemData.group = props.item.data.group;
  1190. }
  1191. // only apply changes when start or end is actually changed
  1192. if (changed) {
  1193. me.options.onMove(itemData, function (itemData) {
  1194. if (itemData) {
  1195. // apply changes
  1196. itemData[dataset._fieldId] = id; // ensure the item contains its id (can be undefined)
  1197. changes.push(itemData);
  1198. }
  1199. else {
  1200. // restore original values
  1201. me._updateItemProps(props.item, props);
  1202. me.stackDirty = true; // force re-stacking of all items next redraw
  1203. me.body.emitter.emit('change');
  1204. }
  1205. });
  1206. }
  1207. }
  1208. });
  1209. // apply the changes to the data (if there are changes)
  1210. if (changes.length) {
  1211. dataset.update(changes);
  1212. }
  1213. }
  1214. };
  1215. /**
  1216. * Handle selecting/deselecting an item when tapping it
  1217. * @param {Event} event
  1218. * @private
  1219. */
  1220. ItemSet.prototype._onSelectItem = function (event) {
  1221. if (!this.options.selectable) return;
  1222. var ctrlKey = event.gesture.srcEvent && event.gesture.srcEvent.ctrlKey;
  1223. var shiftKey = event.gesture.srcEvent && event.gesture.srcEvent.shiftKey;
  1224. if (ctrlKey || shiftKey) {
  1225. this._onMultiSelectItem(event);
  1226. return;
  1227. }
  1228. var oldSelection = this.getSelection();
  1229. var item = this.itemFromTarget(event);
  1230. var selection = item ? [item.id] : [];
  1231. this.setSelection(selection);
  1232. var newSelection = this.getSelection();
  1233. // emit a select event,
  1234. // except when old selection is empty and new selection is still empty
  1235. if (newSelection.length > 0 || oldSelection.length > 0) {
  1236. this.body.emitter.emit('select', {
  1237. items: newSelection
  1238. });
  1239. }
  1240. };
  1241. /**
  1242. * Handle creation and updates of an item on double tap
  1243. * @param event
  1244. * @private
  1245. */
  1246. ItemSet.prototype._onAddItem = function (event) {
  1247. if (!this.options.selectable) return;
  1248. if (!this.options.editable.add) return;
  1249. var me = this,
  1250. snap = this.options.snap || null,
  1251. item = this.itemFromTarget(event);
  1252. if (item) {
  1253. // update item
  1254. // execute async handler to update the item (or cancel it)
  1255. var itemData = me.itemsData.get(item.id); // get a clone of the data from the dataset
  1256. this.options.onUpdate(itemData, function (itemData) {
  1257. if (itemData) {
  1258. me.itemsData.getDataSet().update(itemData);
  1259. }
  1260. });
  1261. }
  1262. else {
  1263. // add item
  1264. var xAbs = util.getAbsoluteLeft(this.dom.frame);
  1265. var x = event.gesture.center.pageX - xAbs;
  1266. var start = this.body.util.toTime(x);
  1267. var scale = this.body.util.getScale();
  1268. var step = this.body.util.getStep();
  1269. var newItem = {
  1270. start: snap ? snap(start, scale, step) : start,
  1271. content: 'new item'
  1272. };
  1273. // when default type is a range, add a default end date to the new item
  1274. if (this.options.type === 'range') {
  1275. var end = this.body.util.toTime(x + this.props.width / 5);
  1276. newItem.end = snap ? snap(end, scale, step) : end;
  1277. }
  1278. newItem[this.itemsData._fieldId] = util.randomUUID();
  1279. var group = this.groupFromTarget(event);
  1280. if (group) {
  1281. newItem.group = group.groupId;
  1282. }
  1283. // execute async handler to customize (or cancel) adding an item
  1284. this.options.onAdd(newItem, function (item) {
  1285. if (item) {
  1286. me.itemsData.getDataSet().add(item);
  1287. // TODO: need to trigger a redraw?
  1288. }
  1289. });
  1290. }
  1291. };
  1292. /**
  1293. * Handle selecting/deselecting multiple items when holding an item
  1294. * @param {Event} event
  1295. * @private
  1296. */
  1297. ItemSet.prototype._onMultiSelectItem = function (event) {
  1298. if (!this.options.selectable) return;
  1299. var selection,
  1300. item = this.itemFromTarget(event);
  1301. if (item) {
  1302. // multi select items
  1303. selection = this.getSelection(); // current selection
  1304. var shiftKey = event.gesture.touches[0] && event.gesture.touches[0].shiftKey || false;
  1305. if (shiftKey) {
  1306. // select all items between the old selection and the tapped item
  1307. // determine the selection range
  1308. selection.push(item.id);
  1309. var range = ItemSet._getItemRange(this.itemsData.get(selection, this.itemOptions));
  1310. // select all items within the selection range
  1311. selection = [];
  1312. for (var id in this.items) {
  1313. if (this.items.hasOwnProperty(id)) {
  1314. var _item = this.items[id];
  1315. var start = _item.data.start;
  1316. var end = (_item.data.end !== undefined) ? _item.data.end : start;
  1317. if (start >= range.min && end <= range.max) {
  1318. selection.push(_item.id); // do not use id but item.id, id itself is stringified
  1319. }
  1320. }
  1321. }
  1322. }
  1323. else {
  1324. // add/remove this item from the current selection
  1325. var index = selection.indexOf(item.id);
  1326. if (index == -1) {
  1327. // item is not yet selected -> select it
  1328. selection.push(item.id);
  1329. }
  1330. else {
  1331. // item is already selected -> deselect it
  1332. selection.splice(index, 1);
  1333. }
  1334. }
  1335. this.setSelection(selection);
  1336. this.body.emitter.emit('select', {
  1337. items: this.getSelection()
  1338. });
  1339. }
  1340. };
  1341. /**
  1342. * Calculate the time range of a list of items
  1343. * @param {Array.<Object>} itemsData
  1344. * @return {{min: Date, max: Date}} Returns the range of the provided items
  1345. * @private
  1346. */
  1347. ItemSet._getItemRange = function(itemsData) {
  1348. var max = null;
  1349. var min = null;
  1350. itemsData.forEach(function (data) {
  1351. if (min == null || data.start < min) {
  1352. min = data.start;
  1353. }
  1354. if (data.end != undefined) {
  1355. if (max == null || data.end > max) {
  1356. max = data.end;
  1357. }
  1358. }
  1359. else {
  1360. if (max == null || data.start > max) {
  1361. max = data.start;
  1362. }
  1363. }
  1364. });
  1365. return {
  1366. min: min,
  1367. max: max
  1368. }
  1369. };
  1370. /**
  1371. * Find an item from an event target:
  1372. * searches for the attribute 'timeline-item' in the event target's element tree
  1373. * @param {Event} event
  1374. * @return {Item | null} item
  1375. */
  1376. ItemSet.prototype.itemFromTarget = function(event) {
  1377. var target = event.target;
  1378. while (target) {
  1379. if (target.hasOwnProperty('timeline-item')) {
  1380. return target['timeline-item'];
  1381. }
  1382. target = target.parentNode;
  1383. }
  1384. return null;
  1385. };
  1386. /**
  1387. * Find the Group from an event target:
  1388. * searches for the attribute 'timeline-group' in the event target's element tree
  1389. * @param {Event} event
  1390. * @return {Group | null} group
  1391. */
  1392. ItemSet.prototype.groupFromTarget = function(event) {
  1393. var clientY = event.gesture ? event.gesture.center.clientY : event.clientY;
  1394. for (var i = 0; i < this.groupIds.length; i++) {
  1395. var groupId = this.groupIds[i];
  1396. var group = this.groups[groupId];
  1397. var foreground = group.dom.foreground;
  1398. var top = util.getAbsoluteTop(foreground);
  1399. if (clientY > top && clientY < top + foreground.offsetHeight) {
  1400. return group;
  1401. }
  1402. if (this.options.orientation === 'top') {
  1403. if (i === this.groupIds.length - 1 && clientY > top) {
  1404. return group;
  1405. }
  1406. }
  1407. else {
  1408. if (i === 0 && clientY < top + foreground.offset) {
  1409. return group;
  1410. }
  1411. }
  1412. }
  1413. return null;
  1414. };
  1415. /**
  1416. * Find the ItemSet from an event target:
  1417. * searches for the attribute 'timeline-itemset' in the event target's element tree
  1418. * @param {Event} event
  1419. * @return {ItemSet | null} item
  1420. */
  1421. ItemSet.itemSetFromTarget = function(event) {
  1422. var target = event.target;
  1423. while (target) {
  1424. if (target.hasOwnProperty('timeline-itemset')) {
  1425. return target['timeline-itemset'];
  1426. }
  1427. target = target.parentNode;
  1428. }
  1429. return null;
  1430. };
  1431. module.exports = ItemSet;