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.

2257 lines
66 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
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var Hammer = require('../../module/hammer');
  2. var util = require('../../util');
  3. var DataSet = require('../../DataSet');
  4. var DataView = require('../../DataView');
  5. var TimeStep = require('../TimeStep');
  6. var Component = require('./Component');
  7. var Group = require('./Group');
  8. var BackgroundGroup = require('./BackgroundGroup');
  9. var BoxItem = require('./item/BoxItem');
  10. var PointItem = require('./item/PointItem');
  11. var RangeItem = require('./item/RangeItem');
  12. var BackgroundItem = require('./item/BackgroundItem');
  13. import Popup from '../../shared/Popup';
  14. var UNGROUPED = '__ungrouped__'; // reserved group id for ungrouped items
  15. var BACKGROUND = '__background__'; // reserved group id for background items without group
  16. /**
  17. * An ItemSet holds a set of items and ranges which can be displayed in a
  18. * range. The width is determined by the parent of the ItemSet, and the height
  19. * is determined by the size of the items.
  20. * @param {{dom: Object, domProps: Object, emitter: Emitter, range: Range}} body
  21. * @param {Object} [options] See ItemSet.setOptions for the available options.
  22. * @constructor ItemSet
  23. * @extends Component
  24. */
  25. function ItemSet(body, options) {
  26. this.body = body;
  27. this.defaultOptions = {
  28. type: null, // 'box', 'point', 'range', 'background'
  29. orientation: {
  30. item: 'bottom' // item orientation: 'top' or 'bottom'
  31. },
  32. align: 'auto', // alignment of box items
  33. stack: true,
  34. stackSubgroups: true,
  35. groupOrderSwap: function(fromGroup, toGroup, groups) {
  36. var targetOrder = toGroup.order;
  37. toGroup.order = fromGroup.order;
  38. fromGroup.order = targetOrder;
  39. },
  40. groupOrder: 'order',
  41. selectable: true,
  42. multiselect: false,
  43. itemsAlwaysDraggable: false,
  44. editable: {
  45. updateTime: false,
  46. updateGroup: false,
  47. add: false,
  48. remove: false,
  49. overrideItems: false
  50. },
  51. groupEditable: {
  52. order: false,
  53. add: false,
  54. remove: false
  55. },
  56. snap: TimeStep.snap,
  57. onAdd: function (item, callback) {
  58. callback(item);
  59. },
  60. onUpdate: function (item, callback) {
  61. callback(item);
  62. },
  63. onMove: function (item, callback) {
  64. callback(item);
  65. },
  66. onRemove: function (item, callback) {
  67. callback(item);
  68. },
  69. onMoving: function (item, callback) {
  70. callback(item);
  71. },
  72. onAddGroup: function (item, callback) {
  73. callback(item);
  74. },
  75. onMoveGroup: function (item, callback) {
  76. callback(item);
  77. },
  78. onRemoveGroup: function (item, callback) {
  79. callback(item);
  80. },
  81. margin: {
  82. item: {
  83. horizontal: 10,
  84. vertical: 10
  85. },
  86. axis: 20
  87. },
  88. tooltipOnItemUpdateTime: false
  89. };
  90. // options is shared by this ItemSet and all its items
  91. this.options = util.extend({}, this.defaultOptions);
  92. this.options.rtl = options.rtl;
  93. // options for getting items from the DataSet with the correct type
  94. this.itemOptions = {
  95. type: {start: 'Date', end: 'Date'}
  96. };
  97. this.conversion = {
  98. toScreen: body.util.toScreen,
  99. toTime: body.util.toTime
  100. };
  101. this.dom = {};
  102. this.props = {};
  103. this.hammer = null;
  104. var me = this;
  105. this.itemsData = null; // DataSet
  106. this.groupsData = null; // DataSet
  107. // listeners for the DataSet of the items
  108. this.itemListeners = {
  109. 'add': function (event, params, senderId) {
  110. me._onAdd(params.items);
  111. },
  112. 'update': function (event, params, senderId) {
  113. me._onUpdate(params.items);
  114. },
  115. 'remove': function (event, params, senderId) {
  116. me._onRemove(params.items);
  117. }
  118. };
  119. // listeners for the DataSet of the groups
  120. this.groupListeners = {
  121. 'add': function (event, params, senderId) {
  122. me._onAddGroups(params.items);
  123. },
  124. 'update': function (event, params, senderId) {
  125. me._onUpdateGroups(params.items);
  126. },
  127. 'remove': function (event, params, senderId) {
  128. me._onRemoveGroups(params.items);
  129. }
  130. };
  131. this.items = {}; // object with an Item for every data item
  132. this.groups = {}; // Group object for every group
  133. this.groupIds = [];
  134. this.selection = []; // list with the ids of all selected nodes
  135. this.stackDirty = true; // if true, all items will be restacked on next redraw
  136. this.touchParams = {}; // stores properties while dragging
  137. this.groupTouchParams = {};
  138. // create the HTML DOM
  139. this._create();
  140. this.setOptions(options);
  141. }
  142. ItemSet.prototype = new Component();
  143. // available item types will be registered here
  144. ItemSet.types = {
  145. background: BackgroundItem,
  146. box: BoxItem,
  147. range: RangeItem,
  148. point: PointItem
  149. };
  150. /**
  151. * Create the HTML DOM for the ItemSet
  152. */
  153. ItemSet.prototype._create = function(){
  154. var frame = document.createElement('div');
  155. frame.className = 'vis-itemset';
  156. frame['timeline-itemset'] = this;
  157. this.dom.frame = frame;
  158. // create background panel
  159. var background = document.createElement('div');
  160. background.className = 'vis-background';
  161. frame.appendChild(background);
  162. this.dom.background = background;
  163. // create foreground panel
  164. var foreground = document.createElement('div');
  165. foreground.className = 'vis-foreground';
  166. frame.appendChild(foreground);
  167. this.dom.foreground = foreground;
  168. // create axis panel
  169. var axis = document.createElement('div');
  170. axis.className = 'vis-axis';
  171. this.dom.axis = axis;
  172. // create labelset
  173. var labelSet = document.createElement('div');
  174. labelSet.className = 'vis-labelset';
  175. this.dom.labelSet = labelSet;
  176. // create ungrouped Group
  177. this._updateUngrouped();
  178. // create background Group
  179. var backgroundGroup = new BackgroundGroup(BACKGROUND, null, this);
  180. backgroundGroup.show();
  181. this.groups[BACKGROUND] = backgroundGroup;
  182. // attach event listeners
  183. // Note: we bind to the centerContainer for the case where the height
  184. // of the center container is larger than of the ItemSet, so we
  185. // can click in the empty area to create a new item or deselect an item.
  186. this.hammer = new Hammer(this.body.dom.centerContainer);
  187. // drag items when selected
  188. this.hammer.on('hammer.input', function (event) {
  189. if (event.isFirst) {
  190. this._onTouch(event);
  191. }
  192. }.bind(this));
  193. this.hammer.on('panstart', this._onDragStart.bind(this));
  194. this.hammer.on('panmove', this._onDrag.bind(this));
  195. this.hammer.on('panend', this._onDragEnd.bind(this));
  196. this.hammer.get('pan').set({threshold:5, direction: Hammer.DIRECTION_HORIZONTAL});
  197. // single select (or unselect) when tapping an item
  198. this.hammer.on('tap', this._onSelectItem.bind(this));
  199. // multi select when holding mouse/touch, or on ctrl+click
  200. this.hammer.on('press', this._onMultiSelectItem.bind(this));
  201. // add item on doubletap
  202. this.hammer.on('doubletap', this._onAddItem.bind(this));
  203. if (this.options.rtl) {
  204. this.groupHammer = new Hammer(this.body.dom.rightContainer);
  205. } else {
  206. this.groupHammer = new Hammer(this.body.dom.leftContainer);
  207. }
  208. this.groupHammer.on('tap', this._onGroupClick.bind(this));
  209. this.groupHammer.on('panstart', this._onGroupDragStart.bind(this));
  210. this.groupHammer.on('panmove', this._onGroupDrag.bind(this));
  211. this.groupHammer.on('panend', this._onGroupDragEnd.bind(this));
  212. this.groupHammer.get('pan').set({threshold:5, direction: Hammer.DIRECTION_VERTICAL});
  213. this.body.dom.centerContainer.addEventListener('mouseover', this._onMouseOver.bind(this));
  214. this.body.dom.centerContainer.addEventListener('mouseout', this._onMouseOut.bind(this));
  215. // attach to the DOM
  216. this.show();
  217. };
  218. /**
  219. * Set options for the ItemSet. Existing options will be extended/overwritten.
  220. * @param {Object} [options] The following options are available:
  221. * {String} type
  222. * Default type for the items. Choose from 'box'
  223. * (default), 'point', 'range', or 'background'.
  224. * The default style can be overwritten by
  225. * individual items.
  226. * {String} align
  227. * Alignment for the items, only applicable for
  228. * BoxItem. Choose 'center' (default), 'left', or
  229. * 'right'.
  230. * {String} orientation.item
  231. * Orientation of the item set. Choose 'top' or
  232. * 'bottom' (default).
  233. * {Function} groupOrder
  234. * A sorting function for ordering groups
  235. * {Boolean} stack
  236. * If true (default), items will be stacked on
  237. * top of each other.
  238. * {Number} margin.axis
  239. * Margin between the axis and the items in pixels.
  240. * Default is 20.
  241. * {Number} margin.item.horizontal
  242. * Horizontal margin between items in pixels.
  243. * Default is 10.
  244. * {Number} margin.item.vertical
  245. * Vertical Margin between items in pixels.
  246. * Default is 10.
  247. * {Number} margin.item
  248. * Margin between items in pixels in both horizontal
  249. * and vertical direction. Default is 10.
  250. * {Number} margin
  251. * Set margin for both axis and items in pixels.
  252. * {Boolean} selectable
  253. * If true (default), items can be selected.
  254. * {Boolean} multiselect
  255. * If true, multiple items can be selected.
  256. * False by default.
  257. * {Boolean} editable
  258. * Set all editable options to true or false
  259. * {Boolean} editable.updateTime
  260. * Allow dragging an item to an other moment in time
  261. * {Boolean} editable.updateGroup
  262. * Allow dragging an item to an other group
  263. * {Boolean} editable.add
  264. * Allow creating new items on double tap
  265. * {Boolean} editable.remove
  266. * Allow removing items by clicking the delete button
  267. * top right of a selected item.
  268. * {Function(item: Item, callback: Function)} onAdd
  269. * Callback function triggered when an item is about to be added:
  270. * when the user double taps an empty space in the Timeline.
  271. * {Function(item: Item, callback: Function)} onUpdate
  272. * Callback function fired when an item is about to be updated.
  273. * This function typically has to show a dialog where the user
  274. * change the item. If not implemented, nothing happens.
  275. * {Function(item: Item, callback: Function)} onMove
  276. * Fired when an item has been moved. If not implemented,
  277. * the move action will be accepted.
  278. * {Function(item: Item, callback: Function)} onRemove
  279. * Fired when an item is about to be deleted.
  280. * If not implemented, the item will be always removed.
  281. */
  282. ItemSet.prototype.setOptions = function(options) {
  283. if (options) {
  284. // copy all options that we know
  285. var fields = [
  286. 'type', 'rtl', 'align', 'order', 'stack', 'stackSubgroups', 'selectable', 'multiselect', 'itemsAlwaysDraggable',
  287. 'multiselectPerGroup', 'groupOrder', 'dataAttributes', 'template', 'groupTemplate', 'visibleFrameTemplate',
  288. 'hide', 'snap', 'groupOrderSwap', 'tooltipOnItemUpdateTime'
  289. ];
  290. util.selectiveExtend(fields, this.options, options);
  291. if ('orientation' in options) {
  292. if (typeof options.orientation === 'string') {
  293. this.options.orientation.item = options.orientation === 'top' ? 'top' : 'bottom';
  294. }
  295. else if (typeof options.orientation === 'object' && 'item' in options.orientation) {
  296. this.options.orientation.item = options.orientation.item;
  297. }
  298. }
  299. if ('margin' in options) {
  300. if (typeof options.margin === 'number') {
  301. this.options.margin.axis = options.margin;
  302. this.options.margin.item.horizontal = options.margin;
  303. this.options.margin.item.vertical = options.margin;
  304. }
  305. else if (typeof options.margin === 'object') {
  306. util.selectiveExtend(['axis'], this.options.margin, options.margin);
  307. if ('item' in options.margin) {
  308. if (typeof options.margin.item === 'number') {
  309. this.options.margin.item.horizontal = options.margin.item;
  310. this.options.margin.item.vertical = options.margin.item;
  311. }
  312. else if (typeof options.margin.item === 'object') {
  313. util.selectiveExtend(['horizontal', 'vertical'], this.options.margin.item, options.margin.item);
  314. }
  315. }
  316. }
  317. }
  318. if ('editable' in options) {
  319. if (typeof options.editable === 'boolean') {
  320. this.options.editable.updateTime = options.editable;
  321. this.options.editable.updateGroup = options.editable;
  322. this.options.editable.add = options.editable;
  323. this.options.editable.remove = options.editable;
  324. this.options.editable.overrideItems = false;
  325. }
  326. else if (typeof options.editable === 'object') {
  327. util.selectiveExtend(['updateTime', 'updateGroup', 'add', 'remove', 'overrideItems'], this.options.editable, options.editable);
  328. }
  329. }
  330. if ('groupEditable' in options) {
  331. if (typeof options.groupEditable === 'boolean') {
  332. this.options.groupEditable.order = options.groupEditable;
  333. this.options.groupEditable.add = options.groupEditable;
  334. this.options.groupEditable.remove = options.groupEditable;
  335. }
  336. else if (typeof options.groupEditable === 'object') {
  337. util.selectiveExtend(['order', 'add', 'remove'], this.options.groupEditable, options.groupEditable);
  338. }
  339. }
  340. // callback functions
  341. var addCallback = (function (name) {
  342. var fn = options[name];
  343. if (fn) {
  344. if (!(fn instanceof Function)) {
  345. throw new Error('option ' + name + ' must be a function ' + name + '(item, callback)');
  346. }
  347. this.options[name] = fn;
  348. }
  349. }).bind(this);
  350. ['onAdd', 'onUpdate', 'onRemove', 'onMove', 'onMoving', 'onAddGroup', 'onMoveGroup', 'onRemoveGroup'].forEach(addCallback);
  351. // force the itemSet to refresh: options like orientation and margins may be changed
  352. this.markDirty();
  353. }
  354. };
  355. /**
  356. * Mark the ItemSet dirty so it will refresh everything with next redraw.
  357. * Optionally, all items can be marked as dirty and be refreshed.
  358. * @param {{refreshItems: boolean}} [options]
  359. */
  360. ItemSet.prototype.markDirty = function(options) {
  361. this.groupIds = [];
  362. this.stackDirty = true;
  363. if (options && options.refreshItems) {
  364. util.forEach(this.items, function (item) {
  365. item.dirty = true;
  366. if (item.displayed) item.redraw();
  367. });
  368. }
  369. };
  370. /**
  371. * Destroy the ItemSet
  372. */
  373. ItemSet.prototype.destroy = function() {
  374. this.hide();
  375. this.setItems(null);
  376. this.setGroups(null);
  377. this.hammer = null;
  378. this.body = null;
  379. this.conversion = null;
  380. };
  381. /**
  382. * Hide the component from the DOM
  383. */
  384. ItemSet.prototype.hide = function() {
  385. // remove the frame containing the items
  386. if (this.dom.frame.parentNode) {
  387. this.dom.frame.parentNode.removeChild(this.dom.frame);
  388. }
  389. // remove the axis with dots
  390. if (this.dom.axis.parentNode) {
  391. this.dom.axis.parentNode.removeChild(this.dom.axis);
  392. }
  393. // remove the labelset containing all group labels
  394. if (this.dom.labelSet.parentNode) {
  395. this.dom.labelSet.parentNode.removeChild(this.dom.labelSet);
  396. }
  397. };
  398. /**
  399. * Show the component in the DOM (when not already visible).
  400. * @return {Boolean} changed
  401. */
  402. ItemSet.prototype.show = function() {
  403. // show frame containing the items
  404. if (!this.dom.frame.parentNode) {
  405. this.body.dom.center.appendChild(this.dom.frame);
  406. }
  407. // show axis with dots
  408. if (!this.dom.axis.parentNode) {
  409. this.body.dom.backgroundVertical.appendChild(this.dom.axis);
  410. }
  411. // show labelset containing labels
  412. if (!this.dom.labelSet.parentNode) {
  413. if (this.options.rtl) {
  414. this.body.dom.right.appendChild(this.dom.labelSet);
  415. } else {
  416. this.body.dom.left.appendChild(this.dom.labelSet);
  417. }
  418. }
  419. };
  420. /**
  421. * Set selected items by their id. Replaces the current selection
  422. * Unknown id's are silently ignored.
  423. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  424. * selected, or a single item id. If ids is undefined
  425. * or an empty array, all items will be unselected.
  426. */
  427. ItemSet.prototype.setSelection = function(ids) {
  428. var i, ii, id, item;
  429. if (ids == undefined) ids = [];
  430. if (!Array.isArray(ids)) ids = [ids];
  431. // unselect currently selected items
  432. for (i = 0, ii = this.selection.length; i < ii; i++) {
  433. id = this.selection[i];
  434. item = this.items[id];
  435. if (item) item.unselect();
  436. }
  437. // select items
  438. this.selection = [];
  439. for (i = 0, ii = ids.length; i < ii; i++) {
  440. id = ids[i];
  441. item = this.items[id];
  442. if (item) {
  443. this.selection.push(id);
  444. item.select();
  445. }
  446. }
  447. };
  448. /**
  449. * Get the selected items by their id
  450. * @return {Array} ids The ids of the selected items
  451. */
  452. ItemSet.prototype.getSelection = function() {
  453. return this.selection.concat([]);
  454. };
  455. /**
  456. * Get the id's of the currently visible items.
  457. * @returns {Array} The ids of the visible items
  458. */
  459. ItemSet.prototype.getVisibleItems = function() {
  460. var range = this.body.range.getRange();
  461. if (this.options.rtl) {
  462. var right = this.body.util.toScreen(range.start);
  463. var left = this.body.util.toScreen(range.end);
  464. } else {
  465. var left = this.body.util.toScreen(range.start);
  466. var right = this.body.util.toScreen(range.end);
  467. }
  468. var ids = [];
  469. for (var groupId in this.groups) {
  470. if (this.groups.hasOwnProperty(groupId)) {
  471. var group = this.groups[groupId];
  472. var rawVisibleItems = group.visibleItems;
  473. // filter the "raw" set with visibleItems into a set which is really
  474. // visible by pixels
  475. for (var i = 0; i < rawVisibleItems.length; i++) {
  476. var item = rawVisibleItems[i];
  477. // TODO: also check whether visible vertically
  478. if (this.options.rtl) {
  479. if ((item.right < left) && (item.right + item.width > right)) {
  480. ids.push(item.id);
  481. }
  482. } else {
  483. if ((item.left < right) && (item.left + item.width > left)) {
  484. ids.push(item.id);
  485. }
  486. }
  487. }
  488. }
  489. }
  490. return ids;
  491. };
  492. /**
  493. * Deselect a selected item
  494. * @param {String | Number} id
  495. * @private
  496. */
  497. ItemSet.prototype._deselect = function(id) {
  498. var selection = this.selection;
  499. for (var i = 0, ii = selection.length; i < ii; i++) {
  500. if (selection[i] == id) { // non-strict comparison!
  501. selection.splice(i, 1);
  502. break;
  503. }
  504. }
  505. };
  506. /**
  507. * Repaint the component
  508. * @return {boolean} Returns true if the component is resized
  509. */
  510. ItemSet.prototype.redraw = function() {
  511. var margin = this.options.margin,
  512. range = this.body.range,
  513. asSize = util.option.asSize,
  514. options = this.options,
  515. orientation = options.orientation.item,
  516. resized = false,
  517. frame = this.dom.frame;
  518. // recalculate absolute position (before redrawing groups)
  519. this.props.top = this.body.domProps.top.height + this.body.domProps.border.top;
  520. if (this.options.rtl) {
  521. this.props.right = this.body.domProps.right.width + this.body.domProps.border.right;
  522. } else {
  523. this.props.left = this.body.domProps.left.width + this.body.domProps.border.left;
  524. }
  525. // update class name
  526. frame.className = 'vis-itemset';
  527. // reorder the groups (if needed)
  528. resized = this._orderGroups() || resized;
  529. // check whether zoomed (in that case we need to re-stack everything)
  530. // TODO: would be nicer to get this as a trigger from Range
  531. var visibleInterval = range.end - range.start;
  532. var zoomed = (visibleInterval != this.lastVisibleInterval) || (this.props.width != this.props.lastWidth);
  533. if (zoomed) this.stackDirty = true;
  534. this.lastVisibleInterval = visibleInterval;
  535. this.props.lastWidth = this.props.width;
  536. var restack = this.stackDirty;
  537. var firstGroup = this._firstGroup();
  538. var firstMargin = {
  539. item: margin.item,
  540. axis: margin.axis
  541. };
  542. var nonFirstMargin = {
  543. item: margin.item,
  544. axis: margin.item.vertical / 2
  545. };
  546. var height = 0;
  547. var minHeight = margin.axis + margin.item.vertical;
  548. // redraw the background group
  549. this.groups[BACKGROUND].redraw(range, nonFirstMargin, restack);
  550. // redraw all regular groups
  551. util.forEach(this.groups, function (group) {
  552. var groupMargin = (group == firstGroup) ? firstMargin : nonFirstMargin;
  553. var groupResized = group.redraw(range, groupMargin, restack);
  554. resized = groupResized || resized;
  555. height += group.height;
  556. });
  557. height = Math.max(height, minHeight);
  558. this.stackDirty = false;
  559. // update frame height
  560. frame.style.height = asSize(height);
  561. // calculate actual size
  562. this.props.width = frame.offsetWidth;
  563. this.props.height = height;
  564. // reposition axis
  565. this.dom.axis.style.top = asSize((orientation == 'top') ?
  566. (this.body.domProps.top.height + this.body.domProps.border.top) :
  567. (this.body.domProps.top.height + this.body.domProps.centerContainer.height));
  568. if (this.options.rtl) {
  569. this.dom.axis.style.right = '0';
  570. } else {
  571. this.dom.axis.style.left = '0';
  572. }
  573. this.initialItemSetDrawn = true;
  574. // check if this component is resized
  575. resized = this._isResized() || resized;
  576. return resized;
  577. };
  578. /**
  579. * Get the first group, aligned with the axis
  580. * @return {Group | null} firstGroup
  581. * @private
  582. */
  583. ItemSet.prototype._firstGroup = function() {
  584. var firstGroupIndex = (this.options.orientation.item == 'top') ? 0 : (this.groupIds.length - 1);
  585. var firstGroupId = this.groupIds[firstGroupIndex];
  586. var firstGroup = this.groups[firstGroupId] || this.groups[UNGROUPED];
  587. return firstGroup || null;
  588. };
  589. /**
  590. * Create or delete the group holding all ungrouped items. This group is used when
  591. * there are no groups specified.
  592. * @protected
  593. */
  594. ItemSet.prototype._updateUngrouped = function() {
  595. var ungrouped = this.groups[UNGROUPED];
  596. var background = this.groups[BACKGROUND];
  597. var item, itemId;
  598. if (this.groupsData) {
  599. // remove the group holding all ungrouped items
  600. if (ungrouped) {
  601. ungrouped.hide();
  602. delete this.groups[UNGROUPED];
  603. for (itemId in this.items) {
  604. if (this.items.hasOwnProperty(itemId)) {
  605. item = this.items[itemId];
  606. item.parent && item.parent.remove(item);
  607. var groupId = this._getGroupId(item.data);
  608. var group = this.groups[groupId];
  609. group && group.add(item) || item.hide();
  610. }
  611. }
  612. }
  613. }
  614. else {
  615. // create a group holding all (unfiltered) items
  616. if (!ungrouped) {
  617. var id = null;
  618. var data = null;
  619. ungrouped = new Group(id, data, this);
  620. this.groups[UNGROUPED] = ungrouped;
  621. for (itemId in this.items) {
  622. if (this.items.hasOwnProperty(itemId)) {
  623. item = this.items[itemId];
  624. ungrouped.add(item);
  625. }
  626. }
  627. ungrouped.show();
  628. }
  629. }
  630. };
  631. /**
  632. * Get the element for the labelset
  633. * @return {HTMLElement} labelSet
  634. */
  635. ItemSet.prototype.getLabelSet = function() {
  636. return this.dom.labelSet;
  637. };
  638. /**
  639. * Set items
  640. * @param {vis.DataSet | null} items
  641. */
  642. ItemSet.prototype.setItems = function(items) {
  643. var me = this,
  644. ids,
  645. oldItemsData = this.itemsData;
  646. // replace the dataset
  647. if (!items) {
  648. this.itemsData = null;
  649. }
  650. else if (items instanceof DataSet || items instanceof DataView) {
  651. this.itemsData = items;
  652. }
  653. else {
  654. throw new TypeError('Data must be an instance of DataSet or DataView');
  655. }
  656. if (oldItemsData) {
  657. // unsubscribe from old dataset
  658. util.forEach(this.itemListeners, function (callback, event) {
  659. oldItemsData.off(event, callback);
  660. });
  661. // remove all drawn items
  662. ids = oldItemsData.getIds();
  663. this._onRemove(ids);
  664. }
  665. if (this.itemsData) {
  666. // subscribe to new dataset
  667. var id = this.id;
  668. util.forEach(this.itemListeners, function (callback, event) {
  669. me.itemsData.on(event, callback, id);
  670. });
  671. // add all new items
  672. ids = this.itemsData.getIds();
  673. this._onAdd(ids);
  674. // update the group holding all ungrouped items
  675. this._updateUngrouped();
  676. }
  677. this.body.emitter.emit('_change', {queue: true});
  678. };
  679. /**
  680. * Get the current items
  681. * @returns {vis.DataSet | null}
  682. */
  683. ItemSet.prototype.getItems = function() {
  684. return this.itemsData;
  685. };
  686. /**
  687. * Set groups
  688. * @param {vis.DataSet} groups
  689. */
  690. ItemSet.prototype.setGroups = function(groups) {
  691. var me = this,
  692. ids;
  693. // unsubscribe from current dataset
  694. if (this.groupsData) {
  695. util.forEach(this.groupListeners, function (callback, event) {
  696. me.groupsData.off(event, callback);
  697. });
  698. // remove all drawn groups
  699. ids = this.groupsData.getIds();
  700. this.groupsData = null;
  701. this._onRemoveGroups(ids); // note: this will cause a redraw
  702. }
  703. // replace the dataset
  704. if (!groups) {
  705. this.groupsData = null;
  706. }
  707. else if (groups instanceof DataSet || groups instanceof DataView) {
  708. this.groupsData = groups;
  709. }
  710. else {
  711. throw new TypeError('Data must be an instance of DataSet or DataView');
  712. }
  713. if (this.groupsData) {
  714. // go over all groups nesting
  715. var groupsData = this.groupsData;
  716. if (this.groupsData instanceof DataView) {
  717. groupsData = this.groupsData.getDataSet()
  718. }
  719. groupsData.get().forEach(function(group){
  720. if (group.nestedGroups) {
  721. group.nestedGroups.forEach(function(nestedGroupId) {
  722. var updatedNestedGroup = groupsData.get(nestedGroupId);
  723. updatedNestedGroup.nestedInGroup = group.id;
  724. if (group.showNested == false) {
  725. updatedNestedGroup.visible = false;
  726. }
  727. groupsData.update(updatedNestedGroup);
  728. })
  729. }
  730. })
  731. // subscribe to new dataset
  732. var id = this.id;
  733. util.forEach(this.groupListeners, function (callback, event) {
  734. me.groupsData.on(event, callback, id);
  735. });
  736. // draw all ms
  737. ids = this.groupsData.getIds();
  738. this._onAddGroups(ids);
  739. }
  740. // update the group holding all ungrouped items
  741. this._updateUngrouped();
  742. // update the order of all items in each group
  743. this._order();
  744. this.body.emitter.emit('_change', {queue: true});
  745. };
  746. /**
  747. * Get the current groups
  748. * @returns {vis.DataSet | null} groups
  749. */
  750. ItemSet.prototype.getGroups = function() {
  751. return this.groupsData;
  752. };
  753. /**
  754. * Remove an item by its id
  755. * @param {String | Number} id
  756. */
  757. ItemSet.prototype.removeItem = function(id) {
  758. var item = this.itemsData.get(id),
  759. dataset = this.itemsData.getDataSet(),
  760. itemObj = this.items[id];
  761. if (item) {
  762. // confirm deletion
  763. this.options.onRemove(item, function (item) {
  764. if (item) {
  765. // remove by id here, it is possible that an item has no id defined
  766. // itself, so better not delete by the item itself
  767. dataset.remove(id);
  768. // Remove it's popup
  769. if (itemObj.popup) {
  770. itemObj.popup.destroy();
  771. itemObj.popup = null;
  772. }
  773. }
  774. });
  775. }
  776. };
  777. /**
  778. * Get the time of an item based on it's data and options.type
  779. * @param {Object} itemData
  780. * @returns {string} Returns the type
  781. * @private
  782. */
  783. ItemSet.prototype._getType = function (itemData) {
  784. return itemData.type || this.options.type || (itemData.end ? 'range' : 'box');
  785. };
  786. /**
  787. * Get the group id for an item
  788. * @param {Object} itemData
  789. * @returns {string} Returns the groupId
  790. * @private
  791. */
  792. ItemSet.prototype._getGroupId = function (itemData) {
  793. var type = this._getType(itemData);
  794. if (type == 'background' && itemData.group == undefined) {
  795. return BACKGROUND;
  796. }
  797. else {
  798. return this.groupsData ? itemData.group : UNGROUPED;
  799. }
  800. };
  801. /**
  802. * Handle updated items
  803. * @param {Number[]} ids
  804. * @protected
  805. */
  806. ItemSet.prototype._onUpdate = function(ids) {
  807. var me = this;
  808. ids.forEach(function (id) {
  809. var itemData = me.itemsData.get(id, me.itemOptions);
  810. var item = me.items[id];
  811. var type = itemData ? me._getType(itemData) : null;
  812. var constructor = ItemSet.types[type];
  813. var selected;
  814. if (item) {
  815. // update item
  816. if (!constructor || !(item instanceof constructor)) {
  817. // item type has changed, delete the item and recreate it
  818. selected = item.selected; // preserve selection of this item
  819. me._removeItem(item);
  820. item = null;
  821. }
  822. else {
  823. me._updateItem(item, itemData);
  824. }
  825. }
  826. if (!item && itemData) {
  827. // create item
  828. if (constructor) {
  829. item = new constructor(itemData, me.conversion, me.options);
  830. item.id = id; // TODO: not so nice setting id afterwards
  831. me._addItem(item);
  832. if (selected) {
  833. this.selection.push(id);
  834. item.select();
  835. }
  836. }
  837. else if (type == 'rangeoverflow') {
  838. // TODO: deprecated since version 2.1.0 (or 3.0.0?). cleanup some day
  839. throw new TypeError('Item type "rangeoverflow" is deprecated. Use css styling instead: ' +
  840. '.vis-item.vis-range .vis-item-content {overflow: visible;}');
  841. }
  842. else {
  843. throw new TypeError('Unknown item type "' + type + '"');
  844. }
  845. }
  846. }.bind(this));
  847. this._order();
  848. this.stackDirty = true; // force re-stacking of all items next redraw
  849. this.body.emitter.emit('_change', {queue: true});
  850. };
  851. /**
  852. * Handle added items
  853. * @param {Number[]} ids
  854. * @protected
  855. */
  856. ItemSet.prototype._onAdd = ItemSet.prototype._onUpdate;
  857. /**
  858. * Handle removed items
  859. * @param {Number[]} ids
  860. * @protected
  861. */
  862. ItemSet.prototype._onRemove = function(ids) {
  863. var count = 0;
  864. var me = this;
  865. ids.forEach(function (id) {
  866. var item = me.items[id];
  867. if (item) {
  868. count++;
  869. me._removeItem(item);
  870. }
  871. });
  872. if (count) {
  873. // update order
  874. this._order();
  875. this.stackDirty = true; // force re-stacking of all items next redraw
  876. this.body.emitter.emit('_change', {queue: true});
  877. }
  878. };
  879. /**
  880. * Update the order of item in all groups
  881. * @private
  882. */
  883. ItemSet.prototype._order = function() {
  884. // reorder the items in all groups
  885. // TODO: optimization: only reorder groups affected by the changed items
  886. util.forEach(this.groups, function (group) {
  887. group.order();
  888. });
  889. };
  890. /**
  891. * Handle updated groups
  892. * @param {Number[]} ids
  893. * @private
  894. */
  895. ItemSet.prototype._onUpdateGroups = function(ids) {
  896. this._onAddGroups(ids);
  897. };
  898. /**
  899. * Handle changed groups (added or updated)
  900. * @param {Number[]} ids
  901. * @private
  902. */
  903. ItemSet.prototype._onAddGroups = function(ids) {
  904. var me = this;
  905. ids.forEach(function (id) {
  906. var groupData = me.groupsData.get(id);
  907. var group = me.groups[id];
  908. if (!group) {
  909. // check for reserved ids
  910. if (id == UNGROUPED || id == BACKGROUND) {
  911. throw new Error('Illegal group id. ' + id + ' is a reserved id.');
  912. }
  913. var groupOptions = Object.create(me.options);
  914. util.extend(groupOptions, {
  915. height: null
  916. });
  917. group = new Group(id, groupData, me);
  918. me.groups[id] = group;
  919. // add items with this groupId to the new group
  920. for (var itemId in me.items) {
  921. if (me.items.hasOwnProperty(itemId)) {
  922. var item = me.items[itemId];
  923. if (item.data.group == id) {
  924. group.add(item);
  925. }
  926. }
  927. }
  928. group.order();
  929. group.show();
  930. }
  931. else {
  932. // update group
  933. group.setData(groupData);
  934. }
  935. });
  936. this.body.emitter.emit('_change', {queue: true});
  937. };
  938. /**
  939. * Handle removed groups
  940. * @param {Number[]} ids
  941. * @private
  942. */
  943. ItemSet.prototype._onRemoveGroups = function(ids) {
  944. var groups = this.groups;
  945. ids.forEach(function (id) {
  946. var group = groups[id];
  947. if (group) {
  948. group.hide();
  949. delete groups[id];
  950. }
  951. });
  952. this.markDirty();
  953. this.body.emitter.emit('_change', {queue: true});
  954. };
  955. /**
  956. * Reorder the groups if needed
  957. * @return {boolean} changed
  958. * @private
  959. */
  960. ItemSet.prototype._orderGroups = function () {
  961. if (this.groupsData) {
  962. // reorder the groups
  963. var groupIds = this.groupsData.getIds({
  964. order: this.options.groupOrder
  965. });
  966. groupIds = this._orderNestedGroups(groupIds);
  967. var changed = !util.equalArray(groupIds, this.groupIds);
  968. if (changed) {
  969. // hide all groups, removes them from the DOM
  970. var groups = this.groups;
  971. groupIds.forEach(function (groupId) {
  972. groups[groupId].hide();
  973. });
  974. // show the groups again, attach them to the DOM in correct order
  975. groupIds.forEach(function (groupId) {
  976. groups[groupId].show();
  977. });
  978. this.groupIds = groupIds;
  979. }
  980. return changed;
  981. }
  982. else {
  983. return false;
  984. }
  985. };
  986. /**
  987. * Reorder the nested groups
  988. * @return {boolean} changed
  989. * @private
  990. */
  991. ItemSet.prototype._orderNestedGroups = function(groupIds) {
  992. var newGroupIdsOrder = [];
  993. groupIds.forEach(function(groupId){
  994. var groupData = this.groupsData.get(groupId);
  995. if (!groupData.nestedInGroup) {
  996. newGroupIdsOrder.push(groupId)
  997. }
  998. if (groupData.nestedGroups) {
  999. var nestedGroups = this.groupsData.get({
  1000. filter: function(nestedGroup) {
  1001. return nestedGroup.nestedInGroup == groupId;
  1002. }
  1003. });
  1004. var nestedGroupIds = nestedGroups.map(function(nestedGroup) { return nestedGroup.id })
  1005. newGroupIdsOrder = newGroupIdsOrder.concat(nestedGroupIds);
  1006. }
  1007. }, this)
  1008. return newGroupIdsOrder;
  1009. }
  1010. /**
  1011. * Add a new item
  1012. * @param {Item} item
  1013. * @private
  1014. */
  1015. ItemSet.prototype._addItem = function(item) {
  1016. this.items[item.id] = item;
  1017. // add to group
  1018. var groupId = this._getGroupId(item.data);
  1019. var group = this.groups[groupId];
  1020. if (!group) {
  1021. item.groupShowing = false;
  1022. } else if (group && group.data && group.data.showNested) {
  1023. item.groupShowing = true;
  1024. }
  1025. if (group) group.add(item);
  1026. };
  1027. /**
  1028. * Update an existing item
  1029. * @param {Item} item
  1030. * @param {Object} itemData
  1031. * @private
  1032. */
  1033. ItemSet.prototype._updateItem = function(item, itemData) {
  1034. var oldGroupId = item.data.group;
  1035. var oldSubGroupId = item.data.subgroup;
  1036. // update the items data (will redraw the item when displayed)
  1037. item.setData(itemData);
  1038. var groupId = this._getGroupId(item.data);
  1039. var group = this.groups[groupId];
  1040. if (!group) {
  1041. item.groupShowing = false;
  1042. } else if (group && group.data && group.data.showNested) {
  1043. item.groupShowing = true;
  1044. }
  1045. // update group
  1046. if (oldGroupId != item.data.group || oldSubGroupId != item.data.subgroup) {
  1047. var oldGroup = this.groups[oldGroupId];
  1048. if (oldGroup) oldGroup.remove(item);
  1049. if (group) group.add(item);
  1050. }
  1051. };
  1052. /**
  1053. * Delete an item from the ItemSet: remove it from the DOM, from the map
  1054. * with items, and from the map with visible items, and from the selection
  1055. * @param {Item} item
  1056. * @private
  1057. */
  1058. ItemSet.prototype._removeItem = function(item) {
  1059. // remove from DOM
  1060. item.hide();
  1061. // remove from items
  1062. delete this.items[item.id];
  1063. // remove from selection
  1064. var index = this.selection.indexOf(item.id);
  1065. if (index != -1) this.selection.splice(index, 1);
  1066. // remove from group
  1067. item.parent && item.parent.remove(item);
  1068. };
  1069. /**
  1070. * Create an array containing all items being a range (having an end date)
  1071. * @param array
  1072. * @returns {Array}
  1073. * @private
  1074. */
  1075. ItemSet.prototype._constructByEndArray = function(array) {
  1076. var endArray = [];
  1077. for (var i = 0; i < array.length; i++) {
  1078. if (array[i] instanceof RangeItem) {
  1079. endArray.push(array[i]);
  1080. }
  1081. }
  1082. return endArray;
  1083. };
  1084. /**
  1085. * Register the clicked item on touch, before dragStart is initiated.
  1086. *
  1087. * dragStart is initiated from a mousemove event, AFTER the mouse/touch is
  1088. * already moving. Therefore, the mouse/touch can sometimes be above an other
  1089. * DOM element than the item itself.
  1090. *
  1091. * @param {Event} event
  1092. * @private
  1093. */
  1094. ItemSet.prototype._onTouch = function (event) {
  1095. // store the touched item, used in _onDragStart
  1096. this.touchParams.item = this.itemFromTarget(event);
  1097. this.touchParams.dragLeftItem = event.target.dragLeftItem || false;
  1098. this.touchParams.dragRightItem = event.target.dragRightItem || false;
  1099. this.touchParams.itemProps = null;
  1100. };
  1101. /**
  1102. * Given an group id, returns the index it has.
  1103. *
  1104. * @param {Number} groupID
  1105. * @private
  1106. */
  1107. ItemSet.prototype._getGroupIndex = function(groupId) {
  1108. for (var i = 0; i < this.groupIds.length; i++) {
  1109. if (groupId == this.groupIds[i])
  1110. return i;
  1111. }
  1112. };
  1113. /**
  1114. * Start dragging the selected events
  1115. * @param {Event} event
  1116. * @private
  1117. */
  1118. ItemSet.prototype._onDragStart = function (event) {
  1119. if (this.touchParams.itemIsDragging) { return; }
  1120. var item = this.touchParams.item || null;
  1121. var me = this;
  1122. var props;
  1123. if (item && (item.selected || this.options.itemsAlwaysDraggable)) {
  1124. if (this.options.editable.overrideItems &&
  1125. !this.options.editable.updateTime &&
  1126. !this.options.editable.updateGroup) {
  1127. return;
  1128. }
  1129. // override options.editable
  1130. if ((item.editable != null && !item.editable.updateTime && !item.editable.updateGroup)
  1131. && !this.options.editable.overrideItems) {
  1132. return;
  1133. }
  1134. var dragLeftItem = this.touchParams.dragLeftItem;
  1135. var dragRightItem = this.touchParams.dragRightItem;
  1136. this.touchParams.itemIsDragging = true;
  1137. this.touchParams.selectedItem = item;
  1138. if (dragLeftItem) {
  1139. props = {
  1140. item: dragLeftItem,
  1141. initialX: event.center.x,
  1142. dragLeft: true,
  1143. data: this._cloneItemData(item.data)
  1144. };
  1145. this.touchParams.itemProps = [props];
  1146. }
  1147. else if (dragRightItem) {
  1148. props = {
  1149. item: dragRightItem,
  1150. initialX: event.center.x,
  1151. dragRight: true,
  1152. data: this._cloneItemData(item.data)
  1153. };
  1154. this.touchParams.itemProps = [props];
  1155. }
  1156. else {
  1157. var baseGroupIndex = this._getGroupIndex(item.data.group);
  1158. var itemsToDrag = (this.options.itemsAlwaysDraggable && !item.selected) ? [item.id] : this.getSelection();
  1159. this.touchParams.itemProps = itemsToDrag.map(function (id) {
  1160. var item = me.items[id];
  1161. var groupIndex = me._getGroupIndex(item.data.group);
  1162. return {
  1163. item: item,
  1164. initialX: event.center.x,
  1165. groupOffset: baseGroupIndex-groupIndex,
  1166. data: this._cloneItemData(item.data)
  1167. };
  1168. }.bind(this));
  1169. }
  1170. event.stopPropagation();
  1171. }
  1172. else if (this.options.editable.add && (event.srcEvent.ctrlKey || event.srcEvent.metaKey)) {
  1173. // create a new range item when dragging with ctrl key down
  1174. this._onDragStartAddItem(event);
  1175. }
  1176. };
  1177. /**
  1178. * Start creating a new range item by dragging.
  1179. * @param {Event} event
  1180. * @private
  1181. */
  1182. ItemSet.prototype._onDragStartAddItem = function (event) {
  1183. var snap = this.options.snap || null;
  1184. if (this.options.rtl) {
  1185. var xAbs = util.getAbsoluteRight(this.dom.frame);
  1186. var x = xAbs - event.center.x + 10; // plus 10 to compensate for the drag starting as soon as you've moved 10px
  1187. } else {
  1188. var xAbs = util.getAbsoluteLeft(this.dom.frame);
  1189. var x = event.center.x - xAbs - 10; // minus 10 to compensate for the drag starting as soon as you've moved 10px
  1190. }
  1191. var time = this.body.util.toTime(x);
  1192. var scale = this.body.util.getScale();
  1193. var step = this.body.util.getStep();
  1194. var start = snap ? snap(time, scale, step) : time;
  1195. var end = start;
  1196. var itemData = {
  1197. type: 'range',
  1198. start: start,
  1199. end: end,
  1200. content: 'new item'
  1201. };
  1202. var id = util.randomUUID();
  1203. itemData[this.itemsData._fieldId] = id;
  1204. var group = this.groupFromTarget(event);
  1205. if (group) {
  1206. itemData.group = group.groupId;
  1207. }
  1208. var newItem = new RangeItem(itemData, this.conversion, this.options);
  1209. newItem.id = id; // TODO: not so nice setting id afterwards
  1210. newItem.data = this._cloneItemData(itemData);
  1211. this._addItem(newItem);
  1212. this.touchParams.selectedItem = newItem;
  1213. var props = {
  1214. item: newItem,
  1215. initialX: event.center.x,
  1216. data: newItem.data
  1217. };
  1218. if (this.options.rtl) {
  1219. props.dragLeft = true;
  1220. } else {
  1221. props.dragRight = true;
  1222. }
  1223. this.touchParams.itemProps = [props];
  1224. event.stopPropagation();
  1225. };
  1226. /**
  1227. * Drag selected items
  1228. * @param {Event} event
  1229. * @private
  1230. */
  1231. ItemSet.prototype._onDrag = function (event) {
  1232. if (this.touchParams.itemProps) {
  1233. event.stopPropagation();
  1234. var me = this;
  1235. var snap = this.options.snap || null;
  1236. if (this.options.rtl) {
  1237. var xOffset = this.body.dom.root.offsetLeft + this.body.domProps.right.width;
  1238. } else {
  1239. var xOffset = this.body.dom.root.offsetLeft + this.body.domProps.left.width;
  1240. }
  1241. var scale = this.body.util.getScale();
  1242. var step = this.body.util.getStep();
  1243. //only calculate the new group for the item that's actually dragged
  1244. var selectedItem = this.touchParams.selectedItem;
  1245. var updateGroupAllowed = ((this.options.editable.overrideItems || selectedItem.editable == null) && this.options.editable.updateGroup) ||
  1246. (!this.options.editable.overrideItems && selectedItem.editable != null && selectedItem.editable.updateGroup);
  1247. var newGroupBase = null;
  1248. if (updateGroupAllowed && selectedItem) {
  1249. if (selectedItem.data.group != undefined) {
  1250. // drag from one group to another
  1251. var group = me.groupFromTarget(event);
  1252. if (group) {
  1253. //we know the offset for all items, so the new group for all items
  1254. //will be relative to this one.
  1255. newGroupBase = this._getGroupIndex(group.groupId);
  1256. }
  1257. }
  1258. }
  1259. // move
  1260. this.touchParams.itemProps.forEach(function (props) {
  1261. var current = me.body.util.toTime(event.center.x - xOffset);
  1262. var initial = me.body.util.toTime(props.initialX - xOffset);
  1263. if (this.options.rtl) {
  1264. var offset = -(current - initial); // ms
  1265. } else {
  1266. var offset = (current - initial); // ms
  1267. }
  1268. var itemData = this._cloneItemData(props.item.data); // clone the data
  1269. if (props.item.editable != null
  1270. && !props.item.editable.updateTime
  1271. && !props.item.editable.updateGroup
  1272. && !me.options.editable.overrideItems) {
  1273. return;
  1274. }
  1275. var updateTimeAllowed = ((this.options.editable.overrideItems || selectedItem.editable == null) && this.options.editable.updateTime) ||
  1276. (!this.options.editable.overrideItems && selectedItem.editable != null && selectedItem.editable.updateTime);
  1277. if (updateTimeAllowed) {
  1278. if (props.dragLeft) {
  1279. // drag left side of a range item
  1280. if (this.options.rtl) {
  1281. if (itemData.end != undefined) {
  1282. var initialEnd = util.convert(props.data.end, 'Date');
  1283. var end = new Date(initialEnd.valueOf() + offset);
  1284. // TODO: pass a Moment instead of a Date to snap(). (Breaking change)
  1285. itemData.end = snap ? snap(end, scale, step) : end;
  1286. }
  1287. } else {
  1288. if (itemData.start != undefined) {
  1289. var initialStart = util.convert(props.data.start, 'Date');
  1290. var start = new Date(initialStart.valueOf() + offset);
  1291. // TODO: pass a Moment instead of a Date to snap(). (Breaking change)
  1292. itemData.start = snap ? snap(start, scale, step) : start;
  1293. }
  1294. }
  1295. }
  1296. else if (props.dragRight) {
  1297. // drag right side of a range item
  1298. if (this.options.rtl) {
  1299. if (itemData.start != undefined) {
  1300. var initialStart = util.convert(props.data.start, 'Date');
  1301. var start = new Date(initialStart.valueOf() + offset);
  1302. // TODO: pass a Moment instead of a Date to snap(). (Breaking change)
  1303. itemData.start = snap ? snap(start, scale, step) : start;
  1304. }
  1305. } else {
  1306. if (itemData.end != undefined) {
  1307. var initialEnd = util.convert(props.data.end, 'Date');
  1308. var end = new Date(initialEnd.valueOf() + offset);
  1309. // TODO: pass a Moment instead of a Date to snap(). (Breaking change)
  1310. itemData.end = snap ? snap(end, scale, step) : end;
  1311. }
  1312. }
  1313. }
  1314. else {
  1315. // drag both start and end
  1316. if (itemData.start != undefined) {
  1317. var initialStart = util.convert(props.data.start, 'Date').valueOf();
  1318. var start = new Date(initialStart + offset);
  1319. if (itemData.end != undefined) {
  1320. var initialEnd = util.convert(props.data.end, 'Date');
  1321. var duration = initialEnd.valueOf() - initialStart.valueOf();
  1322. // TODO: pass a Moment instead of a Date to snap(). (Breaking change)
  1323. itemData.start = snap ? snap(start, scale, step) : start;
  1324. itemData.end = new Date(itemData.start.valueOf() + duration);
  1325. }
  1326. else {
  1327. // TODO: pass a Moment instead of a Date to snap(). (Breaking change)
  1328. itemData.start = snap ? snap(start, scale, step) : start;
  1329. }
  1330. }
  1331. }
  1332. }
  1333. if (updateGroupAllowed && (!props.dragLeft && !props.dragRight) && newGroupBase!=null) {
  1334. if (itemData.group != undefined) {
  1335. var newOffset = newGroupBase - props.groupOffset;
  1336. //make sure we stay in bounds
  1337. newOffset = Math.max(0, newOffset);
  1338. newOffset = Math.min(me.groupIds.length-1, newOffset);
  1339. itemData.group = me.groupIds[newOffset];
  1340. }
  1341. }
  1342. // confirm moving the item
  1343. itemData = this._cloneItemData(itemData); // convert start and end to the correct type
  1344. me.options.onMoving(itemData, function (itemData) {
  1345. if (itemData) {
  1346. props.item.setData(this._cloneItemData(itemData, 'Date'));
  1347. }
  1348. }.bind(this));
  1349. }.bind(this));
  1350. this.stackDirty = true; // force re-stacking of all items next redraw
  1351. this.body.emitter.emit('_change');
  1352. }
  1353. };
  1354. /**
  1355. * Move an item to another group
  1356. * @param {Item} item
  1357. * @param {String | Number} groupId
  1358. * @private
  1359. */
  1360. ItemSet.prototype._moveToGroup = function(item, groupId) {
  1361. var group = this.groups[groupId];
  1362. if (group && group.groupId != item.data.group) {
  1363. var oldGroup = item.parent;
  1364. oldGroup.remove(item);
  1365. oldGroup.order();
  1366. group.add(item);
  1367. group.order();
  1368. item.data.group = group.groupId;
  1369. }
  1370. };
  1371. /**
  1372. * End of dragging selected items
  1373. * @param {Event} event
  1374. * @private
  1375. */
  1376. ItemSet.prototype._onDragEnd = function (event) {
  1377. if (this.touchParams.itemProps) {
  1378. event.stopPropagation();
  1379. var me = this;
  1380. var dataset = this.itemsData.getDataSet();
  1381. var itemProps = this.touchParams.itemProps ;
  1382. this.touchParams.itemProps = null;
  1383. this.touchParams.itemIsDragging = false;
  1384. itemProps.forEach(function (props) {
  1385. var id = props.item.id;
  1386. var exists = me.itemsData.get(id, me.itemOptions) != null;
  1387. if (!exists) {
  1388. // add a new item
  1389. me.options.onAdd(props.item.data, function (itemData) {
  1390. me._removeItem(props.item); // remove temporary item
  1391. if (itemData) {
  1392. me.itemsData.getDataSet().add(itemData);
  1393. }
  1394. // force re-stacking of all items next redraw
  1395. me.stackDirty = true;
  1396. me.body.emitter.emit('_change');
  1397. });
  1398. }
  1399. else {
  1400. // update existing item
  1401. var itemData = this._cloneItemData(props.item.data); // convert start and end to the correct type
  1402. me.options.onMove(itemData, function (itemData) {
  1403. if (itemData) {
  1404. // apply changes
  1405. itemData[dataset._fieldId] = id; // ensure the item contains its id (can be undefined)
  1406. dataset.update(itemData);
  1407. }
  1408. else {
  1409. // restore original values
  1410. props.item.setData(props.data);
  1411. me.stackDirty = true; // force re-stacking of all items next redraw
  1412. me.body.emitter.emit('_change');
  1413. }
  1414. });
  1415. }
  1416. }.bind(this));
  1417. }
  1418. };
  1419. ItemSet.prototype._onGroupClick = function (event) {
  1420. var group = this.groupFromTarget(event);
  1421. if (!group.nestedGroups) return;
  1422. var groupsData = this.groupsData;
  1423. if (this.groupsData instanceof DataView) {
  1424. groupsData = this.groupsData.getDataSet()
  1425. }
  1426. group.showNested = !group.showNested;
  1427. var nestedGroups = groupsData.get(group.nestedGroups).map(function(nestedGroup) {
  1428. if (nestedGroup.visible == undefined) { nestedGroup.visible = true; }
  1429. nestedGroup.visible = !!group.showNested;
  1430. return nestedGroup;
  1431. });
  1432. groupsData.update(nestedGroups);
  1433. if (group.showNested) {
  1434. util.removeClassName(group.dom.label, 'collapsed');
  1435. util.addClassName(group.dom.label, 'expanded');
  1436. } else {
  1437. util.removeClassName(group.dom.label, 'expanded');
  1438. var collapsedDirClassName = this.options.rtl ? 'collapsed-rtl' : 'collapsed'
  1439. util.addClassName(group.dom.label, collapsedDirClassName);
  1440. }
  1441. }
  1442. ItemSet.prototype._onGroupDragStart = function (event) {
  1443. if (this.options.groupEditable.order) {
  1444. this.groupTouchParams.group = this.groupFromTarget(event);
  1445. if (this.groupTouchParams.group) {
  1446. event.stopPropagation();
  1447. this.groupTouchParams.originalOrder = this.groupsData.getIds({
  1448. order: this.options.groupOrder
  1449. });
  1450. }
  1451. }
  1452. }
  1453. ItemSet.prototype._onGroupDrag = function (event) {
  1454. if (this.options.groupEditable.order && this.groupTouchParams.group) {
  1455. event.stopPropagation();
  1456. var groupsData = this.groupsData;
  1457. if (this.groupsData instanceof DataView) {
  1458. groupsData = this.groupsData.getDataSet()
  1459. }
  1460. // drag from one group to another
  1461. var group = this.groupFromTarget(event);
  1462. // try to avoid toggling when groups differ in height
  1463. if (group && group.height != this.groupTouchParams.group.height) {
  1464. var movingUp = (group.top < this.groupTouchParams.group.top);
  1465. var clientY = event.center ? event.center.y : event.clientY;
  1466. var targetGroupTop = util.getAbsoluteTop(group.dom.foreground);
  1467. var draggedGroupHeight = this.groupTouchParams.group.height;
  1468. if (movingUp) {
  1469. // skip swapping the groups when the dragged group is not below clientY afterwards
  1470. if (targetGroupTop + draggedGroupHeight < clientY) {
  1471. return;
  1472. }
  1473. } else {
  1474. var targetGroupHeight = group.height;
  1475. // skip swapping the groups when the dragged group is not below clientY afterwards
  1476. if (targetGroupTop + targetGroupHeight - draggedGroupHeight > clientY) {
  1477. return;
  1478. }
  1479. }
  1480. }
  1481. if (group && group != this.groupTouchParams.group) {
  1482. var targetGroup = groupsData.get(group.groupId);
  1483. var draggedGroup = groupsData.get(this.groupTouchParams.group.groupId);
  1484. // switch groups
  1485. if (draggedGroup && targetGroup) {
  1486. this.options.groupOrderSwap(draggedGroup, targetGroup, groupsData);
  1487. groupsData.update(draggedGroup);
  1488. groupsData.update(targetGroup);
  1489. }
  1490. // fetch current order of groups
  1491. var newOrder = groupsData.getIds({
  1492. order: this.options.groupOrder
  1493. });
  1494. // in case of changes since _onGroupDragStart
  1495. if (!util.equalArray(newOrder, this.groupTouchParams.originalOrder)) {
  1496. var origOrder = this.groupTouchParams.originalOrder;
  1497. var draggedId = this.groupTouchParams.group.groupId;
  1498. var numGroups = Math.min(origOrder.length, newOrder.length);
  1499. var curPos = 0;
  1500. var newOffset = 0;
  1501. var orgOffset = 0;
  1502. while (curPos < numGroups) {
  1503. // as long as the groups are where they should be step down along the groups order
  1504. while ((curPos+newOffset) < numGroups
  1505. && (curPos+orgOffset) < numGroups
  1506. && newOrder[curPos+newOffset] == origOrder[curPos+orgOffset]) {
  1507. curPos++;
  1508. }
  1509. // all ok
  1510. if (curPos+newOffset >= numGroups) {
  1511. break;
  1512. }
  1513. // not all ok
  1514. // if dragged group was move upwards everything below should have an offset
  1515. if (newOrder[curPos+newOffset] == draggedId) {
  1516. newOffset = 1;
  1517. continue;
  1518. }
  1519. // if dragged group was move downwards everything above should have an offset
  1520. else if (origOrder[curPos+orgOffset] == draggedId) {
  1521. orgOffset = 1;
  1522. continue;
  1523. }
  1524. // found a group (apart from dragged group) that has the wrong position -> switch with the
  1525. // group at the position where other one should be, fix index arrays and continue
  1526. else {
  1527. var slippedPosition = newOrder.indexOf(origOrder[curPos+orgOffset])
  1528. var switchGroup = groupsData.get(newOrder[curPos+newOffset]);
  1529. var shouldBeGroup = groupsData.get(origOrder[curPos+orgOffset]);
  1530. this.options.groupOrderSwap(switchGroup, shouldBeGroup, groupsData);
  1531. groupsData.update(switchGroup);
  1532. groupsData.update(shouldBeGroup);
  1533. var switchGroupId = newOrder[curPos+newOffset];
  1534. newOrder[curPos+newOffset] = origOrder[curPos+orgOffset];
  1535. newOrder[slippedPosition] = switchGroupId;
  1536. curPos++;
  1537. }
  1538. }
  1539. }
  1540. }
  1541. }
  1542. }
  1543. ItemSet.prototype._onGroupDragEnd = function (event) {
  1544. if (this.options.groupEditable.order && this.groupTouchParams.group) {
  1545. event.stopPropagation();
  1546. // update existing group
  1547. var me = this;
  1548. var id = me.groupTouchParams.group.groupId;
  1549. var dataset = me.groupsData.getDataSet();
  1550. var groupData = util.extend({}, dataset.get(id)); // clone the data
  1551. me.options.onMoveGroup(groupData, function (groupData) {
  1552. if (groupData) {
  1553. // apply changes
  1554. groupData[dataset._fieldId] = id; // ensure the group contains its id (can be undefined)
  1555. dataset.update(groupData);
  1556. }
  1557. else {
  1558. // fetch current order of groups
  1559. var newOrder = dataset.getIds({
  1560. order: me.options.groupOrder
  1561. });
  1562. // restore original order
  1563. if (!util.equalArray(newOrder, me.groupTouchParams.originalOrder)) {
  1564. var origOrder = me.groupTouchParams.originalOrder;
  1565. var numGroups = Math.min(origOrder.length, newOrder.length);
  1566. var curPos = 0;
  1567. while (curPos < numGroups) {
  1568. // as long as the groups are where they should be step down along the groups order
  1569. while (curPos < numGroups && newOrder[curPos] == origOrder[curPos]) {
  1570. curPos++;
  1571. }
  1572. // all ok
  1573. if (curPos >= numGroups) {
  1574. break;
  1575. }
  1576. // found a group that has the wrong position -> switch with the
  1577. // group at the position where other one should be, fix index arrays and continue
  1578. var slippedPosition = newOrder.indexOf(origOrder[curPos])
  1579. var switchGroup = dataset.get(newOrder[curPos]);
  1580. var shouldBeGroup = dataset.get(origOrder[curPos]);
  1581. me.options.groupOrderSwap(switchGroup, shouldBeGroup, dataset);
  1582. groupsData.update(switchGroup);
  1583. groupsData.update(shouldBeGroup);
  1584. var switchGroupId = newOrder[curPos];
  1585. newOrder[curPos] = origOrder[curPos];
  1586. newOrder[slippedPosition] = switchGroupId;
  1587. curPos++;
  1588. }
  1589. }
  1590. }
  1591. });
  1592. me.body.emitter.emit('groupDragged', { groupId: id });
  1593. }
  1594. }
  1595. /**
  1596. * Handle selecting/deselecting an item when tapping it
  1597. * @param {Event} event
  1598. * @private
  1599. */
  1600. ItemSet.prototype._onSelectItem = function (event) {
  1601. if (!this.options.selectable) return;
  1602. var ctrlKey = event.srcEvent && (event.srcEvent.ctrlKey || event.srcEvent.metaKey);
  1603. var shiftKey = event.srcEvent && event.srcEvent.shiftKey;
  1604. if (ctrlKey || shiftKey) {
  1605. this._onMultiSelectItem(event);
  1606. return;
  1607. }
  1608. var oldSelection = this.getSelection();
  1609. var item = this.itemFromTarget(event);
  1610. var selection = item ? [item.id] : [];
  1611. this.setSelection(selection);
  1612. var newSelection = this.getSelection();
  1613. // emit a select event,
  1614. // except when old selection is empty and new selection is still empty
  1615. if (newSelection.length > 0 || oldSelection.length > 0) {
  1616. this.body.emitter.emit('select', {
  1617. items: newSelection,
  1618. event: util.elementsCensor(event)
  1619. });
  1620. }
  1621. };
  1622. /**
  1623. * Handle hovering an item
  1624. * @param {Event} event
  1625. * @private
  1626. */
  1627. ItemSet.prototype._onMouseOver = function (event) {
  1628. var item = this.itemFromTarget(event);
  1629. if (!item) return;
  1630. // Item we just left
  1631. var related = this.itemFromRelatedTarget(event);
  1632. if (item === related) {
  1633. // We haven't changed item, just element in the item
  1634. return;
  1635. }
  1636. if (item.getTitle()) {
  1637. if (item.popup == null) {
  1638. item.setPopup(new Popup(this.body.dom.root));
  1639. }
  1640. var container = this.body.dom.centerContainer;
  1641. item.popup.setPosition(
  1642. event.clientX - util.getAbsoluteLeft(container),
  1643. event.clientY - util.getAbsoluteTop(container)
  1644. );
  1645. item.popup.show();
  1646. }
  1647. this.body.emitter.emit('itemover', {
  1648. item: item.id,
  1649. event: util.elementsCensor(event)
  1650. });
  1651. };
  1652. ItemSet.prototype._onMouseOut = function (event) {
  1653. var item = this.itemFromTarget(event);
  1654. if (!item) return;
  1655. // Item we are going to
  1656. var related = this.itemFromRelatedTarget(event);
  1657. if (item === related) {
  1658. // We aren't changing item, just element in the item
  1659. return;
  1660. }
  1661. if (item.popup != null) {
  1662. item.popup.hide();
  1663. }
  1664. this.body.emitter.emit('itemout', {
  1665. item: item.id,
  1666. event: util.elementsCensor(event)
  1667. });
  1668. };
  1669. /**
  1670. * Handle updates of an item on double tap
  1671. * @param event
  1672. * @private
  1673. */
  1674. ItemSet.prototype._onUpdateItem = function (item) {
  1675. if (!this.options.selectable) return;
  1676. if (!this.options.editable.add) return;
  1677. var me = this;
  1678. if (item) {
  1679. // execute async handler to update the item (or cancel it)
  1680. var itemData = me.itemsData.get(item.id); // get a clone of the data from the dataset
  1681. this.options.onUpdate(itemData, function (itemData) {
  1682. if (itemData) {
  1683. me.itemsData.getDataSet().update(itemData);
  1684. }
  1685. });
  1686. }
  1687. }
  1688. /**
  1689. * Handle creation of an item on double tap
  1690. * @param event
  1691. * @private
  1692. */
  1693. ItemSet.prototype._onAddItem = function (event) {
  1694. if (!this.options.selectable) return;
  1695. if (!this.options.editable.add) return;
  1696. var me = this;
  1697. var snap = this.options.snap || null;
  1698. var item = this.itemFromTarget(event);
  1699. if (!item) {
  1700. // add item
  1701. if (this.options.rtl) {
  1702. var xAbs = util.getAbsoluteRight(this.dom.frame);
  1703. var x = xAbs - event.center.x;
  1704. } else {
  1705. var xAbs = util.getAbsoluteLeft(this.dom.frame);
  1706. var x = event.center.x - xAbs;
  1707. }
  1708. // var xAbs = util.getAbsoluteLeft(this.dom.frame);
  1709. // var x = event.center.x - xAbs;
  1710. var start = this.body.util.toTime(x);
  1711. var scale = this.body.util.getScale();
  1712. var step = this.body.util.getStep();
  1713. var newItemData = {
  1714. start: snap ? snap(start, scale, step) : start,
  1715. content: 'new item'
  1716. };
  1717. if (event.type == 'drop') {
  1718. var itemData = JSON.parse(event.dataTransfer.getData("text"))
  1719. newItemData.content = itemData.content; // content is required
  1720. newItemData.type = itemData.type || 'box';
  1721. newItemData[this.itemsData._fieldId] = itemData.id || util.randomUUID();
  1722. if (itemData.type == 'range' || (itemData.end && itemData.start)) {
  1723. if (!itemData.end) {
  1724. var end = this.body.util.toTime(x + this.props.width / 5);
  1725. newItemData.end = snap ? snap(end, scale, step) : end;
  1726. } else {
  1727. newItemData.end = itemData.end;
  1728. newItemData.start = itemData.start;
  1729. }
  1730. }
  1731. } else {
  1732. newItemData[this.itemsData._fieldId] = util.randomUUID();
  1733. // when default type is a range, add a default end date to the new item
  1734. if (this.options.type === 'range') {
  1735. var end = this.body.util.toTime(x + this.props.width / 5);
  1736. newItemData.end = snap ? snap(end, scale, step) : end;
  1737. }
  1738. }
  1739. var group = this.groupFromTarget(event);
  1740. if (group) {
  1741. newItemData.group = group.groupId;
  1742. }
  1743. // execute async handler to customize (or cancel) adding an item
  1744. newItemData = this._cloneItemData(newItemData); // convert start and end to the correct type
  1745. this.options.onAdd(newItemData, function (item) {
  1746. if (item) {
  1747. me.itemsData.getDataSet().add(item);
  1748. if (event.type == 'drop') {
  1749. me.setSelection([item.id]);
  1750. }
  1751. // TODO: need to trigger a redraw?
  1752. }
  1753. });
  1754. }
  1755. };
  1756. /**
  1757. * Handle selecting/deselecting multiple items when holding an item
  1758. * @param {Event} event
  1759. * @private
  1760. */
  1761. ItemSet.prototype._onMultiSelectItem = function (event) {
  1762. if (!this.options.selectable) return;
  1763. var item = this.itemFromTarget(event);
  1764. if (item) {
  1765. // multi select items (if allowed)
  1766. var selection = this.options.multiselect
  1767. ? this.getSelection() // take current selection
  1768. : []; // deselect current selection
  1769. var shiftKey = event.srcEvent && event.srcEvent.shiftKey || false;
  1770. if (shiftKey && this.options.multiselect) {
  1771. // select all items between the old selection and the tapped item
  1772. var itemGroup = this.itemsData.get(item.id).group;
  1773. // when filtering get the group of the last selected item
  1774. var lastSelectedGroup = undefined;
  1775. if (this.options.multiselectPerGroup) {
  1776. if (selection.length > 0) {
  1777. lastSelectedGroup = this.itemsData.get(selection[0]).group;
  1778. }
  1779. }
  1780. // determine the selection range
  1781. if (!this.options.multiselectPerGroup || lastSelectedGroup == undefined || lastSelectedGroup == itemGroup) {
  1782. selection.push(item.id);
  1783. }
  1784. var range = ItemSet._getItemRange(this.itemsData.get(selection, this.itemOptions));
  1785. if (!this.options.multiselectPerGroup || lastSelectedGroup == itemGroup) {
  1786. // select all items within the selection range
  1787. selection = [];
  1788. for (var id in this.items) {
  1789. if (this.items.hasOwnProperty(id)) {
  1790. var _item = this.items[id];
  1791. var start = _item.data.start;
  1792. var end = (_item.data.end !== undefined) ? _item.data.end : start;
  1793. if (start >= range.min &&
  1794. end <= range.max &&
  1795. (!this.options.multiselectPerGroup || lastSelectedGroup == this.itemsData.get(_item.id).group) &&
  1796. !(_item instanceof BackgroundItem)) {
  1797. selection.push(_item.id); // do not use id but item.id, id itself is stringified
  1798. }
  1799. }
  1800. }
  1801. }
  1802. }
  1803. else {
  1804. // add/remove this item from the current selection
  1805. var index = selection.indexOf(item.id);
  1806. if (index == -1) {
  1807. // item is not yet selected -> select it
  1808. selection.push(item.id);
  1809. }
  1810. else {
  1811. // item is already selected -> deselect it
  1812. selection.splice(index, 1);
  1813. }
  1814. }
  1815. this.setSelection(selection);
  1816. this.body.emitter.emit('select', {
  1817. items: this.getSelection(),
  1818. event: util.elementsCensor(event)
  1819. });
  1820. }
  1821. };
  1822. /**
  1823. * Calculate the time range of a list of items
  1824. * @param {Array.<Object>} itemsData
  1825. * @return {{min: Date, max: Date}} Returns the range of the provided items
  1826. * @private
  1827. */
  1828. ItemSet._getItemRange = function(itemsData) {
  1829. var max = null;
  1830. var min = null;
  1831. itemsData.forEach(function (data) {
  1832. if (min == null || data.start < min) {
  1833. min = data.start;
  1834. }
  1835. if (data.end != undefined) {
  1836. if (max == null || data.end > max) {
  1837. max = data.end;
  1838. }
  1839. }
  1840. else {
  1841. if (max == null || data.start > max) {
  1842. max = data.start;
  1843. }
  1844. }
  1845. });
  1846. return {
  1847. min: min,
  1848. max: max
  1849. }
  1850. };
  1851. /**
  1852. * Find an item from an element:
  1853. * searches for the attribute 'timeline-item' in the element's tree
  1854. * @param {HTMLElement} element
  1855. * @return {Item | null} item
  1856. */
  1857. ItemSet.prototype.itemFromElement = function(element) {
  1858. var cur = element;
  1859. while (cur) {
  1860. if (cur.hasOwnProperty('timeline-item')) {
  1861. return cur['timeline-item'];
  1862. }
  1863. cur = cur.parentNode;
  1864. }
  1865. return null;
  1866. };
  1867. /**
  1868. * Find an item from an event target:
  1869. * searches for the attribute 'timeline-item' in the event target's element tree
  1870. * @param {Event} event
  1871. * @return {Item | null} item
  1872. */
  1873. ItemSet.prototype.itemFromTarget = function(event) {
  1874. return this.itemFromElement(event.target);
  1875. };
  1876. /**
  1877. * Find an item from an event's related target:
  1878. * searches for the attribute 'timeline-item' in the related target's element tree
  1879. * @param {Event} event
  1880. * @return {Item | null} item
  1881. */
  1882. ItemSet.prototype.itemFromRelatedTarget = function(event) {
  1883. return this.itemFromElement(event.relatedTarget);
  1884. };
  1885. /**
  1886. * Find the Group from an event target:
  1887. * searches for the attribute 'timeline-group' in the event target's element tree
  1888. * @param {Event} event
  1889. * @return {Group | null} group
  1890. */
  1891. ItemSet.prototype.groupFromTarget = function(event) {
  1892. var clientY = event.center ? event.center.y : event.clientY;
  1893. for (var i = 0; i < this.groupIds.length; i++) {
  1894. var groupId = this.groupIds[i];
  1895. var group = this.groups[groupId];
  1896. var foreground = group.dom.foreground;
  1897. var top = util.getAbsoluteTop(foreground);
  1898. if (clientY > top && clientY < top + foreground.offsetHeight) {
  1899. return group;
  1900. }
  1901. if (this.options.orientation.item === 'top') {
  1902. if (i === this.groupIds.length - 1 && clientY > top) {
  1903. return group;
  1904. }
  1905. }
  1906. else {
  1907. if (i === 0 && clientY < top + foreground.offset) {
  1908. return group;
  1909. }
  1910. }
  1911. }
  1912. return null;
  1913. };
  1914. /**
  1915. * Find the ItemSet from an event target:
  1916. * searches for the attribute 'timeline-itemset' in the event target's element tree
  1917. * @param {Event} event
  1918. * @return {ItemSet | null} item
  1919. */
  1920. ItemSet.itemSetFromTarget = function(event) {
  1921. var target = event.target;
  1922. while (target) {
  1923. if (target.hasOwnProperty('timeline-itemset')) {
  1924. return target['timeline-itemset'];
  1925. }
  1926. target = target.parentNode;
  1927. }
  1928. return null;
  1929. };
  1930. /**
  1931. * Clone the data of an item, and "normalize" it: convert the start and end date
  1932. * to the type (Date, Moment, ...) configured in the DataSet. If not configured,
  1933. * start and end are converted to Date.
  1934. * @param {Object} itemData, typically `item.data`
  1935. * @param {string} [type] Optional Date type. If not provided, the type from the DataSet is taken
  1936. * @return {Object} The cloned object
  1937. * @private
  1938. */
  1939. ItemSet.prototype._cloneItemData = function (itemData, type) {
  1940. var clone = util.extend({}, itemData);
  1941. if (!type) {
  1942. // convert start and end date to the type (Date, Moment, ...) configured in the DataSet
  1943. type = this.itemsData.getDataSet()._options.type;
  1944. }
  1945. if (clone.start != undefined) {
  1946. clone.start = util.convert(clone.start, type && type.start || 'Date');
  1947. }
  1948. if (clone.end != undefined) {
  1949. clone.end = util.convert(clone.end , type && type.end || 'Date');
  1950. }
  1951. return clone;
  1952. };
  1953. module.exports = ItemSet;