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.

506 lines
15 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. var util = require('../../util');
  2. var stack = require('../Stack');
  3. var RangeItem = require('./item/RangeItem');
  4. /**
  5. * @constructor Group
  6. * @param {Number | String} groupId
  7. * @param {Object} data
  8. * @param {ItemSet} itemSet
  9. */
  10. function Group (groupId, data, itemSet) {
  11. this.groupId = groupId;
  12. this.subgroups = {};
  13. this.subgroupIndex = 0;
  14. this.subgroupOrderer = data && data.subgroupOrder;
  15. this.itemSet = itemSet;
  16. this.dom = {};
  17. this.props = {
  18. label: {
  19. width: 0,
  20. height: 0
  21. }
  22. };
  23. this.className = null;
  24. this.items = {}; // items filtered by groupId of this group
  25. this.visibleItems = []; // items currently visible in window
  26. this.orderedItems = { // items sorted by start and by end
  27. byStart: [],
  28. byEnd: []
  29. };
  30. this._create();
  31. this.setData(data);
  32. }
  33. /**
  34. * Create DOM elements for the group
  35. * @private
  36. */
  37. Group.prototype._create = function() {
  38. var label = document.createElement('div');
  39. label.className = 'vlabel';
  40. this.dom.label = label;
  41. var inner = document.createElement('div');
  42. inner.className = 'inner';
  43. label.appendChild(inner);
  44. this.dom.inner = inner;
  45. var foreground = document.createElement('div');
  46. foreground.className = 'group';
  47. foreground['timeline-group'] = this;
  48. this.dom.foreground = foreground;
  49. this.dom.background = document.createElement('div');
  50. this.dom.background.className = 'group';
  51. this.dom.axis = document.createElement('div');
  52. this.dom.axis.className = 'group';
  53. // create a hidden marker to detect when the Timelines container is attached
  54. // to the DOM, or the style of a parent of the Timeline is changed from
  55. // display:none is changed to visible.
  56. this.dom.marker = document.createElement('div');
  57. this.dom.marker.style.visibility = 'hidden'; // TODO: ask jos why this is not none?
  58. this.dom.marker.innerHTML = '?';
  59. this.dom.background.appendChild(this.dom.marker);
  60. };
  61. /**
  62. * Set the group data for this group
  63. * @param {Object} data Group data, can contain properties content and className
  64. */
  65. Group.prototype.setData = function(data) {
  66. // update contents
  67. var content = data && data.content;
  68. if (content instanceof Element) {
  69. this.dom.inner.appendChild(content);
  70. }
  71. else if (content !== undefined && content !== null) {
  72. this.dom.inner.innerHTML = content;
  73. }
  74. else {
  75. this.dom.inner.innerHTML = this.groupId || ''; // groupId can be null
  76. }
  77. // update title
  78. this.dom.label.title = data && data.title || '';
  79. if (!this.dom.inner.firstChild) {
  80. util.addClassName(this.dom.inner, 'hidden');
  81. }
  82. else {
  83. util.removeClassName(this.dom.inner, 'hidden');
  84. }
  85. // update className
  86. var className = data && data.className || null;
  87. if (className != this.className) {
  88. if (this.className) {
  89. util.removeClassName(this.dom.label, this.className);
  90. util.removeClassName(this.dom.foreground, this.className);
  91. util.removeClassName(this.dom.background, this.className);
  92. util.removeClassName(this.dom.axis, this.className);
  93. }
  94. util.addClassName(this.dom.label, className);
  95. util.addClassName(this.dom.foreground, className);
  96. util.addClassName(this.dom.background, className);
  97. util.addClassName(this.dom.axis, className);
  98. this.className = className;
  99. }
  100. // update style
  101. if (this.style) {
  102. util.removeCssText(this.dom.label, this.style);
  103. this.style = null;
  104. }
  105. if (data && data.style) {
  106. util.addCssText(this.dom.label, data.style);
  107. this.style = data.style;
  108. }
  109. };
  110. /**
  111. * Get the width of the group label
  112. * @return {number} width
  113. */
  114. Group.prototype.getLabelWidth = function() {
  115. return this.props.label.width;
  116. };
  117. /**
  118. * Repaint this group
  119. * @param {{start: number, end: number}} range
  120. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  121. * @param {boolean} [restack=false] Force restacking of all items
  122. * @return {boolean} Returns true if the group is resized
  123. */
  124. Group.prototype.redraw = function(range, margin, restack) {
  125. var resized = false;
  126. this.visibleItems = this._updateVisibleItems(this.orderedItems, this.visibleItems, range);
  127. // force recalculation of the height of the items when the marker height changed
  128. // (due to the Timeline being attached to the DOM or changed from display:none to visible)
  129. var markerHeight = this.dom.marker.clientHeight;
  130. if (markerHeight != this.lastMarkerHeight) {
  131. this.lastMarkerHeight = markerHeight;
  132. util.forEach(this.items, function (item) {
  133. item.dirty = true;
  134. if (item.displayed) item.redraw();
  135. });
  136. restack = true;
  137. }
  138. // reposition visible items vertically
  139. if (this.itemSet.options.stack) { // TODO: ugly way to access options...
  140. stack.stack(this.visibleItems, margin, restack);
  141. }
  142. else { // no stacking
  143. stack.nostack(this.visibleItems, margin, this.subgroups);
  144. }
  145. // recalculate the height of the group
  146. var height = this._calculateHeight(margin);
  147. // calculate actual size and position
  148. var foreground = this.dom.foreground;
  149. this.top = foreground.offsetTop;
  150. this.left = foreground.offsetLeft;
  151. this.width = foreground.offsetWidth;
  152. resized = util.updateProperty(this, 'height', height) || resized;
  153. // recalculate size of label
  154. resized = util.updateProperty(this.props.label, 'width', this.dom.inner.clientWidth) || resized;
  155. resized = util.updateProperty(this.props.label, 'height', this.dom.inner.clientHeight) || resized;
  156. // apply new height
  157. this.dom.background.style.height = height + 'px';
  158. this.dom.foreground.style.height = height + 'px';
  159. this.dom.label.style.height = height + 'px';
  160. // update vertical position of items after they are re-stacked and the height of the group is calculated
  161. for (var i = 0, ii = this.visibleItems.length; i < ii; i++) {
  162. var item = this.visibleItems[i];
  163. item.repositionY(margin);
  164. }
  165. return resized;
  166. };
  167. /**
  168. * recalculate the height of the group
  169. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  170. * @returns {number} Returns the height
  171. * @private
  172. */
  173. Group.prototype._calculateHeight = function (margin) {
  174. // recalculate the height of the group
  175. var height;
  176. var visibleItems = this.visibleItems;
  177. //var visibleSubgroups = [];
  178. //this.visibleSubgroups = 0;
  179. this.resetSubgroups();
  180. var me = this;
  181. if (visibleItems.length) {
  182. var min = visibleItems[0].top;
  183. var max = visibleItems[0].top + visibleItems[0].height;
  184. util.forEach(visibleItems, function (item) {
  185. min = Math.min(min, item.top);
  186. max = Math.max(max, (item.top + item.height));
  187. if (item.data.subgroup !== undefined) {
  188. me.subgroups[item.data.subgroup].height = Math.max(me.subgroups[item.data.subgroup].height,item.height);
  189. me.subgroups[item.data.subgroup].visible = true;
  190. //if (visibleSubgroups.indexOf(item.data.subgroup) == -1){
  191. // visibleSubgroups.push(item.data.subgroup);
  192. // me.visibleSubgroups += 1;
  193. //}
  194. }
  195. });
  196. if (min > margin.axis) {
  197. // there is an empty gap between the lowest item and the axis
  198. var offset = min - margin.axis;
  199. max -= offset;
  200. util.forEach(visibleItems, function (item) {
  201. item.top -= offset;
  202. });
  203. }
  204. height = max + margin.item.vertical / 2;
  205. }
  206. else {
  207. height = margin.axis + margin.item.vertical;
  208. }
  209. height = Math.max(height, this.props.label.height);
  210. return height;
  211. };
  212. /**
  213. * Show this group: attach to the DOM
  214. */
  215. Group.prototype.show = function() {
  216. if (!this.dom.label.parentNode) {
  217. this.itemSet.dom.labelSet.appendChild(this.dom.label);
  218. }
  219. if (!this.dom.foreground.parentNode) {
  220. this.itemSet.dom.foreground.appendChild(this.dom.foreground);
  221. }
  222. if (!this.dom.background.parentNode) {
  223. this.itemSet.dom.background.appendChild(this.dom.background);
  224. }
  225. if (!this.dom.axis.parentNode) {
  226. this.itemSet.dom.axis.appendChild(this.dom.axis);
  227. }
  228. };
  229. /**
  230. * Hide this group: remove from the DOM
  231. */
  232. Group.prototype.hide = function() {
  233. var label = this.dom.label;
  234. if (label.parentNode) {
  235. label.parentNode.removeChild(label);
  236. }
  237. var foreground = this.dom.foreground;
  238. if (foreground.parentNode) {
  239. foreground.parentNode.removeChild(foreground);
  240. }
  241. var background = this.dom.background;
  242. if (background.parentNode) {
  243. background.parentNode.removeChild(background);
  244. }
  245. var axis = this.dom.axis;
  246. if (axis.parentNode) {
  247. axis.parentNode.removeChild(axis);
  248. }
  249. };
  250. /**
  251. * Add an item to the group
  252. * @param {Item} item
  253. */
  254. Group.prototype.add = function(item) {
  255. this.items[item.id] = item;
  256. item.setParent(this);
  257. // add to
  258. if (item.data.subgroup !== undefined) {
  259. if (this.subgroups[item.data.subgroup] === undefined) {
  260. this.subgroups[item.data.subgroup] = {height:0, visible: false, index:this.subgroupIndex, items: []};
  261. this.subgroupIndex++;
  262. }
  263. this.subgroups[item.data.subgroup].items.push(item);
  264. }
  265. this.orderSubgroups();
  266. if (this.visibleItems.indexOf(item) == -1) {
  267. var range = this.itemSet.body.range; // TODO: not nice accessing the range like this
  268. this._checkIfVisible(item, this.visibleItems, range);
  269. }
  270. };
  271. Group.prototype.orderSubgroups = function() {
  272. if (this.subgroupOrderer !== undefined) {
  273. var sortArray = [];
  274. if (typeof this.subgroupOrderer == 'string') {
  275. for (var subgroup in this.subgroups) {
  276. sortArray.push({subgroup: subgroup, sortField: this.subgroups[subgroup].items[0].data[this.subgroupOrderer]})
  277. }
  278. sortArray.sort(function (a, b) {
  279. return a.sortField - b.sortField;
  280. })
  281. }
  282. else if (typeof this.subgroupOrderer == 'function') {
  283. for (var subgroup in this.subgroups) {
  284. sortArray.push(this.subgroups[subgroup].items[0].data);
  285. }
  286. sortArray.sort(this.subgroupOrderer);
  287. }
  288. if (sortArray.length > 0) {
  289. for (var i = 0; i < sortArray.length; i++) {
  290. this.subgroups[sortArray[i].subgroup].index = i;
  291. }
  292. }
  293. }
  294. };
  295. Group.prototype.resetSubgroups = function() {
  296. for (var subgroup in this.subgroups) {
  297. if (this.subgroups.hasOwnProperty(subgroup)) {
  298. this.subgroups[subgroup].visible = false;
  299. }
  300. }
  301. };
  302. /**
  303. * Remove an item from the group
  304. * @param {Item} item
  305. */
  306. Group.prototype.remove = function(item) {
  307. delete this.items[item.id];
  308. item.setParent(null);
  309. // remove from visible items
  310. var index = this.visibleItems.indexOf(item);
  311. if (index != -1) this.visibleItems.splice(index, 1);
  312. // TODO: also remove from ordered items?
  313. };
  314. /**
  315. * Remove an item from the corresponding DataSet
  316. * @param {Item} item
  317. */
  318. Group.prototype.removeFromDataSet = function(item) {
  319. this.itemSet.removeItem(item.id);
  320. };
  321. /**
  322. * Reorder the items
  323. */
  324. Group.prototype.order = function() {
  325. var array = util.toArray(this.items);
  326. this.orderedItems.byStart = array;
  327. this.orderedItems.byEnd = this._constructByEndArray(array);
  328. stack.orderByStart(this.orderedItems.byStart);
  329. stack.orderByEnd(this.orderedItems.byEnd);
  330. };
  331. /**
  332. * Create an array containing all items being a range (having an end date)
  333. * @param {Item[]} array
  334. * @returns {RangeItem[]}
  335. * @private
  336. */
  337. Group.prototype._constructByEndArray = function(array) {
  338. var endArray = [];
  339. for (var i = 0; i < array.length; i++) {
  340. if (array[i] instanceof RangeItem) {
  341. endArray.push(array[i]);
  342. }
  343. }
  344. return endArray;
  345. };
  346. /**
  347. * Update the visible items
  348. * @param {{byStart: Item[], byEnd: Item[]}} orderedItems All items ordered by start date and by end date
  349. * @param {Item[]} visibleItems The previously visible items.
  350. * @param {{start: number, end: number}} range Visible range
  351. * @return {Item[]} visibleItems The new visible items.
  352. * @private
  353. */
  354. Group.prototype._updateVisibleItems = function(orderedItems, visibleItems, range) {
  355. var initialPosByStart,
  356. newVisibleItems = [],
  357. i;
  358. // first check if the items that were in view previously are still in view.
  359. // this handles the case for the RangeItem that is both before and after the current one.
  360. if (visibleItems.length > 0) {
  361. for (i = 0; i < visibleItems.length; i++) {
  362. this._checkIfVisible(visibleItems[i], newVisibleItems, range);
  363. }
  364. }
  365. // If there were no visible items previously, use binarySearch to find a visible PointItem or RangeItem (based on startTime)
  366. if (newVisibleItems.length == 0) {
  367. initialPosByStart = util.binarySearch(orderedItems.byStart, range, 'data','start');
  368. }
  369. else {
  370. initialPosByStart = orderedItems.byStart.indexOf(newVisibleItems[0]);
  371. }
  372. // use visible search to find a visible RangeItem (only based on endTime)
  373. var initialPosByEnd = util.binarySearch(orderedItems.byEnd, range, 'data','end');
  374. // if we found a initial ID to use, trace it up and down until we meet an invisible item.
  375. if (initialPosByStart != -1) {
  376. for (i = initialPosByStart; i >= 0; i--) {
  377. if (this._checkIfInvisible(orderedItems.byStart[i], newVisibleItems, range)) {break;}
  378. }
  379. for (i = initialPosByStart + 1; i < orderedItems.byStart.length; i++) {
  380. if (this._checkIfInvisible(orderedItems.byStart[i], newVisibleItems, range)) {break;}
  381. }
  382. }
  383. // if we found a initial ID to use, trace it up and down until we meet an invisible item.
  384. if (initialPosByEnd != -1) {
  385. for (i = initialPosByEnd; i >= 0; i--) {
  386. if (this._checkIfInvisible(orderedItems.byEnd[i], newVisibleItems, range)) {break;}
  387. }
  388. for (i = initialPosByEnd + 1; i < orderedItems.byEnd.length; i++) {
  389. if (this._checkIfInvisible(orderedItems.byEnd[i], newVisibleItems, range)) {break;}
  390. }
  391. }
  392. return newVisibleItems;
  393. };
  394. /**
  395. * this function checks if an item is invisible. If it is NOT we make it visible
  396. * and add it to the global visible items. If it is, return true.
  397. *
  398. * @param {Item} item
  399. * @param {Item[]} visibleItems
  400. * @param {{start:number, end:number}} range
  401. * @returns {boolean}
  402. * @private
  403. */
  404. Group.prototype._checkIfInvisible = function(item, visibleItems, range) {
  405. if (item.isVisible(range)) {
  406. if (!item.displayed) item.show();
  407. item.repositionX();
  408. if (visibleItems.indexOf(item) == -1) {
  409. visibleItems.push(item);
  410. }
  411. return false;
  412. }
  413. else {
  414. if (item.displayed) item.hide();
  415. return true;
  416. }
  417. };
  418. /**
  419. * this function is very similar to the _checkIfInvisible() but it does not
  420. * return booleans, hides the item if it should not be seen and always adds to
  421. * the visibleItems.
  422. * this one is for brute forcing and hiding.
  423. *
  424. * @param {Item} item
  425. * @param {Array} visibleItems
  426. * @param {{start:number, end:number}} range
  427. * @private
  428. */
  429. Group.prototype._checkIfVisible = function(item, visibleItems, range) {
  430. if (item.isVisible(range)) {
  431. if (!item.displayed) item.show();
  432. // reposition item horizontally
  433. item.repositionX();
  434. visibleItems.push(item);
  435. }
  436. else {
  437. if (item.displayed) item.hide();
  438. }
  439. };
  440. module.exports = Group;