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.

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