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.

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