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.

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