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.

769 lines
23 KiB

9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var util = require('../../util');
  2. var stack = require('../Stack');
  3. /**
  4. * @param {number | string} groupId
  5. * @param {Object} data
  6. * @param {ItemSet} itemSet
  7. * @constructor Group
  8. */
  9. function Group (groupId, data, itemSet) {
  10. this.groupId = groupId;
  11. this.subgroups = {};
  12. this.subgroupIndex = 0;
  13. this.subgroupOrderer = data && data.subgroupOrder;
  14. this.itemSet = itemSet;
  15. this.isVisible = null;
  16. this.stackDirty = true; // if true, items will be restacked on next redraw
  17. if (data && data.nestedGroups) {
  18. this.nestedGroups = data.nestedGroups;
  19. if (data.showNested == false) {
  20. this.showNested = false;
  21. } else {
  22. this.showNested = true;
  23. }
  24. }
  25. this.nestedInGroup = null;
  26. this.dom = {};
  27. this.props = {
  28. label: {
  29. width: 0,
  30. height: 0
  31. }
  32. };
  33. this.className = null;
  34. this.items = {}; // items filtered by groupId of this group
  35. this.visibleItems = []; // items currently visible in window
  36. this.itemsInRange = []; // items currently in range
  37. this.orderedItems = {
  38. byStart: [],
  39. byEnd: []
  40. };
  41. this.checkRangedItems = false; // needed to refresh the ranged items if the window is programatically changed with NO overlap.
  42. var me = this;
  43. this.itemSet.body.emitter.on("checkRangedItems", function () {
  44. me.checkRangedItems = true;
  45. })
  46. this._create();
  47. this.setData(data);
  48. }
  49. /**
  50. * Create DOM elements for the group
  51. * @private
  52. */
  53. Group.prototype._create = function() {
  54. var label = document.createElement('div');
  55. if (this.itemSet.options.groupEditable.order) {
  56. label.className = 'vis-label draggable';
  57. } else {
  58. label.className = 'vis-label';
  59. }
  60. this.dom.label = label;
  61. var inner = document.createElement('div');
  62. inner.className = 'vis-inner';
  63. label.appendChild(inner);
  64. this.dom.inner = inner;
  65. var foreground = document.createElement('div');
  66. foreground.className = 'vis-group';
  67. foreground['timeline-group'] = this;
  68. this.dom.foreground = foreground;
  69. this.dom.background = document.createElement('div');
  70. this.dom.background.className = 'vis-group';
  71. this.dom.axis = document.createElement('div');
  72. this.dom.axis.className = 'vis-group';
  73. // create a hidden marker to detect when the Timelines container is attached
  74. // to the DOM, or the style of a parent of the Timeline is changed from
  75. // display:none is changed to visible.
  76. this.dom.marker = document.createElement('div');
  77. this.dom.marker.style.visibility = 'hidden';
  78. this.dom.marker.style.position = 'absolute';
  79. this.dom.marker.innerHTML = '';
  80. this.dom.background.appendChild(this.dom.marker);
  81. };
  82. /**
  83. * Set the group data for this group
  84. * @param {Object} data Group data, can contain properties content and className
  85. */
  86. Group.prototype.setData = function(data) {
  87. // update contents
  88. var content;
  89. var templateFunction;
  90. if (this.itemSet.options && this.itemSet.options.groupTemplate) {
  91. templateFunction = this.itemSet.options.groupTemplate.bind(this);
  92. content = templateFunction(data, this.dom.inner);
  93. } else {
  94. content = data && data.content;
  95. }
  96. if (content instanceof Element) {
  97. this.dom.inner.appendChild(content);
  98. while (this.dom.inner.firstChild) {
  99. this.dom.inner.removeChild(this.dom.inner.firstChild);
  100. }
  101. this.dom.inner.appendChild(content);
  102. } else if (content instanceof Object) {
  103. templateFunction(data, this.dom.inner);
  104. } else if (content !== undefined && content !== null) {
  105. this.dom.inner.innerHTML = content;
  106. } else {
  107. this.dom.inner.innerHTML = this.groupId || ''; // groupId can be null
  108. }
  109. // update title
  110. this.dom.label.title = data && data.title || '';
  111. if (!this.dom.inner.firstChild) {
  112. util.addClassName(this.dom.inner, 'vis-hidden');
  113. }
  114. else {
  115. util.removeClassName(this.dom.inner, 'vis-hidden');
  116. }
  117. if (data && data.nestedGroups) {
  118. if (!this.nestedGroups || this.nestedGroups != data.nestedGroups) {
  119. this.nestedGroups = data.nestedGroups;
  120. }
  121. if (data.showNested !== undefined || this.showNested === undefined) {
  122. if (data.showNested == false) {
  123. this.showNested = false;
  124. } else {
  125. this.showNested = true;
  126. }
  127. }
  128. util.addClassName(this.dom.label, 'vis-nesting-group');
  129. var collapsedDirClassName = this.itemSet.options.rtl ? 'collapsed-rtl' : 'collapsed'
  130. if (this.showNested) {
  131. util.removeClassName(this.dom.label, collapsedDirClassName);
  132. util.addClassName(this.dom.label, 'expanded');
  133. } else {
  134. util.removeClassName(this.dom.label, 'expanded');
  135. util.addClassName(this.dom.label, collapsedDirClassName);
  136. }
  137. } else if (this.nestedGroups) {
  138. this.nestedGroups = null;
  139. collapsedDirClassName = this.itemSet.options.rtl ? 'collapsed-rtl' : 'collapsed'
  140. util.removeClassName(this.dom.label, collapsedDirClassName);
  141. util.removeClassName(this.dom.label, 'expanded');
  142. util.removeClassName(this.dom.label, 'vis-nesting-group');
  143. }
  144. if (data && data.nestedInGroup) {
  145. util.addClassName(this.dom.label, 'vis-nested-group');
  146. if (this.itemSet.options && this.itemSet.options.rtl) {
  147. this.dom.inner.style.paddingRight = '30px';
  148. } else {
  149. this.dom.inner.style.paddingLeft = '30px';
  150. }
  151. }
  152. // update className
  153. var className = data && data.className || null;
  154. if (className != this.className) {
  155. if (this.className) {
  156. util.removeClassName(this.dom.label, this.className);
  157. util.removeClassName(this.dom.foreground, this.className);
  158. util.removeClassName(this.dom.background, this.className);
  159. util.removeClassName(this.dom.axis, this.className);
  160. }
  161. util.addClassName(this.dom.label, className);
  162. util.addClassName(this.dom.foreground, className);
  163. util.addClassName(this.dom.background, className);
  164. util.addClassName(this.dom.axis, className);
  165. this.className = className;
  166. }
  167. // update style
  168. if (this.style) {
  169. util.removeCssText(this.dom.label, this.style);
  170. this.style = null;
  171. }
  172. if (data && data.style) {
  173. util.addCssText(this.dom.label, data.style);
  174. this.style = data.style;
  175. }
  176. };
  177. /**
  178. * Get the width of the group label
  179. * @return {number} width
  180. */
  181. Group.prototype.getLabelWidth = function() {
  182. return this.props.label.width;
  183. };
  184. /**
  185. * Repaint this group
  186. * @param {{start: number, end: number}} range
  187. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  188. * @param {boolean} [forceRestack=false] Force restacking of all items
  189. * @return {boolean} Returns true if the group is resized
  190. */
  191. Group.prototype.redraw = function(range, margin, forceRestack) {
  192. var resized = false;
  193. // force recalculation of the height of the items when the marker height changed
  194. // (due to the Timeline being attached to the DOM or changed from display:none to visible)
  195. var markerHeight = this.dom.marker.clientHeight;
  196. if (markerHeight != this.lastMarkerHeight) {
  197. this.lastMarkerHeight = markerHeight;
  198. util.forEach(this.items, function (item) {
  199. item.dirty = true;
  200. if (item.displayed) item.redraw();
  201. });
  202. forceRestack = true;
  203. }
  204. // recalculate the height of the subgroups
  205. this._calculateSubGroupHeights(margin);
  206. // calculate actual size and position
  207. var foreground = this.dom.foreground;
  208. this.top = foreground.offsetTop;
  209. this.right = foreground.offsetLeft;
  210. this.width = foreground.offsetWidth;
  211. var lastIsVisible = this.isVisible;
  212. this.isVisible = this._isGroupVisible(range, margin);
  213. var restack = forceRestack || this.stackDirty || (this.isVisible && !lastIsVisible);
  214. this._updateSubgroupsSizes();
  215. // if restacking, reposition visible items vertically
  216. if(restack) {
  217. if (typeof this.itemSet.options.order === 'function') {
  218. // a custom order function
  219. // brute force restack of all items
  220. // show all items
  221. var me = this;
  222. var limitSize = false;
  223. util.forEach(this.items, function (item) {
  224. if (!item.dom) { // If this item has never been displayed then the dom property will not be defined, this means we need to measure the size
  225. item.redraw();
  226. me.visibleItems.push(item);
  227. }
  228. item.repositionX(limitSize);
  229. });
  230. // order all items and force a restacking
  231. var customOrderedItems = this.orderedItems.byStart.slice().sort(function (a, b) {
  232. return me.itemSet.options.order(a.data, b.data);
  233. });
  234. stack.stack(customOrderedItems, margin, true /* restack=true */);
  235. this.visibleItems = this._updateItemsInRange(this.orderedItems, this.visibleItems, range);
  236. }
  237. else {
  238. // no custom order function, lazy stacking
  239. this.visibleItems = this._updateItemsInRange(this.orderedItems, this.visibleItems, range);
  240. if (this.itemSet.options.stack) { // TODO: ugly way to access options...
  241. stack.stack(this.visibleItems, margin, true /* restack=true */);
  242. }
  243. else { // no stacking
  244. stack.nostack(this.visibleItems, margin, this.subgroups, this.itemSet.options.stackSubgroups);
  245. }
  246. }
  247. this.stackDirty = false;
  248. }
  249. // recalculate the height of the group
  250. var height = this._calculateHeight(margin);
  251. // calculate actual size and position
  252. foreground = this.dom.foreground;
  253. this.top = foreground.offsetTop;
  254. this.right = foreground.offsetLeft;
  255. this.width = foreground.offsetWidth;
  256. resized = util.updateProperty(this, 'height', height) || resized;
  257. // recalculate size of label
  258. resized = util.updateProperty(this.props.label, 'width', this.dom.inner.clientWidth) || resized;
  259. resized = util.updateProperty(this.props.label, 'height', this.dom.inner.clientHeight) || resized;
  260. // apply new height
  261. this.dom.background.style.height = height + 'px';
  262. this.dom.foreground.style.height = height + 'px';
  263. this.dom.label.style.height = height + 'px';
  264. // update vertical position of items after they are re-stacked and the height of the group is calculated
  265. for (var i = 0, ii = this.visibleItems.length; i < ii; i++) {
  266. var item = this.visibleItems[i];
  267. item.repositionY(margin);
  268. if (!this.isVisible && this.groupId != "__background__") {
  269. if (item.displayed) item.hide();
  270. }
  271. }
  272. if (!this.isVisible && this.height) {
  273. return resized = false;
  274. }
  275. return resized;
  276. };
  277. /**
  278. * recalculate the height of the subgroups
  279. *
  280. * @param {{item: vis.Item}} margin
  281. * @private
  282. */
  283. Group.prototype._calculateSubGroupHeights = function (margin) {
  284. if (Object.keys(this.subgroups).length > 0) {
  285. var me = this;
  286. this.resetSubgroups();
  287. util.forEach(this.visibleItems, function (item) {
  288. if (item.data.subgroup !== undefined) {
  289. me.subgroups[item.data.subgroup].height = Math.max(me.subgroups[item.data.subgroup].height, item.height + margin.item.vertical);
  290. me.subgroups[item.data.subgroup].visible = true;
  291. }
  292. });
  293. }
  294. };
  295. /**
  296. * check if group is visible
  297. *
  298. * @param {vis.Range} range
  299. * @param {{axis: vis.DataAxis}} margin
  300. * @returns {boolean} is visible
  301. * @private
  302. */
  303. Group.prototype._isGroupVisible = function (range, margin) {
  304. return (this.top <= range.body.domProps.centerContainer.height - range.body.domProps.scrollTop + margin.axis)
  305. && (this.top + this.height + margin.axis >= - range.body.domProps.scrollTop);
  306. };
  307. /**
  308. * recalculate the height of the group
  309. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  310. * @returns {number} Returns the height
  311. * @private
  312. */
  313. Group.prototype._calculateHeight = function (margin) {
  314. // recalculate the height of the group
  315. var height;
  316. var itemsInRange = this.visibleItems;
  317. if (itemsInRange.length > 0) {
  318. var min = itemsInRange[0].top;
  319. var max = itemsInRange[0].top + itemsInRange[0].height;
  320. util.forEach(itemsInRange, function (item) {
  321. min = Math.min(min, item.top);
  322. max = Math.max(max, (item.top + item.height));
  323. });
  324. if (min > margin.axis) {
  325. // there is an empty gap between the lowest item and the axis
  326. var offset = min - margin.axis;
  327. max -= offset;
  328. util.forEach(itemsInRange, function (item) {
  329. item.top -= offset;
  330. });
  331. }
  332. height = max + margin.item.vertical / 2;
  333. }
  334. else {
  335. height = 0;
  336. }
  337. height = Math.max(height, this.props.label.height);
  338. return height;
  339. };
  340. /**
  341. * Show this group: attach to the DOM
  342. */
  343. Group.prototype.show = function() {
  344. if (!this.dom.label.parentNode) {
  345. this.itemSet.dom.labelSet.appendChild(this.dom.label);
  346. }
  347. if (!this.dom.foreground.parentNode) {
  348. this.itemSet.dom.foreground.appendChild(this.dom.foreground);
  349. }
  350. if (!this.dom.background.parentNode) {
  351. this.itemSet.dom.background.appendChild(this.dom.background);
  352. }
  353. if (!this.dom.axis.parentNode) {
  354. this.itemSet.dom.axis.appendChild(this.dom.axis);
  355. }
  356. };
  357. /**
  358. * Hide this group: remove from the DOM
  359. */
  360. Group.prototype.hide = function() {
  361. var label = this.dom.label;
  362. if (label.parentNode) {
  363. label.parentNode.removeChild(label);
  364. }
  365. var foreground = this.dom.foreground;
  366. if (foreground.parentNode) {
  367. foreground.parentNode.removeChild(foreground);
  368. }
  369. var background = this.dom.background;
  370. if (background.parentNode) {
  371. background.parentNode.removeChild(background);
  372. }
  373. var axis = this.dom.axis;
  374. if (axis.parentNode) {
  375. axis.parentNode.removeChild(axis);
  376. }
  377. };
  378. /**
  379. * Add an item to the group
  380. * @param {Item} item
  381. */
  382. Group.prototype.add = function(item) {
  383. this.items[item.id] = item;
  384. item.setParent(this);
  385. this.stackDirty = true;
  386. // add to
  387. if (item.data.subgroup !== undefined) {
  388. this._addToSubgroup(item);
  389. this.orderSubgroups();
  390. }
  391. if (this.visibleItems.indexOf(item) == -1) {
  392. var range = this.itemSet.body.range; // TODO: not nice accessing the range like this
  393. this._checkIfVisible(item, this.visibleItems, range);
  394. }
  395. };
  396. Group.prototype._addToSubgroup = function(item, subgroupId) {
  397. subgroupId = subgroupId || item.data.subgroup;
  398. if (subgroupId != undefined && this.subgroups[subgroupId] === undefined) {
  399. this.subgroups[subgroupId] = {
  400. height:0,
  401. top: 0,
  402. start: item.data.start,
  403. end: item.data.end,
  404. visible: false,
  405. index:this.subgroupIndex,
  406. items: []
  407. };
  408. this.subgroupIndex++;
  409. }
  410. if (new Date(item.data.start) < new Date(this.subgroups[subgroupId].start)) {
  411. this.subgroups[subgroupId].start = item.data.start;
  412. }
  413. if (new Date(item.data.end) > new Date(this.subgroups[subgroupId].end)) {
  414. this.subgroups[subgroupId].end = item.data.end;
  415. }
  416. this.subgroups[subgroupId].items.push(item);
  417. };
  418. Group.prototype._updateSubgroupsSizes = function () {
  419. var me = this;
  420. if (me.subgroups) {
  421. for (var subgroup in me.subgroups) {
  422. var newStart = me.subgroups[subgroup].items[0].data.start;
  423. var newEnd = me.subgroups[subgroup].items[0].data.end - 1;
  424. me.subgroups[subgroup].items.forEach(function(item) {
  425. if (new Date(item.data.start) < new Date(newStart)) {
  426. newStart = item.data.start;
  427. }
  428. if (new Date(item.data.end) > new Date(newEnd)) {
  429. newEnd = item.data.end;
  430. }
  431. })
  432. me.subgroups[subgroup].start = newStart;
  433. me.subgroups[subgroup].end = new Date(newEnd - 1) // -1 to compensate for colliding end to start subgroups;
  434. }
  435. }
  436. }
  437. Group.prototype.orderSubgroups = function() {
  438. if (this.subgroupOrderer !== undefined) {
  439. var sortArray = [];
  440. var subgroup;
  441. if (typeof this.subgroupOrderer == 'string') {
  442. for (subgroup in this.subgroups) {
  443. sortArray.push({subgroup: subgroup, sortField: this.subgroups[subgroup].items[0].data[this.subgroupOrderer]})
  444. }
  445. sortArray.sort(function (a, b) {
  446. return a.sortField - b.sortField;
  447. })
  448. }
  449. else if (typeof this.subgroupOrderer == 'function') {
  450. for (subgroup in this.subgroups) {
  451. sortArray.push(this.subgroups[subgroup].items[0].data);
  452. }
  453. sortArray.sort(this.subgroupOrderer);
  454. }
  455. if (sortArray.length > 0) {
  456. for (var i = 0; i < sortArray.length; i++) {
  457. this.subgroups[sortArray[i].subgroup].index = i;
  458. }
  459. }
  460. }
  461. };
  462. Group.prototype.resetSubgroups = function() {
  463. for (var subgroup in this.subgroups) {
  464. if (this.subgroups.hasOwnProperty(subgroup)) {
  465. this.subgroups[subgroup].visible = false;
  466. this.subgroups[subgroup].height = 0;
  467. }
  468. }
  469. };
  470. /**
  471. * Remove an item from the group
  472. * @param {Item} item
  473. */
  474. Group.prototype.remove = function(item) {
  475. delete this.items[item.id];
  476. item.setParent(null);
  477. this.stackDirty = true;
  478. // remove from visible items
  479. var index = this.visibleItems.indexOf(item);
  480. if (index != -1) this.visibleItems.splice(index, 1);
  481. if(item.data.subgroup !== undefined){
  482. this._removeFromSubgroup(item);
  483. this.orderSubgroups();
  484. }
  485. };
  486. Group.prototype._removeFromSubgroup = function(item, subgroupId) {
  487. subgroupId = subgroupId || item.data.subgroup;
  488. if (subgroupId != undefined) {
  489. var subgroup = this.subgroups[subgroupId];
  490. if (subgroup){
  491. var itemIndex = subgroup.items.indexOf(item);
  492. // Check the item is actually in this subgroup. How should items not in the group be handled?
  493. if (itemIndex >= 0) {
  494. subgroup.items.splice(itemIndex,1);
  495. if (!subgroup.items.length){
  496. delete this.subgroups[subgroupId];
  497. } else {
  498. this._updateSubgroupsSizes();
  499. }
  500. }
  501. }
  502. }
  503. };
  504. /**
  505. * Remove an item from the corresponding DataSet
  506. * @param {Item} item
  507. */
  508. Group.prototype.removeFromDataSet = function(item) {
  509. this.itemSet.removeItem(item.id);
  510. };
  511. /**
  512. * Reorder the items
  513. */
  514. Group.prototype.order = function() {
  515. var array = util.toArray(this.items);
  516. var startArray = [];
  517. var endArray = [];
  518. for (var i = 0; i < array.length; i++) {
  519. if (array[i].data.end !== undefined) {
  520. endArray.push(array[i]);
  521. }
  522. startArray.push(array[i]);
  523. }
  524. this.orderedItems = {
  525. byStart: startArray,
  526. byEnd: endArray
  527. };
  528. stack.orderByStart(this.orderedItems.byStart);
  529. stack.orderByEnd(this.orderedItems.byEnd);
  530. };
  531. /**
  532. * Update the visible items
  533. * @param {{byStart: Item[], byEnd: Item[]}} orderedItems All items ordered by start date and by end date
  534. * @param {Item[]} oldVisibleItems The previously visible items.
  535. * @param {{start: number, end: number}} range Visible range
  536. * @return {Item[]} visibleItems The new visible items.
  537. * @private
  538. */
  539. Group.prototype._updateItemsInRange = function(orderedItems, oldVisibleItems, range) {
  540. var visibleItems = [];
  541. var visibleItemsLookup = {}; // we keep this to quickly look up if an item already exists in the list without using indexOf on visibleItems
  542. var interval = (range.end - range.start) / 4;
  543. var lowerBound = range.start - interval;
  544. var upperBound = range.end + interval;
  545. // this function is used to do the binary search.
  546. var searchFunction = function (value) {
  547. if (value < lowerBound) {return -1;}
  548. else if (value <= upperBound) {return 0;}
  549. else {return 1;}
  550. };
  551. // first check if the items that were in view previously are still in view.
  552. // IMPORTANT: this handles the case for the items with startdate before the window and enddate after the window!
  553. // also cleans up invisible items.
  554. if (oldVisibleItems.length > 0) {
  555. for (var i = 0; i < oldVisibleItems.length; i++) {
  556. this._checkIfVisibleWithReference(oldVisibleItems[i], visibleItems, visibleItemsLookup, range);
  557. }
  558. }
  559. // we do a binary search for the items that have only start values.
  560. var initialPosByStart = util.binarySearchCustom(orderedItems.byStart, searchFunction, 'data','start');
  561. // trace the visible items from the inital start pos both ways until an invisible item is found, we only look at the start values.
  562. this._traceVisible(initialPosByStart, orderedItems.byStart, visibleItems, visibleItemsLookup, function (item) {
  563. return (item.data.start < lowerBound || item.data.start > upperBound);
  564. });
  565. // if the window has changed programmatically without overlapping the old window, the ranged items with start < lowerBound and end > upperbound are not shown.
  566. // We therefore have to brute force check all items in the byEnd list
  567. if (this.checkRangedItems == true) {
  568. this.checkRangedItems = false;
  569. for (i = 0; i < orderedItems.byEnd.length; i++) {
  570. this._checkIfVisibleWithReference(orderedItems.byEnd[i], visibleItems, visibleItemsLookup, range);
  571. }
  572. }
  573. else {
  574. // we do a binary search for the items that have defined end times.
  575. var initialPosByEnd = util.binarySearchCustom(orderedItems.byEnd, searchFunction, 'data','end');
  576. // trace the visible items from the inital start pos both ways until an invisible item is found, we only look at the end values.
  577. this._traceVisible(initialPosByEnd, orderedItems.byEnd, visibleItems, visibleItemsLookup, function (item) {
  578. return (item.data.end < lowerBound || item.data.end > upperBound);
  579. });
  580. }
  581. // finally, we reposition all the visible items.
  582. for (i = 0; i < visibleItems.length; i++) {
  583. var item = visibleItems[i];
  584. if (!item.displayed) item.show();
  585. // reposition item horizontally
  586. item.repositionX();
  587. }
  588. return visibleItems;
  589. };
  590. Group.prototype._traceVisible = function (initialPos, items, visibleItems, visibleItemsLookup, breakCondition) {
  591. if (initialPos != -1) {
  592. var i, item;
  593. for (i = initialPos; i >= 0; i--) {
  594. item = items[i];
  595. if (breakCondition(item)) {
  596. break;
  597. }
  598. else {
  599. if (visibleItemsLookup[item.id] === undefined) {
  600. visibleItemsLookup[item.id] = true;
  601. visibleItems.push(item);
  602. }
  603. }
  604. }
  605. for (i = initialPos + 1; i < items.length; i++) {
  606. item = items[i];
  607. if (breakCondition(item)) {
  608. break;
  609. }
  610. else {
  611. if (visibleItemsLookup[item.id] === undefined) {
  612. visibleItemsLookup[item.id] = true;
  613. visibleItems.push(item);
  614. }
  615. }
  616. }
  617. }
  618. }
  619. /**
  620. * this function is very similar to the _checkIfInvisible() but it does not
  621. * return booleans, hides the item if it should not be seen and always adds to
  622. * the visibleItems.
  623. * this one is for brute forcing and hiding.
  624. *
  625. * @param {Item} item
  626. * @param {Array} visibleItems
  627. * @param {{start:number, end:number}} range
  628. * @private
  629. */
  630. Group.prototype._checkIfVisible = function(item, visibleItems, range) {
  631. if (item.isVisible(range)) {
  632. if (!item.displayed) item.show();
  633. // reposition item horizontally
  634. item.repositionX();
  635. visibleItems.push(item);
  636. }
  637. else {
  638. if (item.displayed) item.hide();
  639. }
  640. };
  641. /**
  642. * this function is very similar to the _checkIfInvisible() but it does not
  643. * return booleans, hides the item if it should not be seen and always adds to
  644. * the visibleItems.
  645. * this one is for brute forcing and hiding.
  646. *
  647. * @param {Item} item
  648. * @param {Array.<vis.Item>} visibleItems
  649. * @param {Object<number, boolean>} visibleItemsLookup
  650. * @param {{start:number, end:number}} range
  651. * @private
  652. */
  653. Group.prototype._checkIfVisibleWithReference = function(item, visibleItems, visibleItemsLookup, range) {
  654. if (item.isVisible(range)) {
  655. if (visibleItemsLookup[item.id] === undefined) {
  656. visibleItemsLookup[item.id] = true;
  657. visibleItems.push(item);
  658. }
  659. }
  660. else {
  661. if (item.displayed) item.hide();
  662. }
  663. };
  664. Group.prototype.changeSubgroup = function(item, oldSubgroup, newSubgroup) {
  665. this._removeFromSubgroup(item, oldSubgroup);
  666. this._addToSubgroup(item, newSubgroup);
  667. this.orderSubgroups();
  668. };
  669. module.exports = Group;