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.

826 lines
25 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. Group.prototype._didMarkerHeightChange = function() {
  185. var markerHeight = this.dom.marker.clientHeight;
  186. if (markerHeight != this.lastMarkerHeight) {
  187. this.lastMarkerHeight = markerHeight;
  188. util.forEach(this.items, function (item) {
  189. item.dirty = true;
  190. if (item.displayed) item.redraw();
  191. });
  192. return true;
  193. }
  194. }
  195. Group.prototype._calculateGroupSizeAndPosition = function() {
  196. var foreground = this.dom.foreground;
  197. this.top = foreground.offsetTop;
  198. this.right = foreground.offsetLeft;
  199. this.width = foreground.offsetWidth;
  200. }
  201. Group.prototype._redrawItems = function(forceRestack, lastIsVisible, margin, range) {
  202. var restack = forceRestack || this.stackDirty || this.isVisible && !lastIsVisible;
  203. // if restacking, reposition visible items vertically
  204. if (restack) {
  205. if (typeof this.itemSet.options.order === 'function') {
  206. // a custom order function
  207. // brute force restack of all items
  208. // show all items
  209. var me = this;
  210. var limitSize = false;
  211. util.forEach(this.items, function (item) {
  212. if (!item.displayed) {
  213. item.redraw();
  214. me.visibleItems.push(item);
  215. }
  216. item.repositionX(limitSize);
  217. });
  218. // order all items and force a restacking
  219. var customOrderedItems = this.orderedItems.byStart.slice().sort(function (a, b) {
  220. return me.itemSet.options.order(a.data, b.data);
  221. });
  222. stack.stack(customOrderedItems, margin, true /* restack=true */);
  223. this.visibleItems = this._updateItemsInRange(this.orderedItems, this.visibleItems, range);
  224. } else {
  225. // no custom order function, lazy stacking
  226. this.visibleItems = this._updateItemsInRange(this.orderedItems, this.visibleItems, range);
  227. if (this.itemSet.options.stack) {
  228. // TODO: ugly way to access options...
  229. stack.stack(this.visibleItems, margin, true /* restack=true */);
  230. } else {
  231. // no stacking
  232. stack.nostack(this.visibleItems, margin, this.subgroups, this.itemSet.options.stackSubgroups);
  233. }
  234. }
  235. this.stackDirty = false;
  236. }
  237. }
  238. Group.prototype._didResize = function(resized, height) {
  239. resized = util.updateProperty(this, 'height', height) || resized;
  240. // recalculate size of label
  241. var labelWidth = this.dom.inner.clientWidth;
  242. var labelHeight = this.dom.inner.clientHeight;
  243. resized = util.updateProperty(this.props.label, 'width', labelWidth) || resized;
  244. resized = util.updateProperty(this.props.label, 'height', labelHeight) || resized;
  245. return resized;
  246. }
  247. Group.prototype._applyGroupHeight = function(height) {
  248. this.dom.background.style.height = height + 'px';
  249. this.dom.foreground.style.height = height + 'px';
  250. this.dom.label.style.height = height + 'px';
  251. }
  252. // update vertical position of items after they are re-stacked and the height of the group is calculated
  253. Group.prototype._updateItemsVerticalPosition = function(resized, margin) {
  254. for (var i = 0, ii = this.visibleItems.length; i < ii; i++) {
  255. var item = this.visibleItems[i];
  256. item.repositionY(margin);
  257. if (!this.isVisible && this.groupId != "__background__") {
  258. if (item.displayed) item.hide();
  259. }
  260. }
  261. if (!this.isVisible && this.height) {
  262. return false;
  263. }
  264. return resized;
  265. }
  266. /**
  267. * Repaint this group
  268. * @param {{start: number, end: number}} range
  269. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  270. * @param {boolean} [forceRestack=false] Force restacking of all items
  271. * @param {boolean} [returnQueue=false] return the queue or if the group resized
  272. * @return {boolean} Returns true if the group is resized or the redraw queue if returnQueue=true
  273. */
  274. Group.prototype.redraw = function(range, margin, forceRestack, returnQueue) {
  275. var resized = false;
  276. var lastIsVisible = this.isVisible;
  277. var height;
  278. var queue = [
  279. // force recalculation of the height of the items when the marker height changed
  280. // (due to the Timeline being attached to the DOM or changed from display:none to visible)
  281. (function () {
  282. forceRestack = this._didMarkerHeightChange.bind(this);
  283. }).bind(this),
  284. // recalculate the height of the subgroups
  285. this._updateSubGroupHeights.bind(this, margin),
  286. // calculate actual size and position
  287. this._calculateGroupSizeAndPosition.bind(this),
  288. // check if group is visible
  289. (function() {
  290. this.isVisible = this._isGroupVisible.bind(this)(range, margin);
  291. }).bind(this),
  292. // redraw Items if needed
  293. (function() {
  294. this._redrawItems.bind(this)(forceRestack, lastIsVisible, margin, range)
  295. }).bind(this),
  296. // update subgroups
  297. this._updateSubgroupsSizes.bind(this),
  298. // recalculate the height of the group
  299. (function() {
  300. height = this._calculateHeight.bind(this)(margin);
  301. }).bind(this),
  302. // calculate actual size and position again
  303. this._calculateGroupSizeAndPosition.bind(this),
  304. // check if resized
  305. (function() {
  306. resized = this._didResize.bind(this)(resized, height)
  307. }).bind(this),
  308. // apply group height
  309. (function() {
  310. this._applyGroupHeight.bind(this)(height)
  311. }).bind(this),
  312. // update vertical position of items after they are re-stacked and the height of the group is calculated
  313. (function() {
  314. return resized = this._updateItemsVerticalPosition.bind(this)(resized, margin)
  315. }).bind(this)
  316. ]
  317. if (returnQueue) {
  318. return queue;
  319. } else {
  320. var result;
  321. queue.forEach(function (fn) {
  322. result = fn();
  323. });
  324. return result;
  325. }
  326. };
  327. /**
  328. * recalculate the height of the subgroups
  329. *
  330. * @param {{item: vis.Item}} margin
  331. * @private
  332. */
  333. Group.prototype._updateSubGroupHeights = function (margin) {
  334. if (Object.keys(this.subgroups).length > 0) {
  335. var me = this;
  336. this.resetSubgroups();
  337. util.forEach(this.visibleItems, function (item) {
  338. if (item.data.subgroup !== undefined) {
  339. me.subgroups[item.data.subgroup].height = Math.max(me.subgroups[item.data.subgroup].height, item.height + margin.item.vertical);
  340. me.subgroups[item.data.subgroup].visible = true;
  341. }
  342. });
  343. }
  344. };
  345. /**
  346. * check if group is visible
  347. *
  348. * @param {vis.Range} range
  349. * @param {{axis: vis.DataAxis}} margin
  350. * @returns {boolean} is visible
  351. * @private
  352. */
  353. Group.prototype._isGroupVisible = function (range, margin) {
  354. return (this.top <= range.body.domProps.centerContainer.height - range.body.domProps.scrollTop + margin.axis)
  355. && (this.top + this.height + margin.axis >= - range.body.domProps.scrollTop);
  356. };
  357. /**
  358. * recalculate the height of the group
  359. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  360. * @returns {number} Returns the height
  361. * @private
  362. */
  363. Group.prototype._calculateHeight = function (margin) {
  364. // recalculate the height of the group
  365. var height;
  366. var itemsInRange = this.visibleItems;
  367. if (itemsInRange.length > 0) {
  368. var min = itemsInRange[0].top;
  369. var max = itemsInRange[0].top + itemsInRange[0].height;
  370. util.forEach(itemsInRange, function (item) {
  371. min = Math.min(min, item.top);
  372. max = Math.max(max, (item.top + item.height));
  373. });
  374. if (min > margin.axis) {
  375. // there is an empty gap between the lowest item and the axis
  376. var offset = min - margin.axis;
  377. max -= offset;
  378. util.forEach(itemsInRange, function (item) {
  379. item.top -= offset;
  380. });
  381. }
  382. height = max + margin.item.vertical / 2;
  383. }
  384. else {
  385. height = 0;
  386. }
  387. height = Math.max(height, this.props.label.height);
  388. return height;
  389. };
  390. /**
  391. * Show this group: attach to the DOM
  392. */
  393. Group.prototype.show = function() {
  394. if (!this.dom.label.parentNode) {
  395. this.itemSet.dom.labelSet.appendChild(this.dom.label);
  396. }
  397. if (!this.dom.foreground.parentNode) {
  398. this.itemSet.dom.foreground.appendChild(this.dom.foreground);
  399. }
  400. if (!this.dom.background.parentNode) {
  401. this.itemSet.dom.background.appendChild(this.dom.background);
  402. }
  403. if (!this.dom.axis.parentNode) {
  404. this.itemSet.dom.axis.appendChild(this.dom.axis);
  405. }
  406. };
  407. /**
  408. * Hide this group: remove from the DOM
  409. */
  410. Group.prototype.hide = function() {
  411. var label = this.dom.label;
  412. if (label.parentNode) {
  413. label.parentNode.removeChild(label);
  414. }
  415. var foreground = this.dom.foreground;
  416. if (foreground.parentNode) {
  417. foreground.parentNode.removeChild(foreground);
  418. }
  419. var background = this.dom.background;
  420. if (background.parentNode) {
  421. background.parentNode.removeChild(background);
  422. }
  423. var axis = this.dom.axis;
  424. if (axis.parentNode) {
  425. axis.parentNode.removeChild(axis);
  426. }
  427. };
  428. /**
  429. * Add an item to the group
  430. * @param {Item} item
  431. */
  432. Group.prototype.add = function(item) {
  433. this.items[item.id] = item;
  434. item.setParent(this);
  435. this.stackDirty = true;
  436. // add to
  437. if (item.data.subgroup !== undefined) {
  438. this._addToSubgroup(item);
  439. this.orderSubgroups();
  440. }
  441. if (this.visibleItems.indexOf(item) == -1) {
  442. var range = this.itemSet.body.range; // TODO: not nice accessing the range like this
  443. this._checkIfVisible(item, this.visibleItems, range);
  444. }
  445. };
  446. Group.prototype._addToSubgroup = function(item, subgroupId) {
  447. subgroupId = subgroupId || item.data.subgroup;
  448. if (subgroupId != undefined && this.subgroups[subgroupId] === undefined) {
  449. this.subgroups[subgroupId] = {
  450. height:0,
  451. top: 0,
  452. start: item.data.start,
  453. end: item.data.end,
  454. visible: false,
  455. index:this.subgroupIndex,
  456. items: []
  457. };
  458. this.subgroupIndex++;
  459. }
  460. if (new Date(item.data.start) < new Date(this.subgroups[subgroupId].start)) {
  461. this.subgroups[subgroupId].start = item.data.start;
  462. }
  463. if (new Date(item.data.end) > new Date(this.subgroups[subgroupId].end)) {
  464. this.subgroups[subgroupId].end = item.data.end;
  465. }
  466. this.subgroups[subgroupId].items.push(item);
  467. };
  468. Group.prototype._updateSubgroupsSizes = function () {
  469. var me = this;
  470. if (me.subgroups) {
  471. for (var subgroup in me.subgroups) {
  472. var newStart = me.subgroups[subgroup].items[0].data.start;
  473. var newEnd = me.subgroups[subgroup].items[0].data.end - 1;
  474. me.subgroups[subgroup].items.forEach(function(item) {
  475. if (new Date(item.data.start) < new Date(newStart)) {
  476. newStart = item.data.start;
  477. }
  478. if (new Date(item.data.end) > new Date(newEnd)) {
  479. newEnd = item.data.end;
  480. }
  481. })
  482. me.subgroups[subgroup].start = newStart;
  483. me.subgroups[subgroup].end = new Date(newEnd - 1) // -1 to compensate for colliding end to start subgroups;
  484. }
  485. }
  486. }
  487. Group.prototype.orderSubgroups = function() {
  488. if (this.subgroupOrderer !== undefined) {
  489. var sortArray = [];
  490. var subgroup;
  491. if (typeof this.subgroupOrderer == 'string') {
  492. for (subgroup in this.subgroups) {
  493. sortArray.push({subgroup: subgroup, sortField: this.subgroups[subgroup].items[0].data[this.subgroupOrderer]})
  494. }
  495. sortArray.sort(function (a, b) {
  496. return a.sortField - b.sortField;
  497. })
  498. }
  499. else if (typeof this.subgroupOrderer == 'function') {
  500. for (subgroup in this.subgroups) {
  501. sortArray.push(this.subgroups[subgroup].items[0].data);
  502. }
  503. sortArray.sort(this.subgroupOrderer);
  504. }
  505. if (sortArray.length > 0) {
  506. for (var i = 0; i < sortArray.length; i++) {
  507. this.subgroups[sortArray[i].subgroup].index = i;
  508. }
  509. }
  510. }
  511. };
  512. Group.prototype.resetSubgroups = function() {
  513. for (var subgroup in this.subgroups) {
  514. if (this.subgroups.hasOwnProperty(subgroup)) {
  515. this.subgroups[subgroup].visible = false;
  516. this.subgroups[subgroup].height = 0;
  517. }
  518. }
  519. };
  520. /**
  521. * Remove an item from the group
  522. * @param {Item} item
  523. */
  524. Group.prototype.remove = function(item) {
  525. delete this.items[item.id];
  526. item.setParent(null);
  527. this.stackDirty = true;
  528. // remove from visible items
  529. var index = this.visibleItems.indexOf(item);
  530. if (index != -1) this.visibleItems.splice(index, 1);
  531. if(item.data.subgroup !== undefined){
  532. this._removeFromSubgroup(item);
  533. this.orderSubgroups();
  534. }
  535. };
  536. Group.prototype._removeFromSubgroup = function(item, subgroupId) {
  537. subgroupId = subgroupId || item.data.subgroup;
  538. if (subgroupId != undefined) {
  539. var subgroup = this.subgroups[subgroupId];
  540. if (subgroup){
  541. var itemIndex = subgroup.items.indexOf(item);
  542. // Check the item is actually in this subgroup. How should items not in the group be handled?
  543. if (itemIndex >= 0) {
  544. subgroup.items.splice(itemIndex,1);
  545. if (!subgroup.items.length){
  546. delete this.subgroups[subgroupId];
  547. } else {
  548. this._updateSubgroupsSizes();
  549. }
  550. }
  551. }
  552. }
  553. };
  554. /**
  555. * Remove an item from the corresponding DataSet
  556. * @param {Item} item
  557. */
  558. Group.prototype.removeFromDataSet = function(item) {
  559. this.itemSet.removeItem(item.id);
  560. };
  561. /**
  562. * Reorder the items
  563. */
  564. Group.prototype.order = function() {
  565. var array = util.toArray(this.items);
  566. var startArray = [];
  567. var endArray = [];
  568. for (var i = 0; i < array.length; i++) {
  569. if (array[i].data.end !== undefined) {
  570. endArray.push(array[i]);
  571. }
  572. startArray.push(array[i]);
  573. }
  574. this.orderedItems = {
  575. byStart: startArray,
  576. byEnd: endArray
  577. };
  578. stack.orderByStart(this.orderedItems.byStart);
  579. stack.orderByEnd(this.orderedItems.byEnd);
  580. };
  581. /**
  582. * Update the visible items
  583. * @param {{byStart: Item[], byEnd: Item[]}} orderedItems All items ordered by start date and by end date
  584. * @param {Item[]} oldVisibleItems The previously visible items.
  585. * @param {{start: number, end: number}} range Visible range
  586. * @return {Item[]} visibleItems The new visible items.
  587. * @private
  588. */
  589. Group.prototype._updateItemsInRange = function(orderedItems, oldVisibleItems, range) {
  590. var visibleItems = [];
  591. var visibleItemsLookup = {}; // we keep this to quickly look up if an item already exists in the list without using indexOf on visibleItems
  592. var interval = (range.end - range.start) / 4;
  593. var lowerBound = range.start - interval;
  594. var upperBound = range.end + interval;
  595. // this function is used to do the binary search.
  596. var searchFunction = function (value) {
  597. if (value < lowerBound) {return -1;}
  598. else if (value <= upperBound) {return 0;}
  599. else {return 1;}
  600. };
  601. // first check if the items that were in view previously are still in view.
  602. // IMPORTANT: this handles the case for the items with startdate before the window and enddate after the window!
  603. // also cleans up invisible items.
  604. if (oldVisibleItems.length > 0) {
  605. for (var i = 0; i < oldVisibleItems.length; i++) {
  606. this._checkIfVisibleWithReference(oldVisibleItems[i], visibleItems, visibleItemsLookup, range);
  607. }
  608. }
  609. // we do a binary search for the items that have only start values.
  610. var initialPosByStart = util.binarySearchCustom(orderedItems.byStart, searchFunction, 'data','start');
  611. // trace the visible items from the inital start pos both ways until an invisible item is found, we only look at the start values.
  612. this._traceVisible(initialPosByStart, orderedItems.byStart, visibleItems, visibleItemsLookup, function (item) {
  613. return (item.data.start < lowerBound || item.data.start > upperBound);
  614. });
  615. // if the window has changed programmatically without overlapping the old window, the ranged items with start < lowerBound and end > upperbound are not shown.
  616. // We therefore have to brute force check all items in the byEnd list
  617. if (this.checkRangedItems == true) {
  618. this.checkRangedItems = false;
  619. for (i = 0; i < orderedItems.byEnd.length; i++) {
  620. this._checkIfVisibleWithReference(orderedItems.byEnd[i], visibleItems, visibleItemsLookup, range);
  621. }
  622. }
  623. else {
  624. // we do a binary search for the items that have defined end times.
  625. var initialPosByEnd = util.binarySearchCustom(orderedItems.byEnd, searchFunction, 'data','end');
  626. // trace the visible items from the inital start pos both ways until an invisible item is found, we only look at the end values.
  627. this._traceVisible(initialPosByEnd, orderedItems.byEnd, visibleItems, visibleItemsLookup, function (item) {
  628. return (item.data.end < lowerBound || item.data.end > upperBound);
  629. });
  630. }
  631. // finally, we reposition all the visible items.
  632. for (i = 0; i < visibleItems.length; i++) {
  633. var item = visibleItems[i];
  634. if (!item.displayed) item.show();
  635. // reposition item horizontally
  636. item.repositionX();
  637. }
  638. return visibleItems;
  639. };
  640. Group.prototype._traceVisible = function (initialPos, items, visibleItems, visibleItemsLookup, breakCondition) {
  641. if (initialPos != -1) {
  642. var i, item;
  643. for (i = initialPos; i >= 0; i--) {
  644. item = items[i];
  645. if (breakCondition(item)) {
  646. break;
  647. }
  648. else {
  649. if (visibleItemsLookup[item.id] === undefined) {
  650. visibleItemsLookup[item.id] = true;
  651. visibleItems.push(item);
  652. }
  653. }
  654. }
  655. for (i = initialPos + 1; i < items.length; i++) {
  656. item = items[i];
  657. if (breakCondition(item)) {
  658. break;
  659. }
  660. else {
  661. if (visibleItemsLookup[item.id] === undefined) {
  662. visibleItemsLookup[item.id] = true;
  663. visibleItems.push(item);
  664. }
  665. }
  666. }
  667. }
  668. }
  669. /**
  670. * this function is very similar to the _checkIfInvisible() but it does not
  671. * return booleans, hides the item if it should not be seen and always adds to
  672. * the visibleItems.
  673. * this one is for brute forcing and hiding.
  674. *
  675. * @param {Item} item
  676. * @param {Array} visibleItems
  677. * @param {{start:number, end:number}} range
  678. * @private
  679. */
  680. Group.prototype._checkIfVisible = function(item, visibleItems, range) {
  681. if (item.isVisible(range)) {
  682. if (!item.displayed) item.show();
  683. // reposition item horizontally
  684. item.repositionX();
  685. visibleItems.push(item);
  686. }
  687. else {
  688. if (item.displayed) item.hide();
  689. }
  690. };
  691. /**
  692. * this function is very similar to the _checkIfInvisible() but it does not
  693. * return booleans, hides the item if it should not be seen and always adds to
  694. * the visibleItems.
  695. * this one is for brute forcing and hiding.
  696. *
  697. * @param {Item} item
  698. * @param {Array.<vis.Item>} visibleItems
  699. * @param {Object<number, boolean>} visibleItemsLookup
  700. * @param {{start:number, end:number}} range
  701. * @private
  702. */
  703. Group.prototype._checkIfVisibleWithReference = function(item, visibleItems, visibleItemsLookup, range) {
  704. if (item.isVisible(range)) {
  705. if (visibleItemsLookup[item.id] === undefined) {
  706. visibleItemsLookup[item.id] = true;
  707. visibleItems.push(item);
  708. }
  709. }
  710. else {
  711. if (item.displayed) item.hide();
  712. }
  713. };
  714. Group.prototype.changeSubgroup = function(item, oldSubgroup, newSubgroup) {
  715. this._removeFromSubgroup(item, oldSubgroup);
  716. this._addToSubgroup(item, newSubgroup);
  717. this.orderSubgroups();
  718. };
  719. module.exports = Group;