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.

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